fix(mcp): reuse cached OAuth redirect port on re-auth (salvage of #49249) - #65718
Merged
Conversation
Collaborator
Related to #49249: this is its current-main salvage, preserving cached redirect-port reuse while composing with the newer callback-port reservation/waiter work. |
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.
Infographic
Summary
MCP OAuth re-authentication reuses the callback port pinned in the cached client registration, so providers that bind redirect URIs at dynamic-client-registration time no longer reject re-auth with
invalid_redirect_uri.Salvage of #49249 by @x9x9x9x9x9x91 (cherry-picked, authorship preserved). Their branch predated the reserved-socket TOCTOU pool (#65622) and per-flow waiters (#65664); the conflict resolution composes both: port precedence is now explicit config → cached registration port → fresh port through
_reserve_callback_port()(only truly-fresh picks enter the TOCTOU reservation pool; cached/explicit ports are fixed values that bind via the reuse_address path).Changes
tools/mcp_oauth.py:_cached_redirect_port(storage)parses the loopback callback port from the cached client registration (handles both127.0.0.1andlocalhostURI forms);_configure_callback_port(cfg, storage)gains the precedence chaintools/mcp_oauth_manager.py: pass storage throughtests/tools/test_mcp_oauth.py: cached-port reuse + explicit-override tests (@x9x9x9x9x9x91)Validation (E2E, isolated HERMES_HOME)
localhost-form cached URI parses (redirect_host users)redirect_portwins over cache; corrupt cache falls throughtest_mcp_oauth.py+test_mcp_oauth_manager.py