Skip to content

fix(mcp): start the OAuth sign-in when the MCP server never answers 401 (OHE-3880) - #1170

Merged
hieptl merged 1 commit into
mainfrom
hiep/ohe-3880-start-oauth-sign-in-without-401
Nov 4, 2026
Merged

hieptl merged 1 commit into
mainfrom
hiep/ohe-3880-start-oauth-sign-in-without-401

Conversation

@hieptl

@hieptl hieptl commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

  • A human has tested these changes.

AGENT:


Why

Clicking Connect on a user-managed Google Drive OAuth listing on Settings > MCP shows "OAuth state carries no access token" and the Google sign-in never opens. The sign-in job (POST /api/v1/mcp/oauth/start) runs the SDK probe (initialize and tools/list) and relied on the server answering 401 to start the authorization. Google's Drive MCP server answers both without credentials (200, 8 tools) and only asks for a login on tool calls (401 with WWW-Authenticate: Bearer resource_metadata=...), so the job finished as a success with a state that held only the static client info, and the connection route refused it. The MCP Library install on Settings > MCP had the same gap and saved that token-less state.

Summary

  • The app server's OAuth client (_CloudCoordinatedOAuth) now starts the authorization itself when it holds no usable token: it answers its own unauthenticated request with a 401, so the upstream mcp client runs its normal flow unchanged (well-known metadata discovery, consent through the public callback, token exchange). Valid or refreshable stored tokens still skip the consent.
  • A job whose probe ends without an access token is stored as failed with "The OAuth authorization did not obtain an access token for this MCP server", for the status route, the organization-managed save and the member connection alike.
  • Tests: consent starts for a server that never answers 401, stored tokens are used without a new authorization, and a token-less job is reported as failed.

Issue Number

OHE-3880 (bug bash OHE-3866 for OHE-3091). Backend of #1140.

How to Test

uv run --group test pytest tests/unit/app_server/test_mcp_oauth_router.py tests/unit/server/routes/test_org_mcp_servers.py

55 tests pass. Two of the three new ones fail without the change; the stored-tokens one guards the "no consent needed" behaviour that must stay. ruff, ruff-format and mypy (dev_config/python/mypy.ini) are clean on the touched files.

Against Google's Drive MCP server (https://drivemcp.googleapis.com/mcp/v1) with a fake client id: on main the probe lists 8 tools, never calls the redirect handler and exports a state with only client_info; with this change the client publishes the consent URL at https://accounts.google.com/o/oauth2/v2/auth with the app server's callback as redirect_uri.

Manual, on shared-5 (not done yet): add an organization server for Google Drive (the URL above, OAuth, the client id and client secret of a Google Cloud "Web application" OAuth client whose authorized redirect URIs include https://app.shared-5.replicated.all-hands-testing.dev/api/v1/mcp/oauth/callback, "Each member signs in") and click Connect: Google's consent opens, and the row shows Connected afterwards.

Video/Screenshots

Backend only.

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

Notes

  • The upstream client's scope selection replaces the configured scope with the server's scopes_supported (Google: drive, drive.readonly, drive.file), after a real 401 as much as after this one. Unchanged here.
  • A listing marked OAuth whose server publishes no OAuth metadata now ends as "not completed" (the popup lands on <origin>/authorize) instead of a token-less success, the same as the upstream client's handling of a real 401 from such a server.
  • The organization-managed Authorize path already refused a token-less job with 422 at save time; the form only showed "Authorized" first. It now fails at the sign-in instead.
  • The agent server's _BrowserCoordinatedOAuth (canvas install) shares the probe and has the same gap; to be handled in the SDK separately.

Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-0630044

…01 (OHE-3880)

The OAuth sign-in job (POST /api/v1/mcp/oauth/start) runs the SDK probe
(initialize and tools/list) and relied on the server answering 401 to
start the authorization. Google's Drive MCP server answers both without
credentials and only asks for a login on tool calls, so the job finished
as a success with a state that held only the static client info, and
Connect on Settings > MCP failed with "OAuth state carries no access
token".

- The app server's OAuth client now starts the authorization itself when
  it holds no usable token: it answers its own unauthenticated request
  with a 401, so the upstream client runs its normal discovery and
  consent flow. Valid or refreshable stored tokens still skip the consent.
- A job whose probe ends without an access token is stored as failed with
  a clear message instead of as a success.
@github-actions github-actions Bot added the type: fix A bug fix label Nov 4, 2026
@hieptl
hieptl marked this pull request as ready for review November 4, 2026 05:30
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  openhands/app_server/mcp
  mcp_oauth_router.py 310, 384, 413-427
Project Total  

This report was generated by python-coverage-comment-action

@tofarr tofarr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍰

@hieptl
hieptl merged commit cddaf81 into main Nov 4, 2026
24 checks passed
@hieptl
hieptl deleted the hiep/ohe-3880-start-oauth-sign-in-without-401 branch November 4, 2026 19:37
@openhands-release-bot openhands-release-bot Bot added the released: 1.86.0 Shipped in 1.86.0 label Nov 4, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.86.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released: 1.86.0 Shipped in 1.86.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants