Skip to content

Streamable Http transport handle ping message #1146

@Gujiawei-Edinburgh

Description

@Gujiawei-Edinburgh

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions