|
6 | 6 | from typing import Any, Generic, TypeVar |
7 | 7 |
|
8 | 8 | import anyio |
9 | | -import anyio.lowlevel |
10 | 9 | import httpx |
11 | 10 | from anyio.streams.memory import MemoryObjectReceiveStream, MemoryObjectSendStream |
12 | 11 | from pydantic import BaseModel |
|
24 | 23 | JSONRPCNotification, |
25 | 24 | JSONRPCRequest, |
26 | 25 | JSONRPCResponse, |
| 26 | + NotificationParams, |
27 | 27 | RequestParams, |
28 | 28 | ServerNotification, |
29 | 29 | ServerRequest, |
@@ -276,8 +276,23 @@ async def send_notification( |
276 | 276 | Emits a notification, which is a one-way message that does not expect |
277 | 277 | a response. |
278 | 278 | """ |
| 279 | + # Some transport implementations may need to set the related_request_id |
| 280 | + # to attribute to the notifications to the request that triggered |
| 281 | + # them. |
| 282 | + # Update notification meta with related request ID if provided |
279 | 283 | if related_request_id is not None and notification.root.params is not None: |
280 | | - notification.root.params.related_request_id = related_request_id |
| 284 | + # Create meta if it doesn't exist |
| 285 | + if notification.root.params.meta is None: |
| 286 | + # Create meta dict with related_request_id |
| 287 | + meta_dict = {"related_request_id": related_request_id} |
| 288 | + |
| 289 | + else: |
| 290 | + # Update existing meta with model_validate to properly handle extra fields |
| 291 | + meta_dict = notification.root.params.meta.model_dump( |
| 292 | + by_alias=True, mode="json", exclude_none=True |
| 293 | + ) |
| 294 | + meta_dict["related_request_id"] = related_request_id |
| 295 | + notification.root.params.meta = NotificationParams.Meta(**meta_dict) |
281 | 296 | jsonrpc_notification = JSONRPCNotification( |
282 | 297 | jsonrpc="2.0", |
283 | 298 | **notification.model_dump(by_alias=True, mode="json", exclude_none=True), |
|
0 commit comments