Skip to content

fix: accumulate OAuth scopes across 401/403 for progressive authorization#2169

Open
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone:fix/scope-accumulation-progressive-auth
Open

fix: accumulate OAuth scopes across 401/403 for progressive authorization#2169
giulio-leone wants to merge 1 commit intomodelcontextprotocol:mainfrom
giulio-leone:fix/scope-accumulation-progressive-auth

Conversation

@giulio-leone
Copy link

Summary

Companion fix to modelcontextprotocol/typescript-sdk#1582 / modelcontextprotocol/typescript-sdk#1604.

The OAuthClientProvider overwrites client_metadata.scope on each 401/403 response instead of accumulating scopes. This causes an infinite re-authorization loop when an MCP server uses per-operation scopes (progressive/step-up authorization):

  1. initialize succeeds with scope init
  2. tools/list returns 403 → scope overwritten to mcp:tools:read (init lost)
  3. New token grants mcp:tools:read but not init
  4. Next operation needing init fails → scope overwritten to init (read lost)
  5. Loop between steps 2–4

Changes

  • merge_scopes() utility in utils.py: Computes the set-union of space-delimited OAuth scope strings per RFC 6749 §3.3
  • 401 handler in oauth2.py: merge_scopes(existing, new) instead of overwrite
  • 403 handler in oauth2.py: merge_scopes(existing, new) instead of overwrite
  • Updated existing test to verify scope accumulation
  • Added 7 unit tests for merge_scopes() covering all edge cases

Tests

All 91 auth tests pass (2 consecutive clean runs). ruff check and ruff format clean.

… authorization

Replace scope overwrite with union-based accumulation in OAuthClientProvider.
Both the 401 and 403 (insufficient_scope) handlers now merge new scopes with
previously-granted scopes via merge_scopes(), preventing infinite
re-authorization loops when a server uses per-operation scopes.

Companion fix to modelcontextprotocol/typescript-sdk#1604.

Closes modelcontextprotocol/typescript-sdk#1582

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant