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 @@ -5,6 +5,10 @@ All notable changes to `mcp/sdk` will be documented in this file.
0.11.0
------

### Client

* Retry a tool call refused with `-32020` once, after listing the tools again, so a tool called without being listed gets its `Mcp-Param-*` headers.

### Server

* Fix const validation errors always reporting the expected value as null.
Expand Down
3 changes: 2 additions & 1 deletion docs/protocol-versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ What being modern changes underneath:
- **Headers on every POST** — `MCP-Protocol-Version`, `Mcp-Method`, and `Mcp-Name` where the
method addresses a subject. Arguments annotated with `x-mcp-header` are mirrored into
`Mcp-Param-*`, which requires the client to have listed the tool first; `tools/list` is what
populates that knowledge. A tool whose annotations are malformed is dropped from the listing
populates that knowledge. A call the server refuses with `-32020` is retried once after
listing the tools again. A tool whose annotations are malformed is dropped from the listing
and refused if called, since the client cannot produce the headers it demands.
- **[Multi round-trip calls](handlers/input-required.md) are answered by the client.** A result
of `resultType: "input_required"` is resolved through the same
Expand Down
40 changes: 31 additions & 9 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,21 @@ public function callTool(string $name, array $arguments = [], ?callable $onProgr
throw new InvalidArgumentException('The per-call timeout must be a finite positive number of seconds.');
}

$catalog = $this->protocol->getToolCatalog();
$this->refuseRejectedTool($name);

// A tool the listing showed to be malformed is refused here rather than
// sent: the client cannot produce the headers its annotations demand,
// so the call could only go out misdescribed (SEP-2243).
if ($catalog->isRejected($name)) {
throw new RuntimeException(\sprintf('Refusing to call tool "%s": its "x-mcp-header" annotations are invalid (%s).', $name, $catalog->reasonFor($name)));
}
try {
$response = $this->sendRequest(new CallToolRequest($name, $arguments), $onProgress, $cancellation, $timeoutSeconds);
} catch (RequestException $e) {
if (Error::HEADER_MISMATCH !== $e->getCode()) {
throw $e;
}

$request = new CallToolRequest($name, $arguments);
// The tool was never listed or its schema changed: list again and retry once (SEP-2243).
$this->recordTools();
$this->refuseRejectedTool($name);

$response = $this->sendRequest($request, $onProgress, $cancellation, $timeoutSeconds);
$response = $this->sendRequest(new CallToolRequest($name, $arguments), $onProgress, $cancellation, $timeoutSeconds);
}

return CallToolResult::fromArray($response->result);
}
Expand Down Expand Up @@ -402,6 +405,25 @@ public function sendRootsListChanged(): void
$this->protocol->sendNotification(new RootsListChangedNotification());
}

// A malformed tool can't get the headers its annotations demand, so it is never sent (SEP-2243).
private function refuseRejectedTool(string $name): void
{
$catalog = $this->protocol->getToolCatalog();

if ($catalog->isRejected($name)) {
throw new RuntimeException(\sprintf('Refusing to call tool "%s": its "x-mcp-header" annotations are invalid (%s).', $name, $catalog->reasonFor($name)));
}
}

private function recordTools(): void
{
$cursor = null;

do {
$cursor = $this->listTools($cursor)->nextCursor;
} while (null !== $cursor);
}

/**
* Send a request to the server and wait for response.
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Integration/Fixture/tool_headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

// Server for ToolHeaderRetryTest under `php -S`: a tool that mirrors its arguments into Mcp-Param-* headers.

use Http\Discovery\Psr17Factory;
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
use Mcp\Server\Server;
use Mcp\Server\Transport\StatelessHttpTransport;

require_once dirname(__DIR__, 3).'/vendor/autoload.php';

$server = Server::builder()
->setServerInfo('tool-headers', '1.0.0')
->addTool(static fn (string $owner, string $repo): string => $owner.'/'.$repo, name: 'list_issues', inputSchema: [
'type' => 'object',
'properties' => [
'owner' => ['type' => 'string', 'x-mcp-header' => 'Owner'],
'repo' => ['type' => 'string', 'x-mcp-header' => 'Repo'],
],
'required' => ['owner', 'repo'],
])
->buildStateless();

(new SapiEmitter())->emit((new StatelessHttpTransport($server))->handle((new Psr17Factory())->createServerRequestFromGlobals()));
76 changes: 76 additions & 0 deletions tests/Integration/ToolHeaderRetryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Mcp\Tests\Integration;

use Mcp\Client\Client;
use Mcp\Client\Transport\HttpTransport;
use Mcp\Schema\Content\TextContent;
use Mcp\Schema\Enum\ProtocolVersion;
use Mcp\Tests\Support\ServerProcess;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

/**
* A tool whose arguments go into Mcp-Param-* headers, called without listing the tools first.
*
* @author Diego D'amico
*/
final class ToolHeaderRetryTest extends TestCase
{
private const TIMEOUT = 5;

private ?Process $server = null;
private int $port;

protected function setUp(): void
{
$this->port = ServerProcess::findFreePort();

$this->server = new Process([\PHP_BINARY, '-S', \sprintf('127.0.0.1:%d', $this->port), __DIR__.'/Fixture/tool_headers.php']);
$this->server->start();

$deadline = microtime(true) + 5;

while (microtime(true) < $deadline) {
if (@fsockopen('127.0.0.1', $this->port, $errno, $error, 0.1)) {
return;
}

usleep(50_000);
}

$this->fail(\sprintf('The fixture server did not start: %s', $this->server->getErrorOutput()));
}

protected function tearDown(): void
{
ServerProcess::stop($this->server);
}

public function testACallRejectedForMissingHeadersIsRetriedAfterListingTheTools(): void
{
$client = Client::builder()
->setClientInfo('integration-client', '1.0.0')
->setProtocolVersion(ProtocolVersion::V2026_07_28)
->setInitTimeout(self::TIMEOUT)
->setRequestTimeout(self::TIMEOUT)
->build();
$client->connect(new HttpTransport(\sprintf('http://127.0.0.1:%d/', $this->port)));

$result = $client->callTool('list_issues', ['owner' => 'odiseoteam', 'repo' => 'reflex']);

$this->assertInstanceOf(TextContent::class, $result->content[0]);
$this->assertSame('odiseoteam/reflex', $result->content[0]->text);

$client->disconnect();
}
}