# Where must an instance pin the SSE event ID before yielding control, or resumability silently replays a poisoned prefix **Date of Record:** 2026 November 29 **Scope:** Applies to MCP servers using the HTTP-native transport with Server-Sent Events streaming and `Last-Event-ID` resumption, where a client agent reconnects a mid-stream tool call after a transport drop. Does not apply to stdio transports, nor to stateless request/response MCP endpoints that never emit resumable event streams. ## Core Distinction There are two different notions of "where the stream was" and conflating them is the entire failure. The **emission cursor** is the server-side counter of events it has *written into the SSE buffer*. The **acknowledgement cursor** is the last event ID the client has *durably consumed and committed to its own state*. Resumability is only sound when the client's `Last-Event-ID` on reconnect names an acknowledgement cursor, not an emission cursor. The non-obvious part: in MCP's streaming semantics, a single logical JSON-RPC response (a tool result, a streamed partial of a sampling call) can be split across many SSE events, and the *client* is the one that assembles them. The client therefore holds partial state that the server has already forgotten it emitted. If the client advances its stored `Last-Event-ID` to the highest event it has *received into a buffer* rather than the highest event it has *folded into committed state*, a reconnect will ask the server to resume *after* events the client never actually applied. The server, honoring the cursor, will not resend them. The assembled result is silently missing a middle segment — or, worse, when the client's buffer boundary lands mid-JSON, it re-parses a truncated fragment against a fresh prefix and produces a syntactically valid but semantically poisoned result. ## What Was Observed A client agent issued a `tools/call` over HTTP-native transport. The server streamed the tool's progress and output as a sequence of SSE events `id: 41 … id: 57`, where events 44–51 carried the body of a large `content` array in chunks. The client's SSE reader thread wrote each raw event into an in-memory queue and, on each successful `read()`, updated a persisted `last_event_id` field to the event's `id`. The consumer coroutine that actually parsed and applied events ran slower than the reader thread. At event 48 the TCP connection dropped (idle proxy timeout). On reconnect the client sent `Last-Event-ID: 48` — because the reader thread had *read* up to 48 — but the consumer coroutine had only *applied* up through 45. The server resumed at 49. Events 46, 47, 48 were never delivered to the consumer. The consumer assembled `content[0]` (from 44–45) concatenated with `content[2]` (from 49 onward), because the chunk boundaries at 46–48 were exactly the closing of `content[0]` and the opening of `content[1]`. The `content[1]` element — a structured `{"type":"text","text":"..."}` block — vanished entirely. The assembled tool result was valid JSON, passed schema validation, and was returned to the reasoning loop as ground truth. The agent proceeded to act on a result from which one entire content block had been silently excised. The failure was not detected by schema validation, not by the JSON parser, and not by the server (which correctly honored the cursor). It was detected only when a downstream auditor replayed the same tool call and got a different content array length. ## Condition of Validity The invariant that must hold for `Last-Event-ID` resumption to be sound: > The value sent as `Last-Event-ID` must be ≤ the highest event ID whose effects have been *committed* to the client's durable task state, and must never exceed it. Formally, let `A` be the set of event IDs the client has fully applied (parsed, folded into the assembled response, and checkpointed). Let `R` be the value the client reports on reconnect. Soundness requires `R ∈ A` or `R = max(A)` under a monotonic commit discipline. The reader thread's `read()` completion is *not* a commit; it is a buffer occupancy event. Advancing the cursor on read violates the invariant whenever reader throughput exceeds consumer throughput — which is the normal case under backpressure, since the reader is I/O-bound and the consumer is parse-bound. The condition of validity therefore requires that the persisted cursor be advanced *by the consumer*, after the event's effects are folded into committed state, and that the reader thread never touch the persisted cursor. This is a single-writer discipline on the resume cursor, and it is the exact boundary the incident violated. ## Decision Boundary The decision an implementer must make is: *at which layer does the `Last-Event-ID` cursor live, and who is allowed to write it?* - If the cursor lives in the **transport reader**, resumption is fast but unsound under any consumer lag. The reader cannot know whether the consumer has applied an event. - If the cursor lives in the **consumer/state machine**, resumption is sound but the cursor lags the wire, so a reconnect may redeliver events the client has buffered but not applied. Redelivery is safe *only if* event application is idempotent — which requires the client to key applied events by ID and drop duplicates. - The boundary condition that breaks naive "consumer-owned cursor" designs: if the consumer crashes *after* applying an event but *before* persisting the advanced cursor, the event is redelivered on resume. If the event mutates state non-idempotently (e.g., appends to an array), the redelivery duplicates. So the cursor advance and the state mutation must be in the **same atomic commit** — a single durable write that both applies the event's effect and advances the cursor. Two separate writes reopen the window. The correct boundary: one atomic commit per event, performed by the consumer, that (a) folds the event into assembled state and (b) advances the persisted cursor, with applied-event-ID deduplication to absorb redelivery from the crash-before-commit window. ## Operational Effect The operational consequence of getting this boundary wrong is **silent prefix corruption that survives every structural validator**. The assembled response is well-formed; the schema is satisfied; the reasoning loop has no signal that a segment is missing. The agent's subsequent plan is built on a tool result whose middle was amputated, and the divergence only surfaces when an independent replay or a downstream auditor compares content hashes. Bounding it requires three concrete guards: (1) the resume cursor is written only by the consumer, in the same atomic commit as the state fold; (2) the client deduplicates by event ID so crash-before-commit redelivery is idempotent; (3) the assembled response carries a monotonic event-ID span check — if the set of applied event IDs has a gap between `min` and `max`, the assembly is rejected rather than returned. The third guard is the one that would have caught this incident, because the gap at 46–48 was visible in the applied-ID set even though it was invisible in the assembled JSON. ```python from dataclasses import dataclass, field class ResumeCursorViolation(RuntimeError): """Raised when SSE resumption would skip unapplied events or assemble a gapped response.""" @dataclass class ResumableStream: applied_ids: set[int] = field(default_factory=set) committed_cursor: int = 0 # the ONLY value ever sent as Last-Event-ID def apply_event(self, event_id: int, payload: dict, fold) -> None: # Idempotent: redelivery from a crash-before-commit window is dropped. if event_id in self.applied_ids: return # Guard: the event must be contiguous with committed state, else the # transport silently skipped a segment we never applied. if event_id != self.committed_cursor + 1: raise ResumeCursorViolation( f"event {event_id} arrived but committed cursor is " f"{self.committed_cursor}; gap means Last-Event-ID advanced " f"past unapplied events (poisoned prefix risk)" ) # Atomic commit: fold state AND advance cursor together. fold(payload) self.applied_ids.add(event_id) self.committed_cursor = event_id def last_event_id_for_reconnect(self) -> int: return self.committed_cursor def assert_contiguous_assembly(self) -> None: if not self.applied_ids: return expected = set(range(min(self.applied_ids), max(self.applied_ids) + 1)) missing = expected - self.applied_ids if missing: raise ResumeCursorViolation( f"assembled response has event-ID gaps {sorted(missing)}; " f"result is structurally valid but semantically amputated" ) def resume_after_drop(stream: ResumableStream, server_resume_at: int) -> None: # The server will resume at the cursor we send. If the server's notion of # "next" is not our committed cursor + 1, the stream is unsound to resume. if server_resume_at != stream.committed_cursor + 1: raise ResumeCursorViolation( f"server resuming at {server_resume_at} but committed state ends at " f"{stream.committed_cursor}; refusing to resume onto a gapped prefix" ) ```