fix: handle URL-encoded token responses from OAuth providers#1599
Open
giulio-leone wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
Open
fix: handle URL-encoded token responses from OAuth providers#1599giulio-leone wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
giulio-leone wants to merge 2 commits intomodelcontextprotocol:v1.xfrom
Conversation
🦋 Changeset detectedLatest commit: f465500 The changes in this PR will be included in the next version bump. 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 |
commit: |
added 2 commits
February 28, 2026 15:50
Some OAuth providers (e.g. GitHub) return token responses in application/x-www-form-urlencoded format instead of JSON. The SDK already sends Accept: application/json, but non-compliant servers may still return URL-encoded responses. This adds Content-Type-aware parsing in executeTokenRequest: - If Content-Type is application/x-www-form-urlencoded, parse with URLSearchParams - Otherwise, default to response.json() (existing behavior) Fixes modelcontextprotocol#759
Author
|
All CI checks pass. Ready for review. |
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
Some OAuth providers (e.g. GitHub) return token endpoint responses in
application/x-www-form-urlencodedformat instead of JSON. The SDK callsresponse.json()unconditionally, causing aSyntaxErrorwhen the response is URL-encoded.Root Cause
In
executeTokenRequest(), line 1246:While the SDK already sends
Accept: application/json(which handles compliant servers like GitHub), some OAuth providers may still return URL-encoded responses regardless of the Accept header.Fix
Added
parseTokenResponse()helper that checks the responseContent-Typeheader:application/x-www-form-urlencoded: Parses withURLSearchParamsand converts to a plain objectapplication/jsonand missing header): Falls through toresponse.json()(existing behavior, fully backward-compatible)The
OAuthTokensSchemausesz.coerce.number()forexpires_in, so string values from URL-encoded responses are automatically coerced to numbers.Tests
Added 3 new tests in
exchangeAuthorization:content-type: application/x-www-form-urlencodedexpires_inas string (coerced to number)content-type: application/json(regression test)Verification
2 consecutive clean test runs: 1550/1550 tests passed, 0 failures.
Fixes #759