Repository navigation
Description
Is there an existing issue for the same bug?
- I have searched existing issues and this is not a duplicate.
Bug Description
_BrowserCoordinatedOAuth.callback_handler in openhands-agent-server/openhands/agent_server/mcp_router.py returns tuple[str, str | None]. That is the contract mcp 1.x used. mcp 2.x changed it to AuthorizationCodeResult and reads .state off the return value, so every OAuth-based MCP server install now fails immediately after the user authorizes, with:
MCP test connection failed for server '<name>': Client failed to connect: 'tuple' object has no attribute 'state'
The user-visible symptom in Agent Canvas is the misleading banner "Could not reach the server. Check the URL and server type." The provider is reachable and the OAuth flow succeeds end to end. In a real GitLab reproduction, discovery returned 200, POST /oauth/register returned 201, GET /oauth/authorize returned 302, and the user approved the consent screen. Across ten failed attempts the provider returned no error beyond the expected initial 401 challenge that starts the flow. The crash is entirely client-side, after the redirect.
This is not tied to one agent-server release. The method is byte-identical in 1.42.1, 1.44.1 and on main (94fca57). What changed is the resolved dependency: openhands-sdk declares fastmcp>=3.0.0 with no ceiling, so a fresh install of either release now pulls fastmcp 4.0.1 with mcp 2.1.1.
uv pip compile openhands-agent-server==1.42.1 --no-cache -> fastmcp 4.0.1, mcp 2.1.1
uv pip compile openhands-agent-server==1.44.1 --no-cache -> fastmcp 4.0.1, mcp 2.1.1
uv pip compile openhands-agent-server==1.44.1 + "fastmcp<4" -> fastmcp 3.4.7, mcp 1.29.1
Why CI does not catch it. The root pyproject.toml sets [tool.uv] exclude-newer = "7 days" and uv.lock pins fastmcp 3.2.0 / mcp 1.28.1, so every workspace install and CI run gets a working pair. Only consumers that resolve from PyPI without the lock (for example the Agent Canvas uvx invocation) hit this.
Root cause is a fork, not just a type. _BrowserCoordinatedOAuth.callback_handler is a copy of fastmcp's own OAuth.callback_handler (34 vs 33 non-blank lines, 66% identical line for line). The only intended addition is self._job.set_callback_ready(callback_url). Copying froze three behaviors that have since drifted:
| Behavior | fastmcp base | the forked copy |
|---|---|---|
| Return value | tracks the installed version (tuple in 3.x, AuthorizationCodeResult in 4.x) |
always a tuple — this outage |
| Callback host | passes host=self._callback_host |
omits it, so the server binds create_oauth_callback_server's default |
| Timeout | uses self._callback_timeout |
hardcodes 300.0, ignoring the constructor argument |
The existing # type: ignore[return-value] on the return line was already suppressing the mismatch.
Note that fastmcp 4's base also forwards RFC 9207 iss, which the fork drops.
Expected Behavior
Completing the provider's OAuth consent screen connects the MCP server, regardless of which mcp / fastmcp version the environment resolved.
Actual Behavior
The flow dies right after the redirect with AttributeError: 'tuple' object has no attribute 'state', surfaced to the UI as "Could not reach the server."
Reproduced offline on main (94fca57) with no network, no browser and no provider account. Two virtualenvs are built over byte-identical agent-server source; only the MCP client libraries differ.
git clone https://github.com/OpenHands/software-agent-sdk.git && cd software-agent-sdk
git checkout 94fca57
# NOTE: run the installs from outside the repo. The root pyproject sets
# exclude-newer = "7 days", which hides fastmcp 4 and makes the bug vanish.
cd /
uv venv /opt/mcp1 --python 3.13
VIRTUAL_ENV=/opt/mcp1 uv pip install --no-cache \
<repo>/openhands-sdk <repo>/openhands-agent-server "fastmcp<4"
uv venv /opt/mcp2 --python 3.13
VIRTUAL_ENV=/opt/mcp2 uv pip install --no-cache \
<repo>/openhands-sdk <repo>/openhands-agent-server "fastmcp==4.0.1" "mcp==2.1.1"
/opt/mcp1/bin/python repro.py # OK
/opt/mcp2/bin/python repro.py # AttributeError: 'tuple' object has no attribute 'state'repro.py runs the real _BrowserCoordinatedOAuth against a stub OAuth authorization server, with a thread standing in for the browser. A self-contained Dockerfile and the script are attached in the comment below.
Observed traceback in the failing environment:
File "/opt/mcp2/lib/python3.13/site-packages/mcp/client/auth/oauth2.py", line 421,
in _perform_authorization_code_grant
if result.state is None or not secrets.compare_digest(result.state, state):
AttributeError: 'tuple' object has no attribute 'state'
Matrix over the same source, six runs:
mcp 1.x | as shipped OK
mcp 2.1.1 | as shipped FAILS ('tuple' has no attribute 'state')
mcp 1.x | delegate to fastmcp (proposed fix) OK
mcp 2.1.1 | delegate to fastmcp (proposed fix) OK
Steps to Reproduce
- Install
openhands-agent-serverfrom PyPI (or from this repo, from outside the repo root soexclude-newerdoes not apply) so thatfastmcp 4.x/mcp 2.1.xresolve. - In Agent Canvas, add an MCP server that authenticates with OAuth, for example
https://gitlab.com/api/v4/mcp, and click Install. - Complete the provider's consent screen.
- The dialog shows "Could not reach the server. Check the URL and server type", and the agent-server log shows
MCP test connection failed ... 'tuple' object has no attribute 'state'.
Acceptance Criteria
-
_BrowserCoordinatedOAuth.callback_handlerno longer reimplements fastmcp'sOAuth.callback_handler; it publishes the callback URL and delegates viasuper(), so the return type tracks the installedfastmcpby construction. - Installing an OAuth-authenticated MCP server succeeds with
fastmcp 4.x/mcp 2.1.xresolved. - The same flow still succeeds with
fastmcp 3.x/mcp 1.xresolved. - The RFC 9207
issvalue returned by the callback is no longer discarded. - The configured
callback_hostandcallback_timeoutare honoured rather than hardcoded. - A regression test covers the OAuth callback flow against a stub authorization server, so a future contract change fails CI rather than only PyPI consumers.
- CI exercises a resolution that is not pinned by
uv.lock, oropenhands-sdkdeclares afastmcpceiling, so this class of drift is caught before release.
Installation Method
Other
If you selected "Other", please specify
uvx / uv pip install from PyPI, as Agent Canvas does; also reproduced from a source checkout of main.
SDK Version
main @ 94fca57, and released 1.42.1 and 1.44.1 (the method is identical in all three).
Version Confirmation
- I have confirmed this bug exists on the LATEST version of OpenHands SDK
Proposed Fix
async def callback_handler(self):
self._job.set_callback_ready(
f"http://{self._callback_host}:{self.redirect_port}/callback"
)
return await super().callback_handler()Verified in both environments. It is version-agnostic by construction, stops discarding iss, and removes the two latent divergences above. It is behaviour-preserving today because the defaults coincide (callback_timeout=300.0, and the host resolves to the same loopback address).
Caveat for review: this relies on _callback_host and _callback_timeout, which are private attributes of fastmcp's class. The current code already depends on redirect_port and on the private create_oauth_callback_server, so it is not new coupling, but it is worth a deliberate decision.
A narrower alternative is to adapt the tuple behind a guarded import of mcp.shared.auth.AuthorizationCodeResult (absent in mcp 1.x, so it cannot be a straight swap). That also passes the matrix, but it keeps the fork and still drops iss.
Suggested stopgap, independent of the code fix: add a fastmcp<4 ceiling so PyPI consumers stop resolving the broken pair, then lift it once the delegation lands.
Activity
github-actions commentedon Sep 2, 2026
✅
ready-for-devlabel appliedThis issue meets the readiness criteria and is ready for contribution.
A contributor can now open a PR that links this issue (
Fixes #<number>).This is an automated check — no AI was used to generate this comment.
VascoSch92 commentedon Sep 2, 2026
Self-contained reproduction. No network at run time, no browser, no provider account: the image ships a stub OAuth authorization server and a thread that plays the browser.
It builds two virtualenvs over byte-identical agent-server source (the entrypoint diffs the installed
mcp_router.pyagainst the checkout and prints the result), then runs the flow as shipped and with each candidate fix.Pass
--build-arg SDK_REF=mainto retest a newer revision.Dockerfile
run-matrix.sh
repro.py
jpelletier1 commentedon Sep 2, 2026
@VascoSch92 Is this related to: OpenHands/OpenHands#17077
The symptom I was seeing was OAuth sessions were not being refreshed, and Atlassian and GitLab servers were rejecting because the Callback URI was 'invalid'. I think it has to do with these root causes:
Token expiry not loaded on init: FastMCP's OAuthClientProvider._initialize() loads tokens and client info from storage but NOT token_expiry_time. Without the expiry, is_token_valid() always returns True for any access token — even expired ones — so the refresh-token flow is never triggered. The expired token gets sent, the server returns 401, and a full browser re-authorization is attempted (which fails in the runtime context).
Random callback port: The OAuth callback server used a random port on each authorization attempt. OAuth providers like Atlassian Rovo and GitLab that require pre-registered redirect URIs reject callbacks with unregistered ports.