Skip to content

Fix: Correct Client State Management in LaravelHttpTransport #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions src/Transports/LaravelHttpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
use Evenement\EventEmitterTrait;
use PhpMcp\Server\Contracts\LoggerAwareInterface;
use PhpMcp\Server\Contracts\ServerTransportInterface;
use PhpMcp\Server\Exception\TransportException;
use PhpMcp\Server\State\ClientStateManager;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use React\Promise\PromiseInterface;

use function React\Promise\reject;
use function React\Promise\resolve;

class LaravelHttpTransport implements ServerTransportInterface, LoggerAwareInterface
Expand All @@ -24,23 +22,11 @@ class LaravelHttpTransport implements ServerTransportInterface, LoggerAwareInter

protected ClientStateManager $clientStateManager;

/** @var array<string, true> Tracks active client IDs managed by this transport */
private array $activeClients = [];

public function __construct(ClientStateManager $clientStateManager)
{
$this->clientStateManager = $clientStateManager;
$this->logger = new NullLogger;

$this->on('client_connected', function (string $clientId) {
$this->activeClients[$clientId] = true;
$this->clientStateManager->updateClientActivity($clientId);
});

$this->on('client_disconnected', function (string $clientId, string $reason) {
unset($this->activeClients[$clientId]);
});

$this->on('message', function (string $message, string $clientId) {
$this->clientStateManager->updateClientActivity($clientId);
});
Expand Down Expand Up @@ -68,12 +54,6 @@ public function listen(): void
*/
public function sendToClientAsync(string $clientId, string $rawFramedMessage): PromiseInterface
{
if (! isset($this->activeClients[$clientId])) {
$this->logger->warning('Attempted to send message to inactive or unknown client.', ['clientId' => $clientId]);

return reject(new TransportException("Client '{$clientId}' is not actively managed by this transport."));
}

$messagePayload = rtrim($rawFramedMessage, "\n");

if (empty($messagePayload)) {
Expand All @@ -90,13 +70,7 @@ public function sendToClientAsync(string $clientId, string $rawFramedMessage): P
*/
public function close(): void
{
$activeClientIds = array_keys($this->activeClients);

foreach ($activeClientIds as $clientId) {
$this->emit('client_disconnected', [$clientId, 'Transport globally closed']);
$this->emit('close', ['Transport closed.']);
}

$this->emit('close', ['Transport closed.']);
$this->removeAllListeners();
}
}