diff --git a/CHANGELOG.md b/CHANGELOG.md index c977af64..a5897053 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/docs/protocol-versions.md b/docs/protocol-versions.md index 5b29d147..f36d8a6b 100644 --- a/docs/protocol-versions.md +++ b/docs/protocol-versions.md @@ -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 diff --git a/src/Client/Client.php b/src/Client/Client.php index d6214a44..df8f95cb 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -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); } @@ -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. * diff --git a/tests/Integration/Fixture/tool_headers.php b/tests/Integration/Fixture/tool_headers.php new file mode 100644 index 00000000..8a663ec9 --- /dev/null +++ b/tests/Integration/Fixture/tool_headers.php @@ -0,0 +1,33 @@ +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())); diff --git a/tests/Integration/ToolHeaderRetryTest.php b/tests/Integration/ToolHeaderRetryTest.php new file mode 100644 index 00000000..34bfff66 --- /dev/null +++ b/tests/Integration/ToolHeaderRetryTest.php @@ -0,0 +1,76 @@ +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(); + } +}