modelcontextprotocol / modelcontextprotocol/typescript-sdk
OAuth: Resource metadata URL lost after redirect, causing token exchange to fail
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 13.4k
- Forks
- 2.2k
- Avg merge
- 3d 15h
- Merged PRs (30d)
- 4
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing the transport instance's PRM URL extraction through the browser redirect and token exchange flow described in the issue. Compare the SDK-level cache and OAuth provider callback approaches; done means the resource metadata and authorization server metadata survive navigation and the token exchange succeeds without re-probing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- authentication, web-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100