Velamuvi AIAcademy

Lesson 4 of 9 · Concepts

Transports

How MCP messages travel: stdio for local servers and Streamable HTTP for remote ones, with message framing, streamed responses, headers and security rules.

  • 6 min read
  • Intermediate
  • Reviewed
In this lesson you will learn
  • Explain what a transport is in MCP
  • Describe how the stdio transport frames messages
  • Describe how Streamable HTTP sends requests and responses
  • Choose a transport and apply its security rules

Same messages, different roads

MCP defines two standard transports:

stdioStreamable HTTP
Server runsOn the user's computer, started by the hostAnywhere: a server, the cloud, or locally
How it connectsThe host launches the server as a child processThe client sends HTTP POST requests to one URL
Who can use itOnly the host that started itMany clients, over the network
Typical useLocal files, local tools, personal serversShared, hosted and company-wide servers
AuthenticationCredentials from environment variablesOAuth-based authorization (or another scheme)

stdio: one message per line

Each message is a single line of JSON, so a message must never contain a raw newline. The server reads a line, handles it, and writes its response as a line:

Try it yourselfPython
import io
import json

def meta():
    return {"io.modelcontextprotocol/protocolVersion": "2026-07-28",
            "io.modelcontextprotocol/clientCapabilities": {}}

call = {"jsonrpc": "2.0", "id": 1, "method": "tools/call",
        "params": {"name": "search_notes", "arguments": {"query": "tram"}, "_meta": meta()}}

# ---- stdio: one JSON message per line on the server process's stdin/stdout ----
pipe = io.StringIO()                       # stands in for the server's stdin
pipe.write(json.dumps(call) + "\n")        # newline-delimited: no newlines INSIDE a message
pipe.write(json.dumps({**call, "id": 2, "params": {**call["params"], "arguments": {"query": "umbrella"}}}) + "\n")
pipe.seek(0)
for line in pipe:                          # the server reads one complete message per line
    msg = json.loads(line)
    print(f"stdio message {msg['id']}: {msg['method']} {msg['params']['arguments']}")

# ---- Streamable HTTP: each message is its own POST to one endpoint, e.g. /mcp ----
def http_headers(message):
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json, text/event-stream",
        "MCP-Protocol-Version": message["params"]["_meta"]["io.modelcontextprotocol/protocolVersion"],
        "Mcp-Method": message["method"],             # mirrored from the body, for gateways and routing
    }
    if message["method"] in ("tools/call", "prompts/get"):
        headers["Mcp-Name"] = message["params"]["name"]
    return headers

print("\nPOST /mcp")
for name, value in http_headers(call).items():
    print(f"{name}: {value}")

# The server must check that the headers match the body
def check(headers, body):
    if headers.get("Mcp-Method") != body["method"]:
        return {"code": -32020, "message": "HeaderMismatch: Mcp-Method doesn't match the body"}
    return "ok"

print("\nHonest request: ", check(http_headers(call), call))
tampered = {**http_headers(call), "Mcp-Method": "tools/list"}
print("Tampered request:", check(tampered, call))

Streamable HTTP: one POST per message

The second half of the program above builds a request's headers. Besides the usual Content-Type and Accept, the client mirrors key fields of the body into headers: MCP-Protocol-Version, Mcp-Method and, for tool calls, Mcp-Name. Gateways and load balancers can then route and rate-limit requests without reading the JSON. The server must check that headers and body agree, and rejects a mismatch with the HeaderMismatch error (code -32020), which stops anyone from routing on one value while the server executes another.

Because every request carries its own metadata, a Streamable HTTP server needs no sessions: any copy of the server can answer any request, which makes scaling straightforward.

Security rules

  • stdio: only run servers you trust. The host executes the launch command with your permissions.
  • Streamable HTTP, local: bind to 127.0.0.1, not 0.0.0.0, and the server must validate the Origin header, so a malicious website can't reach your local server through your browser (a DNS rebinding attack).
  • Streamable HTTP, remote: use HTTPS and proper authorization; MCP defines an OAuth-based authorization flow for HTTP servers.

Common mistakes

Practice exercise

Read messages the way the stdio transport frames them: one JSON message per line.

Your solutionPython
import json

# Three messages arrived on a stdio stream, one per line
stream = '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}\n{"jsonrpc": "2.0", "id": 2, "method": "resources/list"}\n{"jsonrpc": "2.0", "id": 3, "method": "prompts/list"}\n'

# Split the stream into lines, parse each line as JSON, and print id and method, like:
# 1 tools/list
Knowledge check

Why must a stdio MCP server never print ordinary text to standard output?

Summary

  • A transport carries the same JSON-RPC messages in different ways.
  • stdio: the host starts the server; one JSON message per line; logs go to stderr.
  • Streamable HTTP: one POST per message to a single endpoint, JSON or streamed responses, with mirrored headers checked against the body.
  • Bind local HTTP servers to localhost, validate Origin, and use HTTPS and authorization for remote ones.

Next: build a real MCP server in Python.

Finished reading? Tick it off to track your progress.