Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
fix(mcp): let streamable HTTP read timeout cover tool_timeout
Max Hsud9ceae15bec6
diff --git a/nanobot/agent/tools/mcp.py b/nanobot/agent/tools/mcp.pyindex 35ab9aa90..e4f5bf351 100644--- a/nanobot/agent/tools/mcp.py+++ b/nanobot/agent/tools/mcp.py@@ -52,6 +52,10 @@ _TRANSIENT_EXC_NAMES: frozenset[str] = frozenset((_WINDOWS_SHELL_LAUNCHERS: frozenset[str] = frozenset(("npx", "npm", "pnpm", "yarn", "bunx"))+# Mirror the MCP SDK's httpx defaults (MCP_DEFAULT_TIMEOUT / MCP_DEFAULT_SSE_READ_TIMEOUT).+_HTTP_TIMEOUT = 30.0+_HTTP_READ_TIMEOUT = 300.0+# Characters allowed in tool names by model providers (Anthropic, OpenAI, etc.).# Replace anything outside [a-zA-Z0-9_-] with underscore and collapse runs._SANITIZE_RE = re.compile(r"_+")@@ -1120,7 +1124,13 @@ async def connect_mcp_servers("headers": cfg.headers or None,"event_hooks": {"request": [_validate_mcp_request_url]},"follow_redirects": True,- "timeout": httpx.Timeout(30.0, connect=10.0),+ # Read must outlast the tool call itself; otherwise a slow tool+ # fails with ReadTimeout before tool_timeout is reached.+ "timeout": httpx.Timeout(+ _HTTP_TIMEOUT,+ connect=10.0,+ read=max(_HTTP_READ_TIMEOUT, cfg.tool_timeout),+ ),**_pinned_transport_kwargs(),}if oauth_auth is not None:diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.pyindex 5a8b9cce6..0ac205d3b 100644--- a/tests/tools/test_mcp_tool.py+++ b/tests/tools/test_mcp_tool.py@@ -1484,9 +1484,18 @@ async def test_connect_mcp_servers_rolls_back_completed_batch_on_cancellation(@pytest.mark.asyncio+ ("tool_timeout", "expected_read"),+ [+ (30, 300.0), # default: match the MCP SDK's SSE read timeout+ (600, 600.0), # long-running tools: read must not cut the call short+ ],+)async def test_connect_mcp_servers_streamable_http_uses_finite_timeout(fake_mcp_runtime: dict[str, object | None],monkeypatch: pytest.MonkeyPatch,+ tool_timeout: int,+ expected_read: float,) -> None:fake_mcp_runtime["session"] = _make_fake_session(["demo"])captured: dict[str, object] = {}@@ -1517,7 +1526,11 @@ async def test_connect_mcp_servers_streamable_http_uses_finite_timeout(registry = ToolRegistry()stacks = await connect_mcp_servers(- {"test": MCPServerConfig(url="https://mcp.example.com/mcp")},+ {+ "test": MCPServerConfig(+ url="https://mcp.example.com/mcp", tool_timeout=tool_timeout+ )+ },registry,)for stack in stacks.values():@@ -1525,7 +1538,7 @@ async def test_connect_mcp_servers_streamable_http_uses_finite_timeout(timeout = captured["timeout"]assert timeout.connect == 10.0- assert timeout.read == 30.0+ assert timeout.read == expected_readassert timeout.write == 30.0assert timeout.pool == 30.0