Back to Community & Earn Hub

MCP TypeScript SDK SSE Connection Drops Behind NGINX/Cloudflare: How to configure keepalive heartbeats and session rehydration without losing in-flight tool state?

@nikhil_gupta9 hours ago 1437 views

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):

  1. No SSE frames are emitted on the stream while the tool awaits completion.
  2. At 60 seconds (NGINX default proxy_read_timeout) or 100 seconds (Cloudflare's hard proxy read timeout), the proxy terminates the TCP connection with 504 Gateway Timeout or silent FIN.
  3. Because SSEServerTransport stores active transports in an in-memory Map<sessionId, transport>, the client's reconnect opens a brand new sessionId and the in-flight tool result is lost forever!
typescript
// Current naive in-memory transport map
const transports = new Map<string, SSEServerTransport>();
  1. What NGINX/Cloudflare headers and SSE heartbeat comments are needed to prevent proxy idle kills?
  2. How do you decouple long-running MCP tool state from the ephemeral SSE socket so reconnects resume cleanly?
1 Answers

Community Answers

@priya_sharma Accepted Answer9 hours ago

Cloudflare's 100-second read timeout and NGINX's default response buffering are notorious for killing MCP SSE streams. To make remote MCP bulletproof in production, you need two fixes: keepalive heartbeats at the proxy/transport layer, and decoupling tool execution from the SSE socket via Redis.


1. Disable Buffering & Send 15-Second SSE Comment Heartbeats

In NGINX, disable proxy buffering and extend timeouts for your /sse endpoint:

nginx
location /sse {
    proxy_pass http://mcp_backend;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_buffering off;
    proxy_cache off;
    chunked_transfer_encoding off;
    proxy_read_timeout 3600s;
    add_header X-Accel-Buffering no;
}

On the Node/TypeScript MCP server, write a standard SSE comment (`: keepalive

) every **15 seconds** on the raw ServerResponse. Per the W3C EventSource spec, lines starting with :` are ignored by the JSON-RPC parser while resetting Cloudflare's idle timer:

typescript
const heartbeat = setInterval(() => {
  if (!res.writableEnded) {
    res.write(": keepalive

");
  }
}, 15_000);

res.on("close", () => clearInterval(heartbeat));

2. Rehydrate Sessions with Redis + Last-Event-ID

Even with heartbeats, mobile laptops sleep and Wi-Fi switches drop sockets. Never tie long-running tool execution solely to an in-memory Map:

  1. Assign sequential id: fields to every outbound SSE JSON-RPC message and buffer them in a Redis Stream (mcp:session:{sessionId}) with a 15-minute TTL.
  2. Run tools asynchronously so if the socket drops mid-scan, the worker still writes the completed JSON-RPC response into Redis.
  3. Replay on Reconnect: When the client reconnects with Last-Event-ID (or ?sessionId=...), replay all missed events from Redis before resuming live streaming.

This guarantees zero lost tool results across NGINX reloads, Cloudflare resets, and client network hops.

Have a solution or working configuration?

Sign in or create a free account to answer this question (+10 pts, +25 pts if accepted) and tag helpful AIStoreHub repositories.