feat: add subject and claims fields to AccessToken#2209
Open
shivama205 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
feat: add subject and claims fields to AccessToken#2209shivama205 wants to merge 1 commit intomodelcontextprotocol:mainfrom
shivama205 wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
c76d6d5 to
cf4bce5
Compare
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
cf4bce5 to
f57edfa
Compare
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.
Closes #1038
Summary
subject: str | None = NonetoAccessToken— stores the JWTsubclaim (user ID) so token verifiers can populate it and callers can read it viaget_access_token().subjectclaims: dict[str, Any] | None = NonetoAccessToken— stores arbitrary custom JWT claims beyond the reserved set (as suggested by @robertofalk)Context.subjectproperty — lets tool handlers read the authenticated user's subject directly viactx.subjectwithout needing to importget_access_token(as suggested by @lukebuehler)Both new fields are optional with
Nonedefaults, so this is fully backward compatible.Test plan
TestAccessTokenFieldsintest_bearer_auth.py— backward compat,subjectfield,claimsfield, both together, propagation throughAuthenticatedUserTestContextSubjectintest_context.py— unauthenticated returnsNone, token without subject returnsNone, token with subject returns value, reflects live contextvar stateruff checkandruff formatcleanpyrightclean (0 errors, 0 warnings)