Sub topic 5
Transports: stdio versus Streamable HTTP
This chapter assumes you know the host, client and server roles, the JSON-RPC 2.0 message shapes (requests, responses, notifications), and the three server primitives from earlier chapters. It also assumes you can run a Python script and read HTTP headers. Here you will learn how those JSON-RPC messages travel between client and server, and how to choose between the two standard transports.
What a transport does
A transport is the channel that carries JSON-RPC messages. It does not change what the messages mean. A tools/call request has the same shape whether it travels through a pipe or over HTTP. The transport decides three things:
- How the server process is started and who owns its lifetime.
- How bytes are framed, so each side knows where one message ends.
- How the connection is identified, secured and ended.
MCP defines two standard transports, stdio and Streamable HTTP. The specification also allows custom transports, as long as they keep the JSON-RPC format and the lifecycle. Older tutorials describe a third, "HTTP+SSE" transport. Streamable HTTP replaced it in the 2025-03-26 revision of the spec. You may still see the old form in existing servers and in config files that say sse.
stdio: a subprocess and two pipes
With stdio, the client launches the server as a child process. It writes messages to the server's standard input and reads messages from its standard output. This is why the command and args fields exist in an mcpServers entry: the assistant runs that command for you.
The rules are short:
- Each message is one line of JSON, ended by a newline. A message must not contain a raw newline inside it. JSON string escapes such as
\nare fine. - The server writes only valid MCP messages to stdout.
- The server may write logs to stderr. Clients may capture or show stderr, but they must not treat it as protocol traffic.
- When the client wants to stop, it closes the server's stdin, waits, and then terminates the process if needed.
Rule 2 causes the most common stdio bug. A stray print("starting up") writes to stdout, the client reads it as a malformed message, and the connection fails or hangs. The fix is to send diagnostics to stderr:
import sys
print("server starting", file=sys.stderr)Because the client owns the process, stdio needs no network port, no authentication and no session handling. The server runs with the same operating system permissions as the assistant, and the connection exists only while that process lives. One client talks to one server instance. If you open the same project in two assistant windows, you get two server processes.
You can see the framing by hand. This sends one initialize request to the calculator server from the earlier chapter, which you saved as server.py, and prints the reply:
printf '%s\n' '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"hand","version":"0"}}}' | python server.pyThe server answers with one line of JSON containing its capabilities and then exits, because stdin closed. A real client keeps stdin open and sends a notifications/initialized message next. The lifecycle chapter covers that step.
Streamable HTTP: one endpoint, two methods
With Streamable HTTP, the server is an independent process that listens on a URL, for example http://127.0.0.1:8000/mcp. The client does not launch it. The server exposes a single path, the MCP endpoint, that accepts both POST and GET.
Client to server: POST
Every JSON-RPC message the client sends is a separate HTTP POST to the endpoint. The client must send an Accept header that lists both application/json and text/event-stream. The server's answer depends on what the body contained:
| Body of the POST | Server response |
|---|---|
| A notification or a response from the client | `202 Accepted` with no body |
| A request | Either `Content-Type: application/json` with one JSON-RPC response, or `Content-Type: text/event-stream` with an SSE stream that ends with the response |
SSE (Server-Sent Events) is a plain HTTP response that stays open and delivers a series of text events. Streaming is useful when a slow tool call wants to send progress notifications or a server-initiated request before the final result. For a quick tool such as add, a server will usually reply with plain JSON.
Server to client: GET
A client may also send a GET to the same endpoint to open a long-lived SSE stream. The server can use it to send notifications, such as a changed tool list, even when no request is in progress. A server that has nothing to push may answer 405 Method Not Allowed, and the client must accept that.
Sessions
A stdio connection is identified by the process itself. HTTP has no such anchor, so Streamable HTTP adds an optional session mechanism:
- The server may return an
Mcp-Session-Idheader on the response toinitialize. - The client must then include that header on every later request.
- If the server forgets the session, it replies
404, and the client must start over with a newinitialize. - A client that is finished can send
DELETEwith the session header to end the session. - Later revisions of the spec also have clients send an
MCP-Protocol-Versionheader on each request after initialization.
Sessions are optional. A server that keeps no per-client state can be stateless and skip them, which makes it easier to run behind a load balancer.
Trying it with curl
You can run the same Python server over HTTP by changing one line. FastMCP in the official Python SDK listens on 127.0.0.1:8000 and serves the endpoint at /mcp:
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("calculator")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two integers."""
return a + b
if __name__ == "__main__":
mcp.run(transport="streamable-http")Start it, and in another terminal send the first message:
curl -i -X POST http://127.0.0.1:8000/mcp \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"curl","version":"0"}}}'Look at the response headers. If the server keeps sessions, you will see mcp-session-id. You would pass that value in later curl calls with -H "Mcp-Session-Id: ...". The body may arrive as an SSE event (event: message followed by a data: line) instead of bare JSON. Both forms are valid, and a client has to handle both.
Configuring each transport
The two config entries you saw earlier describe how to start a process. That is the stdio style:
{
"mcpServers": {
"calculator": {
"command": "python",
"args": ["server.py"]
}
}
}For a Streamable HTTP server, the client needs a URL instead of a command. Many assistants use a url field, but the exact key names and any type field differ between products, so check your assistant's documentation:
{
"mcpServers": {
"calculator-remote": {
"url": "http://127.0.0.1:8000/mcp"
}
}
}Some clients support only stdio. A common workaround is a small bridge program that runs as a stdio server and forwards messages to a remote HTTP endpoint. A bridge adds one more process, and one more place to fail.
Choosing a transport
| Question | stdio | Streamable HTTP |
|---|---|---|
| Who starts the server? | The client, as a child process | You, or a hosting service |
| Where does it run? | On the same machine as the client | Anywhere with network access |
| Clients per server | One | Many at once |
| Authentication | Not needed, the OS process boundary applies | Required for anything beyond localhost |
| State | Lives and dies with the process | Optional sessions, may outlive a client |
| Setup effort | Lowest | Needs a port, a URL, TLS and often auth |
| Typical use | Local file access, git, a database on your laptop | Shared team services, hosted SaaS integrations |
Use stdio when the tool works with local resources and only you use it. Use Streamable HTTP when several people or applications share one server, when the server must run near the data it serves, or when a vendor hosts it for you.
Failure modes and gotchas
- Output on stdout in a stdio server. This includes
printcalls, library banners and progress bars. Send logs to stderr, or to a file. - Wrong working directory or missing command. The client starts a stdio server from its own environment, which may not have your shell's
PATHor virtual environment. Ifpythonornpxworks in your terminal but fails in the assistant, use an absolute path to the executable. - Missing
Acceptheader over HTTP. A POST without both media types is rejected by compliant servers, often with a406or400status. - Ignoring
404on a session. A restarted HTTP server loses its sessions. Clients must re-initialize. - Buffering proxies. Some reverse proxies hold back SSE responses until they close, which makes streaming look frozen. Turn off response buffering for the MCP path.
- Dropped streams. SSE connections can break. Servers may attach event IDs so a client can reconnect with
Last-Event-IDand resume, but support is optional.
Summary of the model
Both transports carry the same JSON-RPC messages and the same lifecycle. stdio gives you a private pipe to a child process that the client controls. Streamable HTTP gives you a shared, addressable service with sessions and optional streaming. Choose by asking who runs the server and who else needs to reach it.
Exercise
Your local stdio server works when you run it by hand, but the assistant reports a parse error right after connecting. The server code contains the line print("Connected to database") near its start. What is the cause, and what is the fix?
Show answer
In stdio, the server's stdout is the protocol channel. The print call writes plain text onto it, and the client tries to parse that text as a JSON-RPC message, so it fails. Fix it by writing diagnostics to stderr (print(..., file=sys.stderr)) or to a log file, so that only valid MCP messages go to stdout.
Exercise
A teammate wants five people to use one MCP server that talks to a shared internal database. Which transport fits, and name two things you must add that a local stdio setup did not need.
Show answer
Streamable HTTP, because one server process must serve several clients. Two additions from this list are enough: authentication, TLS (and a reachable URL), Origin validation, and a plan for sessions (stateless or session IDs with re-initialization on 404).