[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:
parent
e2243bc2d9
commit
2dbd411194
1 changed files with 82 additions and 74 deletions
|
|
@ -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)) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue