[Server] Simplify server entrypoint with run-based transport lifecycle (#103)

* feat: replace connect/listen flow with run lifecycle

* feat(transport): add phpstan generics for typed transport returns

* refactor: standardize variable naming for MCP server instances in transport handling

* test: remove unnecessary phpstan-ignore annotation in DocBlockTestFixture
This commit is contained in:
Kyrian Obikwelu 2025-10-10 18:52:06 +01:00 • committed by GitHub
commit 2dbd411194
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,6 +16,8 @@ use Psr\Log\NullLogger;
use Symfony\Component\Uid\Uuid;
/**
* @implements TransportInterface<int>
*
* @author Kyrian Obikwelu <koshnawaza@gmail.com>
*/
class StdioTransport implements TransportInterface
@ -59,13 +61,18 @@ class StdioTransport implements TransportInterface
fwrite($this->output, $data.\PHP_EOL);
}
public function listen(): mixed
public function listen(): int
{
$this->logger->info('StdioTransport is listening for messages on STDIN...');
$status = 0;
while (!feof($this->input)) {
$line = fgets($this->input);
if (false === $line) {
if (!feof($this->input)) {
$status = 1;
}
break;
}
@ -82,9 +89,10 @@ class StdioTransport implements TransportInterface
if (\is_callable($this->sessionEndListener) && null !== $this->sessionId) {
\call_user_func($this->sessionEndListener, $this->sessionId);
$this->sessionId = null;
}
return null;
return $status;
}
public function onSessionEnd(callable $listener): void
@ -96,6 +104,7 @@ class StdioTransport implements TransportInterface
{
if (\is_callable($this->sessionEndListener) && null !== $this->sessionId) {
\call_user_func($this->sessionEndListener, $this->sessionId);
$this->sessionId = null;
}
if (\is_resource($this->input)) {