Skip to content

fix(mcp): reuse cached OAuth redirect port on re-auth (salvage of #49249) - #65718

Merged
teknium1 merged 1 commit into
mainfrom
fix/mcp-oauth-cached-redirect-port
Jul 16, 2026
Merged

fix(mcp): reuse cached OAuth redirect port on re-auth (salvage of #49249)#65718
teknium1 merged 1 commit into
mainfrom
fix/mcp-oauth-cached-redirect-port

Conversation

@teknium1

Copy link
Copy Markdown
Contributor

Infographic

mcp-oauth-cached-redirect-port

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 both 127.0.0.1 and localhost URI forms); _configure_callback_port(cfg, storage) gains the precedence chain
  • tools/mcp_oauth_manager.py: pass storage through
  • tests/tools/test_mcp_oauth.py: cached-port reuse + explicit-override tests (@x9x9x9x9x9x91)

Validation (E2E, isolated HERMES_HOME)

Check Result
Re-auth with cached registration reuses pinned port 57727 pass
First auth (no cache) unchanged — fresh port, TOCTOU pool engaged pass
localhost-form cached URI parses (redirect_host users) pass
Explicit redirect_port wins over cache; corrupt cache falls through pass
test_mcp_oauth.py + test_mcp_oauth_manager.py 120/120

@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists tool/mcp MCP client and OAuth area/auth Authentication, OAuth, credential pools sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data labels Jul 16, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

This was generated by AI during triage.

Related to #49249: this is its current-main salvage, preserving cached redirect-port reuse while composing with the newer callback-port reservation/waiter work.

@teknium1
teknium1 merged commit 58010c8 into main Jul 16, 2026
33 checks passed
@teknium1
teknium1 deleted the fix/mcp-oauth-cached-redirect-port branch July 16, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/auth Authentication, OAuth, credential pools P2 Medium — degraded but workaround exists sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data tool/mcp MCP client and OAuth type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants