feat(core): add opt-in periodic ping for connection health monitoring#1717
feat(core): add opt-in periodic ping for connection health monitoring#1717travisbreaks wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
🦋 Changeset detectedLatest commit: 810c72e The changes in this PR will be included in the next version bump. This PR includes changesets to release 6 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
51a9eae to
b41b17e
Compare
|
@claude review |
Implements the missing periodic ping functionality specified in issue modelcontextprotocol#1000. Per the MCP specification, implementations SHOULD periodically issue pings to detect connection health, with configurable frequency. Changes: - Add `pingIntervalMs` option to `ProtocolOptions` (disabled by default) - Implement `startPeriodicPing()` and `stopPeriodicPing()` in Protocol - Client starts periodic ping after successful initialization - Server starts periodic ping after receiving initialized notification - Timer uses `unref()` so it does not prevent clean process exit - Ping failures are reported via `onerror` without stopping the timer - Timer is automatically cleaned up on close or unexpected disconnect Fixes modelcontextprotocol#1000 Co-authored-by: Br1an67 <932039080@qq.com>
b41b17e to
9e1b5da
Compare
Mirrors the Go SDK pattern where the initialized notification handler is a named method rather than an inline closure.
|
@claude review |
|
|
||
| // Start periodic ping after successful initialization | ||
| this.startPeriodicPing(); |
There was a problem hiding this comment.
🔴 Bug: Periodic pings are not restarted after client reconnection. The reconnection path (line 497-501) returns early when transport.sessionId is set, skipping the startPeriodicPing() call at line 546. Since _onclose() clears the ping timer during disconnection, connection health monitoring silently stops working after any reconnect cycle. Fix: add this.startPeriodicPing() before the return at line 501.
Extended reasoning...
What the bug is
In Client.connect(), there are two code paths: the initial connection path (which performs the full MCP initialization handshake) and the reconnection path (when transport.sessionId is already set). The startPeriodicPing() call at line 546 is only reached via the initial connection path. The reconnection path at lines 497-501 returns early, skipping it entirely.
How it manifests
When a client disconnects and reconnects (e.g., due to a transient network failure with an HTTP transport), the following sequence occurs:
- The old transport closes, triggering
_onclose()inProtocol, which callsstopPeriodicPing()— this clears_pingTimerviaclearInterval. - The application calls
client.connect(newTransport)wherenewTransport.sessionIdis already set (since it is a reconnection to an existing session). super.connect(transport)is called, setting up the new transport.- The guard
transport.sessionId \!== undefinedevaluates totrue, so the method sets the protocol version and returns at line 501. startPeriodicPing()at line 546 is never reached.- Since
_pingTimerwas cleared in step 1 and_pingIntervalMsis still set from construction,startPeriodicPing()would successfully start a new timer — but it is simply never called.
Why existing code does not prevent it
The startPeriodicPing() method has a guard if (this._pingTimer || \!this._pingIntervalMs) that prevents duplicate timers, but after _onclose() runs, _pingTimer is undefined, so the guard would allow the timer to be started. The problem is purely that the method is never invoked in the reconnection path. Neither super.connect() (i.e., Protocol.connect()) nor any other part of the reconnection flow calls it.
Impact
Any client that uses pingIntervalMs for connection health monitoring will silently lose that monitoring after the first reconnection. This is particularly problematic because reconnections are exactly the scenario where health monitoring is most valuable — the connection was already unstable once, and the user wants to know if it becomes unresponsive again. The client will believe pings are active (since _pingIntervalMs is still set), but no pings will be sent.
Fix
Add this.startPeriodicPing() to the reconnection branch in Client.connect(), just before the return statement at line 501:
if (transport.sessionId \!== undefined) {
if (this._negotiatedProtocolVersion \!== undefined && transport.setProtocolVersion) {
transport.setProtocolVersion(this._negotiatedProtocolVersion);
}
this.startPeriodicPing();
return;
}The idempotency guard in startPeriodicPing() ensures this is safe even if called multiple times.
Summary
pingIntervalMsoption toProtocolOptionsthat enables automatic periodic pings to verify the remote side is still responsiveinitializednotificationonerrorwithout stopping the timerunref()so it does not prevent clean Node.js process exitPer the MCP specification: "Implementations SHOULD periodically issue pings to detect connection health" with "configurable frequency."
Usage
Key design decisions
pingIntervalMsto enableunref()on the timer: the periodic ping does not keep the Node.js process alive if all other work is doneRelationship to other PRs
This complements PR #1712 (host process watchdog for stdio orphan detection). That PR detects when the host process is gone at the OS level; this PR detects when the remote MCP peer is unresponsive at the protocol level.
Fixes #1000
Test plan
should not send periodic pings when pingIntervalMs is not set(default behavior unchanged)should send periodic pings when pingIntervalMs is set and startPeriodicPing is calledshould stop periodic pings on closeshould report ping errors via onerror without stopping the timershould not start duplicate timers if startPeriodicPing is called multiple timesshould stop periodic pings when transport closes unexpectedlyChangeset
@modelcontextprotocol/core: minor,@modelcontextprotocol/client: minor,@modelcontextprotocol/server: minorCo-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com