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
9 changes: 9 additions & 0 deletions src/Client/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
use Mcp\Schema\Notification\ProgressNotification;
use Mcp\Schema\Request\DiscoverRequest;
use Mcp\Schema\Request\InitializeRequest;
use Mcp\Schema\Request\PingRequest;
use Mcp\Schema\RequestMeta;
use Mcp\Schema\Result\EmptyResult;
use Mcp\Schema\Result\InitializeResult;
use Mcp\Schema\ServerCapabilities;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -931,6 +933,13 @@ private function handleRequest(Request $request): void
}
}

// Either side may ping, and the receiver MUST answer with an empty result.
if ($request instanceof PingRequest) {
$this->sendResponse(new Response($request->getId(), new EmptyResult()));

return;
}

$error = Error::forMethodNotFound(
\sprintf('Client does not handle "%s" requests.', $method),
$request->getId()
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Client/ProtocolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,22 @@ public function handle(Request $request): Response
$this->assertInstanceOf(\JsonException::class, $logger->warnings[0]['exception'] ?? null);
}

#[TestDox('answers a ping from the server with an empty result')]
public function testAnswersServerPingWithEmptyResult(): 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('{"jsonrpc": "2.0", "id": "srv-1", "method": "ping"}');

$this->assertSame([['jsonrpc' => '2.0', 'id' => 'srv-1', 'result' => []]], $messages);
}

private function createConfiguration(ProtocolVersion $protocolVersion, ?ProtocolVersion $fallback = ProtocolVersion::V2025_11_25): Configuration
{
return new Configuration(
Expand Down