Repository navigation
The gateway can keep its own streams alive, at a price worth stating - #94
Merged
Merged
Conversation
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]>
6 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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; noServersection → 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 answeredsession 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/2to 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
sessionIdleTimeoutMsfor any client that answers. A ping is answered with aPOST, and aPOSTis 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
sessionIdleTimeoutMsto bound session-state growth against long-lived idle clients.KeepAliveFailureThreshold: 3Rather 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 cleankeepAliveMs: 25runs alongside, and "zero pings" is only asserted once the control client has answered 4 — so the window is demonstrably capable of producing themkeepAliveMs, sinceserveris construction-wired2 × interval, which a threshold of 1 could not satisfyKnown 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
pingwith method-not-found (the SDK silently stops the loop for that session), and the interaction withsubscriptions/listen.🤖 Generated with Claude Code