Skip to content
AI Engineer Guide.

concept · concepts · intermediate · concept 6 of 16

Lifecycle & transports

0 of 16 complete

Why it matters

A protocol that only works in-process cannot support a hosted integration, and one that only works over HTTP cannot run a local filesystem server. MCP keeps the message layer identical and swaps the transport, which is why the same server logic can ship as a local process and a remote service. The transport then determines the operational questions: who authenticates, where the session lives, how cancellation works and what a reconnect means.

Mental model

The data layer is a language and the transport is the postal service: the same letter travels by hand or by air mail. The metaphor breaks because the postal service can lose ordering, duplicate delivery and change the sender's identity, all of which the letter's content does not anticipate.

How it works

Same messages, two framing layersInteractive

Two independent connections, not one session that changes transport. The 2025-06-18 handshake is shown, which is where framing and session headers are visible; the 2026-07-28 revision removes initialize and carries metadata per request.

Client

one connection

Server

same handlers

Alternative transport — new connection: HTTP
Step 1/6 · stdio initialize
initialize result · stdio linereturn · Server → Client
one line on the wire (wrapped here for display):

{"jsonrpc":"2.0","id":1,"result":{"protocolVersion":"2025-06-18","capabilities":{"tools":{"listChanged":true}},"serverInfo":{"name":"issues","version":"0.9"}}}

No HTTP status; stdout carries the frame.

Step 1: stdio initialize. Hover, tap or tab to any message to inspect its payload.

Figure. The data layer is identical: initialize, the initialized notification, tools/call and its result carry the same fields and ids. The figure shows two independent connections — stdio first, then an HTTP session opened by its own initialize. Only framing, authentication and session headers change; neither connection continues the other.

What to notice. The failure is framing. On stdio, one log line written to stdout corrupts the JSON stream, and the server looks broken while it is only noisy. On HTTP, accepting a session identifier without binding it to the authenticated principal is session fixation — the session identifies state, it does not authenticate the caller.

Text version

Sequence across two lifelines, Client and Server, in two independent sessions. Session 1, stdio: Client to Server initialize as one newline-delimited JSON line with protocolVersion 2025-06-18, roots capability and clientInfo; Server to Client the initialize result as one line; Client to Server notifications/initialized; Client to Server tools/call for issues_search; Server to Client the result content with isError false. A divider marks an alternative transport and a new connection. Session 2, HTTP: Client to Server POST /mcp initialize with Content-Type, Accept, Authorization and the same body; Server to Client HTTP 200 with Mcp-Session-Id 7f3c9a2e and the same result; Client to Server notifications/initialized with MCP-Protocol-Version 2025-06-18 and the session header; Client to Server tools/call with MCP-Protocol-Version, Mcp-Session-Id and the same body; Server to Client the same result as a JSON body or request-scoped SSE event. The two sessions are not one conversation that changes transport.

  1. Start with an initialise request that carries the protocol version, client capabilities and client info, and check the server's response for the negotiated version.
  2. Send an initialised notification before issuing ordinary requests, so the server knows the handshake completed.
  3. For stdio, spawn the process, frame messages as newline-delimited JSON on stdin and stdout, and send logs to stderr.
  4. For Streamable HTTP, POST each message to the endpoint and accept either a JSON response or an SSE stream carrying related messages.
  5. Propagate cancellation and timeouts to the underlying work, and close the stream or terminate the process on shutdown.
  6. Authenticate the HTTP transport with OAuth, validate the token audience and bind any session identifier to the authenticated principal.

Trade-offs

ChoiceGood forCostFails when
stdioLocal tools, no network exposureOne client per process, no built-in authThe host runs remotely
Streamable HTTPRemote and multi-clientAuth, session and load-balancer complexityThe server needs server-initiated messages without SSE support
Stateful session idCheap continuity for a conversationSticky routing and session hijack riskRequests are spread across instances
Stateless requestsAny instance can serve any callState must be carried explicitlyThe workflow spans many steps
Trade-off mapInteractive

Good for

Local tools, no network exposure

Cost

One client per process, no built-in auth

Fails when

The host runs remotely

  1. The host runs remotely
  2. The server needs server-initiated messages without SSE support
  3. Requests are spread across instances
  4. The workflow spans many steps

Figure. Each choice with what it buys, what it costs, and the case where it loses.

What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.

Text version

Four trade-offs: stdio is good for Local tools, no network exposure, costs One client per process, no built-in auth, and fails when The host runs remotely. Streamable HTTP is good for Remote and multi-client, costs Auth, session and load-balancer complexity, and fails when The server needs server-initiated messages without SSE support. Stateful session id is good for Cheap continuity for a conversation, costs Sticky routing and session hijack risk, and fails when Requests are spread across instances. Stateless requests is good for Any instance can serve any call, costs State must be carried explicitly, and fails when The workflow spans many steps

Failure modes

  • Handshake skipped. The client sends requests before initialise and the server rejects or misbehaves. Debug: capture the first three frames on the wire and check the order.
  • Framing error on stdio. A log line is written to stdout and corrupts the JSON stream. Debug: run the server and pipe stdout through a JSON parser, failing on the first non-JSON line.
  • Session fixation. A session id is accepted without binding to the authenticated user. Debug: replay a session id with a different token and confirm the server rejects it.

In production

Monitor initialise failures, transport-level disconnects and SSE stream duration. The specification mandates the handshake and message framing, while sticky sessions and gateway timeouts are deployment choices, so cap concurrent streams per client and test reconnection against a load balancer without sticky sessions.

Common misconceptions

People think: stdio and HTTP servers are different protocols.

Actually: They share the data layer; only framing, auth and session behaviour differ.

People think: A session id authenticates the caller.

Actually: It identifies state; authentication comes from the token and must be checked independently.

Interview angle

Strong answers separate the data layer from the transport and explain what each transport costs operationally. They mention auth only on HTTP and the session binding problem. Weak answers treat stdio as a legacy mode without saying what it is good for.

Sources