fix(mcp): fail fast on HTML content-type instead of waiting full connect_timeout - #36058
fix(mcp): fail fast on HTML content-type instead of waiting full connect_timeout#36058liuhao1024 wants to merge 1 commit into
Conversation
…ect_timeout A misconfigured MCP server URL that returns text/html (e.g. pointing at a web app root instead of an MCP endpoint) causes the MCP SDK to block for the full connect_timeout (default 60 s) before surfacing CancelledError. Add a lightweight HEAD pre-flight check that detects text/html responses in ≤5 s and raises ConnectionError with an actionable message. Non-HTML responses, missing headers, and network errors pass through silently so the normal MCP handshake proceeds unaffected. Fixes NousResearch#36052
|
@stanleycyang Thanks for the review. I see #36064 handles the content-type check in a different path and has green CI. Since both PRs are still open, I'll wait for the maintainer's decision. If #36064 is preferred, I'm happy to close this PR. |
|
Your preflight-probe approach was the architecturally correct one — probing on a separate httpx client before the SDK connects sidesteps the The salvage hardened it on top: allow-list detection ( |
What does this PR do?
Adds a lightweight HTTP pre-flight check to
MCPServerTask._run_http()that detects misconfigured MCP server URLs returningtext/html(e.g. pointing at a web app root) and fails fast with an actionable error message, instead of waiting the fullconnect_timeout(default 60 s) before surfacingCancelledError.Related Issue
Fixes #36052
Type of Change
Changes Made
tools/mcp_tool.py: Added_preflight_content_type()static method onMCPServerTaskthat sends a HEAD request (with GET fallback on 405) and raisesConnectionErrorif the responseContent-Typeistext/html. Called from_run_http()before the MCP SDK handshake.tests/tools/test_mcp_preflight_content_type.py: 6 tests covering HTML rejection (HEAD + GET fallback), JSON acceptance, missing Content-Type, network error passthrough, and header/verify forwarding.How to Test
config.yaml:MCP server 'https://example.com' returned Content-Type: text/html; ...pytest tests/tools/test_mcp_preflight_content_type.py -vChecklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/ACode Intelligence
MCPServerTask._run_http(callers: 2 —_rundispatcher, SSE skip path)ConnectionError) are swallowed to preserve existing behavior