From 950d52baf2eb7ffb8d3e48abf2bbd1ef2117d6fe Mon Sep 17 00:00:00 2001 From: virgil-at-biocompute <254577601+virgil-at-biocompute@users.noreply.github.com> Date: Wed, 8 Apr 2026 22:57:32 -0400 Subject: [PATCH] fix: negotiate MCP protocol version instead of hardcoding The initialize handler hardcoded protocolVersion "2024-11-05", which causes newer MCP clients (e.g. Claude Code) to reject the connection when they negotiate "2025-11-25" or later. Echo the client's requested version if it is in the supported set, otherwise fall back to the latest supported version. This keeps backwards compatibility with older clients while allowing newer ones to connect. Co-Authored-By: Claude Opus 4.6 (1M context) --- mempalace/mcp_server.py | 16 ++++++++++++++- tests/test_mcp_server.py | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/mempalace/mcp_server.py b/mempalace/mcp_server.py index 7d263a6..7969d40 100644 --- a/mempalace/mcp_server.py +++ b/mempalace/mcp_server.py @@ -717,17 +717,31 @@ TOOLS = { } +SUPPORTED_PROTOCOL_VERSIONS = [ + "2025-11-25", + "2025-06-18", + "2025-03-26", + "2024-11-05", +] + + def handle_request(request): method = request.get("method", "") params = request.get("params", {}) req_id = request.get("id") if method == "initialize": + client_version = params.get("protocolVersion", SUPPORTED_PROTOCOL_VERSIONS[-1]) + negotiated = ( + client_version + if client_version in SUPPORTED_PROTOCOL_VERSIONS + else SUPPORTED_PROTOCOL_VERSIONS[0] + ) return { "jsonrpc": "2.0", "id": req_id, "result": { - "protocolVersion": "2024-11-05", + "protocolVersion": negotiated, "capabilities": {"tools": {}}, "serverInfo": {"name": "mempalace", "version": __version__}, }, diff --git a/tests/test_mcp_server.py b/tests/test_mcp_server.py index 24258a9..3f7b1c2 100644 --- a/tests/test_mcp_server.py +++ b/tests/test_mcp_server.py @@ -42,6 +42,50 @@ class TestHandleRequest: assert resp["result"]["serverInfo"]["name"] == "mempalace" assert resp["id"] == 1 + def test_initialize_negotiates_client_version(self): + from mempalace.mcp_server import handle_request + + resp = handle_request( + { + "method": "initialize", + "id": 1, + "params": {"protocolVersion": "2025-11-25"}, + } + ) + assert resp["result"]["protocolVersion"] == "2025-11-25" + + def test_initialize_negotiates_older_supported_version(self): + from mempalace.mcp_server import handle_request + + resp = handle_request( + { + "method": "initialize", + "id": 1, + "params": {"protocolVersion": "2025-03-26"}, + } + ) + assert resp["result"]["protocolVersion"] == "2025-03-26" + + def test_initialize_unknown_version_falls_back_to_latest(self): + from mempalace.mcp_server import handle_request + + resp = handle_request( + { + "method": "initialize", + "id": 1, + "params": {"protocolVersion": "9999-12-31"}, + } + ) + from mempalace.mcp_server import SUPPORTED_PROTOCOL_VERSIONS + + assert resp["result"]["protocolVersion"] == SUPPORTED_PROTOCOL_VERSIONS[0] + + def test_initialize_missing_version_uses_oldest(self): + from mempalace.mcp_server import handle_request, SUPPORTED_PROTOCOL_VERSIONS + + resp = handle_request({"method": "initialize", "id": 1, "params": {}}) + assert resp["result"]["protocolVersion"] == SUPPORTED_PROTOCOL_VERSIONS[-1] + def test_notifications_initialized_returns_none(self): from mempalace.mcp_server import handle_request