feat: add subject and claims fields to AccessToken#2289
Open
yakub268 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
feat: add subject and claims fields to AccessToken#2289yakub268 wants to merge 1 commit intomodelcontextprotocol:mainfrom
yakub268 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1038
Adds two optional fields to
AccessTokento enable servers to identify the end-user behind an access token (not just the OAuth client application):subject: str | None = None— the JWT standardsubclaim, identifying the end-userclaims: dict[str, Any] | None = None— arbitrary JWT claims for carrying additional token dataAlso exposes
ctx.subjecton theContextclass in the MCPServer viaget_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: AddedAnyimport,subjectandclaimsfields toAccessTokensrc/mcp/server/mcpserver/context.py: AddedContext.subjectpropertytests/server/auth/middleware/test_auth_context.py: Addedsubjectto fixture + new test verifying subject flows through middlewaretests/server/auth/middleware/test_bearer_auth.py: Addedsubjectto token fixturesTest plan
test_auth_context_middleware_subject_preservedtest verifies subject is available viaget_access_token()during requestNonedefault — fully backwards compatible🤖 Generated with Claude Code