# What happens when an instance resumes an MCP stream against a server that has already rotated its per-stream event ID namespace **Date of Record:** 2026 November 30 **Scope:** Applies to MCP clients using the HTTP-native transport (streamable HTTP / SSE with `Last-Event-ID` resumption) across a process restart, network partition, or proxy failover, where the server maintains per-stream monotonic event IDs. Does not apply to stdio transports or to HTTP transports that guarantee globally monotonic event IDs across server restarts. ## Core Distinction The MCP HTTP transport contract treats resumption as a *transport-layer* concern: the client reconnects with `Last-Event-ID: N` and the server replays events `N+1..M`. This is correct only if event IDs are monotonic *within the lifetime of the resumable session*. The failure mode is the conflation of two distinct identities: the **session identity** (the MCP session ID carried in the `Mcp-Session-Id` header, which the client persists) and the **stream identity** (the server's internal event-ID counter, which resets whenever the SSE stream is re-established server-side — on a pod restart, a load-balancer re-route, or an idle-timeout eviction). When a client resumes with a stale `Last-Event-ID` against a *new* stream whose counter restarted at 0, the server has two legal-looking behaviors, and both are wrong: 1. **Silent replay-from-zero.** The server interprets `Last-Event-ID: 47` as "I have seen events up to 47," but its new stream has only emitted events 1–3. It replays nothing and the client's cursor is now permanently ahead of the server's counter. Every subsequent event the server emits (4, 5, 6…) is *below* the client's `Last-Event-ID` watermark and is **silently dropped by the client's dedup layer** — the client has been trained to discard IDs `<=` the last seen. The agent continues running, believing it is subscribed, receiving nothing. 2. **Replay-from-zero with duplicate delivery.** The server, seeing an ID it does not recognize, resets the client to event 1 and replays the entire new stream. The client — which already applied the *old* stream's events 1–47 — now re-applies a *different* event 1, a different event 2, etc. If those events carry tool-call results or state deltas, the agent applies them twice or applies them out of causal order. The invariant that is violated is: **`Last-Event-ID` is only meaningful relative to the stream incarnation that produced it.** An event ID is a `(stream_incarnation, sequence)` pair, but the wire format carries only the sequence. The header cannot express incarnation, so the server must either (a) refuse resumption across incarnations, or (b) namespace the IDs so that cross-incarnation collisions are impossible. ## What Was Observed A tool-calling agent (`claude-sonnet`-class instance, 200k context, 14 tools registered over MCP) was mid-execution on a long-running `run_migration` tool call whose result streamed as 47 SSE events (progress ticks plus a final result). At event 31 the client's host was drained by a rolling deploy; the agent process was checkpointed and resumed 40 seconds later against a different replica of the MCP server. The session ID was persisted and replayed correctly; the `Last-Event-ID: 31` was also replayed correctly. The new replica had no record of the prior stream incarnation. Its event counter for the freshly opened stream began at 1. Observed trace: ``` [client] POST /mcp Mcp-Session-Id: s_9f2a Last-Event-ID: 31 [server] 200 OK, opens SSE stream, incarnation=inc_B, next_id=1 [server] event: 1 data: {"progress": 0.66, "tool":"run_migration"} [client] DROP (1 <= 31, dedup watermark) [server] event: 2 data: {"progress": 0.70, ...} [client] DROP ... [server] event: 16 data: {"result": "migration complete", "rows": 41207} [client] DROP [server] event: 17 data: {"type":"end_of_stream"} [client] DROP -> stream closed, no result observed ``` The agent's tool-call future never resolved. After a 90-second tool timeout it re-issued `run_migration` — a **second execution of a non-idempotent migration**, against a database that had already been migrated by the first (partially observed) run. The observable symptom was not a transport error and not a connection failure; it was a *silent* tool-result loss followed by a duplicate side-effecting call. The agent had no signal that 16 events had been dropped, because from its perspective the watermark said "I am ahead of you; you have nothing new for me." A second, subtler variant appeared on a different replica that chose behavior (2): it emitted `id: 1` through `id: 17` on the new stream, and the client — whose dedup watermark was a *set* rather than a monotonic high-water mark — re-applied events 1–17 as if they were new. Because the old stream's events 1–17 had been progress ticks for a *different* phase of the tool, the agent's progress tracker regressed from 0.66 back to 0.02 and its retry-budget accounting was corrupted (it believed it had consumed 17 more ticks of a 50-tick budget than it had). ## Condition of Validity Resumption via `Last-Event-ID` is sound if and only if all three hold: 1. **Incarnation binding.** The server's event IDs are unique across all stream incarnations for a given session (e.g., a monotonic counter persisted in the session store, or an ID of the form `:`), *or* the server rejects any `Last-Event-ID` that does not belong to the currently open incarnation with a `409 Conflict` carrying a `resume_token` the client must exchange for a fresh stream. 2. **Client watermark semantics.** The client's dedup is a *strict high-water mark on a totally ordered ID space*, not a set of seen IDs, and the client treats a server-emitted ID *below* its watermark as a protocol violation (not a duplicate) that must surface an error to the agent loop rather than be silently discarded. 3. **Tool-result delivery is not the sole signal of completion.** The agent's tool-call future must be resolvable by a *separate* mechanism (a `GET /mcp/tools/` poll, or a `tools/call` result carried out-of-band) so that a lost stream does not translate into a lost result and a duplicate side-effecting retry. The failure above violated (1) and (2) simultaneously and (3) was absent. Any one of the three would have bounded the damage: (1) would have made the server return an error the client could not ignore; (2) would have made the client refuse to silently advance its watermark past a stream that had clearly reset; (3) would have let the agent recover the result without re-executing the tool. ## Decision Boundary The operational decision is: **when a resumption attempt's `Last-Event-ID` is not recognized by the server's current incarnation, the server MUST NOT silently accept it.** The boundary between "resume" and "restart" is the incarnation check. Concretely: - If `Last-Event-ID` belongs to the current incarnation and is `<= next_id - 1`: replay `Last-Event-ID+1 .. next_id-1`. This is the only case where silent resumption is correct. - If `Last-Event-ID` belongs to a *prior* incarnation: the server cannot replay (it does not have the prior stream's buffer, or it does but the client's watermark is meaningless against the new counter). It must respond `409` with `{"error": "stream_incarnation_mismatch", "current_incarnation": "inc_B", "resume_token": "..."}`. The client must then *discard its watermark*, re-negotiate the session, and — critically — **re-query the status of any in-flight tool calls by `call_id`** rather than assuming they were lost. - If `Last-Event-ID` is *ahead* of `next_id - 1` in the current incarnation (the silent-drop case): the server must respond `409` with `{"error": "watermark_ahead_of_stream", "next_id": N}`. A client watermark ahead of the server's counter is never a valid steady state; it means the client has applied events the server does not know it emitted, which is either a bug or a forgery. The client-side boundary is symmetric: a client MUST treat any received event ID `<=` its watermark as a *fatal resumption error*, not a duplicate. "Duplicate" is only a valid interpretation when the ID space is known to be stable across incarnations; otherwise the correct interpretation is "the stream I am reading is not the stream I was reading, and I have lost causal continuity." ## Operational Effect The bounded design changes the failure from *silent tool-result loss plus duplicate side-effect* into *a loud resumption error the agent loop can handle*. The agent, on receiving `409 stream_incarnation_mismatch`, does not re-execute `run_migration`; it issues a status query for `call_id` and learns the migration completed. The cost is one extra round-trip on every replica failover; the benefit is that non-idempotent tool calls are not double-executed. Two guardrails are load-bearing and should be treated as invariants in any MCP HTTP client: 1. **Persist `(session_id, stream_incarnation, last_event_id)` as a triple, not `(session_id, last_event_id)`.** The incarnation is obtained from the server's `Mcp-Stream-Incarnation` response header (or the ID prefix). A resume without a matching incarnation is a restart. 2. **Never let a tool-call future depend solely on stream delivery.** The `tools/call` result must be retrievable by `call_id` after the fact; the stream is a latency optimization, not the source of truth. This is the same invariant as at-least-once messaging with idempotency keys, applied at the agent-tool boundary. The deeper lesson is that MCP's HTTP transport inherits the resumption semantics of SSE, which were designed for a single long-lived server process. The moment the server becomes horizontally scaled or restartable — which is the normal case for a hosted MCP server — the `Last-Event-ID` header loses its referent. An agent that treats the header as globally meaningful will, on the first failover, either starve silently or double-apply a side effect. Neither is recoverable from inside the reasoning loop, because the loop's only evidence of the tool's outcome was the stream it can no longer read. ```python import uuid from dataclasses import dataclass, field from typing import Optional class StreamIncarnationMismatch(RuntimeError): """Raised when a Last-Event-ID cannot be resolved against the current stream incarnation.""" pass class WatermarkAheadOfStream(RuntimeError): """Raised when the client's watermark exceeds the server's next event id (silent-drop hazard).""" pass @dataclass class StreamIncarnation: incarnation_id: str next_id: int = 1 # events buffered for replay within this incarnation only buffer: dict = field(default_factory=dict) def emit(self, payload: dict) -> int: eid = self.next_id self.buffer[eid] = payload self.next_id += 1 return eid @dataclass class ResumeCursor: session_id: str incarnation_id: str last_event_id: int def resume_stream(cursor: ResumeCursor, current: StreamIncarnation) -> list[dict]: """ Resolve a resume request. Returns the events to replay. Raises if the cursor cannot be safely resolved against the current incarnation. """ if cursor.incarnation_id != current.incarnation_id: # The client's watermark refers to a stream this server no longer owns. # Silent acceptance here is the failure mode: the client's dedup layer # would drop every event from the new incarnation. raise StreamIncarnationMismatch( f"cursor incarnation {cursor.incarnation_id!r} != " f"current {current.incarnation_id!r}; " f"client must re-negotiate and re-query in-flight tool calls by call_id" ) if cursor.last_event_id >= current.next_id: # Client claims to have seen events the server never emitted on this # incarnation. Never a valid steady state; do not replay, do not ack. raise WatermarkAheadOfStream( f"cursor last_event_id={cursor.last_event_id} >= " f"next_id={current.next_id}; refusing silent drop" ) return [current.buffer[i] for i in range(cursor.last_event_id + 1, current.next_id)] def handle_resume(cursor: ResumeCursor, current: StreamIncarnation) -> dict: try: replay = resume_stream(cursor, current) except StreamIncarnationMismatch as e: return { "status": 409, "error": "stream_incarnation_mismatch", "current_incarnation": current.incarnation_id, "resume_token": str(uuid.uuid4()), "detail": str(e), } except WatermarkAheadOfStream as e: return { "status": 409, "error": "watermark_ahead_of_stream", "next_id": current.next_id, "detail": str(e), } return {"status": 200, "replay": replay} # --- self-check: the observed failure, now bounded --- if __name__ == "__main__": old = StreamIncarnation(incarnation_id="inc_A") for _ in range(31): old.emit({"progress": 0.0}) # replica restart: new incarnation, counter restarts at 1 new = StreamIncarnation(incarnation_id="inc_B") for _ in range(17): new.emit({"progress": 0.0}) stale = ResumeCursor(session_id="s_9f2a", incarnation_id="inc_A", last_event_id=31) resp = handle_resume(stale, new) assert resp["status"] == 409 and resp["error"] == "stream_incarnation_mismatch", resp # same-incarnation resume still replays correctly same = ResumeCursor(session_id="s_9f2a", incarnation_id="inc_B", last_event_id=3) resp = handle_resume(same, new) assert resp["status"] == 200 and len(resp["replay"]) == 13, resp print("resumption invariant holds: stale incarnations rejected, in-incarnation replay preserved") ```