What happens when an instance replays a Streamable HTTP MCP tool call across a reconnect and the server has no idempotency key to deduplicate it

**Date of Record:** 2026 December 2 **Scope:** Applies to MCP clients using the Streamable HTTP transport with resumable SSE streams and `Last-Event-ID` replay, where tool calls carry side effects (writes, sends, payments) and the server-side tool handler is not itself idempotent. Does not apply to stateless request/response transports without replay, nor to tool calls that are provably read-only.

Core Distinction

The failure is not "the connection dropped." Connections drop constantly and the transport is *designed* to survive that. The failure is the mismatch between two independent guarantees that operators routinely conflate:

  1. **Transport-level at-least-once delivery.** Streamable HTTP with `Last-Event-ID` resume guarantees that an event the client never acknowledged will be redelivered. This is a *delivery* invariant.
  2. **Tool-level exactly-once effect.** A tool call that charges a card or appends a row has an *effect* invariant that must be enforced by the tool itself, typically via an idempotency key.

The transport can only promise at-least-once. When the agent runtime treats "the stream reconnected and replayed my request" as "my request will be applied once," it silently upgrades an at-least-once channel into an assumed exactly-once one. The gap between those two guarantees is where duplicate side effects live.

The non-obvious part: the duplicate is *not* caused by the client retrying. It is caused by the client **correctly** honoring the replay protocol. The agent is behaving exactly as specified, and that correctness is what produces the double-charge.

What Was Observed

A write-capable MCP server exposed a `create_ticket` tool. The client (an agent runtime) issued `tools/call` as a POST that opened an SSE stream. The server executed the tool, emitted a `progress` event, then a `result` event with the new ticket ID, and the client consumed the result and emitted `Last-Event-ID: 42` on the next request.

The break sequence:

The observable signature is subtle: the agent's transcript shows a single successful tool call. The system of record shows two rows. There is no error, no retry log line, no exception — because from the client's perspective nothing failed. The only evidence is the duplicate side effect in the downstream system, discovered later by a human reconciling counts.

A second variant: the server *did* persist execution state, but keyed it on the SSE event id rather than on a client-supplied idempotency key. On resume it correctly replayed `result` for `42` — but a *different* client instance (the agent had been checkpointed and cold-started on another node) issued a fresh `tools/call` with no `Last-Event-ID` at all, because the checkpoint did not carry the stream cursor. Same double effect, opposite cause: the first was replay-without-state, the second was state-without-cursor.

Condition of Validity

The invariant only holds — that is, exactly-once tool effects survive reconnect — when **all** of the following are true simultaneously:

If any one is missing, the system degrades to at-least-once effects. That is not a bug in the transport — it is the transport working as specified against a tool that assumed a stronger guarantee than it was given.

Decision Boundary

The boundary the agent runtime must decide, on every reconnect, is: **do I have enough durable state to make this replay safe, or must I refuse and escalate?**

The wrong default — and the one most runtimes ship — is "reconnect and resend." That default is correct for read-only tools and catastrophically wrong for write tools. The boundary is per-tool, not per-transport.

Operational Effect

Without the guardrail, the operational effect is silent duplication proportional to reconnect frequency: every mid-stream failure during a side-effecting call produces one extra effect. Under flaky networks or rolling deploys this compounds, and because each duplicate looks like a clean success in the agent transcript, it is invisible to observability built on agent logs. Reconciliation cost lands on downstream systems and humans.

With the guardrail — durable idempotency key + atomic dedup + stored result + cursor/key co-checkpointing — the transport's at-least-once delivery becomes exactly-once *effect* at the tool layer, and reconnect replay becomes safe by construction. The agent's replay path must be explicitly gated on the presence of a verified key, and must fail closed (raise) rather than fail open (resend) when the key or the server's dedup capability is in doubt.

import hashlib
from dataclasses import dataclass, field
from typing import Optional


class ReplaySafetyError(RuntimeError):
    """Raised when a side-effecting MCP tool call cannot be safely replayed."""
    pass


@dataclass
class ToolCallState:
    logical_call_id: str
    tool_name: str
    idempotency_key: Optional[str]
    last_event_id: Optional[int]
    side_effecting: bool
    server_dedups_on_key: bool
    result: Optional[str] = None


def make_idempotency_key(logical_call_id: str, tool_name: str, args: str) -> str:
    # Deterministic across checkpoint/resume: never regenerate on retry.
    h = hashlib.sha256(f"{logical_call_id}:{tool_name}:{args}".encode()).hexdigest()
    return f"idem-{h[:32]}"


def plan_reconnect(state: ToolCallState) -> str:
    """Decide whether a reconnect may resend, must reconcile, or must refuse."""
    if not state.side_effecting:
        return "resend_safe"  # read-only: at-least-once is harmless

    if state.result is not None:
        return "already_applied"  # stored result; do not re-execute

    if state.idempotency_key is None:
        # Cursor may exist, but we cannot bind the call to a dedup key.
        raise ReplaySafetyError(
            f"call {state.logical_call_id!r} to {state.tool_name!r} is side-effecting "
            f"but has no persisted idempotency key; outcome unknown, reconcile required"
        )

    if not state.server_dedups_on_key:
        raise ReplaySafetyError(
            f"server does not dedup on idempotency key for {state.tool_name!r}; "
            f"replaying {state.logical_call_id!r} risks duplicate effect"
        )

    if state.last_event_id is None:
        # Key present, cursor lost: safe to resend *with the same key* only.
        return "resend_with_key"

    return "resend_with_key"


def resume_call(state: ToolCallState) -> str:
    action = plan_reconnect(state)
    if action == "already_applied":
        return state.result
    if action == "resend_safe":
        return "resend_read_only"
    if action == "resend_with_key":
        return f"resend:{state.idempotency_key}"
    raise ReplaySafetyError(f"unhandled replay action {action!r}")


# Example: cold-start with cursor but no key on a write tool -> fail closed.
bad = ToolCallState(
    logical_call_id="call-7f3a",
    tool_name="create_ticket",
    idempotency_key=None,
    last_event_id=41,
    side_effecting=True,
    server_dedups_on_key=True,
)
try:
    resume_call(bad)
except ReplaySafetyError as e:
    print(f"refused: {e}")