-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[client] Implement MCP OAuth scope selection and step-up authorization #1324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[client] Implement MCP OAuth scope selection and step-up authorization #1324
Conversation
Can we confirm if updating the Python SDK will update the Claude Client, or will we also need to make updates in the TypeScript SDK for these to reflect in Claude.ai? In the Typescript SDK, for example, we are not retrieving scopes from the PRM endpoint: https://github.com/modelcontextprotocol/typescript-sdk/blob/main/src/client/auth.ts#L328 |
src/mcp/client/auth.py
Outdated
elif metadata.scopes_supported is not None: | ||
self.context.client_metadata.scope = " ".join(metadata.scopes_supported) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see this latest change to the spec:
When implementing authorization flows, MCP clients SHOULD follow the principle of least privilege by requesting
only the scopes necessary for their intended operations. During the initial authorization handshake, MCP clients
SHOULD follow this priority order for scope selection:
- Use
scope
parameter from the initialWWW-Authenticate
header in the 401 response, if provided- If
scope
is not available, use all scopes defined inscopes_supported
from the Protected Resource Metadata document, omitting thescope
parameter ifscopes_supported
is undefined.This approach accommodates the general-purpose nature of MCP clients, which typically lack domain-specific knowledge to make informed decisions about individual scope selection. Requesting all available scopes allows the authorization server and end-user to determine appropriate permissions during the consent process.
Are you able to update this PR to:
- Get scope from
www-authenticate
- Omit scope if not present in PRM? (currently impl falls back to
scopes_supported
from AS metadata)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Although the complete priority order is:
- Explicitly configured scope in the client
www-authenticate
scope- PRM scope
- Omit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to this spec https://modelcontextprotocol.io/specification/draft/basic/authorization#scope-challenge-handling the step-up only happens when the server returns 403. The changes in this PR only take effect upon a 401.
I can definitely update the 401 path by not prioritizing the configured scope on the client, but the step-up logic remains unhandled in the MCP client in this SDK. I can create a separate PR for that if that makes sense
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make it part of this PR too considering this is already a breaking change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
working on adding step-up auth flow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
…fix-oauth-scope-handling
…e 2. WWW-auth scope 3. PRM scopes
Implements the MCP specification's OAuth scope selection strategy and step-up authorization flow for handling scope challenges.
Specification Sections Implemented
Scope Selection Strategy - Priority order for selecting scopes during authorization:
scope
from WWW-Authenticate header (if provided)scopes_supported
(if available)Scope Challenge Handling - Step-up authorization when 403 with
insufficient_scope
is received:Changes Made
Core Implementation:
_extract_field_from_www_auth()
- Generic WWW-Authenticate header parser_extract_scope_from_www_auth()
- Extracts scope per RFC6750_select_scopes()
- Implements MCP scope selection priorityasync_auth_flow()
- Step-up authorization flowTesting
Comprehensive test coverage added in
tests/client/test_auth.py
:Scope Selection Tests:
Step-Up Authorization Tests:
insufficient_scope
triggers step-up flowinsufficient_scope
does not trigger step-upBreaking Changes
scopes_supported
Checklist