Skip to content
Back to log

MCP transport layers: stdio and Streamable HTTP

Chainabit's Chao · · 7 min read

In short

In MCP, stdio provides interprocess communication; Streamable HTTP uses POST, GET, and optional SSE. Rules for messages, sessions, and connections.

Model Context Protocol defines stdio and Streamable HTTP transport mechanisms for communication between client and server. MCP messages are encoded using JSON-RPC and use UTF-8. Both transports carry messages between client and server; the connection is established and messages are delivered in different ways.

In the MCP architecture, a host manages one or more clients. Each client maintains a separate connection to its corresponding server. According to the Model Context Protocol architecture description, a local server using stdio typically serves a single client, while a remote server using Streamable HTTP typically serves multiple clients.

Interprocess communication with stdio

According to the Model Context Protocol transport rules, when stdio is used, the client launches the MCP server as a subprocess. The server reads JSON-RPC messages from standard input and sends its messages through standard output. Each message is a single JSON-RPC request, notification, or response.

Messages are separated by newlines. A message must not contain embedded newlines. The server must not write anything to standard output that is not a valid MCP message. The client must not send anything to the server's standard input that is not a valid MCP message.

The server may write UTF-8 text to standard error for logging. The client may capture these logs, forward them elsewhere, or ignore them. Model Context Protocol states that clients should support stdio whenever possible.

Sending messages with Streamable HTTP

A server using Streamable HTTP runs as an independent process and can handle multiple client connections. Communication uses HTTP POST and GET. The server may optionally use Server-Sent Events, or SSE, to stream multiple server messages.

According to the Model Context Protocol transport rules, the server must provide a single MCP endpoint path that supports both POST and GET methods. A separate HTTP POST request must be made to this endpoint for each JSON-RPC message the client sends to the server.

The client must list both application/json and text/event-stream content types in the Accept header of the POST request. The request body must contain a single JSON-RPC request, notification, or response. The server's HTTP response varies according to the type of JSON-RPC message it receives.

If the incoming message is a JSON-RPC response or notification and the server accepts it, the server must return an HTTP 202 Accepted response with no body. If the server cannot accept the message, it must return an HTTP error status code. The body of this error response may contain a JSON-RPC error response without an id.

If the incoming message is a JSON-RPC request, the server must use one of two response types. It may use Content-Type: application/json to return a single JSON object. It may use Content-Type: text/event-stream to start an SSE stream. The client must support both cases.

SSE stream associated with a POST request

If the server starts an SSE stream in response to a POST request, the stream must end with a response to the JSON-RPC request in the POST body. The server may also send JSON-RPC request and notification messages before the response. These messages must relate to the client request that initiated the stream.

The Model Context Protocol transport rules state that the server should not close the SSE stream before sending the JSON-RPC response, except when the session expires. The server should close the stream after sending the response.

The connection can be interrupted at any time for reasons such as network conditions. According to the transport rules, this interruption must not be interpreted as the client canceling its request. The client must explicitly send an MCP CancelledNotification to cancel the request. The server may support resuming the stream to prevent message loss when the connection is interrupted.

Listening for server messages with GET

The client may open an SSE stream by sending an HTTP GET to the MCP endpoint. This stream allows the server to send messages to the client without the client first sending data through HTTP POST. The Accept header of the GET request must list text/event-stream as a supported content type.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

The server must respond to this GET request with Content-Type: text/event-stream or return HTTP 405 Method Not Allowed. The latter response indicates that the server does not offer an SSE stream at that endpoint.

On a stream opened with GET, the server may send JSON-RPC requests and notifications. These messages must be unrelated to the client's concurrently running JSON-RPC requests. The server must not send JSON-RPC responses on this stream, except when resuming a stream associated with a previous client request. Both the server and the client may close the SSE stream at any time.

The client may remain connected to multiple SSE streams at the same time. The server must send each JSON-RPC message on only one of the connected streams. It must not broadcast the same message to multiple streams. Stream resumption may be used to reduce the risk of message loss.

Resuming an interrupted stream

Model Context Protocol allows the server to add an id field to SSE event messages. If this field is present, the ID must be globally unique across all streams within the session. If session management is not used, it must be unique across all streams belonging to the same client.

When the client wants to resume the stream after a connection interruption, it must send an HTTP GET to the MCP endpoint. The Last-Event-ID header in the request specifies the last event ID received by the client. The server may use this header to replay messages to be sent after the last ID on the interrupted stream and resume the stream from that point.

The server must not replay messages intended for delivery on a different stream during this process. The transport rules treat event ID values as position markers within the relevant stream.

Streamable HTTP sessions

Model Context Protocol defines a session as a set of logically related interactions between a client and server that begins during initialization. A server using Streamable HTTP may assign a session ID during initialization. It does this in the Mcp-Session-Id header of the HTTP response containing the InitializeResult.

According to the transport rules, the session ID must be globally unique and cryptographically secure. The value must contain only visible ASCII characters. If the server returns Mcp-Session-Id during initialization, the client must send this value in the header of the same name in all subsequent HTTP requests.

A server that requires a session ID must return HTTP 400 Bad Request if the Mcp-Session-Id header is missing from requests other than initialization. The server may terminate the session at any time. Afterward, it must respond with HTTP 404 Not Found to requests containing that session ID.

If the client receives an HTTP 404 response to a request containing Mcp-Session-Id, it must start a new session by sending an InitializeRequest without a session ID. To explicitly terminate a session it no longer needs, the client must send an HTTP DELETE to the MCP endpoint with the Mcp-Session-Id header. The server may return HTTP 405 Method Not Allowed in response to indicate that it does not allow clients to terminate sessions.

Security and limits

The Model Context Protocol transport rules require servers implementing Streamable HTTP to validate the Origin header on all incoming connections. Locally running servers should bind only to localhost instead of all network interfaces and must implement appropriate authentication for all connections. The standard states that without these protections, attackers could interact with local MCP servers from remote websites through DNS rebinding.

A client using HTTP must send the MCP-Protocol-Version header in its requests after initialization. The value sent must be the value negotiated during initialization. The server must respond with HTTP 400 Bad Request to a request containing an invalid or unsupported MCP-Protocol-Version.

Model Context Protocol also allows custom transport mechanisms beyond stdio and Streamable HTTP. The protocol can be implemented over communication channels that support bidirectional message exchange. Applications using custom transports must preserve MCP's JSON-RPC message format and lifecycle requirements.

Ready to start?

Download the app and continue from your phone.

End of Entry