Repository navigation
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
Open
debojyoti-dev wants to merge 2 commits into
debojyoti-dev wants to merge 2 commits into
Conversation
…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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_urithe 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 sameclient_id, so providers that remember consent per client don't ask again.oauth_client_id) are left alone, since their client info is rebuilt each connection.Tests
tests/test_mcp_oauth_redirect_port.pyruns a real fastmcp server with an in-memory OAuth provider and DCR in a subprocess, drives it throughcall_tool_sync(..., use_oauth=True)with Studio's on-disk token store, and approves sign-in by following the authorize redirect to the callback.client_idand redirect URI (fails on main: a second client is registered).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.