test(mcp): keep reconnect fixture idle across SDK versions

MCP SDK 1.30 treats an open GET notification stream as an in-flight request, so the real-server fixture no longer expires its session and the shutdown regression waits forever for a reconnect that never starts.

Decline the optional notification stream with the protocol-defined HTTP 405 response while retaining real POST, DELETE, lifespan, idle expiry and reconnect handling. Assert that successful recovery replaces the tool wrapper so the first regression cannot pass without reconnecting.

Reproduced the failure with SDK 1.30 before the repair. Both 1.29 and 1.30 now pass, including five repeated 1.30 runs and 141 related MCP tests. Runtime code and timeout limits are unchanged.
This commit is contained in:
Xubin Ren committed 2026-09-08 10:04:15 +08:00
1 parent ddf095938e
commit 2363530891
1 file changed
+16 -1
+16 -1
View File
@@ -44,8 +44,11 @@ def _run_mcp_server(port: int, ready_event: multiprocessing.Event) -> None:
The server exposes a single ``greet`` tool and terminates idle sessions
after ``_IDLE_TIMEOUT_SECONDS``.
"""
import uvicorn
from mcp.server.fastmcp import FastMCP
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
from starlette.responses import Response
from starlette.types import Receive, Scope, Send
mcp = FastMCP("IdleTimeoutDemo", json_response=True, port=port)
@@ -62,8 +65,19 @@ def _run_mcp_server(port: int, ready_event: multiprocessing.Event) -> None:
session_idle_timeout=_IDLE_TIMEOUT_SECONDS,
)
app = mcp.streamable_http_app()
async def without_notification_stream(scope: Scope, receive: Receive, send: Send) -> None:
# A standalone GET stream keeps a session active in MCP SDK 1.30+.
# This fixture needs a genuinely idle session: decline the optional
# notification stream, but keep real POST/DELETE and lifespan handling.
if scope["type"] == "http" and scope["method"] == "GET":
await Response(status_code=405, headers={"Allow": "POST, DELETE"})(scope, receive, send)
else:
await app(scope, receive, send)
ready_event.set()
mcp.run(transport="streamable-http")
uvicorn.run(without_notification_stream, host="127.0.0.1", port=port)
async def _wait_for_server(url: str, timeout: float = 10.0) -> bool:
@@ -175,6 +189,7 @@ async def test_mcp_reconnect_after_session_timeout(tmp_path, mcp_server_url):
output = await asyncio.create_task(tool.execute(name="second"))
assert "Hello, second" in output
assert registry.get("mcp_repro_greet") is not tool # Recovery must replace the session's wrapper.
await asyncio.create_task(provider.aclose())