fix: include scope parameter in OAuth authorization code token exchange#1669
Open
Vadaski wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: include scope parameter in OAuth authorization code token exchange#1669Vadaski wants to merge 1 commit intomodelcontextprotocol:mainfrom
Vadaski wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
…ontextprotocol#941) - `prepareAuthorizationCodeRequest()` now accepts an optional `scope` parameter and appends it to the token request body when provided, keeping the call backwards compatible. - `fetchToken()` gains a `scope` option. For the `authorization_code` path it forwards *only* the explicitly supplied scope so that a server which narrowed the granted scope during authorization does not receive a broader re-assertion (RFC 6749 §4.1.3). For the custom `prepareTokenRequest()` path (client_credentials, jwt-bearer, …) it retains the previous behaviour of falling back to `provider.clientMetadata.scope`, because those flows initiate a fresh token request rather than redeeming a pre-authorized grant. - `auth()` computes `tokenExchangeScope` as the explicit scope from the WWW-Authenticate challenge only; it does not inject PRM scopes or clientMetadata.scope into the token exchange, preventing invalid_scope errors from providers (e.g. Azure AD) that narrow the granted scope at the authorization endpoint. Closes modelcontextprotocol#941 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
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.
Problem
When exchanging an authorization code for tokens, the SDK never included a
scopeparameter in the token request. This breaks providers (e.g. Azure AD) that require the scope to be re-stated in the token exchange, and prevents callers from controlling which scope is requested.Closes #941
Root Cause
prepareAuthorizationCodeRequest()had noscopeparameter; andfetchToken()/auth()had no way to thread scope through to the token endpoint.Changes
prepareAuthorizationCodeRequest()Added an optional
scope?: stringparameter. When provided it is appended to theURLSearchParams; when omitted the function behaves exactly as before (no scope field, fully backwards compatible).fetchToken()— newscopeoptionThe function now accepts an explicit
scopeoption.authorization_codevia built-inprepareAuthorizationCodeRequestscopeis forwarded.clientMetadata.scopeis never injected. This preventsinvalid_scopefrom servers that narrowed the grant during authorization (RFC 6749 §4.1.3).prepareTokenRequest()(client_credentials, jwt-bearer, …)provider.clientMetadata.scopeas before, because these flows start a fresh request rather than redeeming a pre-authorized grant.auth()—tokenExchangeScopeauth()now computes a separatetokenExchangeScope = scope(the raw scope from theWWW-Authenticatechallenge, if any). It passes only that explicit server-provided value intofetchToken(), so neither PRMscopes_supportednorclientMetadata.scopeare re-injected at the token-exchange step.resolvedScope(the full fallback chain: WWW-Authenticate → PRM → clientMetadata) continues to be used for DCR and the authorization redirect, as before.Test Coverage
prepareAuthorizationCodeRequest: includes scope when provided; omits scope when not provided.fetchToken: explicit scope reaches the token body; no scope injection fromclientMetadatawhen called without explicit scope.exchangeAuthorization/ fullauth()tests continue to pass unchanged.Compatibility
No breaking changes. All existing call sites that do not pass
scopetoprepareAuthorizationCodeRequestorfetchTokenbehave identically to before.