Heuristic:21st dev Magic mcp Sequential Pump For Handshake Ordering
| Knowledge Sources | |
|---|---|
| Domains | Protocol, Networking, Concurrency |
| Last Updated | 2026-10-19 16:40 GMT |
Overview
Pump stdin one line at a time (one in-flight `forward()` call) so the `initialize` → `mcp-session-id` handshake lands before any later request.
Description
The `@21st-dev/magic` proxy reads JSON-RPC lines from stdin and forwards each to the 21st MCP endpoint. It uses a sequential pump — a single `Promise` chain (`queue = queue.then(() => forward(line))`) — so that exactly one request is in flight at a time. The reason is ordering: MCP capability negotiation requires the `initialize` response (which carries the `mcp-session-id` header) to be processed before any subsequent request is sent, because every later request must echo that session id. If requests were pipelined concurrently, a `tools/list` or `tools/call` could reach the server before `initialize`'s session id was latched, and capability negotiation (and inline UI) would break through the proxy.
The trade-off is throughput: the proxy is strictly serial, so it cannot exploit request pipelining. In practice MCP clients pipeline rarely enough that this is not a bottleneck, and the correctness win (guaranteed handshake ordering, guaranteed response correlation) outweighs it. The same serial design also makes the `authRefused` latch and `id`-restoration logic deterministic, since no two `forward` calls race on shared state.
Usage
Apply this heuristic when implementing or reasoning about the proxy's stdin pump. Do not parallelize `forward` calls or introduce a concurrent queue — the session-id latch depends on strict ordering. If you ever batch JSON-RPC messages, keep the batch as a single `forward` invocation (the function already handles array batches).
The Insight (Rule of Thumb)
- Action: Keep the stdin pump serial — `queue = queue.then(() => forward(line))`, one in-flight request at a time.
- Value: Exactly one in-flight `forward()` per process at any moment.
- Trade-off: Lower throughput (no pipelining) in exchange for guaranteed `initialize` → `mcp-session-id` ordering and deterministic latch state.
- Compatibility: Required for the 21st MCP proxy; any stdio↔HTTP MCP proxy with a session-id handshake should follow the same pattern.
Reasoning
MCP's `initialize` response carries `mcp-session-id`; every later request must send that header. A concurrent pump would let a later request reach the server before the `initialize` response was processed (the latch at `src/index.ts:170-171` runs in `forward`'s async body). Without the session id on those early requests, the server cannot correlate them to the session and capability negotiation fails. Serializing the pump makes the latch a happens-before relationship: `initialize` fully resolves (and sets `sessionId`) before the next line's `forward` starts. The same ordering guarantees the `authRefused` latch is observed before any later request decides to short-circuit.
Code Evidence
The sequential pump with its rationale — `src/index.ts:206-214`:
// Sequential pump: strict request ordering, one in-flight request at a time.
// MCP clients pipeline rarely enough that this is not a bottleneck, and it
// guarantees the initialize -> session-id handshake lands before anything else.
let queue: Promise<void> = Promise.resolve();
rl.on("line", (line) => {
if (!line.trim()) return;
queue = queue.then(() => forward(line));
});
The session-id latch that depends on this ordering — `src/index.ts:170-171`:
const newSession = res.headers.get("mcp-session-id");
if (newSession) sessionId = newSession;
And its use on later requests — `src/index.ts:147`:
if (sessionId) headers["mcp-session-id"] = sessionId;
The `forward` implementation page documents the same rationale — `implementations/21st_dev_Magic_mcp_forward.md:33`:
The readline pump at L206-217 feeds `forward` sequentially — one in-flight
request at a time, guaranteeing the initialize → session-id handshake lands
before anything else.