Skip to content

fix(mcp): AccessToken.scopes carries the token's real OAuth scope - #134

Merged
Ooscaar merged 1 commit into
developfrom
t3/oauth-token-scopes
Nov 27, 2026
Merged

Ooscaar merged 1 commit into
developfrom
t3/oauth-token-scopes

Conversation

@Ooscaar

@Ooscaar Ooscaar commented Nov 26, 2026

Copy link
Copy Markdown
Member

Why

ZernioTokenVerifier._grant() set AccessToken.scopes to every scope for every token. The real OAuth scope from GET /api/v1/auth/verify was already kept in claims["scope"], so scopes said the wrong thing about narrow OAuth grants.

What

  • OAuth token (claims has a scope string): scopes is that scope, expanded the way the API expands it. A legacy full list (at least the original 8, and a prefix of OAUTH_SCOPES in order) means every current scope. expand_legacy_full_scope is a port of expandLegacyFullScope from Schedule-Posts-API libs/oauth/scopes.ts. OAUTH_SCOPES order already matches the API's (checked against main).
  • API key / no scope claim: list(OAUTH_SCOPES), as before.
  • Cache and grace grants read the scope from the cached identity claims, with the same fallback.
  • claims["scope"] stays the raw string, so MissingScopeMiddleware is unchanged. No required_scopes, no scopes_supported change, no tool hidden.

Readers of AccessToken.scopes

  • Ours (src/late/mcp): none. MissingScopeMiddleware reads claims["scope"], not scopes.
  • FastMCP server/telemetry.py get_auth_span_attributes: sets enduser.scope on OTel spans. Now shows the real scope for OAuth tokens. No OTel exporter is configured in this repo, so nothing observable changes.
  • FastMCP utilities/authorization.py (require_scopes, restrict_tag) and server/middleware/authorization.py AuthMiddleware: only run for components or middleware with auth=. We set none.
  • MCP SDK server/auth/middleware/bearer_auth.py: AuthCredentials(scopes) and RequireAuthMiddleware, which checks only required_scopes. Ours is empty, so the loop never runs.

Tests

tests/test_mcp_auth_cache.py: narrow scope, legacy full lists (8, 13, 22), 8 scopes that are not the original 8 stay narrow, API key, cache hit, grace for OAuth token and API key.

Follow-ups

Hide tools per connection with FastMCP auth= plus an any-of check and the per-connection BM25 tool search, in log-only mode first. Not in this PR.

🤖 Generated with Claude Code

OAuth tokens get the scope from /v1/auth/verify, with legacy full lists
expanded to every current scope (port of expandLegacyFullScope). API keys
keep every scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ooscaar
Ooscaar merged commit 49ac55e into develop Nov 27, 2026
4 checks passed
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