Skip to content
mentoor

Topic 4 of 5 · 5 min read

Serving one server over stdio and Streamable HTTP

Goal: Find the transport bug in a server that corrupts its stdio stream or mishandles HTTP requests and correct the configuration.

Write your tools once, then attach them to a transport: stdio for a local process the client launches, Streamable HTTP for a server that runs somewhere and serves many clients. The transport changes how bytes travel, not what the server does.

Check yourself · 1 of 3

A stdio MCP server prints "Server ready" with console.log. What happens?

Choose one answer

Why it matters

Two bugs cause most "my server does not connect" reports. On stdio, something writes to stdout that is not a protocol message. On HTTP, the endpoint, the method or the headers do not match what the client expects.

stdio

The client starts your server as a child process. Messages are JSON-RPC, one per line, over the process's stdin and stdout. That gives one rule: stdout carries protocol messages only. A stray console.log or print writes text into the stream, and the client fails to parse it, so the connection breaks. Send logs to stderr (clients usually capture it) or to a file, using console.error or Python's logging configured for stderr. Also watch libraries that print banners or progress bars on startup.

Streamable HTTP

The server exposes one endpoint, for example /mcp, that accepts POST requests with JSON-RPC and can answer with a normal JSON response or an event stream. Key points:

  • Mount the endpoint on a web framework route and pass requests to the SDK's transport.
  • Check the Origin header and bind to 127.0.0.1 for local servers, to protect against browser-based attacks such as DNS rebinding.
  • Add authentication for anything that is not local. Do not expose an unauthenticated tool server to a network.
  • Keep it stateless. Since revision 2026-07-28 there are no protocol sessions: each request carries its own version and capabilities, so any instance behind a load balancer can answer. State a tool needs across calls goes in shared storage behind an explicit handle, such as an export_id argument. Legacy clients (2025-11-25 and earlier) may still get an Mcp-Session-Id; their requests then need sticky routing or a shared session store.

One codebase, two modes

Build the server in one function. Then the entry point chooses the transport:

const server = buildServer();           // registers tools, resources, prompts

if (process.argv.includes("--http")) {
  await startHttp(server, { port: 3000 });   // express route at /mcp
} else {
  await server.connect(new StdioServerTransport());
}

Before and after

# Weak: breaks stdio, because print writes to stdout
print("starting server")
mcp.run(transport="stdio")
# Fixed: logs go to stderr
import sys, logging
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
logging.info("starting server")
mcp.run(transport="stdio")

What goes wrong

  • Debug prints on stdout. The most common stdio failure.
  • Binding HTTP to 0.0.0.0 with no auth. Anyone on the network can call your tools.
  • Wrong path or method. A client posts to /mcp and the route only accepts GET.
  • Shared state in a stateless deployment. Two requests land on different instances and disagree.

Examples

Diagnosing a stdio failure from a client log

[client] spawn node dist/server.js
[client] <- Server starting on port 3000...
[client] ERROR: Unexpected token 'S', "Server sta"... is not valid JSON
[client] connection closed

The server printed a banner to stdout, and the client tried to read it as JSON-RPC. The fix is console.error("Server starting"), not changing the client.

Stateless HTTP behind a load balancer

A team deploys three copies of an HTTP server. Each request builds a new server object, answers, and discards it, so any copy can serve any request. Their earlier version kept session data in memory, and calls randomly failed with "unknown session" when the balancer sent a request to a different copy.

Key points

  • With stdio, stdout is reserved for JSON-RPC messages, so every log line must go to stderr or a file.
  • Streamable HTTP serves JSON-RPC through a single endpoint, and the client must post to the exact path you mount.
  • Check the Origin header and bind local servers to 127.0.0.1 to reduce the risk of browser-based attacks.
  • Remote servers need authentication, because a tool endpoint with no auth lets anyone run your tools.
  • Build the server in one function and let the entry point choose the transport, so both modes share the same code.

Common mistakes

  • Using console.log or print for diagnostics in a stdio server.
  • Exposing an HTTP server on all interfaces with no authentication or Origin check.
  • Keeping per-session state in memory while running several instances behind a load balancer.
  • Maintaining two copies of the tool code, one per transport, which drift apart.

Check yourself · 2 of 3

You want the same tools available locally and as a shared network service. What is the best structure?

Choose one answer

Check yourself · 3 of 3

Which setting is the safest default for a Streamable HTTP server used only on a developer's laptop?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic