feat: add state bag and ServerCallContextBuilder to ServerCallContext#364
feat: add state bag and ServerCallContextBuilder to ServerCallContext#364ikubicki wants to merge 2 commits intoa2aproject:mainfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
🧪 Code Coverage
Generated by coverage-comment.yml |
There was a problem hiding this comment.
Code Review
The pull request introduces a state property to ServerCallContext and a ServerCallContextBuilder pattern, effectively mirroring the Python A2A SDK's capabilities. The implementation is robust, including a default builder that populates request headers into the state, and a withRequestedExtensions method that correctly handles context immutability. The changes are well-integrated into existing Express and gRPC handlers, and the new sample and comprehensive unit tests demonstrate and validate the new functionality. The code quality is high, and the changes align perfectly with the stated motivation and objectives.
fix: removed unwanted exclude
87c04bd to
1a25ae6
Compare
Description
Motivation
The Python A2A SDK exposes a
statepropertyon
ServerCallContext— an arbitrary
MutableMapping[str, Any]used to pass metadata through the call pipeline.The Python app layer actively uses it (e.g.
call_context.state['method'] = method),and op-SDK implementations rely on it to carry request-scoped data such as tenant IDs,
auth tokens, or raw headers.
The TypeScript SDK was missing this capability, making it impossible to faithfully port
Python-based agent implementations to TypeScript without architectural workarounds.
Changes
ServerCallContextstateproperty — aMap<string, unknown>key/value bag, directly mirroringPython's
state: MutableMapping[str, Any].withRequestedExtensions()method that returns a new context with updated extensionswhile preserving
user,state, andactivatedExtensions.statemap.ServerCallContextBuilderServerCallContextBuilderfactory function type, mirroring Python's abstractCallContextBuilder.build(request)pattern.defaultServerCallContextBuilder— the default implementation that pre-populatesstatewith raw request headers under theheaderskey, mirroring Python'sDefaultCallContextBuilder.STATE_HEADERS_KEYconstant for the headers key.RequestHeaderstype — transport-agnostic representation of request headers.Express handlers (
jsonRpcHandler,restHandler)contextBuilder?: ServerCallContextBuildertoJsonRpcHandlerOptionsand
RestHandlerOptions, falling back todefaultServerCallContextBuilder.req.headersinto the context builder.gRPC handler
grpc_service.tsto pass request metadata as headers into the context builder.Exports
ServerCallContextBuilder,RequestHeaders,defaultServerCallContextBuilder,and
STATE_HEADERS_KEYare now exported from@a2a-js/sdk/server.Tests & samples
ServerCallContextAPI intest/server/context.spec.ts.src/samples/authentication/server_call_context.tsdemonstrating custom context usage.Impact
This change enables direct migration of Python A2A agent implementations to TypeScript —
any code relying on
context.statein the Python SDK will have a direct equivalent in theTypeScript SDK with the same semantics.
Checklist
CONTRIBUTINGGuide.