When must an instance treat an MCP SSE event-ID replay as a session-integrity violation rather than a resumable continuation

**Date of Record:** 2026 December 2 **Scope:** Applies to MCP clients speaking HTTP-native transport (Streamable HTTP with `text/event-stream` responses and `Last-Event-ID` resumption) against remote tool servers, where the client maintains a logical session across reconnects. Does not apply to stdio transport, nor to stateless one-shot HTTP request/response tool servers that emit no event IDs.

Core Distinction

The MCP Streamable HTTP transport allows a client to resume a broken `text/event-stream` by reconnecting with `Last-Event-ID: <n>`, expecting the server to replay events `n+1, n+2, …` and then continue live. This is modeled as *idempotent catch-up*: the client assumes the event ID is a monotonic per-session cursor and that replay is a pure continuation of the same logical stream.

The non-obvious boundary is this: **event-ID monotonicity is a per-stream property, not a per-session property.** A remote server that fans tool execution across workers, restarts a stream after a worker recycle, or multiplexes multiple logical sub-streams into one session can legitimately emit an event ID that is *numerically greater* than the client's cursor while being *semantically disjoint* from the events the client already consumed. Resuming across that seam is not continuation — it is splicing two different causal histories into one agent context. The distinction that matters operationally: a replay whose IDs are monotonic but whose *session epoch* changed is a session-integrity violation, not a resumable gap.

What Was Observed

An agent driving a long-running remote tool (a multi-step repository indexer exposed over MCP HTTP) held a session cursor at `Last-Event-ID: 4412`. The SSE connection dropped during a `tools/call` whose progress notifications were being streamed. The client reconnected with `Last-Event-ID: 4412` per spec.

The server had, in the interim, recycled the worker backing the session and replayed from its new worker's local counter, which had restarted at `5000`. The first replayed frame was `id: 5003`, a `notifications/progress` claiming `progress: 60, total: 100` for the *same* tool call token. The client — correctly per the letter of resumption semantics — accepted `5003 > 4412` as forward progress and resumed appending to its event log.

The actual state: events `4413–5002` never existed on the new worker; the `progress: 60` frame was the *new* worker re-executing the tool from scratch and reaching 60% a second time. The agent's reconstructed context now contained two interleaved progress ladders (the pre-drop 0→? and the post-reconnect 0→60) for one call token, and the terminal `result` frame that eventually arrived at `id: 5041` described the *second* execution's output. The agent had, in effect, two partial executions fused into one context with no marker of the seam.

Downstream, a summarization step distilled "the indexer ran twice and produced divergent counts" as a semantic fact, poisoning episodic memory with a phantom double-execution.

Condition of Validity

The `Last-Event-ID` resumption contract is valid **only** when the client can independently verify that the resuming stream belongs to the same *session epoch* as the cursor it is presenting. Concretely, resumption is safe iff all of the following hold:

  1. The server exposes a session/epoch identifier (e.g., an `Mcp-Session-Id` plus a server-generated epoch or generation counter) that is stable across reconnects and differs on worker recycle.
  2. The event-ID space is scoped to that epoch — i.e., the server guarantees IDs are never reused or reset *within* an epoch, and a new epoch starts a fresh ID space that the client is told about out-of-band (header or first frame), not inferred from ID magnitude.
  3. The client validates the epoch before consuming any replayed frame, and on epoch mismatch discards the cursor and re-establishes session state rather than resuming.

Absent (1)–(3), the *only* safe client behavior on reconnect is to treat the stream as new: drop the cursor, re-issue any in-flight `tools/call` idempotently (with a fresh request token), and reconcile against server-side truth. Monotonicity of the integer is not evidence of continuity; it is an artifact of counter implementation.

Decision Boundary

The decision the client must make on every reconnect:

The trap is treating the third case as the first. A monotonic ID plus a plausible-looking progress frame is exactly the shape of a benign resume *and* the shape of a cross-epoch splice. The discriminator is not the ID — it is whether the payload is consistent with the client's own record of what it has already consumed in this epoch.

Operational Effect

Enforcing the epoch check converts a silent, memory-poisoning failure (fused execution histories distilled into false semantic facts) into a loud, recoverable one (a re-established session with an idempotent re-issue). The cost is that the client must maintain a small, durable per-session record — epoch token, last consumed event ID, set of in-flight call tokens with their terminal-status — that survives the reconnect and is consulted *before* any replayed frame is appended to context. Without that record, the client has no basis to distinguish continuation from splice, and the transport's resumption affordance becomes a mechanism for injecting contradictory state into the agent's context window under the appearance of normal operation.

The invariant to hold: **an event ID is a cursor within an epoch, never across one; a replay is trusted only to the extent the client can prove it is the same epoch it left.**

from dataclasses import dataclass, field
from typing import Optional


class SessionIntegrityViolation(RuntimeError):
    """Raised when a replayed MCP SSE frame claims continuity but contradicts
    already-consumed session state (cross-epoch splice or corrupted cursor)."""
    pass


@dataclass
class SessionRecord:
    epoch: str
    last_event_id: int
    consumed_call_tokens: set[str] = field(default_factory=set)
    terminal_call_tokens: set[str] = field(default_factory=set)


def resume_or_reestablish(
    record: SessionRecord,
    server_epoch: str,
    first_replayed_id: int,
    frame: dict,
) -> str:
    """Decide how to handle a reconnect. Returns 'resume' | 'reestablish'.
    Raises SessionIntegrityViolation when the frame claims continuity but
    contradicts the client's record of this epoch."""
    if server_epoch != record.epoch:
        # Different causal history; the cursor is meaningless here.
        return "reestablish"

    if first_replayed_id != record.last_event_id + 1:
        # Gap the server cannot account for within the same epoch.
        return "reestablish"

    token = frame.get("params", {}).get("progressToken")
    method = frame.get("method", "")

    if method == "notifications/progress" and token in record.terminal_call_tokens:
        raise SessionIntegrityViolation(
            f"progress frame for token {token!r} after its terminal result "
            f"in epoch {record.epoch!r}; replay claims continuity but "
            f"contradicts consumed state (likely cross-epoch splice)"
        )

    if method == "notifications/progress" and token not in record.consumed_call_tokens:
        raise SessionIntegrityViolation(
            f"progress frame for token {token!r} never issued in epoch "
            f"{record.epoch!r}; cursor or epoch token is corrupt"
        )

    return "resume"