Skip to content

Commit cbf7ea4

Browse files
cliffhallclaude
andcommitted
Serve only the legacy era from the Python servers
SDK v2's Server.run() drives a dual-era loop, so the port also answered server/discover and 2026-07-28 per-request envelopes. That changes the wire, and adopting 2026-07-28 is modelcontextprotocol#4853, not modelcontextprotocol#4851. Each server now runs the SDK's handshake-only serve_loop (what run() uses minus era routing) inside the server lifespan, so an enveloped request gets "Method not found" as it did on SDK v1. Each suite gains a protocol test pinning that refusal; git's raise_exceptions test (modelcontextprotocol#4658) now patches serve_loop instead of Server.run. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 7355509 commit cbf7ea4

2 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎src/mcp_server_fetch/server.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import readabilipy.simple_json
1313
from mcp.shared.exceptions import MCPError
1414
from mcp.server import Server, ServerRequestContext
15+
from mcp.server.runner import serve_loop
1516
from mcp.server.stdio import stdio_server
1617
from mcp.types import (
1718
CallToolRequestParams,
@@ -536,4 +537,15 @@ async def get_prompt(
536537

537538
options = server.create_initialization_options()
538539
async with stdio_server() as (read_stream, write_stream):
539-
await server.run(read_stream, write_stream, options, raise_exceptions=False)
540+
# Legacy era only. Server.run() would also serve 2026-07-28 (its
541+
# dual-era loop answers server/discover and per-request envelopes);
542+
# adopting that era is #4853, so this port (#4851) serves the
543+
# handshake loop alone and keeps the wire unchanged.
544+
async with server.lifespan(server) as lifespan_state:
545+
await serve_loop(
546+
server,
547+
read_stream,
548+
write_stream,
549+
lifespan_state=lifespan_state,
550+
init_options=options,
551+
)

‎tests/test_protocol.py‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,25 @@
1616
from __future__ import annotations
1717

1818
import os
19+
from collections.abc import AsyncIterator
20+
from contextlib import asynccontextmanager
1921
from importlib.metadata import version
2022
from typing import Any
23+
from unittest.mock import patch
2124

25+
import anyio
2226
import httpx
2327
import pytest
2428
from mcp import ClientSession
2529
from mcp.shared.exceptions import MCPError
26-
from mcp.types import INVALID_PARAMS, CallToolResult
30+
from mcp.shared.memory import create_client_server_memory_streams
31+
from mcp.shared.message import SessionMessage
32+
from mcp.types import INVALID_PARAMS, METHOD_NOT_FOUND, CallToolResult, JSONRPCRequest
2733

2834
from mcp_server_fetch.server import (
2935
DEFAULT_USER_AGENT_AUTONOMOUS,
3036
DEFAULT_USER_AGENT_MANUAL,
37+
serve,
3138
)
3239

3340
from .harness import FakeWeb, connect
@@ -998,3 +1005,55 @@ async def test_get_prompt_does_not_validate_the_url(
9981005
assert data["messages"][0]["content"]["text"].startswith(
9991006
"Failed to fetch not a url: UnsupportedProtocol("
10001007
)
1008+
1009+
1010+
# ---------------------------------------------------------------------------
1011+
# Protocol era
1012+
# ---------------------------------------------------------------------------
1013+
1014+
1015+
async def test_modern_era_is_not_served() -> None:
1016+
# #4851: the SDK v2 port serves the 2025-11-25 handshake era only.
1017+
# Server.run() would also open a 2026-07-28 connection for a request that
1018+
# carries the per-request `_meta` envelope; adopting that era is #4853.
1019+
# Until then such a request is refused as it was on SDK v1.
1020+
discover = JSONRPCRequest(
1021+
jsonrpc="2.0",
1022+
id=1,
1023+
method="server/discover",
1024+
params={
1025+
"_meta": {
1026+
"io.modelcontextprotocol/protocolVersion": "2026-07-28",
1027+
"io.modelcontextprotocol/clientInfo": {"name": "test", "version": "0"},
1028+
"io.modelcontextprotocol/clientCapabilities": {},
1029+
}
1030+
},
1031+
)
1032+
response: SessionMessage | Exception | None = None
1033+
async with create_client_server_memory_streams() as (
1034+
(client_read, client_write),
1035+
server_streams,
1036+
):
1037+
1038+
@asynccontextmanager
1039+
async def fake_stdio_server() -> AsyncIterator[Any]:
1040+
yield server_streams
1041+
1042+
with patch("mcp_server_fetch.server.stdio_server", fake_stdio_server):
1043+
async with anyio.create_task_group() as tg:
1044+
tg.start_soon(serve)
1045+
await client_write.send(SessionMessage(discover))
1046+
with anyio.fail_after(5):
1047+
response = await client_read.receive()
1048+
tg.cancel_scope.cancel()
1049+
1050+
assert isinstance(response, SessionMessage)
1051+
assert response.message.model_dump(by_alias=True, exclude_none=True) == {
1052+
"jsonrpc": "2.0",
1053+
"id": 1,
1054+
"error": {
1055+
"code": METHOD_NOT_FOUND,
1056+
"message": "Method not found",
1057+
"data": "server/discover",
1058+
},
1059+
}

0 commit comments

Comments
 (0)