Skip to content

feat: add subject and claims fields to AccessToken#2209

Open
shivama205 wants to merge 1 commit intomodelcontextprotocol:mainfrom
shivama205:feat/access-token-subject-claims
Open

feat: add subject and claims fields to AccessToken#2209
shivama205 wants to merge 1 commit intomodelcontextprotocol:mainfrom
shivama205:feat/access-token-subject-claims

Conversation

@shivama205
Copy link

@shivama205 shivama205 commented Mar 4, 2026

Closes #1038

Summary

  • Add subject: str | None = None to AccessToken — stores the JWT sub claim (user ID) so token verifiers can populate it and callers can read it via get_access_token().subject
  • Add claims: dict[str, Any] | None = None to AccessToken — stores arbitrary custom JWT claims beyond the reserved set (as suggested by @robertofalk)
  • Add Context.subject property — lets tool handlers read the authenticated user's subject directly via ctx.subject without needing to import get_access_token (as suggested by @lukebuehler)

Both new fields are optional with None defaults, so this is fully backward compatible.

Test plan

  • TestAccessTokenFields in test_bearer_auth.py — backward compat, subject field, claims field, both together, propagation through AuthenticatedUser
  • TestContextSubject in test_context.py — unauthenticated returns None, token without subject returns None, token with subject returns value, reflects live contextvar state
  • Full auth + mcpserver test suite passes (400/400)
  • ruff check and ruff format clean
  • pyright clean (0 errors, 0 warnings)

@shivama205 shivama205 force-pushed the feat/access-token-subject-claims branch from c76d6d5 to cf4bce5 Compare March 4, 2026 19:10
Add two optional fields to AccessToken:
- subject: str | None — stores the JWT sub claim (user ID)
- claims: dict[str, Any] | None — stores arbitrary custom JWT claims

Also add Context.subject property so tool handlers can read the
authenticated user's subject via ctx.subject without importing
get_access_token directly.

Both fields default to None, preserving full backward compatibility.

Closes modelcontextprotocol#1038
@shivama205 shivama205 force-pushed the feat/access-token-subject-claims branch from cf4bce5 to f57edfa Compare March 4, 2026 19:17
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.

MCP server: AccessToken class should have field for subject claim ("sub")

1 participant