Skip to content

Commit cb06f58

Browse files
fixes Protocol is not evaluated on client request #38
1 parent 3395f33 commit cb06f58

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-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).

0 commit comments

Comments
 (0)