Skip to content

feat(client): add periodic ping support for connection health monitoring#1645

Open
Br1an67 wants to merge 3 commits intomodelcontextprotocol:mainfrom
Br1an67:fix/issue-1000-periodic-ping
Open

feat(client): add periodic ping support for connection health monitoring#1645
Br1an67 wants to merge 3 commits intomodelcontextprotocol:mainfrom
Br1an67:fix/issue-1000-periodic-ping

Conversation

@Br1an67
Copy link

@Br1an67 Br1an67 commented Mar 8, 2026

Fixes #1000

Summary

This PR implements the missing periodic ping functionality for the MCP TypeScript SDK Client, as specified in the MCP protocol. The implementation allows automatic connection health monitoring by sending ping requests at a configurable interval.

Changes

  • Added PingConfig interface to configure periodic ping options
  • Added ping configuration option to ClientOptions
  • Implemented _startPeriodicPing() method to begin periodic ping after connection
  • Implemented _stopPeriodicPing() method to stop periodic ping on close
  • Override close() method to ensure periodic ping is stopped when disconnecting
  • Default configuration: disabled (opt-in), 30 second interval when enabled
  • Ping failures are reported via the onerror callback without stopping the interval

Usage

const client = new Client(
    { name: 'my-client', version: '1.0.0' },
    {
        ping: {
            enabled: true,
            intervalMs: 60000 // Ping every 60 seconds
        }
    }
);

// Handle ping errors
client.onerror = (error) => {
    if (error.message.includes('Periodic ping failed')) {
        console.warn('Connection health check failed:', error);
    }
};

await client.connect(transport);
// Periodic pings will start automatically after connection

Testing

Added comprehensive test suite covering:

  • Periodic ping disabled by default
  • Periodic ping starts when enabled after connection
  • Periodic ping stops on client close
  • Custom interval configuration
  • Graceful error handling with onerror callback
  • Configuration defaults

Diff Stats

 packages/client/src/client/client.ts     |  90 +++++++++++++
 packages/client/test/client/ping.test.ts | 214 +++++++++++++++++++++++++++++++
 2 files changed, 304 insertions(+)

Add configurable periodic ping functionality to the Client class as specified
in the MCP protocol. This allows automatic connection health monitoring by
sending ping requests at a configurable interval.

Changes:
- Add PingConfig interface with enabled and intervalMs options
- Add ping configuration option to ClientOptions
- Start periodic ping after successful connection initialization
- Stop periodic ping on client close
- Emit errors via onerror callback when ping fails
- Default to disabled (opt-in feature)
- Default interval of 30 seconds when enabled

The implementation follows the MCP specification recommendation that
implementations SHOULD periodically issue pings to detect connection health.
@Br1an67 Br1an67 requested a review from a team as a code owner March 8, 2026 14:11
@changeset-bot
Copy link

changeset-bot bot commented Mar 8, 2026

⚠️ No Changeset found

Latest commit: 5d290c3

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 8, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1645

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1645

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1645

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1645

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1645

commit: f00103b

Br1an67 and others added 2 commits March 8, 2026 16:41
…nding

The MockTransport class in ping.test.ts was not responding to initialize
and ping requests, causing client.connect() to hang indefinitely and
tests to timeout. Updated MockTransport.send() to properly respond to
these requests with appropriate JSON-RPC responses.
…fault

Add underscore separator to 30000 -> 30_000 for better readability
and to satisfy unicorn/numeric-separators-style lint rule.

Also apply prettier formatting to ping.test.ts.

Co-Authored-By: Claude <noreply@anthropic.com>
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.

Bug Report-2: Missing Periodic Ping Implementation

1 participant