Skip to content

Drive MCP connection login (RFC 8707 resource) from ucode mcp-proxy - #548

Open
sunishsheth2009 wants to merge 1 commit into
databricks:mainfrom
sunishsheth2009:mcp-connection-login
Open

Drive MCP connection login (RFC 8707 resource) from ucode mcp-proxy#548
sunishsheth2009 wants to merge 1 commit into
databricks:mainfrom
sunishsheth2009:mcp-connection-login

Conversation

@sunishsheth2009

Copy link
Copy Markdown
Collaborator

What did you change, and why?

Change: Teach ucode mcp-proxy to complete an MCP connection login when the AI Gateway asks for one.

Why: When an AI Gateway MCP service is backed by a per-user connection (e.g. system.ai.github) and the user hasn't logged in to that connection yet, the gateway answers a tools call with an RFC 9728 challenge — HTTP 401 + WWW-Authenticate: Bearer resource_metadata="…". A valid Databricks workspace token isn't enough; the connection needs its own login. Today the proxy just forwards that response to the coding agent, which shows connected · tools fetch failed and the user is stuck with no way to log in.

The proxy's httpx auth now detects that challenge and drives an interactive resource-scoped OAuth login: authorization-code + PKCE against the workspace /oidc, using the published databricks-cli app, sending the MCP endpoint URL as the RFC 8707 resource indicator. That indicator makes /oidc route the browser through the connection's /mcp-service-login page → the user signs in to the backing SaaS → the connection credential is stored server-side → the flow returns → the proxy retries the original request, which now succeeds. No coding-agent OAuth (RFC 9728 / dynamic client registration) support required.

How do you know it works?

Testing: uv run pytest tests/test_resource_login.py tests/test_mcp_proxy.py — 35 passing. New tests cover the authorize-URL + PKCE construction and the 401/resource_metadata challenge detection; existing proxy tests still pass. ruff check/format clean. The interactive browser + token-exchange path is validated end-to-end against staging separately (it requires the gateway-side RFC 9728 challenge to be enabled for the connection).

Notes:

  • Interactive login is skipped under DATABRICKS_BEARER (headless/CI) — no browser to drive.
  • One connection-login attempt per proxy lifetime, guarding against a retry loop.
  • resource_login.py is stdlib-only (no new dependency).

This pull request and its description were written by Isaac.

When an AI Gateway MCP service is backed by a per-user connection (e.g.
system.ai.github) with no stored credential, the gateway answers a tools call
with an RFC 9728 challenge (HTTP 401 + WWW-Authenticate resource_metadata). The
proxy currently forwards that to the coding agent as a plain "tools fetch
failed" — the connection login never happens.

Teach the proxy's httpx auth to detect that challenge and drive an interactive
resource-scoped OAuth login: authorization-code + PKCE against /oidc using the
published databricks-cli app, with the MCP endpoint URL sent as the RFC 8707
resource indicator so /oidc routes through the connection's /mcp-service-login
page. After login the connection credential exists, so the same request is
retried and succeeds.

- resource_login.py: standalone stdlib OAuth-with-resource flow + loopback callback.
- mcp_proxy.py: async_auth_flow detects the challenge, runs the login off the
  event loop, and retries once. Skipped under DATABRICKS_BEARER (headless/CI).
- Tests for authorize-URL/PKCE construction and challenge detection.

Co-authored-by: Isaac <no-reply@databricks.com>
@sunishsheth2009
sunishsheth2009 marked this pull request as ready for review September 10, 2026 00:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant