Skip to content

The gateway can keep its own streams alive, at a price worth stating - #94

Merged
blakebauman merged 1 commit into
mainfrom
feat/downstream-keepalive
Aug 26, 2026
Merged

blakebauman merged 1 commit into
mainfrom
feat/downstream-keepalive

Conversation

@blakebauman

Copy link
Copy Markdown
Member

server.keepAliveMs — fold pings each connected client so a long-lived stream keeps carrying bytes past an intermediary's idle timeout. Off by default.

MCP expects a client to ping if it wants a session kept alive. Reasonable advice to a server, awkward advice to a gateway: the idle timeout cutting the stream usually belongs to a load balancer the operator configured, and the gateway is frequently the only thing in that path they control.

Verified before writing anything: keepAliveMs: 60 → 6 pings in 400 ms; no Server section → 0.

The part that needs review: three consequences, two of which I initially got wrong

My first version of the documentation asserted the opposite of two of these. All three are measured.

1. It disconnects clients that decline the standalone SSE stream. A server-to-client ping needs that stream to land on, and the transport makes it a MAY, not a MUST. A POST-only client is correct, healthy, fully functional — and unreachable by a ping. Every tick fails immediately; measured, the session is closed after three ticks and the client's next call is answered session not found.

This is the one I'd weigh hardest. Turning on a keepalive should not disconnect conforming clients, and the SDK offers no way to condition the ping on a listening stream. The only levers are documentation and the default being off.

2. The interval must clear twice the slowest legitimate round trip. Each ping is given interval/2 to be answered (mcp/shared.go). A client slower than that burns a failure every tick and is closed on the third. My first draft said "longer than the round trip", which understates it by a factor of two.

3. It supersedes sessionIdleTimeoutMs for any client that answers. A ping is answered with a POST, and a POST is exactly what resets the idle timer — so fold's own keepalive keeps alive the session it is pinging. Measured: 250 ms idle timeout, 50 ms keepalive, client sends nothing after connecting → still alive and being pinged at 1.8 s.

My first draft said the two settings "are about different things, and enabling this does not disable that." It does. What actually changes is the criterion: reclamation moves from activity to liveness. A departed client is reclaimed faster (three misses vs. the whole timeout); a present-but-idle one is never reclaimed. That matters to anyone who set sessionIdleTimeoutMs to bound session-state growth against long-lived idle clients.

KeepAliveFailureThreshold: 3

Rather than the SDK's default of closing on the first unanswered ping. A gateway's clients sit behind whatever network the operator has, and the spec's guidance is that multiple failed pings may trigger a reset. Measurements support it — teardown lands at ~3 intervals in both failure modes, and a single blip cannot close a session.

Verification

  • go test -race ./... green (keepalive tests run 5× under -race, no flakes); lint 0 issues; gofmt / vet clean
  • conformance 40/40; bench 0.171 ms added p50
  • Off-by-default test is calibrated rather than slept: a control gateway at keepAliveMs: 25 runs alongside, and "zero pings" is only asserted once the control client has answered 4 — so the window is demonstrably capable of producing them
  • The keepalive is asserted to produce zero upstream pings — it must not fan out across the federation
  • Reload correctly rejects a document differing only in keepAliveMs, since server is construction-wired
  • Teardown test asserts a lower bound of 2 × interval, which a threshold of 1 could not satisfy

Known gaps

A keepalive teardown emits no audit event — there is no terminal response and no session-lifecycle event type, so a fleet-wide misconfiguration (interval below client RTT) would mass-disconnect clients leaving only SDK log lines. Worth considering if this sees real use. Also untested: a non-SDK client answering ping with method-not-found (the SDK silently stops the loop for that session), and the interaction with subscriptions/listen.

🤖 Generated with Claude Code

MCP expects a client to ping if it wants a session kept alive. That is
reasonable advice to a server and awkward advice to a gateway: the idle
timeout cutting the stream usually belongs to a load balancer the
operator configured, and the gateway is frequently the only thing in
that path they control. A cut stream was survivable — both ends
reconnect with backoff and Last-Event-ID — but it meant reconnect churn
and a window with no notifications delivered.

server.keepAliveMs sends the pings from fold's side. Absent it is off,
which is what fold has always done.

Three consequences, all measured rather than reasoned about, and two of
them change a working deployment. They are in the configuration guide
next to the field rather than left to be discovered, because the first
draft of that guide asserted the opposite of two.

It disconnects clients that decline the standalone SSE stream. A
server-to-client ping needs that stream to land on, the transport makes
it a MAY rather than a MUST, and a POST-only client is correct, healthy,
and unreachable by a ping — closed after three ticks, then answered
"session not found" on its next call.

The interval must clear twice the slowest legitimate round trip, not
once: each ping is given half the interval to be answered, so a slower
client burns a failure on every tick and is closed on the third.

And it supersedes sessionIdleTimeoutMs for any client that answers. A
ping is answered with a POST, and a POST is what resets the idle timer,
so fold's own keepalive keeps alive the session it is pinging — measured
at a 250 ms idle timeout against a 50 ms keepalive, still alive after
1.8 seconds having sent nothing since connecting. What changes is the
criterion rather than the setting: reclamation moves from activity to
liveness, so a client that has genuinely gone is reclaimed faster than
before, and one that is present but idle is not reclaimed at all.

Three consecutive misses are tolerated rather than the one the SDK
defaults to, because a gateway's clients sit behind whatever network an
operator has and the specification's guidance is that multiple failed
pings may trigger a reset.

ping is a method the 2026-07-28 revision removed, so this joins the list
of things correct for the era fold serves with no counterpart in the
next.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@blakebauman
blakebauman merged commit 4400e3f into main Aug 26, 2026
8 checks passed
@blakebauman
blakebauman deleted the feat/downstream-keepalive branch August 26, 2026 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant