Skip to content

Conversation

@abliznyuk
Copy link
Contributor

This PR fixes an issue where the OAuth2 client passed the protected-resource URL as the scope value during token requests. The implementation now correctly uses the parsed scope (or None when no scope is provided).
Fix #1630

Motivation and Context

The previous behavior incorrectly substituted the OAuth protected-resource URL for the scope parameter, leading to malformed token requests and potential authorization failures. By ensuring that the correct scope is passed, OAuth2 flows behave as expected and remain compliant with spec and provider expectations.

How Has This Been Tested?

The fix was tested against an OAuth2 provider in a real application environment. Token requests were validated to ensure that:

  • the correct scope value is included,
  • requests without an explicit scope omit the field rather than using the resource URL,
  • no regressions occur in authenticated MCP interactions.

Breaking Changes

No breaking changes. This is a bug fix that restores correct behavior and should not require user code changes.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Documentation
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have added or updated documentation as needed

Additional context

The problematic logic was located around the OAuth2 request-building code near lines 508–512, where the URL was incorrectly forwarded as the scope. The updated implementation ensures correct scope derivation and preserves compatibility with standard OAuth2 servers.

@maxisbey maxisbey added bug Something isn't working auth Issues and PRs related to Authentication / OAuth P1 Significant bug affecting many users, highly requested feature labels Nov 16, 2025
maxisbey
maxisbey previously approved these changes Nov 16, 2025
@maxisbey maxisbey enabled auto-merge (squash) November 16, 2025 17:11
@maxisbey
Copy link
Contributor

Thank you for this fix! I've gone ahead and added a unit test which fails on main just to confirm the fix.

This test verifies that when a 401 response contains both resource_metadata
and scope in the WWW-Authenticate header, the actual scope is used instead
of incorrectly using the resource_metadata URL.

Github-Issue:modelcontextprotocol#1630
@maxisbey maxisbey merged commit 5489e8b into modelcontextprotocol:main Nov 16, 2025
18 checks passed
@abliznyuk abliznyuk deleted the fix-aouth2-scope-bug branch November 17, 2025 11:27
maxisbey added a commit that referenced this pull request Nov 17, 2025
Co-authored-by: Max Isbey <224885523+maxisbey@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auth Issues and PRs related to Authentication / OAuth bug Something isn't working P1 Significant bug affecting many users, highly requested feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth2: Protected-resource URL passed as scope instead of parsed scope

2 participants