Skip to content

Studio: keep the registered OAuth client when an MCP server asks to sign in again - #18176

Open
debojyoti-dev wants to merge 2 commits into
unslothai:mainfrom
debojyoti-dev:claude/project-thread-3yh1vo
Open

debojyoti-dev wants to merge 2 commits into
unslothai:mainfrom
debojyoti-dev:claude/project-thread-3yh1vo

Conversation

@debojyoti-dev

Copy link
Copy Markdown
Contributor

Fixes #18164

What was wrong

fastmcp binds a new random loopback port for the OAuth callback on every connection. Saved tokens are reused and refreshed fine, but as soon as a server needs a fresh sign-in (a refresh is refused, a grant is revoked), the authorize request carries a redirect_uri the stored DCR client never registered. The server rejects it, fastmcp drops the stored client and registers a new one, and a new client always gets the provider's consent screen. So every re-authorization looked like a first-time connection.

What changes

  • _oauth() now wraps the provider's _initialize: once the stored client registration is loaded, if its redirect URI is the same loopback callback on a different port and that port is free, Studio listens on the registered port and sends that redirect URI. Re-authorization then keeps the same client_id, so providers that remember consent per client don't ask again.
  • If the registered port is taken, nothing changes from today: the fresh port is used and fastmcp re-registers.
  • Pre-registered clients (oauth_client_id) are left alone, since their client info is rebuilt each connection.

Tests

tests/test_mcp_oauth_redirect_port.py runs a real fastmcp server with an in-memory OAuth provider and DCR in a subprocess, drives it through call_tool_sync(..., use_oauth=True) with Studio's on-disk token store, and approves sign-in by following the authorize redirect to the callback.

  • Reconnecting with a saved token opens no sign-in.
  • After the server revokes the grant, the next connection signs in again with the same client_id and redirect URI (fails on main: a second client is registered).
  • With the registered port occupied, it falls back to a new port and a new client and still connects.

Also passing locally on Windows: test_mcp_oauth_issuer_slash, test_mcp_oauth_basic_auth, test_mcp_preregistered_oauth, test_mcp_http_integration, test_mcp_http_sessions, test_mcp_servers, test_mcp_errors.

…ign in again

fastmcp binds a new random callback port on every connection, so a
re-authorization sends a redirect_uri the stored DCR client never
registered. The server rejects it, fastmcp registers a new client, and
the provider shows the consent screen again. Sign in on the port the
stored registration uses when it is free; keep the new port otherwise.
@bezabyte

Copy link
Copy Markdown
Contributor

@danielhanchen This PR addresses the re-authorization path in #18164 by preserving the stored DCR client and redirect URI. NilayYadavs #18171 covers a separate concurrent refresh-token race; the two fixes are complementary rather than duplicate. If both are accepted, this PR should be applied on top of the refresh-lock change or merged with it.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] MCP OAuth: consent prompt shown on every connection instead of reusing stored token.

3 participants