diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a845fb9..ef973af0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ All notable changes to `mcp/sdk` will be documented in this file. +0.2.1 +----- + +* Add `RunnerControl` for `StdioTransport` to allow break out from continuously listening for new input. + 0.2.0 ----- @@ -13,7 +18,6 @@ All notable changes to `mcp/sdk` will be documented in this file. * Removed `Protocol::getTransport()` * Added parameter for `TransportInterface` to `Protocol::processInput()` - 0.1.0 ----- diff --git a/src/Server/Transport/Stdio/RunnerControl.php b/src/Server/Transport/Stdio/RunnerControl.php new file mode 100644 index 00000000..2b3c941b --- /dev/null +++ b/src/Server/Transport/Stdio/RunnerControl.php @@ -0,0 +1,28 @@ + + */ +class RunnerControl implements RunnerControlInterface +{ + public static RunnerState $state = RunnerState::RUNNING; + + public function getState(): RunnerState + { + return self::$state; + } +} diff --git a/src/Server/Transport/Stdio/RunnerControlInterface.php b/src/Server/Transport/Stdio/RunnerControlInterface.php new file mode 100644 index 00000000..d8de01f7 --- /dev/null +++ b/src/Server/Transport/Stdio/RunnerControlInterface.php @@ -0,0 +1,22 @@ + + */ +interface RunnerControlInterface +{ + public function getState(): RunnerState; +} diff --git a/src/Server/Transport/Stdio/RunnerState.php b/src/Server/Transport/Stdio/RunnerState.php new file mode 100644 index 00000000..c35e5e1a --- /dev/null +++ b/src/Server/Transport/Stdio/RunnerState.php @@ -0,0 +1,24 @@ + + */ +enum RunnerState +{ + case RUNNING; + case STOP_AND_END_SESSION; + case STOP; +} diff --git a/src/Server/Transport/StdioTransport.php b/src/Server/Transport/StdioTransport.php index e9b3f7ee..9060f39a 100644 --- a/src/Server/Transport/StdioTransport.php +++ b/src/Server/Transport/StdioTransport.php @@ -12,6 +12,9 @@ namespace Mcp\Server\Transport; use Mcp\Schema\JsonRpc\Error; +use Mcp\Server\Transport\Stdio\RunnerControl; +use Mcp\Server\Transport\Stdio\RunnerControlInterface; +use Mcp\Server\Transport\Stdio\RunnerState; use Psr\Log\LoggerInterface; /** @@ -21,6 +24,8 @@ */ class StdioTransport extends BaseTransport { + private RunnerControlInterface $runnerControl; + /** * @param resource $input * @param resource $output @@ -29,7 +34,9 @@ public function __construct( private $input = \STDIN, private $output = \STDOUT, ?LoggerInterface $logger = null, + ?RunnerControlInterface $runnerControl = null, ) { + $this->runnerControl = $runnerControl ?? new RunnerControl(); parent::__construct($logger); } @@ -47,14 +54,17 @@ public function listen(): int $this->logger->info('StdioTransport is listening for messages on STDIN...'); stream_set_blocking($this->input, false); - while (!feof($this->input)) { + while (!feof($this->input) && RunnerState::RUNNING === $this->runnerControl->getState()) { $this->processInput(); $this->processFiber(); $this->flushOutgoingMessages(); } $this->logger->info('StdioTransport finished listening.'); - $this->handleSessionEnd($this->sessionId); + if (\in_array($this->runnerControl->getState(), [RunnerState::RUNNING, RunnerState::STOP_AND_END_SESSION], true)) { + $this->logger->info('StdioTransport end session.'); + $this->handleSessionEnd($this->sessionId); + } return 0; }