Skip to content

fix(mcp): support OAuth for organization MCP server listings (OHE-3861) - #1140

Merged
hieptl merged 4 commits into
mainfrom
hiep/ohe-3861-restore-oauth-for-organization-mcp-servers
Nov 3, 2026
Merged

hieptl merged 4 commits into
mainfrom
hiep/ohe-3861-restore-oauth-for-organization-mcp-servers

Conversation

@hieptl

@hieptl hieptl commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

HUMAN:

  • A human has tested these changes.

AGENT:


Why

Organization MCP server listings (OHE-3091) reject OAuth: server/routes/org_mcp_servers.py answers 422 "OAuth-authenticated MCP servers cannot be shared with the organization yet". Servers that only support OAuth cannot be offered to an organization at all. This support was built on top of #1121 but was not part of it when it merged.

Summary

  • Organization-managed OAuth listing: the admin authorizes it before saving. Its tokens are stored on the listing, encrypted, and every member launches with them.
  • User-managed OAuth listing: only the client settings are stored. Each member connects with their own account through the new PUT and DELETE /api/organizations/{org_id}/mcp-servers/{name}/connection routes. Tokens live on org_member.org_mcp_oauth_states (migration 212) and join the listing at launch; a member who has not connected does not get the server.
  • Tokens and the client secret never reach the browser. A member's tokens stop applying when an admin changes the listing's URL or client ID, and deleting the listing removes them. The sandbox write-back webhook stores refreshed tokens on the right row.

Issue Number

OHE-3861 (OHE-3091). The UI is in #1141.

How to Test

uv run --group test pytest \
  tests/unit/server/routes/test_org_mcp_servers.py \
  tests/unit/storage/test_org_mcp_servers_store.py \
  tests/unit/app_server/test_mcp_oauth_router.py \
  tests/unit/app_server/test_webhook_router_mcp_oauth_state.py \
  tests/unit/app_server/test_live_status_app_conversation_service.py \
  tests/unit/test_agent_profiles.py

510 tests pass (the store tests start Postgres through Docker). ruff 0.12.5, the version pre-commit pins, is clean, and mypy reports nothing new on the touched files.

It also runs on shared-5 together with the UI PR and the OHE-3866 fixes: the connection route is mounted and answers 401 when signed out. I have not yet signed in with a real OAuth provider there.

Video/Screenshots

Backend only. Screenshots of the UI are in #1141.

Type

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

Notes

  • Migration 212_add_org_member_org_mcp_oauth_states adds a nullable org_member.org_mcp_oauth_states column (encrypted, stored as a string), with no backfill.
  • Known limitation: an organization-managed and a user-managed OAuth listing at the same URL would share tokens, because tokens are matched by URL.

Enterprise server image for this PR:

ghcr.io/openhands/enterprise-server:sha-8fb958d

Organization MCP server listings rejected oauth2 credentials outright.
Both management kinds now accept them:

- An organization-managed listing must be authorized by the admin before
  it is saved. Its tokens are stored on the listing, encrypted, and every
  member launches with them.
- A user-managed listing stores only the OAuth client settings. Each
  member authorizes with their own account through the new PUT and DELETE
  /api/organizations/{org_id}/mcp-servers/{name}/connection routes. The
  tokens live on org_member.org_mcp_oauth_states (migration 212) and join
  the listing at launch; a member who has not connected does not get the
  server.

Tokens and the client secret never reach the browser. An OAuth start that
names a listing restores its client secret but not its stored tokens, so
it always needs fresh consent, and that flow's status omits oauth_state.
The organization routes read the tokens server-side from the finished job
after checking its owner and URL.

A member's tokens stop applying when an admin changes the listing's URL or
client ID, and deleting a listing removes them. The sandbox write-back
webhook falls back to the listings of the sandbox's organization, on the
listing or on the owner's row, and ignores owners who left that org.
@github-actions github-actions Bot added the type: fix A bug fix label Nov 3, 2026
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

⚠️ This PR contains migrations. Please synchronize before merging to prevent conflicts.

@hieptl
hieptl marked this pull request as ready for review November 3, 2026 11:38
…on routes (OHE-3861)

The service account guard requires every mutating route to say whether a
service account key may call it. The PUT and DELETE connection routes
store or forget the caller's own OAuth tokens for a user-managed listing,
like the account-level key and secret routes, and a service account cannot
complete an OAuth sign-in anyway, so both now reject service account keys.
@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  migrations/versions
  212_add_org_member_org_mcp_oauth_states.py 29, 35
  openhands/app_server/app_conversation
  live_status_app_conversation_service.py 2616
  openhands/app_server/event_callback
  webhook_router.py 737, 754-767, 781, 795-807
  openhands/app_server/mcp
  mcp_oauth_router.py 199-213, 426-427, 497-513, 539-558
  mcp_test_router.py 353, 358-360, 373-376, 383-386
  server/routes
  org_mcp_servers.py 141-142, 155-177, 229-233, 265-275, 308-318, 349-350, 369-379, 415, 437-462, 476-482
  storage
  agent_profile_resolution.py
  org_mcp_servers.py 123-124, 141, 193-194
  org_mcp_servers_store.py
  org_member.py
  saas_settings_store.py
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 89673f4 into main Nov 3, 2026
23 checks passed
@hieptl
hieptl deleted the hiep/ohe-3861-restore-oauth-for-organization-mcp-servers branch November 3, 2026 13:19
@openhands-release-bot openhands-release-bot Bot added the released: 1.85.0 Shipped in 1.85.0 label Nov 3, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.85.0.

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants