Skip to content

Commit f879431

Browse files
authored
Merge pull request #40 from Wolfgang-check24/fix-protocol-version-check
Fix protocol version check
2 parents 3395f33 + 6570fc9 commit f879431

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

src/Server/MCPServer.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,10 @@ public function initialize(InitializeData $data): InitializeResource
205205

206206
$this->initialized = true;
207207

208-
$protocolVersion = $data->protocolVersion ?? MCPProtocolInterface::PROTOCOL_VERSION_SSE;
208+
// Validate and determine the protocol version to use
209+
$requestedProtocolVersion = $data->protocolVersion ?? MCPProtocolInterface::PROTOCOL_VERSION_SSE;
210+
$protocolVersion = $this->getValidatedProtocolVersion($requestedProtocolVersion);
211+
209212
$hasSamplingCapability = isset($data->capabilities['sampling']);
210213
$this->protocol->setClientSamplingCapability($hasSamplingCapability);
211214

@@ -217,6 +220,36 @@ public function initialize(InitializeData $data): InitializeResource
217220
);
218221
}
219222

223+
/**
224+
* Validates the requested protocol version and returns a supported version.
225+
*
226+
* @param string $requestedVersion The protocol version requested by the client
227+
* @return string A supported protocol version
228+
* @throws JsonRpcErrorException If the requested protocol version is not supported
229+
*/
230+
private function getValidatedProtocolVersion(string $requestedVersion): string
231+
{
232+
$supportedVersions = [
233+
MCPProtocolInterface::PROTOCOL_VERSION_SSE,
234+
MCPProtocolInterface::PROTOCOL_VERSION_STREAMABE_HTTP,
235+
];
236+
237+
// If the requested version is supported, return it
238+
if (in_array($requestedVersion, $supportedVersions, true)) {
239+
return $requestedVersion;
240+
}
241+
242+
// Throw error for unsupported protocol version
243+
throw new JsonRpcErrorException(
244+
message: 'Unsupported protocol version',
245+
code: JsonRpcErrorCode::INVALID_PARAMS,
246+
data: [
247+
'supported' => $supportedVersions,
248+
'requested' => $requestedVersion,
249+
]
250+
);
251+
}
252+
220253
/**
221254
* Forwards a request message to a specific client via the protocol handler.
222255
* Used for server-initiated requests to the client (if supported by the protocol/transport).

tests/Server/MCPServerTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,4 +542,37 @@ public function test_initialize_sets_client_sampling_capability_when_enabled():
542542
$this->assertSame(['mock-capability' => true], $initializeResource->capabilities);
543543
$this->assertEquals($serverInfo, $initializeResource->serverInfo);
544544
}
545+
546+
/**
547+
* Tests that the initialize method throws an exception for unsupported protocol versions
548+
*/
549+
public function test_initialize_throws_exception_for_unsupported_protocol_version(): void
550+
{
551+
// Arrange
552+
$mockProtocol = $this->createMock(MCPProtocolInterface::class);
553+
$serverInfo = ['name' => 'TestServer', 'version' => '1.0'];
554+
$mockCapabilities = $this->createMock(ServerCapabilitiesInterface::class);
555+
556+
$mockProgressNotifierRepository = $this->createMock(ProgressNotifierRepository::class);
557+
$server = MCPServer::create($mockProtocol, $mockProgressNotifierRepository, $serverInfo['name'], $serverInfo['version'], $mockCapabilities);
558+
$unsupportedVersion = '2025-12-01';
559+
$initializeData = new InitializeData('2.0', ['mock-capability' => true], $unsupportedVersion);
560+
561+
// Act & Assert
562+
try {
563+
$server->initialize($initializeData);
564+
$this->fail('Expected JsonRpcErrorException not thrown');
565+
} catch (JsonRpcErrorException $e) {
566+
$this->assertEquals(-32602, $e->getJsonRpcErrorCode());
567+
$this->assertEquals('Unsupported protocol version', $e->getMessage());
568+
569+
$errorData = $e->getErrorData();
570+
$this->assertIsArray($errorData);
571+
$this->assertArrayHasKey('supported', $errorData);
572+
$this->assertArrayHasKey('requested', $errorData);
573+
$this->assertEquals($unsupportedVersion, $errorData['requested']);
574+
$this->assertContains(MCPProtocolInterface::PROTOCOL_VERSION_SSE, $errorData['supported']);
575+
$this->assertContains(MCPProtocolInterface::PROTOCOL_VERSION_STREAMABE_HTTP, $errorData['supported']);
576+
}
577+
}
545578
}

0 commit comments

Comments
 (0)