Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: correct MCP streamable_http_client import name (#10388)

Giovanni Smith-Alvarezbca168f9a4db
diff --git a/marimo/_server/ai/mcp/transport.py b/marimo/_server/ai/mcp/transport.py
index cbbed4ed3d..fca7ec9b38 100644
--- a/marimo/_server/ai/mcp/transport.py
+++ b/marimo/_server/ai/mcp/transport.py
@@ -83,20 +83,42 @@ class StreamableHTTPTransportConnector(MCPTransportConnector):
self, server_def: MCPServerDefinition, exit_stack: AsyncExitStack
) -> TransportConnectorResponse:
# Import MCP SDK components for streamable HTTP transport
- from mcp.client.streamable_http import streamablehttp_client
+ # Try for latest MCP SDK v2.0
+ try:
+ import httpx
- # Type narrowing for mypy
- assert "url" in server_def.config
- config = cast("MCPServerStreamableHttpConfig", server_def.config)
+ from mcp.client.streamable_http import streamable_http_client
- # Establish streamable HTTP connection
- read, write, *_ = await exit_stack.enter_async_context(
- streamablehttp_client(
- config["url"],
- headers=config.get("headers", {}),
- timeout=server_def.timeout,
+ # Type narrowing for mypy
+ assert "url" in server_def.config
+ config = cast("MCPServerStreamableHttpConfig", server_def.config)
+
+ headers = config.get("headers", {})
+ http_client = httpx.AsyncClient(
+ headers=headers, timeout=server_def.timeout
+ )
+
+ # Establish streamable HTTP connection
+ read, write, *_ = await exit_stack.enter_async_context(
+ streamable_http_client(
+ config["url"],
+ http_client=http_client,
+ )
+ )
+ # Fallback to ealierst implementation of MCP SDK v1.8 to v1.28
+ except ImportError:
+ from mcp.client.streamable_http import streamablehttp_client
+
+ assert "url" in server_def.config
+ config = cast("MCPServerStreamableHttpConfig", server_def.config)
+
+ read, write, *_ = await exit_stack.enter_async_context(
+ streamablehttp_client(
+ config["url"],
+ headers=config.get("headers", {}),
+ timeout=server_def.timeout,
+ )
)
- )
return read, write
diff --git a/tests/_server/ai/test_mcp.py b/tests/_server/ai/test_mcp.py
index cd9eb80133..ddcb23720b 100644
--- a/tests/_server/ai/test_mcp.py
+++ b/tests/_server/ai/test_mcp.py
@@ -520,7 +520,7 @@ class TestMCPTransportConnectors:
@pytest.mark.skipif(
not DependencyManager.mcp.has(), reason="MCP SDK not available"
)
- @patch("mcp.client.streamable_http.streamablehttp_client")
+ @patch("mcp.client.streamable_http.streamable_http_client")
async def test_http_connector_connect(self, mock_http_client):
"""Test HTTP transport connector connection."""
# Setup mocks
@@ -554,6 +554,53 @@ class TestMCPTransportConnectors:
assert read == mock_read
assert write == mock_write
+ @pytest.mark.skipif(
+ not DependencyManager.mcp.has(), reason="MCP SDK not available"
+ )
+ @patch("mcp.client.streamable_http.streamablehttp_client")
+ @patch.dict("sys.modules", {})
+ async def test_http_connector_connect_legacy_fallback(
+ self, mock_streamablehttp_client
+ ):
+ """Test HTTP transport connector fallback to streamablehttp_client on ImportError."""
+ mock_read = AsyncMock()
+ mock_write = AsyncMock()
+ mock_context = AsyncMock()
+ mock_context.__aenter__ = AsyncMock(
+ return_value=(mock_read, mock_write)
+ )
+ mock_context.__aexit__ = AsyncMock(return_value=None)
+ mock_streamablehttp_client.return_value = mock_context
+
+ connector = StreamableHTTPTransportConnector()
+ config = MCPServerStreamableHttpConfig(
+ url="https://api.example.com/mcp",
+ headers={"Authorization": "Bearer token"},
+ timeout=30.0,
+ )
+ server_def = MCPServerDefinition(
+ name="test",
+ transport=MCPTransportType.STREAMABLE_HTTP,
+ config=config,
+ timeout=30.0,
+ )
+
+ from contextlib import AsyncExitStack
+
+ with patch(
+ "mcp.client.streamable_http.streamable_http_client",
+ side_effect=ImportError,
+ ):
+ async with AsyncExitStack() as exit_stack:
+ read, write = await connector.connect(server_def, exit_stack)
+ assert read == mock_read
+ assert write == mock_write
+ mock_streamablehttp_client.assert_called_once_with(
+ "https://api.example.com/mcp",
+ headers={"Authorization": "Bearer token"},
+ timeout=30.0,
+ )
+
class TestMCPClientConfiguration:
"""Test cases for MCPClient configuration parsing and initialization."""