fix(mcp): respect stateful routing and harden initialize detection

Ensure streamable MCP requests are dispatched via the computed target session manager, and guard initialize detection against non-object JSON bodies. Update stale-session test patches to target the stateful manager so routing assertions remain correct.

Made-with: Cursor
This commit is contained in:
Sameer Kankute 2026-04-30 16:42:23 +05:30
parent ad887c9157
commit 1a2aa62c0f
No known key found for this signature in database
2 changed files with 4 additions and 4 deletions

View file

@ -2544,7 +2544,7 @@ if MCP_AVAILABLE:
return False
try:
data = json.loads(body)
return data.get("method") == "initialize"
return isinstance(data, dict) and data.get("method") == "initialize"
except (json.JSONDecodeError, TypeError):
return False
@ -2848,7 +2848,7 @@ if MCP_AVAILABLE:
mcp_servers,
_client_ip,
):
await session_manager.handle_request(scope, receive, send)
await target_manager.handle_request(scope, receive, send)
except HTTPException:
# Re-raise HTTP exceptions to preserve status codes and details
raise

View file

@ -384,12 +384,12 @@ async def test_valid_mcp_session_id_is_preserved():
True,
),
patch.object(
session_manager,
session_manager_stateful,
"handle_request",
side_effect=mock_handle_request,
),
patch.object(
session_manager,
session_manager_stateful,
"_server_instances",
mock_instances,
),