Repository navigation
OAuth: Resource metadata URL lost after redirect, causing token exchange to fail #1234
Description
Problem
When using OAuth in browser environments, the resource metadata URL discovered during the initial WWW-Authenticate challenge is lost after the redirect to the authorization server and back. This causes the token exchange to fail because the SDK tries to re-probe for the Protected Resource Metadata (PRM) and can't find the token endpoint.
Current Behavior
- Client makes request, gets
401withWWW-Authenticateheader containing resource metadata URL - SDK extracts PRM URL and fetches authorization server metadata
- User is redirected to authorization server
- After redirect back, the SDK needs to exchange the auth code for a token
- Problem: The SDK tries to fetch PRM again but the original resource metadata URL was lost during the redirect
- Token exchange fails
Expected Behavior
The SDK should retain knowledge of:
- The resource metadata URL from the initial
WWW-Authenticateheader - The authorization server metadata (so it can go straight to the token endpoint)
This would avoid needing to re-probe for the token endpoint after the redirect.
Possible Solutions
-
SDK-level fix: Cache both the PRM URL and the authorization server metadata so token exchange can proceed without re-discovery
-
OAuth provider callback: Add a new method to the OAuth provider interface that gets called with the resource metadata, allowing implementations to persist it (e.g., to
sessionStorage) -
Popup/new tab pattern: Open the auth URL in a popup/new tab and use
window.opener.postMessageto communicate back, keeping the original page open with its state intact -
Application-level workaround: Applications can manually capture and store the
resourceMetadataUrlbefore redirect and retrieve it afterward (e.g., inlocalStorage/sessionStorage)
Context
This is particularly problematic in browser environments where navigation away from the page loses the transport instance's internal state. The resource metadata URL is currently extracted within the transport instance, so applications don't have direct access to persist it.
Related
- Inspector issue: needs to either implement a workaround or wait for SDK fix
- Affects any browser-based MCP client using OAuth with redirects
Activity
fix: persist resource metadata URL across OAuth redirects
fix: persist resource metadata URL across OAuth redirects
windro-xdd commentedon Mar 29, 2026
I'd like to take this one. I'll implement persistence of resource metadata through the browser redirect flow so token exchange can continue without re-discovery, with regression tests covering the redirect round-trip.
errmakov commentedon Sep 27, 2026
I'd like to take this one.
felixweinberger commentedon Oct 5, 2026
Fixed by #1527 (2.0.0) and #1533 (1.27.0): a provider that implements
saveDiscoveryState()anddiscoveryState()keeps the metadata address across the redirect. Closing.