Skip to content

Commit d62df7a

Browse files
feat: add JSON-RPC method to ServerCallContext.state (#463)
# Improvement SDK users sometimes need to know which JSON-RPC method is being processed to adjust their implementation, e.g.: * If the method is `message/send`, they may want to use `invoke()`/`run()` when calling their LLM API, and `stream()`/`run_streamed()` for `message/stream`. # Implementation Added a `method` field to `ServerCallContext.state`, similar to `headers`. # Tests Added a small test to ensure that method parsing is correct. Fixes #451
1 parent ceeb4ef commit d62df7a

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/a2a/server/apps/jsonrpc/jsonrpc_app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ async def _handle_requests(self, request: Request) -> Response: # noqa: PLR0911
337337

338338
# 3) Build call context and wrap the request for downstream handling
339339
call_context = self._context_builder.build(request)
340+
call_context.state['method'] = method
340341

341342
request_id = specific_request.id
342343
a2a_request = A2ARequest(root=specific_request)

tests/server/apps/jsonrpc/test_jsonrpc_app.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,26 @@ def test_request_with_comma_separated_extensions_no_space(
289289
call_context = mock_handler.on_message_send.call_args[0][1]
290290
assert call_context.requested_extensions == {'foo', 'bar', 'baz'}
291291

292+
def test_method_added_to_call_context_state(self, client, mock_handler):
293+
response = client.post(
294+
'/',
295+
json=SendMessageRequest(
296+
id='1',
297+
params=MessageSendParams(
298+
message=Message(
299+
message_id='1',
300+
role=Role.user,
301+
parts=[Part(TextPart(text='hi'))],
302+
)
303+
),
304+
).model_dump(),
305+
)
306+
response.raise_for_status()
307+
308+
mock_handler.on_message_send.assert_called_once()
309+
call_context = mock_handler.on_message_send.call_args[0][1]
310+
assert call_context.state['method'] == 'message/send'
311+
292312
def test_request_with_multiple_extension_headers(
293313
self, client, mock_handler
294314
):

0 commit comments

Comments
 (0)