Skip to content

Commit 80780dc

Browse files
committed
use metadata from SessionMessage to propagate related_request_id
1 parent 8650c77 commit 80780dc

File tree

3 files changed

+15
-23
lines changed

3 files changed

+15
-23
lines changed

src/mcp/server/streamable_http.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from starlette.responses import Response
2525
from starlette.types import Receive, Scope, Send
2626

27-
from mcp.shared.message import SessionMessage
27+
from mcp.shared.message import ServerMessageMetadata, SessionMessage
2828
from mcp.types import (
2929
INTERNAL_ERROR,
3030
INVALID_PARAMS,
@@ -836,12 +836,17 @@ async def message_router():
836836
):
837837
# Extract related_request_id from meta if it exists
838838
if (
839-
(params := getattr(message.root, "params", None))
840-
and (meta := params.get("_meta"))
841-
and (related_id := meta.get("related_request_id"))
839+
session_message.metadata is not None
840+
and isinstance(
841+
session_message.metadata,
842+
ServerMessageMetadata,
843+
)
844+
and session_message.metadata.related_request_id
842845
is not None
843846
):
844-
target_request_id = str(related_id)
847+
target_request_id = str(
848+
session_message.metadata.related_request_id
849+
)
845850
else:
846851
target_request_id = str(message.root.id)
847852

src/mcp/shared/session.py

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typing_extensions import Self
1313

1414
from mcp.shared.exceptions import McpError
15-
from mcp.shared.message import SessionMessage
15+
from mcp.shared.message import ServerMessageMetadata, SessionMessage
1616
from mcp.types import (
1717
CancelledNotification,
1818
ClientNotification,
@@ -24,7 +24,6 @@
2424
JSONRPCNotification,
2525
JSONRPCRequest,
2626
JSONRPCResponse,
27-
NotificationParams,
2827
RequestParams,
2928
ServerNotification,
3029
ServerRequest,
@@ -280,22 +279,14 @@ async def send_notification(
280279
"""
281280
# Some transport implementations may need to set the related_request_id
282281
# to attribute to the notifications to the request that triggered them.
283-
if related_request_id is not None and notification.root.params is not None:
284-
# Create meta if it doesn't exist
285-
if notification.root.params.meta is None:
286-
meta_dict = {"related_request_id": related_request_id}
287-
288-
else:
289-
meta_dict = notification.root.params.meta.model_dump(
290-
by_alias=True, mode="json", exclude_none=True
291-
)
292-
meta_dict["related_request_id"] = related_request_id
293-
notification.root.params.meta = NotificationParams.Meta(**meta_dict)
294282
jsonrpc_notification = JSONRPCNotification(
295283
jsonrpc="2.0",
296284
**notification.model_dump(by_alias=True, mode="json", exclude_none=True),
297285
)
298-
session_message = SessionMessage(message=JSONRPCMessage(jsonrpc_notification))
286+
session_message = SessionMessage(
287+
message=JSONRPCMessage(jsonrpc_notification),
288+
metadata=ServerMessageMetadata(related_request_id=related_request_id),
289+
)
299290
await self._write_stream.send(session_message)
300291

301292
async def _send_response(

tests/client/test_logging_callback.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from mcp.shared.session import RequestResponder
1010
from mcp.types import (
1111
LoggingMessageNotificationParams,
12-
NotificationParams,
1312
TextContent,
1413
)
1514

@@ -80,10 +79,7 @@ async def message_handler(
8079
assert log_result.isError is False
8180
assert len(logging_collector.log_messages) == 1
8281
# Create meta object with related_request_id added dynamically
83-
meta = NotificationParams.Meta()
84-
setattr(meta, "related_request_id", "2")
8582
log = logging_collector.log_messages[0]
8683
assert log.level == "info"
8784
assert log.logger == "test_logger"
8885
assert log.data == "Test log message"
89-
assert log.meta == meta

0 commit comments

Comments
 (0)