Merge pull request #324 from virgil-at-biocompute/fix/mcp-protocol-version-negotiation
fix: negotiate MCP protocol version instead of hardcoding
This commit is contained in:
+15
-1
@@ -837,17 +837,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__},
|
||||
},
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user