From a562a0514c2c97ee888d110ae4009d7c5ce2ff86 Mon Sep 17 00:00:00 2001 From: Andre Schade Date: Fri, 20 Nov 2026 06:06:41 +0100 Subject: [PATCH] [Client] Answer a server request with an unknown method or invalid params with an error Co-Authored-By: Claude Opus 5.5 --- src/Client/Protocol.php | 10 ++++++++ tests/Unit/Client/ProtocolTest.php | 39 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Client/Protocol.php b/src/Client/Protocol.php index 7476ef68..2df041bd 100644 --- a/src/Client/Protocol.php +++ b/src/Client/Protocol.php @@ -29,6 +29,7 @@ use Mcp\Schema\Enum\LoggingLevel; use Mcp\Schema\Enum\ProtocolVersion; use Mcp\Schema\Exception\InvalidInputMessageException; +use Mcp\Schema\Exception\UnknownMethodException; use Mcp\Schema\Implementation; use Mcp\Schema\JsonRpc\Error; use Mcp\Schema\JsonRpc\Notification; @@ -857,6 +858,15 @@ public function processMessage(string $input): void } catch (\JsonException|InvalidInputMessageException $e) { $this->logger->warning('Failed to parse message', ['exception' => $e]); + // A server request the client cannot read still needs an answer, or the server waits for its timeout. + if ($e instanceof InvalidInputMessageException && null !== $e->getRequestId()) { + if ($e instanceof UnknownMethodException) { + $this->sendResponse(Error::forMethodNotFound($e->getMessage(), $e->getRequestId())); + } elseif ($e->isInvalidParams()) { + $this->sendResponse(Error::forInvalidParams($e->getMessage(), $e->getRequestId())); + } + } + return; } diff --git a/tests/Unit/Client/ProtocolTest.php b/tests/Unit/Client/ProtocolTest.php index 5ebc8dcd..781d8eda 100644 --- a/tests/Unit/Client/ProtocolTest.php +++ b/tests/Unit/Client/ProtocolTest.php @@ -700,6 +700,45 @@ public function handle(Request $request): Response $this->assertInstanceOf(\JsonException::class, $logger->warnings[0]['exception'] ?? null); } + /** + * @return iterable + */ + public static function provideUnreadableServerRequests(): iterable + { + yield 'unknown method' => ['{"jsonrpc": "2.0", "id": "srv-1", "method": "vendor/unknown", "params": {}}', Error::METHOD_NOT_FOUND]; + yield 'invalid params' => ['{"jsonrpc": "2.0", "id": "srv-1", "method": "sampling/createMessage", "params": {"messages": "not a list"}}', Error::INVALID_PARAMS]; + } + + #[DataProvider('provideUnreadableServerRequests')] + #[TestDox('answers a server request it cannot read with an error instead of dropping it')] + public function testAnswersUnreadableServerRequestWithError(string $input, int $code): void + { + $messages = []; + $transport = $this->createMock(TransportInterface::class); + $transport->method('send')->willReturnCallback(static function (string $data) use (&$messages): void { + $messages[] = json_decode($data, true, flags: \JSON_THROW_ON_ERROR); + }); + $protocol = new Protocol(); + $protocol->connect($transport, $this->createConfiguration(ProtocolVersion::V2025_11_25)); + + $protocol->processMessage($input); + + $this->assertCount(1, $messages); + $this->assertSame('srv-1', $messages[0]['id']); + $this->assertSame($code, $messages[0]['error']['code'] ?? null); + } + + #[TestDox('does not answer a malformed response from the server')] + public function testDoesNotAnswerMalformedResponse(): void + { + $transport = $this->createMock(TransportInterface::class); + $transport->expects($this->never())->method('send'); + $protocol = new Protocol(); + $protocol->connect($transport, $this->createConfiguration(ProtocolVersion::V2025_11_25)); + + $protocol->processMessage('{"jsonrpc": "2.0", "id": 7, "error": {"code": "oops", "message": "bad"}}'); + } + private function createConfiguration(ProtocolVersion $protocolVersion, ?ProtocolVersion $fallback = ProtocolVersion::V2025_11_25): Configuration { return new Configuration(