[v1.x] fix(client): send the scope parameter on refresh-token requests - #2937
Open
thegodshatetexas wants to merge 2 commits into
Open
thegodshatetexas wants to merge 2 commits into
thegodshatetexas wants to merge 2 commits into
Conversation
refreshAuthorization() accepts a new optional `scope` and sets it on the request body when it is a non-empty string. The auth() flow sends exactly the granted scope recorded on the stored tokens. When no granted scope is recorded, no scope parameter is sent, so the wire shape is unchanged by default. Omitting scope on a refresh grant is legal per RFC 6749 section 6, but some authorization servers require it to resolve the target resource. Microsoft Entra ID rejects a scope-less refresh with AADSTS90009 whenever the OAuth client application is also the resource, a common setup for Entra-protected MCP servers. Initial authorization succeeds because the scope travels on the authorize request, so affected clients work until the first access-token expiry and then wedge. Backport of the v2 fix for modelcontextprotocol#2718.
🦋 Changeset detectedLatest commit: a40eb50 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: |
This branch has not been deployed
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.
Backport of #2720 to
v1.x. Fixes #2718 for the v1 line.refreshAuthorization()gains an optionalscopeand sets it on the refresh request when it is a non-empty string, and theauth()flow sends exactly the granted scope recorded on the stored tokens. When no granted scope is recorded, nothing changes on the wire.Motivation and Context
#2720 fixes
main. #2718's reporter and I are both blocked on clients that bundlev1.x, and #2720 says so itself: "v1.x (src/client/auth.ts) has the same two-parameter request body; happy to open the backport if wanted." That backport has not been opened, so the fix currently reaches nobody on v1.Concretely, Claude Code 2.1.287 bundles this line. Its
refreshAuthorizationandexecuteTokenRequestmatch tag1.31.0parameter for parameter and do not matchmain, which addsdpop. Merging #2720 alone therefore does not fix the client named in #2718.The underlying problem:
refreshAuthorization()builds its token request fromgrant_typeandrefresh_tokenonly, so there is no way to putscopeon a refresh request. Omitting it is legal per RFC 6749 §6, but some authorization servers require it to resolve the target resource. Microsoft Entra ID rejects a scope-less refresh withAADSTS90009("Application is requesting a token for itself") whenever the OAuth client application is also the resource, a standard setup for Entra-protected MCP servers. Initial authorization succeeds because the scope travels on/authorize, so affected clients work until the first access-token expiry and then wedge.Design notes, kept deliberately identical to #2720 so the two lines stay consistent:
scopeis set only when non-empty. An empty granted scope (GitHub returns"scope": "") sends no parameter, since a literal emptyscope=is invalid per RFC 6749 §3.3.scopewhen it equals the request, and dropping it would strand the next refresh with nothing to send. Ascopereturned by the server still wins.auth()sends the stored granted scope, not a recomputed value, which may have widened since the grant and would drawinvalid_scopewhere today's scope-less request succeeds.How Has This Been Tested?
Against a live Entra tenant, and in the suite.
On the tenant, using a fresh refresh token and varying only
resourceandscope: three scope-less variants fail withAADSTS90009across three differentresourcevalues, and the same grant carrying the granted scope returns 200 both with and withoutresource. The resource form is irrelevant; the missing scope is the whole cause.Unit tests were written failing-first. With them in place and
src/client/auth.tsreverted, exactly the two behavioral tests fail:refreshAuthorizationwithscopeputs it on the body (fails without the change)refreshAuthorizationwithoutscopesends no parameter (back-compat pin, passes either way)scope: ''sends no parameter (RFC 6749 §3.3 pin, passes either way)scopeoverrides the requested one (down-scope case)Full suite: 1857 passed across 56 files.
tsgo --noEmit, eslint and prettier all clean.Breaking Changes
None. The new option is additive, no public API is removed or changed, and with
scopeomitted the request body is byte-for-byte what it was.Types of changes
Checklist
Additional context
A changeset is included (
patch,@modelcontextprotocol/sdk), and the new option carries TSDoc explaining the RFC 6749 §6 position and the Entra behavior.#2720 has not merged yet, so if its design changes in review I will rebase this to match rather than let the two lines diverge. Happy to hold until #2720 lands if you would rather take them in order.