Skip to content

The agent that grows with you

fix(mcp): accept both SDK generations' streamable-HTTP transport arity

elphamale2e1d724e3ebd
diff --git a/tests/tools/test_mcp_streamable_http_arity.py b/tests/tools/test_mcp_streamable_http_arity.py
new file mode 100644
index 00000000000..49dfd96d255
--- /dev/null
+++ b/tests/tools/test_mcp_streamable_http_arity.py
@@ -0,0 +1,125 @@
+"""The streamable-HTTP transport must accept both SDK generations' arity.
+
+``streamable_http_client`` yields ``(read, write, get_session_id)`` on mcp 1.x
+and ``(read, write)`` on mcp 2.x. ``_run_http`` unpacked a fixed 3-tuple, which
+is 1.x's shape, so on 2.x every HTTP and SSE server failed its handshake with
+``ValueError: not enough values to unpack (expected 3, got 2)`` and parked after
+exhausting its retry ladder.
+
+It survived review because the existing coverage
+(``test_mcp_client_cert.py``) fakes the transport with a 3-tuple — encoding the
+old shape into the test — and because the common server configs are stdio,
+which is a different code path entirely. So the assertion that matters is not
+"does the happy path work" but "does it work for *each* arity the supported SDK
+range actually yields".
+"""
+
+from __future__ import annotations
+
+import asyncio
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+
+def _patch_sdk_async_client(dummy):
+ from tools.mcp_tool import sdk_httpx
+
+ return patch.object(sdk_httpx(), "AsyncClient", dummy)
+
+
+class _DummyAsyncClient:
+ def __init__(self, **kwargs):
+ pass
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, *a):
+ return False
+
+
+class _DummySession:
+ def __init__(self, *args, **kwargs):
+ pass
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, *a):
+ return False
+
+ async def initialize(self):
+ return None
+
+
+def _transport_yielding(*values):
+ class _Ctx:
+ async def __aenter__(self):
+ return values
+
+ async def __aexit__(self, *a):
+ return False
+
+ return _Ctx()
+
+
[email protected]("sdk,streams", [
+ ("mcp 2.x", (MagicMock(), MagicMock())),
+ ("mcp 1.x", (MagicMock(), MagicMock(), (lambda: None))),
+])
+def test_run_http_accepts_the_arity_each_sdk_generation_yields(sdk, streams):
+ from tools.mcp_tool import MCPServerTask
+
+ server = MCPServerTask("remote")
+ seen: dict = {}
+
+ async def _discover_tools(self):
+ seen["connected"] = True
+ self._shutdown_event.set()
+
+ async def _drive():
+ with patch("tools.mcp_tool._MCP_HTTP_AVAILABLE", True), \
+ patch("tools.mcp_tool._MCP_NEW_HTTP", True), \
+ _patch_sdk_async_client(_DummyAsyncClient), \
+ patch("tools.mcp_tool.streamable_http_client",
+ return_value=_transport_yielding(*streams)), \
+ patch("tools.mcp_tool.ClientSession", _DummySession), \
+ patch.object(MCPServerTask, "_discover_tools", _discover_tools):
+ await server._run_http({"url": "https://example.com/mcp"})
+
+ asyncio.run(_drive())
+
+ assert seen.get("connected") is True, f"handshake never completed on {sdk}"
+ assert server._error is None, f"{sdk}: {server._error!r}"
+
+
+def test_the_session_streams_are_the_first_two_yielded():
+ """Positional, not named: 1.x's third element is not a stream."""
+ from tools.mcp_tool import MCPServerTask
+
+ server = MCPServerTask("remote")
+ read, write = MagicMock(), MagicMock()
+ passed: dict = {}
+
+ class _CapturingSession(_DummySession):
+ def __init__(self, *args, **kwargs):
+ passed["args"] = args
+ super().__init__(*args, **kwargs)
+
+ async def _discover_tools(self):
+ self._shutdown_event.set()
+
+ async def _drive():
+ with patch("tools.mcp_tool._MCP_HTTP_AVAILABLE", True), \
+ patch("tools.mcp_tool._MCP_NEW_HTTP", True), \
+ _patch_sdk_async_client(_DummyAsyncClient), \
+ patch("tools.mcp_tool.streamable_http_client",
+ return_value=_transport_yielding(read, write, (lambda: None))), \
+ patch("tools.mcp_tool.ClientSession", _CapturingSession), \
+ patch.object(MCPServerTask, "_discover_tools", _discover_tools):
+ await server._run_http({"url": "https://example.com/mcp"})
+
+ asyncio.run(_drive())
+
+ assert passed["args"][:2] == (read, write)
diff --git a/tools/mcp_tool.py b/tools/mcp_tool.py
index 82d9313404a..bd418745bbc 100644
--- a/tools/mcp_tool.py
+++ b/tools/mcp_tool.py
@@ -316,7 +316,14 @@ def _ensure_mcp_sdk() -> bool:
# deprecated `streamablehttp_client` alias, so gating on that name
# alone made _run_http raise ImportError for every HTTP and SSE
# server on 2.x before it could reach the `streamable_http_client`
- # path that does work.
+ # path.
+ #
+ # Reaching it was necessary and not sufficient: that path also
+ # unpacked the transport as a fixed 3-tuple, which is 1.x's shape.
+ # On 2.x it raised "not enough values to unpack (expected 3, got
+ # 2)" and every HTTP/SSE server parked after its retry ladder.
+ # Only stdio servers kept working, which is why this survived
+ # review - the common configs are all stdio.
_MCP_HTTP_AVAILABLE = _MCP_NEW_HTTP or _MCP_LEGACY_HTTP
try:
from mcp.types import LATEST_PROTOCOL_VERSION
@@ -3351,9 +3358,12 @@ class MCPServerTask:
# http_client is provided, so we wrap in async-with.
try:
async with httpx.AsyncClient(**client_kwargs) as http_client:
- async with streamable_http_client(url, http_client=http_client) as (
- read_stream, write_stream, _get_session_id,
- ):
+ # Unpacked positionally rather than by fixed arity: mcp
+ # 1.x yields (read, write, get_session_id) and 2.x yields
+ # (read, write). This file supports both SDK generations,
+ # and get_session_id was never used here.
+ async with streamable_http_client(url, http_client=http_client) as _streams:
+ read_stream, write_stream = _streams[0], _streams[1]
async with ClientSession(read_stream, write_stream, **sampling_kwargs) as session:
# Bound the handshake (#59349) — see stdio path.
self.initialize_result = await asyncio.wait_for(