Repository navigation
Description
[Bug]: GitLab MCP OAuth succeeds but MCP test fails with Session terminated
Environment
- OpenHands Agent Server:
1.49.4 - OpenHands Agent Canvas: local installation
- OS: Ubuntu 24.04
- MCP server: GitLab Self-Managed
- GitLab MCP endpoint:
https://gitlab.klickrent.de/api/v4/mcp - Installation: local Agent Canvas / Agent Server
Bug description
I am trying to connect the GitLab MCP server to a local OpenHands Agent Canvas.
The OAuth flow starts correctly and OpenHands creates a temporary local OAuth callback server.
The browser redirects to GitLab, authentication succeeds, and the callback page displays:
Authentication successful
However, after the OAuth callback completes, OpenHands fails the MCP connection test and reports:
MCP test failed unexpectedly for server 'gitlab'
In some attempts the MCP client reports:
McpError: Session terminated
The problem happens after successful OAuth authentication, not during the initial authorization redirect.
Steps to reproduce
-
Run OpenHands locally using Agent Canvas.
-
Go to MCP configuration.
-
Add an HTTP MCP server:
https://gitlab.klickrent.de/api/v4/mcp -
Start the OAuth authorization flow.
-
Authenticate in GitLab.
-
Allow the OAuth request.
-
The browser is redirected to the local OpenHands callback, for example:
http://localhost:<dynamic-port>/callback -
The callback page reports:
Authentication successful -
OpenHands then fails the MCP test.
Expected behavior
After successful OAuth authentication, OpenHands should store/use the OAuth credentials and successfully initialize the GitLab MCP session and list the available tools.
Actual behavior
OAuth authentication appears to complete successfully, but the MCP test fails immediately afterwards.
OpenHands logs show:
MCP OAuth authorization URL captured for job ...
MCP OAuth callback server ready for job ... at http://localhost:34483/callback
OAuth callback server started on http://localhost:34483
...
MCP test failed unexpectedly for server 'gitlab'
The failure occurs approximately 10 seconds after the OAuth callback server is started.
Additional observations
-
The OAuth callback is reachable locally and the browser displays
Authentication successful. -
This is a local OpenHands installation, not Docker.
-
The callback uses a dynamically allocated localhost port.
-
Restarting OpenHands / the machine and revoking the GitLab session can change the behavior, but the problem can recur.
-
The same GitLab MCP endpoint is being used:
https://gitlab.klickrent.de/api/v4/mcp
There is already an OpenHands issue concerning OAuth MCP callbacks not being exposed from Docker containers, but this issue is different because the callback itself is successfully reached and completed on a local installation.
Could this be related to OAuth token persistence, scope handling, or the MCP session initialization performed after the OAuth callback?
Acceptance criteria
Added from the investigation in the comments: the OAuth flow works; GitLab then rejects the MCP request (for example 403 {"message":"403 Forbidden - MCP server not enabled for any of your groups"}), and OpenHands hides that reason.
- When a remote MCP server (streamable HTTP or SSE) answers with an HTTP 4xx/5xx while connecting or listing tools, with or without OAuth,
/api/mcp/testand the OAuth test flow return an error that names the HTTP status and the reason the server gave (WWW-Authenticateerror_description/error, a JSONmessage/error/detail/JSON-RPCerror.message, or the body text, capped at 300 characters). - This also applies when the
mcpclient reduces the response toServer returned an error response,Session terminatedorNot Found. - The error does not include URL userinfo, the query string or the path.
- Agent Canvas shows the message in the MCP test result, and the agent-server logs it.
- Successful connections, OAuth and SSE streams work as before.
- Tests cover streamable HTTP and SSE.
Reproduction on main (91e8b5b)
I ran Agent Canvas main (46f20aba) against an agent-server built from SDK main 91e8b5b. The MCP server was a local mock that does real OAuth (dynamic client registration, consent page, token) and then answers MCP requests the way GitLab does when MCP is off for the user: 403 {"message": "403 Forbidden - MCP server not enabled for any of your groups"}. Authorization completes. The Canvas test then shows only Connection failed: MCPError: Server returned an error response, with no status and no reason.
With the fix in #5349, the same steps show HTTP 403 Forbidden from http://127.0.0.1:9301: 403 Forbidden - MCP server not enabled for any of your groups:
This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.


Activity
all-hands-bot commentedon Sep 23, 2026
The following comments were added by the OpenHands AI agent.
Decision needed
I could not safely finalize a scope for this from the repository alone, because the reported symptom sits at a boundary this repo does not fully control, and the two log lines point in different directions.
What the code shows:
MCP test failed unexpectedly for server 'gitlab'is the genericexcept Exceptionfallback in_probe_mcp_server(openhands-agent-server/openhands/agent_server/mcp_router.py), which logs the real exception atexc_info=True.McpError: Session terminatedis emitted by the bundledmcpclient's streamable-http transport when a POST to the MCP endpoint returns HTTP 404 — i.e. the server ended the session, not an authentication rejection. That does not match the closed #4818/#4821 post-consent crash (already in 1.49.4), and it does not match open #4843's pre-registration redirect-URI symptom ("invalid callback URL" during authorization), since here authorization succeeds.To set a real, testable scope I need:
MCP test failed unexpectedly for server 'gitlab'(theexc_info=Truestack), plus the OAuth/timeout lines immediately before it. This distinguishes an OAuth token-persistence failure from an MCP session/transport failure.fastmcpandmcpversions in the failing environment (pip show fastmcp mcp/uv pip list), and whether the server is configured ashttporsse.fastmcpclient script using the obtained token). If it does not, the defect is likely in the GitLab Self-Managed MCP endpoint rather than this repository.gitlab.klickrent.defor the failing request — specifically whether it returns 404 for the post-callback session POST, and any session-expiry or protocol-version message.Once I know whether this is agent-server OAuth state handling, MCP client transport behavior, or upstream GitLab endpoint behavior, I can scope it and write observable acceptance criteria. I have not added
ready-for-dev.majchrosoft commentedon Sep 23, 2026
Thanks. I went back through the logs from the failing attempts and here is everything I currently have.
Environment
1.49.4https://gitlab.klickrent.de/api/v4/mcp127.0.0.1:18000.The actual process topology during the failing run was:
The relevant process was:
Failing attempt #1
OpenHands log:
This attempt is from the full OpenHands log captured on September 22.
Failing attempt #2
A second independent attempt on September 23 produced:
Again, the callback server is started successfully on localhost and the generic MCP failure happens afterwards.
OAuth callback behavior
During the OAuth flow, the browser actually reaches the local callback and displays:
So the authorization flow gets as far as the local callback.
The callback is not an externally exposed URL. It is dynamically created as:
For example:
and in another attempt:
Important limitation
The logs I have captured do NOT contain the traceback behind:
The only line emitted by OpenHands at that point is the generic warning from:
I therefore do not currently have the
exc_info=Truetraceback requested in your comment.I also do not currently have the exact resolved versions from the failing environment for:
and I have not yet captured a server-side GitLab log showing the HTTP response to the MCP session POST.
Additional observation
The failure is reproducible across multiple fresh OpenHands starts. In both captured runs the sequence is:
I am happy to provide the
pip show fastmcp mcpoutput and a fresh run with debug logging / the complete traceback if that is needed to distinguish OAuth state/token handling from MCP streamable-HTTP session initialization.majchrosoft commentedon Sep 23, 2026
i can see in my gitlab account that the session is established and active. I can revoke it on demand from gtilab account
majchrosoft commentedon Sep 23, 2026
Thanks. I checked the environment and the logs again.
Environment
1.49.4The Agent Server is running locally on:
The relevant Python process is:
Installed MCP packages
I checked the exact Python environment used by the Agent Server:
Command used:
Output:
OAuth / MCP failure
I have two independent failing runs.
September 22
September 23
In the browser, after GitLab authentication, the local OAuth callback page displayed:
So the browser did reach the OpenHands callback.
Important limitation
The logs I have do NOT contain the actual exception/traceback behind:
Although the log points to:
there is no traceback in the captured output.
I also cannot currently determine the GitLab transport type from
~/.openhands/settings.json.The only MCP entry currently present there is an unrelated GitHub server:
There is currently no
gitlabentry in~/.openhands/settings.json.This may simply mean that the failed GitLab configuration is not persisted after the failed test.
Process topology
During the failure, the Agent Canvas process tree was:
The relevant agent-server process was PID
39796.Its stdout/stderr were connected through Unix sockets to the parent
agent-canvasprocess rather than directly to the terminal.What I have not verified yet
I have not yet:
exc_info=Truetraceback frommcp_router.py:593.The reproducible sequence from my side is:
The issue is reproducible with both fresh OpenHands starts shown above.
majchrosoft commentedon Sep 24, 2026
there was no problem on gitlab server side. YOu can easily reproduce that bugged behavior in gitlab.com
majchrosoft commentedon Sep 25, 2026
Hi,
I forked You repo and try to make some debug. Finally i have possible answer - the mcp server (the url responds with json) but it must be enabled by the owner of the top-group in gitlab . settings/general/permissions.../mcp
Next week I will reach the owner and ask him aobut that setting.
Most probable cause - that setting is turned off. - the response then is 403 - the oauth authorization works, but the mcp server is forbidden
seek-far commentedon Sep 25, 2026
Hi,
I have tested the issue with both gitlab.com and my self-hosted GitLab. My testing and debugging on gitlab.com were similar to yours: after I enabled MCP in the top-level group, the MCP connection worked. Details are as follows.
1. Test with gitlab.com
First I tested with my account A. The test failed with the same 403, and the log matches yours (
MCP test failed unexpectedly for server ...). I wrote a small standalone debugging program using FastMCP, independent of OpenHands, and found that GitLab actually responded with the failure reason:My account A does not belong to any group.
Then I registered a new account B on gitlab.com and created a top-level group with B as the owner. In that group, I checked "Allow connection to GitLab" under Settings → General → Permissions and group features → MCP client access. But the test still failed. I tested again 10 minutes later, and it failed again. About 90 minutes later, my third test succeeded, so the setting may take a while to take effect.
2. Test with my self-hosted GitLab
The GitLab version is GitLab CE 18.11.3. The MCP connection succeeded for a non-administrator account that does not belong to any group. Note that the MCP access setting was introduced in GitLab 19.0 (generally available in 19.2), so 18.11 has no such setting.
The OAuth flow in OpenHands works correctly, but the error reporting could be improved to ease troubleshooting. GitLab's response body states the reason clearly, but OpenHands currently doesn't surface it anywhere: the agent-server traceback contains only the status code, not the response body; in the Agent Canvas aggregated log even that traceback is missing, leaving only the single line
MCP test failed unexpectedly; and the UI only showsHTTPStatusError: Client error '403 Forbidden' .... I plan to investigate further and may open a PR referencing this issue to improve this.When you contact the owner next week to enable the setting, it would be great if you could come back and share the result. Thanks.
onatozmenn commentedon Sep 27, 2026
I'll take the error-surfacing part:
_probe_mcp_servercurrently swallows the real probe failure behind the genericMCP test failed unexpectedlymessage, so a GitLab 403 likeMCP server not enabled for any of your groupsnever reaches the user. Plan is to surface the underlying status/body in the probe result while keeping the probe non-fatal. Will open a PR shortly.onatozmenn commentedon Sep 27, 2026
Quick update: PR #5346 implementing the probe error-surfacing is open, but its description check now requires this issue to carry
ready-for-dev(post-rollout issue, no labels yet). Could a maintainer please add that label? Happy to adjust scope if you'd rather keep this issue unlabeled.majchrosoft commentedon Sep 27, 2026
thanks a lot!
seek-far commentedon Sep 27, 2026
I have opened #5349 for the error-surfacing part of this issue. #5346 targets the same part; in my humble opinion, it may not fully achieve the goal in Agent Canvas, for two reasons I found while testing:
error_kind="connection"for HTTP errors. Agent Canvas 1.23.0 rendersconnectionas the fixed text "Could not reach the server. Check the URL and server type." and drops theerrordetail, so users would see neither the status nor the reason. Onlyerror_kind="unknown"is shown verbatim (Connection failed: {error}).mcpclient callsraise_for_status()before reading it. The server's reason (e.g. GitLab's "MCP server not enabled for any of your groups") can only be read if the SDK's httpx client reads 4xx/5xx bodies, which is what fix(mcp): show the HTTP status and server reason when an MCP server rejects a connection #5349 adds.#5349 keeps
error_kind="unknown", reads the body in the SDK, and includes before/after Agent Canvas screenshots against gitlab.com and Asana. I'm happy to go with whichever approach the maintainers prefer, or to combine them.Could a maintainer please consider adding
ready-for-devto this issue? Both PRs need it to pass the PR description check.onatozmenn commentedon Oct 23, 2026
This is a valid MCP bug and a fix is ready in #5346. The PR's
Validate PR descriptioncheck is blocked only because this issue is missing theready-for-devlabel. Could a maintainer add it so the fix can move forward?fix(mcp): name the HTTP status and server reason behind MCP connectio…