Repository navigation
Conversation
The mcp client streams requests and calls raise_for_status() before reading the body, so HTTPStatusError.response.text raised ResponseNotRead and /api/mcp/test showed only httpx's generic message; GitLab's "MCP server not enabled for any of your groups" was lost. - SDK: every remote MCP server (streamable HTTP and SSE) now gets an httpx client whose response hook reads 4xx/5xx bodies. _PackageRemoteMCPServer becomes _HookedRemoteMCPServer and keeps the Agent Plugins off-origin header drop (section 7.2.1) for package servers. - agent-server: /api/mcp/test handles HTTPStatusError and returns "HTTP <status> <phrase> from <url>: <reason>", taking the reason from the WWW-Authenticate error_description, common JSON body fields, then the raw body. The URL drops userinfo, query and fragment; error_kind stays "unknown". Refs OpenHands#5276 Co-authored-by: openhands <openhands@all-hands.dev>
Design doc, Agent Canvas before/after screenshots, agent-server log excerpts and the Agent Plugins matrix script. The .pr/ directory is PR-only and removed by the PR Artifacts workflow. Refs OpenHands#5276 Co-authored-by: openhands <openhands@all-hands.dev>
|
📁 PR Artifacts Notice This PR adds or changes temporary PR-specific documents under |
…n errors Merge main and rework the fix for the mcp 2.x client, which reports an HTTP error from a streamable-HTTP server as 'Server returned an error response', 'Session terminated' or 'Not Found' and drops the response. Every remote server's HTTP client now records its error responses, and create_mcp_tools raises MCPServerHTTPError (status, origin, server reason) from the original error. /api/mcp/test returns that message. Fixes OpenHands#5276 Co-authored-by: openhands <openhands@all-hands.dev>
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope: The change belongs here — MCP tool-creation behavior and the /api/mcp/test error contract are owned by this repository (SDK + Agent Server per AGENTS.md cross-repository boundaries).
Code review result: I verified the implementation against the repository guidance and found no material correctness, security, or compatibility defect on head 5d5bbb5a:
MCPServerHTTPErrorsubclassesMCPErrorand is raised with__cause__chained; the router's probe handler catches it before the genericMCPErrorbranch (verified handler order), so the server's status/reason reaches both the direct/api/mcp/testresponse and the OAuth status endpoint, which forwardserror/error_kindfrom the failure result.- The
_HTTPErrorLogresponse hook awaitsaread()before storing, so the SSE path's laterHTTPStatusErrorhandling can read.textwithout hittingResponseNotRead(verified empirically on the httpx2 response objects this code produces). - Reason extraction follows the documented precedence (WWW-Authenticate
error_description→ JSON body fields → WWW-Authenticateerror→ raw body) with a 300-char cap and whitespace normalization;extract_field_from_www_authsemantics verified against the installedmcpversion. - The origin-only URL (
scheme://host:port) keeps userinfo, query, and path out of the new error message, matching the acceptance criteria in #5276; the message itself carries no credentials. - The
_PackageRemoteMCPServer→_RemoteMCPServerrename preserves the Agent Plugins §7.2.1 off-origin header drop, now gated on_bind_headers_to_origin(set only for package-declared servers withliteral_values), and extends the error-recording client factory to both streamable-HTTP and SSE transports. No other code references the old class name. - Multi-server configs record per-server error logs and the message names the origin it came from, so a wrong-server attribution cannot present a false URL.
- Workspace test runs:
tests/agent_server/test_mcp_router.py52/52 passed (includingtest_mcp_test_reports_the_reason_a_server_rejected_the_connection),tests/sdk/plugin431 passed, the new focused MCP tests (http_error,stand_in,reason) 12/12 passed,tests/sdk/conversation/test_local_conversation_mcp.py12/13 with the single failure (test_slow_mcp_servers_start_side_by_side) reproducing identically onmain(pre-existing timing-sensitive test, untouched by this diff).ruff check,ruff format --check, andpyrightare clean on all changed files.
Blocking finding (non-code, must be resolved before merge): The required Validate PR description check fails on this exact head (run 43541290077): "Linked issue(s) (#5276) carry neither ready-for-dev nor a pre-rollout creation date." Issue #5276 was created 2026-09-23 (after the 2026-08-13 rollout date in .github/scripts/check_pr_description.py) and currently carries no labels, so the PR's mergeable_state is blocked. This gate is owned by the deterministic automation/maintainers, not by code in this diff, and the technical work itself is ready; the linked issue needs the ready-for-dev label (or a maintainer-granted exception per the readiness policy) before this PR can merge.
No inline findings: the two notable hardening observations (the chained __cause__ HTTPStatusError still embeds the full config URL for non-router consumers, and the stand-in mapping relies on mcp client message strings) are suggestions/defense-in-depth notes rather than demonstrated defects, since every in-repo consumer scrubs and no caller depends on the raw stand-in types.
🔄 CHANGES REQUESTED
|
@neubig Thanks for taking this over and reworking it for the I retested
For comparison, current I noticed that |
HUMAN:
#5276 I have reproduced the issue in AgentCanvas with gitlab.com using two of my accounts. I have made efforts to make the code change cleaner and more maintainable. I have also done comparison tests before and after the code change.
AGENT:
Why
An MCP server can reject a connection with an HTTP error that explains the problem. In #5276, GitLab completed OAuth and then answered
403 {"message":"403 Forbidden - MCP server not enabled for any of your groups"}. OpenHands hid that reason. On currentmain, themcp2.x client turns an HTTP error from a streamable-HTTP server intoServer returned an error response,Session terminatedorNot Foundand drops the response. For SSE it raises anHTTPStatusErrorwhose body was never read. Agent Canvas showedConnection failed: MCPError: Server returned an error response, and the agent-server log showed onlyMCP test failed unexpectedly.Summary
openhands/sdk/mcp/utils.py): the HTTP client of every remote MCP server (streamable HTTP and SSE) records the last 4xx/5xx response and reads its body. AGETanswered with 405 is ignored, because the spec allows it. Whencreate_mcp_tools()fails with anHTTPStatusErroror one of the three stand-ins above, it raises the newMCPServerHTTPError(MCPError)from the original error. Its message isHTTP <status> <phrase> from <scheme://host:port>: <reason>. The reason comes from theWWW-Authenticateerror_description, then the JSON body (error_description,message, JSON-RPCerror.message,detail, a stringerror), then theWWW-Authenticateerror, then the body text, capped at 300 characters. The URL keeps only the origin, so no userinfo, query or path._PackageRemoteMCPServerbecomes_RemoteMCPServerand keeps the Agent Plugins §7.2.1 off-origin header drop for package servers.mcp_router.py):/api/mcp/testand the OAuth test flow return that message witherror_kind="unknown", which Canvas renders asConnection failed: <message>. The router also logs the message as a WARNING.mcp1.x/httpx and no longer applies after themcp2.x upgrade. This version mergesmainand reworks the fix.Issue Number
Fixes #5276
How to Test
Agent Canvas end-to-end (OAuth + 403).
main(46f20aba) against an agent-server from this branch:OH_AGENT_SERVER_LOCAL_PATH=<this repo> npm run dev:minimalin OpenHands./mcp, choose Add custom server → SHTTP →http://127.0.0.1:9301/mcp→ OAuth → Test connection, then click Authorize in the popup.main:Connection failed: MCPError: Server returned an error response.Connection failed: HTTP 403 Forbidden from http://127.0.0.1:9301: 403 Forbidden - MCP server not enabled for any of your groups.Connected — 2 tool(s) available.Unit and integration tests (real local FastMCP/uvicorn servers, no mocks):
uv run pytest tests/sdk/mcp/test_create_mcp_tool.py -k "http_error or stand_in" uv run pytest tests/agent_server/test_mcp_router.py -k rejectedtest_create_mcp_tools_reports_the_server_reason_for_an_http_error[http|sse]: the server answers 403. The config URL carriesuser:hunter2@…?token=abc123, and the error must equalHTTP 403 Forbidden from http://127.0.0.1:<port>: <reason>exactly.test_create_mcp_tools_reports_the_http_error_behind_a_404_stand_in: initialize succeeds, thentools/listgets a plain-text 404. Themcpclient reports it as a stand-in, and the error isHTTP 404 Not Found from …: session not found.test_http_error_reason[...]: 8 response shapes (WWW-Authenticatedescription and code, JSON-RPC,detail,error, plain text, truncation, empty body).test_mcp_test_reports_the_reason_a_server_rejected_the_connection: the/api/mcp/testresponse body equals the expected JSON.tests/sdk/mcp,tests/sdk/plugin,tests/agent_server/test_mcp_router.pyandtests/sdk/conversation/test_local_conversation_{mcp,agent_plugins}.pypass locally (695).ruffandpyrightare clean.Video/Screenshots
Before (main 91e8b5b): OAuth completes, then the test hides the reason.
After (this PR): same flow; Canvas shows the status and the server's reason.
The original contributor's real-GitLab "before" screenshot, taken on an older release: gitlab-shttp-403-before.png
Type
Notes
create_mcp_tools()now raisesMCPServerHTTPError(a subclass ofopenhands.sdk.mcp.MCPError, exported fromopenhands.sdk.mcp) in place of the rawmcpMCPErrororhttpx2.HTTPStatusError, chained as__cause__. No caller in this repo catches those types.Error calling MCP tool …) are out of scope..pr/evidence for themcp1.x design has been replaced; it is still available at commit 41b8474.This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.