Sub topic 5
Moving messages: stdio and Streamable HTTP
This chapter assumes you have read the earlier chapters on JSON-RPC messages and the initialization handshake. You should also know basic command-line use and how HTTP requests and responses work.
What a transport does
A transport carries JSON-RPC messages between an MCP client and an MCP server. It does not change the messages. The same initialize request or tools/call request looks identical on every transport. Only the delivery mechanism differs.
The MCP specification defines two standard transports: stdio and Streamable HTTP. Pick one based on where the server runs.
stdio: a local subprocess
With stdio, the host starts the server as a child process. The client writes messages to the server's standard input (stdin). The server writes messages to its standard output (stdout).
The rules are simple:
- Each message is one line of JSON. Messages are separated by newlines and must not contain embedded newlines.
- The server must write only valid MCP messages to stdout.
- The server may write logs to stderr. The client may capture or ignore them.
- The client closes stdin to end the session, then stops the process if needed.
The most common stdio mistake is a stray print() in server code. It writes text to stdout, and the client then fails to parse it as JSON. Log to stderr instead:
import sys
# Wrong in a stdio server: this corrupts the message stream.
# print("starting up")
# Right: stderr is safe for diagnostics.
print("starting up", file=sys.stderr)stdio needs no network, no port, and no authentication step. The server runs with the same operating system permissions as the host. That makes it a good fit for local tools such as file access.
Streamable HTTP: a remote endpoint
With Streamable HTTP, the server runs as an independent process and exposes a single HTTP endpoint, for example https://example.com/mcp. Many clients can connect to it.
- The client sends every JSON-RPC message as an HTTP
POSTto the endpoint. - If the POST carries a request, the server answers with either a single JSON response (
Content-Type: application/json) or an SSE stream (Content-Type: text/event-stream). SSE means Server-Sent Events, a way to push several messages over one HTTP response. Streaming lets the server send progress notifications before the final result. - If the POST carries only a notification or a response, the server returns
202 Acceptedwith no body. - The client may also open an HTTP
GETto the same endpoint to receive server-initiated messages.
A server may assign a session by returning an Mcp-Session-Id header on the initialize response. The client then sends that header on every later request. The client also sends an MCP-Protocol-Version header with the negotiated version.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
if __name__ == "__main__":
mcp.run(transport="streamable-http")Only the last line differs from a stdio server. With the Python SDK, the default endpoint is http://127.0.0.1:8000/mcp.
Choosing a transport
| Factor | stdio | Streamable HTTP |
|---|---|---|
| Where the server runs | Same machine, child process | Anywhere reachable by URL |
| Clients per server | One | Many |
| Authentication | Inherits the host user's permissions | HTTP-based, such as OAuth |
| Setup | A command and arguments | A deployed web service |
Choose stdio for local, single-user tools. Choose Streamable HTTP when the server is shared, hosted, or must be updated independently of the clients.
Exercise
A teammate's stdio server makes the client fail with a JSON parse error right after startup. The server code contains print("Server ready"). Why does this fail, and how do you fix it?
Show answer
In a stdio server, stdout is the channel for JSON-RPC messages. The plain text "Server ready" is not valid JSON, so the client cannot parse it. Send diagnostics to stderr instead, for example print("Server ready", file=sys.stderr), or use the logging facilities of the SDK.
Exercise
A company wants one MCP server that 200 employees' hosts all use, and that can be updated without touching their machines. Which transport fits, and why?
Show answer
Streamable HTTP. The server runs as one deployed service at a URL, accepts many clients, supports HTTP authentication, and can be updated centrally. stdio would start a separate subprocess on each machine.