Skip to content

feat: add subject and claims fields to AccessToken#2289

Open
yakub268 wants to merge 1 commit intomodelcontextprotocol:mainfrom
yakub268:feat/access-token-subject-field
Open

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

Conversation

@yakub268
Copy link

Summary

Closes #1038

Adds two optional fields to AccessToken to enable servers to identify the end-user behind an access token (not just the OAuth client application):

  • subject: str | None = None — the JWT standard sub claim, identifying the end-user
  • claims: dict[str, Any] | None = None — arbitrary JWT claims for carrying additional token data

Also exposes ctx.subject on the Context class in the MCPServer via get_access_token() from the auth middleware context var, so tool/resource handlers can access the authenticated user's identity without reaching into the middleware layer directly.

Changes

  • src/mcp/server/auth/provider.py: Added Any import, subject and claims fields to AccessToken
  • src/mcp/server/mcpserver/context.py: Added Context.subject property
  • tests/server/auth/middleware/test_auth_context.py: Added subject to fixture + new test verifying subject flows through middleware
  • tests/server/auth/middleware/test_bearer_auth.py: Added subject to token fixtures

Test plan

  • All 19 existing middleware tests pass
  • New test_auth_context_middleware_subject_preserved test verifies subject is available via get_access_token() during request
  • Both new fields are optional with None default — fully backwards compatible

🤖 Generated with Claude Code

Adds `subject: str | None` (JWT sub claim) and `claims: dict[str, Any] | None`
(arbitrary JWT claims) to AccessToken to enable servers to identify the
end-user behind an access token, not just the OAuth client.

Also exposes `ctx.subject` on the MCPServer Context class via
`get_access_token()` from the auth middleware context, following the
same pattern as `ctx.client_id`.

Fixes modelcontextprotocol#1038

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.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.

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

1 participant