-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Open
Description
Initial Checks
- I confirm that I'm using the latest version of MCP Python SDK
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Description
code in src/mcp/client/streamable_http.py:162
validates the payload from incoming SSE messages.
However, on the server side, EventSourceResponse
is used, which periodically sends ping
message. These ping
messages will cause client side parsing message error.
When SSE is used to stream logs, these ping messages trigger unwanted behavior, such as flushing logs unnecessarily. Currently, there doesn’t appear to be a configuration option to disable the ping messages or adjust their interval.
As a potential improvement, the client could check if the incoming message has an empty data field before validation. If the data
is empty, the client can treat it as a ping message and safely ignore it.
Example Code
async def _handle_sse_event(
self,
sse: ServerSentEvent,
read_stream_writer: StreamWriter,
original_request_id: RequestId | None = None,
resumption_callback: Callable[[str], Awaitable[None]] | None = None,
is_initialization: bool = False,
) -> bool:
"""Handle an SSE event, returning True if the response is complete."""
if sse.event == "message":
try:
# potential changes here
if not sse.data:
logger.debug("Receiving a ping message")
# double check if directly return True is compatible or enough
return True
message = JSONRPCMessage.model_validate_json(sse.data)
logger.debug(f"SSE message: {message}")
Python & MCP Python SDK
python version: 3.13
MCP SDK version: 1.11.0
Metadata
Metadata
Assignees
Labels
No labels