fix: preserve canonical URL format in OAuth resource parameter per MCP auth spec #829
+287
−41
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.
Fix OAuth resource parameter URL handling to preserve canonical URL format and prevent automatic trailing slash addition, and also ensuring compliance with MCP auth specification requirements.
Motivation and Context
When using the MCP Client SDK, I discovered that resource indicators from MCP server backend's resource metadata were automatically getting trailing slashes appended, causing authentication server validation failures when checking the resource indicator.
This issue occurred because the previous implementation used
URL.href
which automatically normalizes URLs by:https://example.com
becomeshttps://example.com/
)HTTPS://EXAMPLE.COM
becomeshttps://example.com/
)Both behaviors violate the MCP auth specification requirements. According to the MCP auth specification:
How Has This Been Tested?
✅ Unit tests: Added comprehensive test coverage for
resourceUrlFromServerUrl
function including:✅ OAuth flow integration tests: Added test suite "resource URL handling (trailing slash preservation)" in
src/client/auth.test.ts
:preserves server URLs without trailing slash in resource parameter
- verifies authorization flowpreserves server URLs with trailing slash in resource parameter
- verifies authorization flowhandles token exchange with preserved resource URL format
- verifies token exchange flow✅ Existing tests: All 667 existing tests pass, ensuring no regressions in:
✅ Build verification: TypeScript compilation and ESLint checks pass
Breaking Changes
Minor breaking change: The exported function signatures in the auth module have changed from accepting
URL
objects tostring
parameters to ensure MCP spec compliance:validateResourceURL?(serverUrl: string, resource?: string)
(wasURL
objects)selectResourceURL(serverUrl: string, ...)
(wasURL
object)resourceUrlFromServerUrl(url: string)
(wasURL
object)Impact: Most users are unlikely to be affected as these functions are primarily used internally by the SDK. The change was necessary because the previous implementation with
URL
objects automatically normalized URLs in ways that violated the MCP auth specification requirements.Migration: If you were directly using these functions, update calls to pass
string
URLs instead ofURL
objects. Use.href
property if converting from URL objects.Types of changes
Checklist