diff --git a/CHANGELOG.md b/CHANGELOG.md index c977af64c..91ac5a5b2 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 14403254f..eda7bab87 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 03db08d6e..a1c9695ed 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 6f45789db..6fc3aa741 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 {