[client] Fix prioritization of requested Oauth scopes #1324
+146
−4
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.
Fix OAuth scope handling to prioritize PRM spec's
scopes_supported
over Authorization Server Metadata spec'sscopes_supported
, ensuring clients only request resource-specific scopes and preventing authorization rejections in multi-resource environments.Motivation and Context
Currently, the MCP client prioritizes all scopes from the OAuth authorization server metadata endpoint, which can cause issues in multi-resource environments. This update reverses the priority order: it first checks the Protected Resource Metadata (PRM) endpoint's
scopes_supported
field to obtain only the necessary scopes for the specific resource, and only falls back to the authorization server metadata if needed.This addresses issues such as:
"resource1:read"
and"resource2:read"
).How Has This Been Tested?
Comprehensive test cases were added in
tests/client/test_auth.py
to cover various scope handling scenarios:Breaking Changes
This is a breaking change for applications relying on the previous scope handling behavior. The new implementation aligns with the PRM specification, which states that
scopes_supported
contains "scope values that are used in authorization requests to request access to this protected resource."Types of Changes
Checklist
Additional Context
The core change modifies the
_handle_oauth_metadata_response
method insrc/mcp/client/auth.py
to properly prioritize scopes from PRM over OAuth metadata. This ensures clients only request scopes relevant to the specific resource, improving security and reducing authorization rejections.