Skip to content

Commit 6380d27

Browse files
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.
1 parent 941f767 commit 6380d27

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/Commands/ServeCommand.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpMcp\Server\Transports\HttpServerTransport;
1111
use PhpMcp\Server\Transports\StdioServerTransport;
1212
use PhpMcp\Server\Transports\StreamableHttpServerTransport;
13+
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1314

1415
use function Laravel\Prompts\select;
1516

@@ -80,10 +81,14 @@ private function handleStdioTransport(Server $server): int
8081
return Command::FAILURE;
8182
}
8283

83-
$this->info('Starting MCP server');
84-
$this->line(" - Transport: STDIO");
85-
$this->line(" - Communication: STDIN/STDOUT");
86-
$this->line(" - Mode: JSON-RPC over Standard I/O");
84+
$output = $this->output->getOutput();
85+
86+
if ($output instanceof ConsoleOutputInterface) {
87+
$output->getErrorOutput()->writeln("Starting MCP server");
88+
$output->getErrorOutput()->writeln(" - Transport: STDIO");
89+
$output->getErrorOutput()->writeln(" - Communication: STDIN/STDOUT");
90+
$output->getErrorOutput()->writeln(" - Mode: JSON-RPC over Standard I/O");
91+
}
8792

8893
try {
8994
$transport = new StdioServerTransport;

tests/Feature/Commands/ServeCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public function test_serve_command_defaults_to_stdio_and_calls_server_listen()
4545
);
4646

4747
$this->artisan('mcp:serve --transport=stdio')
48-
->expectsOutputToContain('Starting MCP server')
49-
->expectsOutputToContain('Transport: STDIO')
48+
->doesntExpectOutputToContain('Starting MCP server')
49+
->doesntExpectOutputToContain('Transport: STDIO')
5050
->assertSuccessful();
5151
}
5252

0 commit comments

Comments
 (0)