Skip to content

Commit f124ddd

Browse files
authored
refactor: Make ServerCallContext a required parameter for RequestHandler methods (#782)
## Changes - make `ServerCallContext` a required parameter for `RequestHandler` methods - update corresponding tests ## Note I will make `ServerCallContext` a required parameter for the rest of the Server in other PRs Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly: ## Contributing Guide - [x] Follow the [`CONTRIBUTING` Guide](https://github.com/a2aproject/a2a-python/blob/main/CONTRIBUTING.md). - [ ] Make your Pull Request title in the <https://www.conventionalcommits.org/> specification. - Important Prefixes for [release-please](https://github.com/googleapis/release-please): - `fix:` which represents bug fixes, and correlates to a [SemVer](https://semver.org/) patch. - `feat:` represents a new feature, and correlates to a SemVer minor. - `feat!:`, or `fix!:`, `refactor!:`, etc., which represent a breaking change (indicated by the `!`) and will result in a SemVer major. - [x] Ensure the tests and linter pass (Run `bash scripts/format.sh` from the repository root to format) - [x] Appropriate docs were updated (if necessary) Fixes #718 🦕
1 parent 72a330d commit f124ddd

4 files changed

Lines changed: 100 additions & 56 deletions

File tree

src/a2a/server/request_handlers/default_request_handler.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def __init__( # noqa: PLR0913
123123
async def on_get_task(
124124
self,
125125
params: GetTaskRequest,
126-
context: ServerCallContext | None = None,
126+
context: ServerCallContext,
127127
) -> Task | None:
128128
"""Default handler for 'tasks/get'."""
129129
validate_history_length(params)
@@ -138,7 +138,7 @@ async def on_get_task(
138138
async def on_list_tasks(
139139
self,
140140
params: ListTasksRequest,
141-
context: ServerCallContext | None = None,
141+
context: ServerCallContext,
142142
) -> ListTasksResponse:
143143
"""Default handler for 'tasks/list'."""
144144
validate_history_length(params)
@@ -159,7 +159,7 @@ async def on_list_tasks(
159159
async def on_cancel_task(
160160
self,
161161
params: CancelTaskRequest,
162-
context: ServerCallContext | None = None,
162+
context: ServerCallContext,
163163
) -> Task | None:
164164
"""Default handler for 'tasks/cancel'.
165165
@@ -231,7 +231,7 @@ async def _run_event_stream(
231231
async def _setup_message_execution(
232232
self,
233233
params: SendMessageRequest,
234-
context: ServerCallContext | None,
234+
context: ServerCallContext,
235235
) -> tuple[TaskManager, str, EventQueue, ResultAggregator, asyncio.Task]:
236236
"""Common setup logic for both streaming and non-streaming message handling.
237237
@@ -322,7 +322,7 @@ async def _send_push_notification_if_needed(
322322
async def on_message_send(
323323
self,
324324
params: SendMessageRequest,
325-
context: ServerCallContext | None = None,
325+
context: ServerCallContext,
326326
) -> Message | Task:
327327
"""Default handler for 'message/send' interface (non-streaming).
328328
@@ -388,7 +388,7 @@ async def push_notification_callback(event: Event) -> None:
388388
async def on_message_send_stream(
389389
self,
390390
params: SendMessageRequest,
391-
context: ServerCallContext | None = None,
391+
context: ServerCallContext,
392392
) -> AsyncGenerator[Event]:
393393
"""Default handler for 'message/stream' (streaming).
394394
@@ -476,7 +476,7 @@ async def _cleanup_producer(
476476
async def on_create_task_push_notification_config(
477477
self,
478478
params: CreateTaskPushNotificationConfigRequest,
479-
context: ServerCallContext | None = None,
479+
context: ServerCallContext,
480480
) -> TaskPushNotificationConfig:
481481
"""Default handler for 'tasks/pushNotificationConfig/create'.
482482
@@ -504,7 +504,7 @@ async def on_create_task_push_notification_config(
504504
async def on_get_task_push_notification_config(
505505
self,
506506
params: GetTaskPushNotificationConfigRequest,
507-
context: ServerCallContext | None = None,
507+
context: ServerCallContext,
508508
) -> TaskPushNotificationConfig:
509509
"""Default handler for 'tasks/pushNotificationConfig/get'.
510510
@@ -538,7 +538,7 @@ async def on_get_task_push_notification_config(
538538
async def on_subscribe_to_task(
539539
self,
540540
params: SubscribeToTaskRequest,
541-
context: ServerCallContext | None = None,
541+
context: ServerCallContext,
542542
) -> AsyncGenerator[Event, None]:
543543
"""Default handler for 'SubscribeToTask'.
544544
@@ -580,7 +580,7 @@ async def on_subscribe_to_task(
580580
async def on_list_task_push_notification_configs(
581581
self,
582582
params: ListTaskPushNotificationConfigsRequest,
583-
context: ServerCallContext | None = None,
583+
context: ServerCallContext,
584584
) -> ListTaskPushNotificationConfigsResponse:
585585
"""Default handler for 'ListTaskPushNotificationConfigs'.
586586
@@ -611,7 +611,7 @@ async def on_list_task_push_notification_configs(
611611
async def on_delete_task_push_notification_config(
612612
self,
613613
params: DeleteTaskPushNotificationConfigRequest,
614-
context: ServerCallContext | None = None,
614+
context: ServerCallContext,
615615
) -> None:
616616
"""Default handler for 'tasks/pushNotificationConfig/delete'.
617617

src/a2a/server/request_handlers/jsonrpc_handler.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def _get_request_id(
141141
async def on_message_send(
142142
self,
143143
request: SendMessageRequest,
144-
context: ServerCallContext | None = None,
144+
context: ServerCallContext,
145145
) -> dict[str, Any]:
146146
"""Handles the 'message/send' JSON-RPC method.
147147
@@ -174,7 +174,7 @@ async def on_message_send(
174174
async def on_message_send_stream(
175175
self,
176176
request: SendMessageRequest,
177-
context: ServerCallContext | None = None,
177+
context: ServerCallContext,
178178
) -> AsyncIterable[dict[str, Any]]:
179179
"""Handles the 'message/stream' JSON-RPC method.
180180
@@ -208,7 +208,7 @@ async def on_message_send_stream(
208208
async def on_cancel_task(
209209
self,
210210
request: CancelTaskRequest,
211-
context: ServerCallContext | None = None,
211+
context: ServerCallContext,
212212
) -> dict[str, Any]:
213213
"""Handles the 'tasks/cancel' JSON-RPC method.
214214
@@ -234,7 +234,7 @@ async def on_cancel_task(
234234
async def on_subscribe_to_task(
235235
self,
236236
request: SubscribeToTaskRequest,
237-
context: ServerCallContext | None = None,
237+
context: ServerCallContext,
238238
) -> AsyncIterable[dict[str, Any]]:
239239
"""Handles the 'SubscribeToTask' JSON-RPC method.
240240
@@ -268,7 +268,7 @@ async def on_subscribe_to_task(
268268
async def get_push_notification_config(
269269
self,
270270
request: GetTaskPushNotificationConfigRequest,
271-
context: ServerCallContext | None = None,
271+
context: ServerCallContext,
272272
) -> dict[str, Any]:
273273
"""Handles the 'tasks/pushNotificationConfig/get' JSON-RPC method.
274274
@@ -298,7 +298,7 @@ async def get_push_notification_config(
298298
async def set_push_notification_config(
299299
self,
300300
request: CreateTaskPushNotificationConfigRequest,
301-
context: ServerCallContext | None = None,
301+
context: ServerCallContext,
302302
) -> dict[str, Any]:
303303
"""Handles the 'tasks/pushNotificationConfig/set' JSON-RPC method.
304304
@@ -331,7 +331,7 @@ async def set_push_notification_config(
331331
async def on_get_task(
332332
self,
333333
request: GetTaskRequest,
334-
context: ServerCallContext | None = None,
334+
context: ServerCallContext,
335335
) -> dict[str, Any]:
336336
"""Handles the 'tasks/get' JSON-RPC method.
337337
@@ -357,7 +357,7 @@ async def on_get_task(
357357
async def list_tasks(
358358
self,
359359
request: ListTasksRequest,
360-
context: ServerCallContext | None = None,
360+
context: ServerCallContext,
361361
) -> dict[str, Any]:
362362
"""Handles the 'tasks/list' JSON-RPC method.
363363
@@ -381,7 +381,7 @@ async def list_tasks(
381381
async def list_push_notification_configs(
382382
self,
383383
request: ListTaskPushNotificationConfigsRequest,
384-
context: ServerCallContext | None = None,
384+
context: ServerCallContext,
385385
) -> dict[str, Any]:
386386
"""Handles the 'ListTaskPushNotificationConfigs' JSON-RPC method.
387387
@@ -406,7 +406,7 @@ async def list_push_notification_configs(
406406
async def delete_push_notification_config(
407407
self,
408408
request: DeleteTaskPushNotificationConfigRequest,
409-
context: ServerCallContext | None = None,
409+
context: ServerCallContext,
410410
) -> dict[str, Any]:
411411
"""Handles the 'tasks/pushNotificationConfig/delete' JSON-RPC method.
412412
@@ -429,7 +429,7 @@ async def delete_push_notification_config(
429429
async def get_authenticated_extended_card(
430430
self,
431431
request: GetExtendedAgentCardRequest,
432-
context: ServerCallContext | None = None,
432+
context: ServerCallContext,
433433
) -> dict[str, Any]:
434434
"""Handles the 'agent/authenticatedExtendedCard' JSON-RPC method.
435435

src/a2a/server/request_handlers/request_handler.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class RequestHandler(ABC):
3333
async def on_get_task(
3434
self,
3535
params: GetTaskRequest,
36-
context: ServerCallContext | None = None,
36+
context: ServerCallContext,
3737
) -> Task | None:
3838
"""Handles the 'tasks/get' method.
3939
@@ -49,7 +49,7 @@ async def on_get_task(
4949

5050
@abstractmethod
5151
async def on_list_tasks(
52-
self, params: ListTasksRequest, context: ServerCallContext | None = None
52+
self, params: ListTasksRequest, context: ServerCallContext
5353
) -> ListTasksResponse:
5454
"""Handles the tasks/list method.
5555
@@ -68,7 +68,7 @@ async def on_list_tasks(
6868
async def on_cancel_task(
6969
self,
7070
params: CancelTaskRequest,
71-
context: ServerCallContext | None = None,
71+
context: ServerCallContext,
7272
) -> Task | None:
7373
"""Handles the 'tasks/cancel' method.
7474
@@ -86,7 +86,7 @@ async def on_cancel_task(
8686
async def on_message_send(
8787
self,
8888
params: SendMessageRequest,
89-
context: ServerCallContext | None = None,
89+
context: ServerCallContext,
9090
) -> Task | Message:
9191
"""Handles the 'message/send' method (non-streaming).
9292
@@ -105,7 +105,7 @@ async def on_message_send(
105105
async def on_message_send_stream(
106106
self,
107107
params: SendMessageRequest,
108-
context: ServerCallContext | None = None,
108+
context: ServerCallContext,
109109
) -> AsyncGenerator[Event]:
110110
"""Handles the 'message/stream' method (streaming).
111111
@@ -129,7 +129,7 @@ async def on_message_send_stream(
129129
async def on_create_task_push_notification_config(
130130
self,
131131
params: CreateTaskPushNotificationConfigRequest,
132-
context: ServerCallContext | None = None,
132+
context: ServerCallContext,
133133
) -> TaskPushNotificationConfig:
134134
"""Handles the 'tasks/pushNotificationConfig/create' method.
135135
@@ -147,7 +147,7 @@ async def on_create_task_push_notification_config(
147147
async def on_get_task_push_notification_config(
148148
self,
149149
params: GetTaskPushNotificationConfigRequest,
150-
context: ServerCallContext | None = None,
150+
context: ServerCallContext,
151151
) -> TaskPushNotificationConfig:
152152
"""Handles the 'tasks/pushNotificationConfig/get' method.
153153
@@ -165,7 +165,7 @@ async def on_get_task_push_notification_config(
165165
async def on_subscribe_to_task(
166166
self,
167167
params: SubscribeToTaskRequest,
168-
context: ServerCallContext | None = None,
168+
context: ServerCallContext,
169169
) -> AsyncGenerator[Event]:
170170
"""Handles the 'SubscribeToTask' method.
171171
@@ -188,7 +188,7 @@ async def on_subscribe_to_task(
188188
async def on_list_task_push_notification_configs(
189189
self,
190190
params: ListTaskPushNotificationConfigsRequest,
191-
context: ServerCallContext | None = None,
191+
context: ServerCallContext,
192192
) -> ListTaskPushNotificationConfigsResponse:
193193
"""Handles the 'ListTaskPushNotificationConfigs' method.
194194
@@ -206,7 +206,7 @@ async def on_list_task_push_notification_configs(
206206
async def on_delete_task_push_notification_config(
207207
self,
208208
params: DeleteTaskPushNotificationConfigRequest,
209-
context: ServerCallContext | None = None,
209+
context: ServerCallContext,
210210
) -> None:
211211
"""Handles the 'tasks/pushNotificationConfig/delete' method.
212212

0 commit comments

Comments
 (0)