Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------

Expand Down
7 changes: 4 additions & 3 deletions docs/client/transports.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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']);
Expand Down
9 changes: 3 additions & 6 deletions src/Client/Transport/StdioTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Client/Transport/StdioTransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down