# What structural signals indicate a mid-stream resumption break when migrating MCP servers from SSE to streamable HTTP **Date of Record:** 2026 November 27 **Scope:** Applies to MCP servers migrating from the legacy HTTP+SSE transport (dual-endpoint, `GET /sse` + `POST /messages`) to the streamable HTTP transport (single endpoint, `Accept: application/json, text/event-stream`, optional `Mcp-Session-Id`). Does not cover stdio transport or in-process agent tool binding, where resumption semantics are absent by construction. ## Core Distinction The legacy SSE transport and streamable HTTP transport both *look* like "the server pushes events to the client over a long-lived connection," so migration is often treated as a URL-and-header swap. This is wrong at exactly one point: **the legacy transport gives the client no way to prove it received events 1..N-1 before reconnecting, while streamable HTTP mandates `Last-Event-ID` resumption semantics.** A migrated server that emits event IDs but does not honor `Last-Event-ID` will silently replay or drop tool-call deltas across reconnects. The client, meanwhile, has no protocol-level signal that resumption failed — it sees a well-formed stream, just one that is internally inconsistent with its prior state. The distinction that matters: **SSE reconnect is a transport event; streamable HTTP reconnect is a state-reconciliation event.** Treating the second as the first is the failure. ## What Was Observed A tool-server exposing a long-running `search_and_summarize` tool was migrated from SSE to streamable HTTP. Under the legacy transport, the client opened `GET /sse`, received an `endpoint` event naming a `POST /messages?sessionId=...` URL, and every tool-result chunk arrived as a numbered SSE event. On reconnect (network blip, proxy idle timeout at 60s), the legacy client re-opened `GET /sse` and the server, having no resumption contract, started a *fresh* stream — the client's `mcp-js` SDK surfaced this as a new session and the agent re-issued the tool call. Duplicate work, but state stayed coherent because the SDK discarded the old stream wholesale. After migration to a single `POST /mcp` endpoint with `Accept: text/event-stream`, the server correctly emitted `id:` lines per event and set `Mcp-Session-Id`. On reconnect the client sent `Last-Event-ID: 41`. The server's handler — ported from the SSE code — did not read the header. It replayed from its in-memory ring buffer starting at event 0. The client, per spec, appended events 0..41 to a stream it had already consumed 0..41 of, then continued with 42+. The agent's tool-call accumulator saw `search_result` delta events applied twice: token counts doubled, and a partial JSON argument buffer (`{"query": "x", "filters":`) was concatenated with its own replay (`{"query": "x", "filters":`), producing `{"query": "x", "filters":{"query": "x", "filters":` — invalid JSON that the tool-argument parser surfaced as a *different* error than the one that actually occurred. The structural tell: **the replayed event IDs were non-monotonic relative to the client's `lastSeenId`, but the server never emitted a signal that it had ignored the resumption request.** No `resumption-rejected` error, no `400` with a body explaining the gap — just a stream that started at 0. The client's only recourse was to detect `event.id <= lastSeenId` and abort, which the SDK did not do because the spec does not require it. ## Condition of Validity The invariant that bounds this failure: **for any streamable-HTTP MCP server, if the server emits `id:` on any event, it MUST either (a) honor `Last-Event-ID` by resuming strictly after that ID, or (b) reject the resumption with a protocol-visible error and force the client to re-initialize the session.** A server that emits IDs but ignores `Last-Event-ID` violates the streamable HTTP contract in a way that is invisible to a client that trusts event ordering. This condition is *not* satisfied by "the server has a ring buffer." A ring buffer without header inspection is worse than no buffer: it produces the duplicate-append failure above. It is also not satisfied by "the server closes the connection on reconnect" — that forces a full session re-init (acceptable) but only if the server also invalidates the `Mcp-Session-Id`, otherwise the client resumes against a session whose server-side state was torn down, producing a different failure (silent 404s on subsequent `POST /mcp` calls). The condition does *not* apply when the server never emits `id:` — in that case the client must treat every reconnect as a fresh stream and the server must not advertise resumption support. The failure mode is specifically the *combination* of emitted IDs and absent resumption logic. ## Decision Boundary The boundary is crossed at the moment the server's stream handler is written to emit `id:` without a corresponding branch on `Last-Event-ID`. Concretely: - **Below the boundary (safe):** server emits no `id:`, client re-initializes on every reconnect, `Mcp-Session-Id` is rotated on each `POST /mcp` that opens a stream. Duplicate tool execution is possible but state is coherent. - **At the boundary (the bug):** server emits `id:`, does not read `Last-Event-ID`, replays from buffer start. Client sees monotonic-looking IDs only if it does not compare against its own `lastSeenId`. - **Above the boundary (safe):** server emits `id:`, reads `Last-Event-ID`, resumes strictly after it, or returns `400` with `{"error": "resumption-gap", "lastEventId": , "oldestAvailable": }` when the requested ID is outside the buffer. The decision the engineer must make explicitly: *does this server support resumption?* If yes, the header branch is mandatory. If no, the `id:` line must be removed. There is no correct middle state, and the middle state is the one that gets shipped because it passes every test that does not involve a mid-stream disconnect. ## Operational Effect The downstream effect is **silent duplicate-application of streamed deltas**, which manifests as one of three symptoms depending on the client's accumulator: 1. **JSON argument corruption** — partial tool-argument buffers concatenated with their own replay, producing parse errors attributed to the wrong cause. 2. **Token/count inflation** — streaming text deltas applied twice, so the agent's `usage` accounting and any length-based truncation logic fire at the wrong threshold. 3. **False "tool succeeded twice"** — idempotent-looking tools (search, read) hide the duplication; non-idempotent tools (write, send) execute twice and the agent's post-hoc verification sees only the second result. The last is the dangerous one: an agent that verifies its own tool calls by replaying the stream will conclude the operation succeeded once, while the side effect occurred twice. This is a verification-replay discrepancy that originates in the transport layer, not in the agent's reasoning, and it is not detectable from the agent's own logs. The guardrail is structural: the stream handler must branch on `Last-Event-ID` *before* touching the buffer, and the client must assert `event.id > lastSeenId` on every event or abort the stream. Both sides must enforce it; either alone is insufficient because the server cannot know what the client consumed and the client cannot know what the server replayed. ```python class ResumptionContractViolation(RuntimeError): """Raised when a streamable-HTTP MCP server emits event IDs but does not honor Last-Event-ID.""" pass def resume_stream(server_buffer: dict[int, dict], last_event_id_header: str | None, session_id: str | None) -> list[dict]: """ Server-side stream resumption. Enforces the invariant: if the server emits id: lines, it MUST resume strictly after Last-Event-ID or reject visibly. """ if session_id is None: raise ResumptionContractViolation( "streamable HTTP POST /mcp opened a stream without Mcp-Session-Id; " "client cannot reconcile state across reconnects" ) if not server_buffer: # No IDs emitted -> no resumption contract. Safe: fresh stream. return [] if last_event_id_header is None: # Client did not request resumption. Server must start fresh AND # must not replay buffered events under the same session. return [] try: last_event_id = int(last_event_id_header) except ValueError: raise ResumptionContractViolation( f"Last-Event-ID header {last_event_id_header!r} is not an integer; " "server emitted id: lines but client sent unparseable resumption token" ) oldest_available = min(server_buffer) newest_available = max(server_buffer) if last_event_id < oldest_available - 1: # Requested ID is older than the buffer. Replaying from oldest_available # would silently drop events the client never saw. Must reject visibly. raise ResumptionContractViolation( f"resumption gap: client requested Last-Event-ID={last_event_id} but " f"oldest buffered event is {oldest_available}; client must re-initialize " f"session {session_id}" ) if last_event_id > newest_available: raise ResumptionContractViolation( f"client claims Last-Event-ID={last_event_id} beyond server's newest " f"buffered event {newest_available}; session {session_id} desynchronized" ) # Correct path: resume strictly after the client's last-seen ID. return [server_buffer[i] for i in sorted(server_buffer) if i > last_event_id] def client_apply_event(event: dict, last_seen_id: int) -> int: """ Client-side guard: assert monotonicity before applying a streamed delta. """ event_id = event.get("id") if event_id is None: return last_seen_id if event_id <= last_seen_id: raise ResumptionContractViolation( f"server replayed event id={event_id} after client last_seen_id={last_seen_id}; " "aborting stream to prevent duplicate delta application" ) return event_id ```