fix(mcp): let streamable HTTP read timeout cover tool_timeout

The streamable HTTP client used httpx.Timeout(30.0, connect=10.0), so the
read timeout was fixed at 30s regardless of tool_timeout. A server that
stays silent until the result is ready (e.g. FastMCP json_response=True)
hit ReadTimeout after 30s; the tool call then hung until tool_timeout
fired and reported "(MCP tool call timed out after Ns)".

Use the MCP SDK's own httpx defaults (30s, read=300s) and raise the read
timeout to tool_timeout when it is larger. Connect stays at 10s.

Fixes #6065
This commit is contained in:
Max Hsu authored and chengyongru committed 2026-10-06 01:41:10 +08:00
1 parent 63bdd40280
commit d9ceae15be
2 files changed
+26 -3

No files matched your search

+11 -1
View File
@@ -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:
+15 -2
View File
@@ -1484,9 +1484,18 @@ async def test_connect_mcp_servers_rolls_back_completed_batch_on_cancellation(
@pytest.mark.asyncio
@pytest.mark.parametrize(
("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_read
assert timeout.write == 30.0
assert timeout.pool == 30.0