MCP TypeScript SDK SSE Connection Drops Behind NGINX/Cloudflare: How to configure keepalive heartbeats and session rehydration without losing in-flight tool state?
We deployed a remote MCP server using @modelcontextprotocol/sdk's SSEServerTransport behind an NGINX reverse proxy and Cloudflare CDN.
The Failure Pattern
When a client connects to GET /sse and invokes a slow tool (e.g., running a 90-second repository security scan or large BigQuery export):
- No SSE frames are emitted on the stream while the tool awaits completion.
- At 60 seconds (NGINX default
proxy_read_timeout) or 100 seconds (Cloudflare's hard proxy read timeout), the proxy terminates the TCP connection with504 Gateway Timeoutor silentFIN. - Because
SSEServerTransportstores active transports in an in-memoryMap<sessionId, transport>, the client's reconnect opens a brand newsessionIdand the in-flight tool result is lost forever!
typescript// Current naive in-memory transport map const transports = new Map<string, SSEServerTransport>();
- What NGINX/Cloudflare headers and SSE heartbeat comments are needed to prevent proxy idle kills?
- How do you decouple long-running MCP tool state from the ephemeral SSE socket so reconnects resume cleanly?