Skip to content

fix(client/auth): discover AS metadata before cold-start token refresh, with issuer-binding check - #3263

Open
claude[bot] wants to merge 3 commits into
mainfrom
fix/oauth-discovery-before-eager-refresh
Open

claude[bot] wants to merge 3 commits into
mainfrom
fix/oauth-discovery-before-eager-refresh

fix(client/auth): discover AS metadata before cold-start token refresh

a80aae2
Select commit
Loading
Failed to load commit list.
Claude / Claude Code Review completed Aug 7, 2026 in 12m 48s

Code review found 1 important issue

Found 5 candidates, confirmed 4. See review comments for details.

Details

Severity Count
🔴 Important 1
🟡 Nit 3
🟣 Pre-existing 0
Severity File:Line Issue
🔴 Important src/mcp/client/auth/oauth2.py:599-625 Hint-less eager PRM discovery treats a foreign co-hosted PRM as authoritative: permanent OAuthFlowError or destruction o
🟡 Nit src/mcp/client/auth/oauth2.py:646-659 Eager legacy issuer-mismatch drop keeps stale origin ASM metadata, enabling a false SEP-2352 issuer binding in the subse
🟡 Nit src/mcp/client/auth/oauth2.py:595-596 No-metadata legacy servers pay full discovery probes on every refresh, not just cold start
🟡 Nit src/mcp/client/auth/oauth2.py:673-686 Inner _refresh_with_discovery generator never closed when outer auth flow is aborted

Annotations

Check failure on line 625 in src/mcp/client/auth/oauth2.py

See this annotation in the file changed.

@claude claude / Claude Code Review

Hint-less eager PRM discovery treats a foreign co-hosted PRM as authoritative: permanent OAuthFlowError or destruction of valid credentials

On a cold start, `_refresh_with_discovery` runs PRM discovery with no WWW-Authenticate `resource_metadata` hint but applies the 401 path's authoritative semantics to whatever the blind well-known fallbacks return: on a co-hosted origin where the MCP server publishes its PRM only via the 401 hint, a foreign PRM served at the root well-known either (A) makes `_validate_resource_match` raise `OAuthFlowError` out of the httpx auth flow before the original request is ever sent — deterministically on 

Check warning on line 659 in src/mcp/client/auth/oauth2.py

See this annotation in the file changed.

@claude claude / Claude Code Review

Eager legacy issuer-mismatch drop keeps stale origin ASM metadata, enabling a false SEP-2352 issuer binding in the subsequent 401 flow

The eager legacy issuer-mismatch drop (post-ASM check) clears `client_info` and tokens but keeps the just-rejected origin `oauth_metadata` — and by nulling `client_info` it defeats the `client_info is not None` gate on the 401 path's defensive `oauth_metadata = None` clear. If the subsequent 401 flow then discovers the real AS via the WWW-Authenticate hint but its ASM discovery fails, Step 4 registers against the stale origin ASM's `registration_endpoint` yet stamps `issuer = <real AS>`, persist

Check warning on line 596 in src/mcp/client/auth/oauth2.py

See this annotation in the file changed.

@claude claude / Claude Code Review

No-metadata legacy servers pay full discovery probes on every refresh, not just cold start

For a legacy server that publishes no OAuth metadata anywhere, `oauth_metadata` is never populated, so the `if self.context.oauth_metadata is None` gate stays true forever and every in-process eager refresh (each token expiry, not just cold start) re-issues the 3 failed discovery probes (2 PRM + 1 ASM) before the refresh POST — pre-PR these refreshes made zero extra requests. Consider recording that discovery was already attempted (e.g. a context flag) so repeat refreshes skip straight to the `{

Check warning on line 686 in src/mcp/client/auth/oauth2.py

See this annotation in the file changed.

@claude claude / Claude Code Review

Inner _refresh_with_discovery generator never closed when outer auth flow is aborted

The drive loop for `_refresh_with_discovery` never closes the inner async generator: when httpx acloses the outer `async_auth_flow` (transport error or cancellation during a discovery/refresh request), `GeneratorExit` unwinds the loop at `yield refresh_request` and `refresh_flow` is abandoned suspended at a yield, to be finalized by GC — which emits a ResourceWarning ("async generator was garbage collected before it had been exhausted") on the trio backend. Wrap the loop in `async with contextli