Skip to content

[v1.x] fix(client): start a new authorization when the step-up needs a scope that the token lacks - #3114

Open
nicolas-grekas wants to merge 3 commits into
modelcontextprotocol:v1.xfrom
nicolas-grekas:fix/v1x-step-up-reauthorize
Open

nicolas-grekas wants to merge 3 commits into
modelcontextprotocol:v1.xfrom
nicolas-grekas:fix/v1x-step-up-reauthorize

Conversation

@nicolas-grekas

@nicolas-grekas nicolas-grekas commented Oct 27, 2026 •

Copy link
Copy Markdown

Backport of the step-up part of #2356 to v1.x. Fixes #2255.

On a 403 insufficient_scope, auth() refreshes the token when the provider has a refresh token. A refresh cannot add scopes (RFC 6749 §6), so the retry gets the same 403 and the transport throws "Server returned 403 after trying upscoping". The user is never asked for the missing scope.

Now the transport passes forceReauthorization to auth() when the token lacks a challenged scope. auth() then starts a new authorization instead of refreshing. Like on main, this authorization also asks for the scopes requested and granted before, so the new token keeps them (#1582 for v1.x). I left out the retry cap of #2356, and kept the existing guard against a repeated challenge. A later 401 keeps these scopes too, and providers without a redirectUrl (client credentials, JWT bearer) send them in their token request, as on main (spotted by the review below).

The redirects test and the 403-scope-upgrade e2e scenario now give the token the challenged scope, so that they still cover a refresh. The e2e suite also gets the stepup:scope-union and stepup:refresh-bypass-on-superset scenarios of main.

I found this while building an MCP server for the Symfony showcase app, with Keycloak as authorization server. With 1.32.2, the step-up refreshes the token and fails. With this branch, it asks for openid profile email proposals:write and the call succeeds.

…a scope that the token lacks

A refresh cannot add scopes, so the retry of a 403 insufficient_scope got the same 403 when the client had a refresh token. The new authorization also asks for the scopes requested and granted before. Same change as modelcontextprotocol#2356 on main. Fixes modelcontextprotocol#2255.
@nicolas-grekas
nicolas-grekas requested review from a team as code owners October 27, 2026 13:07
@changeset-bot

changeset-bot Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 33fc5f3

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

@pkg-pr-new

pkg-pr-new Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@modelcontextprotocol/sdk@3114

commit: 33fc5f3

The 403-scope-upgrade scenario now gives its refreshed token the challenged scope, so that it still covers the refresh and the loop guard.
@claude claude Bot added the v1 Issues / PRs related to v1.x label Oct 28, 2026
}
// Keep the scopes requested and granted before, so that the new token doesn't lose them.
const tokens = await this._authProvider.tokens();
this._scope = computeScopeUnion(this._scope, tokens?.scope, scope);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 401 branch above (line 544) still does this._scope = scope, so after a step-up a later 401 narrows _scope back to whatever that challenge lists, and the new authorization would drop the scopes this union added. On main the 401 path also does this._scope = computeScopeUnion(this._scope, scope) for that reason. Could that part come along with this backport too?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, done in 33fc5f3: the 401 branch keeps the union too, with a test where a 401 after the step-up challenges a narrower scope.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, checked 33fc5f3, the scope union is kept on a later 401 now and the new test covers it

Comment thread src/client/auth.ts

// Handle token refresh or new authorization
if (tokens?.refresh_token) {
if (tokens?.refresh_token && !forceReauthorization) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For providers without redirectUrl (client_credentials, jwt-bearer), the step-up returns earlier at line 650 and fetchToken still takes the scope from provider.clientMetadata.scope, not resolvedScope, so the challenged scope never reaches prepareTokenRequest and the second 403 hits the upscoping guard. Main passes scope: resolvedScope into fetchToken there. Fine if that's meant for a separate PR, just mentioning it since the changeset reads as step-up in general.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in the same commit: fetchToken() takes a scope option and auth() passes resolvedScope, as on main. This also changes the first token request of a client credentials provider when the protected resource metadata lists scopes_supported: it asks for them instead of clientMetadata.scope, as the authorization request already does on v1.x. The changeset says so.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, checked 33fc5f3, fetchToken takes a scope now and auth() passes the resolved scope through, with a test for it

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1 Issues / PRs related to v1.x

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants