Initial Checks
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
Initial Checks
Description
code in
src/mcp/client/streamable_http.py:162validates the payload from incoming SSE messages.However, on the server side,
EventSourceResponseis used, which periodically sendspingmessage. Thesepingmessages 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
datais empty, the client can treat it as a ping message and safely ignore it.Example Code
Python & MCP Python SDK