From a3863f14423419e9eb93859e6437a3f5e0792019 Mon Sep 17 00:00:00 2001 From: Kyrian Obikwelu Date: Sun, 13 Jul 2025 01:00:32 +0100 Subject: [PATCH] fix: redirect ServeCommand output to STDERR for stdio transport Console output methods sometimes write to STDOUT, which interferes with JSON-RPC communication in stdio mode. This redirects startup messages to STDERR when available. --- src/Commands/ServeCommand.php | 13 +++++++++---- tests/Feature/Commands/ServeCommandTest.php | 4 ++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Commands/ServeCommand.php b/src/Commands/ServeCommand.php index 77e3c35..da1dbab 100644 --- a/src/Commands/ServeCommand.php +++ b/src/Commands/ServeCommand.php @@ -10,6 +10,7 @@ use PhpMcp\Server\Transports\HttpServerTransport; use PhpMcp\Server\Transports\StdioServerTransport; use PhpMcp\Server\Transports\StreamableHttpServerTransport; +use Symfony\Component\Console\Output\ConsoleOutputInterface; use function Laravel\Prompts\select; @@ -80,10 +81,14 @@ private function handleStdioTransport(Server $server): int return Command::FAILURE; } - $this->info('Starting MCP server'); - $this->line(" - Transport: STDIO"); - $this->line(" - Communication: STDIN/STDOUT"); - $this->line(" - Mode: JSON-RPC over Standard I/O"); + $output = $this->output->getOutput(); + + if ($output instanceof ConsoleOutputInterface) { + $output->getErrorOutput()->writeln("Starting MCP server"); + $output->getErrorOutput()->writeln(" - Transport: STDIO"); + $output->getErrorOutput()->writeln(" - Communication: STDIN/STDOUT"); + $output->getErrorOutput()->writeln(" - Mode: JSON-RPC over Standard I/O"); + } try { $transport = new StdioServerTransport; diff --git a/tests/Feature/Commands/ServeCommandTest.php b/tests/Feature/Commands/ServeCommandTest.php index 97ed523..270054f 100644 --- a/tests/Feature/Commands/ServeCommandTest.php +++ b/tests/Feature/Commands/ServeCommandTest.php @@ -45,8 +45,8 @@ public function test_serve_command_defaults_to_stdio_and_calls_server_listen() ); $this->artisan('mcp:serve --transport=stdio') - ->expectsOutputToContain('Starting MCP server') - ->expectsOutputToContain('Transport: STDIO') + ->doesntExpectOutputToContain('Starting MCP server') + ->doesntExpectOutputToContain('Transport: STDIO') ->assertSuccessful(); }