Fix OAuth Resource Metadata Extraction During Initial Connection#1472
Open
waltmayf wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
Fix OAuth Resource Metadata Extraction During Initial Connection#1472waltmayf wants to merge 3 commits intomodelcontextprotocol:mainfrom
waltmayf wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
Fixes the extraction of resource metadata URL during OAuth connection to prevent 'Invalid api path' errors. Ensures the SDK correctly handles 401 responses to discover token endpoints for OAuth flows using separate authorization servers.
Extract resource metadata URL and scope from WWW-Authenticate header on 401 response.
🦋 Changeset detectedLatest commit: 261fe74 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
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.
Summary
Fixes resource metadata URL extraction from WWW-Authenticate headers during initial OAuth connection, enabling the SDK to work with MCP servers that use separate authorization servers (AWS Cognito, Auth0, Okta, etc.).
Closes #1450
Motivation and Context
When connecting to MCP servers using OAuth with separate authorization servers, the SDK was failing during token exchange with an "Invalid api path" error.
Root Cause: The
resourceMetadataUrlfrom the WWW-Authenticate header was not being extracted during the initial connection attempt. This causedfinishAuth()to fall back to using the MCP server URL as the authorization server URL, resulting in incorrect token endpoint construction.Example Flow (Before Fix):
Example Flow (After Fix):
This fix is critical for enterprise deployments where MCP servers use managed identity providers separate from the MCP endpoint.
How Has This Been Tested?
Unit Tests ✅
Integration Tests ✅
Real-World Testing ✅
Verified with AWS Bedrock MCP server + AWS Cognito OAuth:
Test Environment:
Manual Verification
Used curl to verify each step of the OAuth discovery chain:
Breaking Changes
None. This is a bug fix that maintains backward compatibility with existing OAuth flows.
Types of changes
Checklist
Additional context
Files Modified
Source Code:
packages/client/src/client/streamableHttp.ts- Extract metadata in_startOrAuthSse()Documentation:
.changeset/fix-resource-metadata-extraction.md- Changeset for release notesImplementation Details
The fix adds resource metadata URL and scope extraction when receiving a 401 response during initial connection:
This ensures the metadata is available when
finishAuth()is called later in the OAuth flow.Impact
This fix enables the TypeScript SDK to work with all MCP servers that use:
resource_metadataparameter in WWW-Authenticate headersAffected Use Cases
Design Decisions
extractResourceMetadataUrl()andextractScope()functions