Fix OAuth Resource Metadata Extraction During Initial Connection #1472
+16
−0
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