fix: refresh auth context for stateful HTTP requests#2221
Open
lavish0000 wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
fix: refresh auth context for stateful HTTP requests#2221lavish0000 wants to merge 3 commits intomodelcontextprotocol:mainfrom
lavish0000 wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
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
auth_context_varfrom the current request metadata at low-level request dispatch timeProblem
In stateful streamable HTTP mode, request handlers can run inside task-group tasks whose context was copied from the session-creating request. That means
get_access_token()keeps returning the bearer token from the first request in the session, even after later requests send a differentAuthorizationheader.Fix
Each handled request already carries its Starlette request object in
ServerMessageMetadata.request_context. At dispatch time, the low-level server now reads the authenticated user from that request scope and bindsauth_context_varfor the duration of the handler call. This refreshesget_access_token()on every request while preserving the existing behavior for unauthenticated apps.Validation
mainwith an in-process ASGI app and a mutable bearer auth client (token-Astayed visible after switching totoken-B)uv run pytest tests/issues/test_2208_stateful_auth_context.py tests/server/auth/middleware/test_auth_context.py tests/shared/test_streamable_http.py -k "2208 or auth_context or request_context_propagation or request_context_isolation"uv run ruff check src/mcp/server/lowlevel/server.py tests/issues/test_2208_stateful_auth_context.pyuv run ruff format --check src/mcp/server/lowlevel/server.py tests/issues/test_2208_stateful_auth_context.pyuv run pyright src/mcp/server/lowlevel/server.py tests/issues/test_2208_stateful_auth_context.py