Skip to content

use metadata from SessionMessage to propagate related_request_id #591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 63 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
2b95598
initial streamable http server
ihrpr Apr 20, 2025
3d790f8
add request validation and tests
ihrpr Apr 20, 2025
27bc01e
session management
ihrpr Apr 20, 2025
3c4cf10
terminations of a session
ihrpr Apr 20, 2025
bce74b3
fix cleaning up
ihrpr Apr 20, 2025
2011579
add happy path test
ihrpr Apr 20, 2025
2cebf08
tests
ihrpr Apr 20, 2025
6c9c320
json mode
ihrpr Apr 20, 2025
ede8cde
clean up
ihrpr Apr 21, 2025
2a3bed8
fix example server
ihrpr Apr 21, 2025
0456b1b
return 405 for get stream
ihrpr Apr 21, 2025
97ca48d
speed up tests
ihrpr Apr 21, 2025
f738cbf
stateless implemetation
ihrpr Apr 21, 2025
92d4287
format
ihrpr Apr 21, 2025
aa9f6e5
uv lock
ihrpr Apr 21, 2025
2fba7f3
Merge branch 'ihrpr/streamablehttp-server' into ihrpr/streamablehttp-…
ihrpr Apr 21, 2025
45723ea
simplify readme
ihrpr Apr 21, 2025
6b7a616
clean up
ihrpr Apr 21, 2025
b1be691
get sse
ihrpr Apr 22, 2025
201ec99
uv lock
ihrpr Apr 22, 2025
46ec72d
clean up
ihrpr Apr 22, 2025
1902abb
Merge branch 'ihrpr/streamablehttp-server' into ihrpr/streamablehttp-…
ihrpr Apr 22, 2025
da1df74
Merge branch 'ihrpr/streamablehttp-stateless' into ihrpr/get-sse
ihrpr Apr 22, 2025
c2be5af
streamable http client
ihrpr Apr 23, 2025
9b096dc
add comments to server example where we use related_request_id
ihrpr Apr 23, 2025
bbe79c2
Merge branch 'main' into ihrpr/streamablehttp-server
ihrpr Apr 23, 2025
a0a9c5b
small fixes
ihrpr Apr 23, 2025
a5ac2e0
use mta field for related_request_id
ihrpr Apr 23, 2025
2e615f3
unrelated test and format
ihrpr Apr 23, 2025
110526d
clean up
ihrpr Apr 23, 2025
7ffd5ba
terminate session
ihrpr Apr 23, 2025
029ec56
format
ihrpr Apr 23, 2025
cae32e2
Merge branch 'ihrpr/streamablehttp-server' into ihrpr/streamablehttp-…
ihrpr Apr 25, 2025
58745c7
remove useless sleep
ihrpr Apr 25, 2025
1387929
rename require_initialization to standalone_mode
ihrpr Apr 25, 2025
bccff75
Merge branch 'ihrpr/streamablehttp-stateless' into ihrpr/get-sse
ihrpr Apr 25, 2025
dd007d7
Merge branch 'ihrpr/get-sse' into ihrpr/client
ihrpr Apr 25, 2025
6482120
remove redundant check for initialize and session
ihrpr Apr 25, 2025
9a6da2e
ruff check
ihrpr Apr 25, 2025
b957fad
Merge branch 'ihrpr/get-sse' into ihrpr/client
ihrpr Apr 25, 2025
3f5fd7e
support for resumability - server
ihrpr Apr 25, 2025
b193242
format
ihrpr Apr 25, 2025
6110435
remove print
ihrpr Apr 25, 2025
e087283
rename files to follow python naming
ihrpr Apr 25, 2025
08247c4
update to use time delta in client
ihrpr Apr 25, 2025
0484dfb
refactor
ihrpr Apr 25, 2025
88ff2ba
Merge branch 'ihrpr/client' into ihrpr/resumability-server
ihrpr Apr 25, 2025
5757f6c
small fixes
ihrpr Apr 25, 2025
ee28ad8
improve event store example implementation
ihrpr Apr 25, 2025
5dbddeb
refactor _create_event_data
ihrpr Apr 25, 2025
8650c77
add session message
ihrpr Apr 27, 2025
80780dc
use metadata from SessionMessage to propagate related_request_id
ihrpr Apr 27, 2025
901dc98
assign server message only when related_request_id is not none
ihrpr Apr 28, 2025
ff70bd6
Merge branch 'main' into ihrpr/streamablehttp-server
ihrpr May 2, 2025
179fbc8
Merge branch 'ihrpr/streamablehttp-server' into ihrpr/streamablehttp-…
ihrpr May 2, 2025
a979864
Merge branch 'ihrpr/streamablehttp-stateless' into ihrpr/get-sse
ihrpr May 2, 2025
11b7dd9
Merge branch 'ihrpr/get-sse' into ihrpr/client
ihrpr May 2, 2025
67a899c
Merge branch 'ihrpr/client' into ihrpr/resumability-server
ihrpr May 2, 2025
c3e0ff3
Merge branch 'ihrpr/resumability-server' into ihrpr/memory-stream-ite…
ihrpr May 2, 2025
83503a0
Merge branch 'ihrpr/memory-stream-item-type' into ihrpr/use-session-m…
ihrpr May 2, 2025
e06e3a5
fixes after merge
ihrpr May 2, 2025
f6cea03
Merge branch 'ihrpr/memory-stream-item-type' into ihrpr/use-session-m…
ihrpr May 2, 2025
e95b00f
Merge branch 'main' into ihrpr/use-session-message-for-related-request
ihrpr May 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/mcp/server/streamable_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from starlette.responses import Response
from starlette.types import Receive, Scope, Send

from mcp.shared.message import SessionMessage
from mcp.shared.message import ServerMessageMetadata, SessionMessage
from mcp.types import (
INTERNAL_ERROR,
INVALID_PARAMS,
Expand Down Expand Up @@ -520,7 +520,7 @@ async def sse_writer():
)
await response(scope, receive, send)
if writer:
await writer.send(err)
await writer.send(Exception(err))
return

async def _handle_get_request(self, request: Request, send: Send) -> None:
Expand Down Expand Up @@ -834,12 +834,17 @@ async def message_router():
):
# Extract related_request_id from meta if it exists
if (
(params := getattr(message.root, "params", None))
and (meta := params.get("_meta"))
and (related_id := meta.get("related_request_id"))
session_message.metadata is not None
and isinstance(
session_message.metadata,
ServerMessageMetadata,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might they be passing their own object (say with added fields) that still has a related_request_id? Not sure the SDK as currently implemented would be capable of handling that (without hacking anyway), but flagging in case we want to relax this to a hasattr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's actually the reason why typed metadata was introduced, before I was using meta field in the message, which was quite bad

)
and session_message.metadata.related_request_id
is not None
):
target_request_id = str(related_id)
target_request_id = str(
session_message.metadata.related_request_id
)
else:
target_request_id = str(message.root.id)

Expand Down
21 changes: 7 additions & 14 deletions src/mcp/shared/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from typing_extensions import Self

from mcp.shared.exceptions import McpError
from mcp.shared.message import SessionMessage
from mcp.shared.message import ServerMessageMetadata, SessionMessage
from mcp.types import (
CancelledNotification,
ClientNotification,
Expand All @@ -24,7 +24,6 @@
JSONRPCNotification,
JSONRPCRequest,
JSONRPCResponse,
NotificationParams,
RequestParams,
ServerNotification,
ServerRequest,
Expand Down Expand Up @@ -288,22 +287,16 @@ async def send_notification(
"""
# Some transport implementations may need to set the related_request_id
# to attribute to the notifications to the request that triggered them.
if related_request_id is not None and notification.root.params is not None:
# Create meta if it doesn't exist
if notification.root.params.meta is None:
meta_dict = {"related_request_id": related_request_id}

else:
meta_dict = notification.root.params.meta.model_dump(
by_alias=True, mode="json", exclude_none=True
)
meta_dict["related_request_id"] = related_request_id
notification.root.params.meta = NotificationParams.Meta(**meta_dict)
jsonrpc_notification = JSONRPCNotification(
jsonrpc="2.0",
**notification.model_dump(by_alias=True, mode="json", exclude_none=True),
)
session_message = SessionMessage(message=JSONRPCMessage(jsonrpc_notification))
session_message = SessionMessage(
message=JSONRPCMessage(jsonrpc_notification),
metadata=ServerMessageMetadata(related_request_id=related_request_id)
if related_request_id
else None,
)
await self._write_stream.send(session_message)

async def _send_response(
Expand Down
4 changes: 0 additions & 4 deletions tests/client/test_logging_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from mcp.shared.session import RequestResponder
from mcp.types import (
LoggingMessageNotificationParams,
NotificationParams,
TextContent,
)

Expand Down Expand Up @@ -80,10 +79,7 @@ async def message_handler(
assert log_result.isError is False
assert len(logging_collector.log_messages) == 1
# Create meta object with related_request_id added dynamically
meta = NotificationParams.Meta()
setattr(meta, "related_request_id", "2")
log = logging_collector.log_messages[0]
assert log.level == "info"
assert log.logger == "test_logger"
assert log.data == "Test log message"
assert log.meta == meta
Loading