From 8c0f52f97ef15a152bc92627771e69580affbce3 Mon Sep 17 00:00:00 2001 From: Alejandro Ibarra Date: Sat, 21 Nov 2026 23:46:55 +0100 Subject: [PATCH] [Client] Start stdio servers whose command path has a space --- CHANGELOG.md | 4 ++++ docs/client/transports.md | 7 ++++--- src/Client/Transport/StdioTransport.php | 9 +++------ .../Client/Transport/StdioTransportTest.php | 20 +++++++++++++++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c977af64..91ac5a5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,10 @@ All notable changes to `mcp/sdk` will be documented in this file. * Fix const validation errors always reporting the expected value as null. +### Client + +* [BC Break] `StdioTransport` starts the command without a shell on every platform, so a command whose path has a space starts. Arguments written into `command` (`'npx -y pkg'`) go in `args` instead. + 0.10.0 ------ diff --git a/docs/client/transports.md b/docs/client/transports.md index 14403254..eda7bab8 100644 --- a/docs/client/transports.md +++ b/docs/client/transports.md @@ -19,8 +19,8 @@ $transport = new StdioTransport( **Parameters:** -- `command` (string): The command to execute -- `args` (array): Command arguments +- `command` (string): The program to start, a path (spaces allowed) or a name looked up in `PATH` +- `args` (array): Its arguments, each passed on as is - `cwd` (string|null): Working directory for the process - `env` (array|null): Environment variables of the process. An array replaces the inherited environment, so pass `[...getenv(), 'KEY' => 'value']` to keep it - `logger` (LoggerInterface|null): Optional PSR-3 logger @@ -32,7 +32,8 @@ If the server process exits while a request waits for its answer, the request fa On Windows, use PHP 8.5 or newer. With older versions, the client blocks while the server sends no output, and its timeouts have no effect. -Windows starts the command directly, so a batch file like `npx` must run through `cmd`: +The command is started directly, without a shell, so its arguments go in `args`, not in `command`. On Windows a batch +file like `npx` must run through `cmd`: ```php $transport = new StdioTransport('cmd', ['/c', 'npx', '-y', '@acme/weather-server']); diff --git a/src/Client/Transport/StdioTransport.php b/src/Client/Transport/StdioTransport.php index 03db08d6..a1c9695e 100644 --- a/src/Client/Transport/StdioTransport.php +++ b/src/Client/Transport/StdioTransport.php @@ -210,14 +210,11 @@ private function spawnProcess(): void 2 => ['pipe', 'w'], // stderr ]; - $cmd = escapeshellcmd($this->command); - foreach ($this->args as $arg) { - $cmd .= ' '.escapeshellarg($arg); - } + $cmd = implode(' ', array_map('escapeshellarg', [$this->command, ...$this->args])); $process = proc_open( - // cmd.exe cannot pass every argument on, and escapeshellarg() blanks what it cannot quote. - 'Windows' === \PHP_OS_FAMILY ? [$this->command, ...$this->args] : $cmd, + // Without a shell: a command path with a space still names one program, and every argument arrives as is. + [$this->command, ...$this->args], $descriptors, $pipes, $this->cwd, diff --git a/tests/Unit/Client/Transport/StdioTransportTest.php b/tests/Unit/Client/Transport/StdioTransportTest.php index 6f45789d..6fc3aa74 100644 --- a/tests/Unit/Client/Transport/StdioTransportTest.php +++ b/tests/Unit/Client/Transport/StdioTransportTest.php @@ -240,6 +240,26 @@ public function testArgumentsReachTheServerUnchanged(): void } } + #[TestDox('a command whose path has a space starts')] + public function testCommandPathWithASpaceStarts(): void + { + $dir = sys_get_temp_dir().'/mcp stdio '.getmypid(); + mkdir($dir); + $command = $dir.'/php'; + symlink(\PHP_BINARY, $command); + $client = Client::builder()->setInitTimeout(10)->setMaxRetries(0)->build(); + + try { + $client->connect(new StdioTransport(command: $command, args: ['-r', 'fwrite(STDERR, "started"); exit(3);'])); + $this->fail('Connecting to a server that exits must fail.'); + } catch (ConnectionException $e) { + $this->assertStringContainsString('exited with code 3: started', $e->getMessage()); + } finally { + unlink($command); + rmdir($dir); + } + } + #[TestDox('a retried connection reports how its own process ended, not the previous one')] public function testRetryReportsItsOwnProcess(): void {