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: