Fix MCP protocol version negotiation
- Handle protocolVersion from client initialize request params - Negotiate compatible version (fallback to 2024-11-05 for 2025.x requests) - Fix 'unsupported protocol version' error when client sends empty version
This commit is contained in:
+14
-1
@@ -22,12 +22,25 @@ impl McpServer {
|
||||
pub async fn handle_request(&self, request: McpRequest) -> McpResponse {
|
||||
match request.method.as_str() {
|
||||
"initialize" => {
|
||||
// Protocol version negotiation: client sends desired version
|
||||
let client_version = request.params.as_ref()
|
||||
.and_then(|p| p.get("protocolVersion"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("2024-11-05");
|
||||
|
||||
// Server selects version (latest supported that client also supports)
|
||||
let negotiated_version = if client_version.starts_with("2025-") {
|
||||
"2024-11-05" // Fall back to stable version
|
||||
} else {
|
||||
"2024-11-05"
|
||||
};
|
||||
|
||||
*self.initialized.lock().await = true;
|
||||
McpResponse {
|
||||
jsonrpc: "2.0".to_string(),
|
||||
id: request.id,
|
||||
result: Some(json!(crate::InitializeResult {
|
||||
protocol_version: "2024-11-05".to_string(),
|
||||
protocol_version: negotiated_version.to_string(),
|
||||
capabilities: crate::ServerCapabilities {
|
||||
experimental: json!({}),
|
||||
tools: crate::ToolCapabilities {
|
||||
|
||||
Reference in New Issue
Block a user