Conversation
closes #590 ``A2ABearerAuthMiddleware`` previously only stashed the principal in ``scope["user"]`` / ``scope["auth"]`` for a2a-sdk's ``ServerCallContext`` path. The contract advertised by the matching MCP middleware (``BearerTokenAuthMiddleware``) — populating ``current_principal``, ``current_tenant``, and ``current_principal_metadata`` ContextVars for the downstream call — silently held only on MCP. Adopters with tenant policies that read ``current_principal.get()`` from a platform method (salesagent's default) saw ``None`` on every A2A ``message/send``, locking buyers out of A2A while MCP worked on the same token. Symmetric fix: set the same three ContextVars in the A2A middleware between auth and the inner-app await, with unconditional reset in ``finally`` so a buggy handler can't poison the contextvars for a shared task. Adds three regression tests to tests/test_serve_auth_both.py exercising the populated, reset, and exception-path resets. Co-Authored-By: Claude Opus 4.7 (1M context) <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 #590.
A2ABearerAuthMiddlewarepreviously only stashed the principal inscope[\"user\"]/scope[\"auth\"]for a2a-sdk'sServerCallContextpath. The contract advertised by the matching MCP middleware (BearerTokenAuthMiddleware) — populatingcurrent_principal,current_tenant, andcurrent_principal_metadataContextVars for the downstream call — silently held only on MCP.Adopters with tenant policies that read
current_principal.get()from a platform method (salesagent's default) sawNoneon every A2Amessage/send, locking buyers out of A2A while MCP worked on the same token.Root cause
BearerTokenAuthMiddleware.dispatchsets all threeA2ABearerAuthMiddleware.__call__only setscope[\"user\"]The user's diagnostic in #590 attributed this to a contextvar boundary (
asyncio.create_taskin a2a-sdk'sDefaultRequestHandler), but reproducing the path locally showed contextvars do propagate throughasyncio.create_task. The actual mechanism was simpler: the A2A middleware never set the contextvars in the first place, so the platform handler saw the defaultNoneregardless of the propagation path.Fix
Symmetric set + finally-reset in
A2ABearerAuthMiddleware.__call__between auth success and the inner-app await. Behavior now identical across transports for sellers readingcurrent_principal.get()directly from a platform method.Test plan
tests/test_serve_auth_both.py:test_valid_token_populates_contextvars— all three contextvars set during downstream calltest_contextvars_reset_after_call— reset to default after successtest_contextvars_reset_even_on_inner_exception— reset on exception pathruff check+mypycleanAdopter impact
Existing readers of
current_principal.get()on A2A start working without code changes. Thectx.caller_identitypath throughServerCallContext.useris unchanged — both APIs now reliably hold the same identity on both transports, matching the docstring contract.🤖 Generated with Claude Code