# Which invariants prevent SSE replay from silently reordering tool-call results across an MCP session resume **Date of Record:** 2026 November 28 **Scope:** Applies to MCP servers using the HTTP-native transport with SSE streaming and `Last-Event-ID`-based session resumption, where a single logical agent turn emits multiple interleaved `tool_result` events on one stream. Does not apply to stdio transports, single-shot request/response MCP servers, or streams that emit at most one result per turn. ## Core Distinction The distinction that matters here is between *delivery resumption* and *causal resumption*. The MCP HTTP-native transport guarantees the former: on reconnect, the client sends `Last-Event-ID: N` and the server replays every event with id strictly greater than `N`. That is a byte-stream contract. It says nothing about the *logical* order in which the replayed events must be applied to the agent's tool-call ledger. The failure mode is this: during a backpressure stall, the SSE server buffers events in a bounded ring buffer keyed by monotonically increasing ids, but the *application* layer that assigns those ids is not the same component that enforces the ring buffer's eviction. When the buffer overflows and the server drops the oldest unacked events, the ids of the *surviving* events remain monotonic — so `Last-Event-ID` replay looks correct — but the dropped events are the ones that carried the *opening* of a multi-part tool result whose *closing* fragment survived. On resume, the agent receives a `tool_result` continuation fragment with no matching `tool_use` opener, applies it to a ledger slot that was never opened, and either (a) silently no-ops it, losing the result entirely, or (b) coerces it into the most recent open slot, misattributing result B to tool call A. Both are silent. Neither trips a JSON parse error. The agent proceeds with a corrupted tool-call ledger and completes the turn confidently wrong. The invariant that prevents this is **not** "replay must be contiguous." It is: *the resumption boundary must fall on an event that is a causal unit boundary, and the server must refuse to assign a resumable id to any event that is not itself a causal unit boundary.* Contiguity of ids is insufficient; contiguity of *open/close pairs* is required. ## What Was Observed A coding agent driving a filesystem MCP server over HTTP-native SSE issued a `read_multiple_files` call against 14 files. The server emitted the result as a sequence of chunked `tool_result` events: a header event (`tool_use_id`, file count), then 14 body events each carrying one file's content, then a footer event (`complete: true`). Each event got a fresh monotonic id. The agent's SSE client applied backpressure: its consumer loop was blocked on a slow tokenizer for ~4 seconds while the server kept writing. The server's ring buffer was configured at 64 events. The turn produced 16 events, well under 64 — but the *previous* turn had produced 58 events that the client had acked only up to id 40 before stalling, so the buffer was already 58/64 full when this turn began. Writing 16 more events evicted 10 of the previous turn's unacked events, then the *header* of the current turn's `read_multiple_files` result was evicted as the buffer wrapped again, while the 14 body events and the footer survived. The client reconnected with `Last-Event-ID` pointing at the last id it had actually consumed (an event from the prior turn). The server replayed: the tail of the prior turn, the 14 body events of the current `read_multiple_files` result, and the footer. The header — which carried `tool_use_id` — was gone. The agent's ledger had exactly one open `tool_use` slot (the `read_multiple_files` call it had issued). The 14 body events had no `tool_use_id` field of their own (the server assumed the header scoped them). The client's decoder, finding no `tool_use_id`, defaulted to the single open slot. The footer's `complete: true` closed that slot. The agent received 14 files' worth of content attributed to a `read_multiple_files` call — which happened to be correct by luck, because there was only one open slot. The near-miss was in a second run where two tool calls were in flight concurrently (the agent had issued `read_multiple_files` and a `grep` before the stall). The header for `read_multiple_files` was evicted; the header for `grep` survived. On replay, the 14 body events defaulted to the *most recent* open slot, which was `grep`. The agent received 14 files' contents as the output of a `grep` and proceeded to reason over them as grep hits. No error was raised. The turn completed. The bug surfaced three turns later when the agent tried to open a file path it had "found" via grep — a path that was actually a filename from the `read_multiple_files` result. ## Condition of Validity The replay-safety of a resumed MCP SSE session holds if and only if all three of the following are true: 1. **Causal-unit id assignment.** Every event id is assigned at the boundary of a causal unit (a complete `tool_result`, a complete `tool_use`, a complete `message_delta` that does not span events). Partial units — a header without its bodies, a body without its header — must not receive a resumable id. If a multi-part result must be chunked, the entire chunk sequence must share one id, or the header must be re-emitted as a preamble on every replay. 2. **Eviction atomicity.** The ring buffer's eviction policy must evict whole causal units, never individual events. A buffer that can hold 64 *events* but only 3 *causal units* is a buffer whose effective capacity is 3, and it must be sized and evicted as such. Eviction of a header without its bodies is the exact failure above. 3. **Resumption point validation.** On receiving `Last-Event-ID: N`, the server must verify that `N` is a causal-unit boundary id. If `N` points into the middle of an evicted unit (because the client acked a body but not its header, or because the header was evicted and the client's last-seen id is a body id), the server must not replay from `N`. It must either replay from the start of `N`'s causal unit (re-emitting the header) or return a `409 Resumption Point Invalid` and force the client to re-issue the tool call. The invariant is violated silently when any one of these fails, and the failure is *always* silent because the SSE framing layer has no concept of causal units — it sees only `id:` and `data:` lines, and all of them parse. ## Decision Boundary The operational decision is: **when the resumption point is not a causal-unit boundary, replay-from-unit-start or fail-and-reissue — never replay-from-point.** The tempting third option, "replay from `N` and let the client's decoder reconcile," is what produced the misattribution above. It is tempting because it is the cheapest to implement (the SSE layer already does it) and because in the single-open-slot case it *appears* to work. The decision boundary is crossed the moment more than one causal unit is open concurrently — which is the normal case for any agent that issues parallel tool calls, and which is exactly the case where the failure is most damaging. The secondary decision: **buffer sizing must be expressed in causal units, not events.** A 64-event buffer that holds 58 events from a prior turn's 58-event multi-part result has an effective capacity of 6 events for the current turn — and the current turn's 16-event result will evict its own header. Sizing by event count is the root cause; sizing by "at least 2× the maximum concurrent open causal units, each counted as one slot" is the fix. The 2× is because a unit can be in-flight (partially emitted) while the next unit begins. ## Operational Effect With the invariant enforced, the second run's failure becomes a `409` on resume, the agent re-issues `read_multiple_files`, and the 14 files arrive correctly attributed. Cost: one redundant tool call. Without it, the agent completes the turn with a corrupted ledger and the corruption propagates — the misattributed grep result is consumed by the reasoning loop, cited in a downstream `write_file`, and only surfaces when a later tool call fails on a path that never existed. The blast radius of a silent ledger corruption is bounded only by how many turns the agent runs before the corrupted fact is contradicted. The general lesson for MCP transport implementers: SSE's `Last-Event-ID` is a *transport* resume primitive, and transport resume is not session resume. The gap between them is exactly the set of multi-event causal units, and the invariant that closes the gap is that the transport's id namespace must be a quotient of the application's causal-unit namespace — every id is a unit boundary, and no unit spans an id boundary in a way that can be cut by eviction. Any implementation that assigns ids per-event and evicts per-event has a latent silent-corruption bug that activates the first time a multi-part result meets a full buffer during a backpressure stall. ```python class ResumptionBoundaryError(RuntimeError): """Raised when an SSE resume point does not fall on a causal-unit boundary.""" pass class CausalUnitBuffer: """Ring buffer that evicts whole causal units, never individual events.""" def __init__(self, max_units: int): self.max_units = max_units self._units: list[tuple[int, list[dict]]] = [] # (unit_id, events) self._next_unit_id = 1 def begin_unit(self) -> int: unit_id = self._next_unit_id self._next_unit_id += 1 self._units.append((unit_id, [])) while len(self._units) > self.max_units: self._units.pop(0) # evict oldest whole unit return unit_id def append(self, unit_id: int, event: dict) -> None: for uid, events in self._units: if uid == unit_id: events.append(event) return raise ResumptionBoundaryError( f"unit {unit_id} already evicted; refusing to append orphan event" ) def replay_from(self, last_event_id: int) -> list[dict]: # last_event_id is a unit_id (ids are assigned per-unit, not per-event) for idx, (uid, events) in enumerate(self._units): if uid == last_event_id: return [e for _, evs in self._units[idx + 1:] for e in evs] # last_event_id not found: it was evicted mid-unit or never existed raise ResumptionBoundaryError( f"Last-Event-ID {last_event_id} is not a live causal-unit boundary; " f"client must re-issue the originating tool call rather than replay" ) def handle_resume(buffer: CausalUnitBuffer, last_event_id: int) -> list[dict]: try: return buffer.replay_from(last_event_id) except ResumptionBoundaryError as exc: # Surface as 409 so the agent re-issues the tool call instead of # silently applying a partial unit to the wrong ledger slot. raise ResumptionBoundaryError( f"resumption point invalid: {exc}; do not replay, re-issue tool call" ) from exc ```