Skip to content

Bug Report-2: Missing Periodic Ping Implementation #1000

@younaman

Description

@younaman

Summary

The MCP TypeScript SDK implements a basic ping method but lacks the periodic ping functionality required by the MCP specification. The current implementation only provides manual ping capability, missing the automatic periodic ping feature specified in the protocol.

Problem Description

The current ping implementation only provides a manual ping method but does not implement the periodic ping functionality specified in the MCP protocol.

Current Implementation

Location: src/client/index.ts:332-334

async ping(options?: RequestOptions) {
  return this.request({ method: "ping" }, EmptyResultSchema, options);
}

Protocol Requirements

According to the MCP specification:

  • "Implementations SHOULD periodically issue pings to detect connection health"
  • "The frequency of pings SHOULD be configurable"

Missing Features

  1. Periodic Ping: No automatic periodic ping functionality
  2. Configurable Frequency: No way to configure ping interval

Impact

  • Connection health cannot be automatically monitored
  • Undetected connection failures may occur
  • Non-compliance with MCP protocol specification

Proposed Solution

Implement periodic ping functionality with configurable frequency:

export interface PingConfig {
  interval: number;  // ping interval in milliseconds
  enabled: boolean;  // whether to enable periodic pings
}

export interface ClientOptions extends ProtocolOptions {
  capabilities?: ClientCapabilities;
  ping?: PingConfig;
}

// Enhanced Client class
export class Client {
  private _pingInterval?: NodeJS.Timeout;
  private _pingConfig: PingConfig;

  constructor(
    private _clientInfo: Implementation,
    options?: ClientOptions,
  ) {
    super(options);
    this._pingConfig = {
      interval: options?.ping?.interval ?? 30000, // 30 seconds default
      enabled: options?.ping?.enabled ?? true,   // enabled by default
    };
  }

  // Start periodic ping when connected
  private startPeriodicPing(): void {
    if (!this._pingConfig.enabled || this._pingInterval) {
      return;
    }

    this._pingInterval = setInterval(async () => {
      try {
        await this.ping();
      } catch (error) {
        // Handle ping failure
        console.warn(`Periodic ping failed: ${error.message}`);
      }
    }, this._pingConfig.interval);
  }

  // Stop periodic ping when disconnected
  private stopPeriodicPing(): void {
    if (this._pingInterval) {
      clearInterval(this._pingInterval);
      this._pingInterval = undefined;
    }
  }

  // Override connect to start ping
  override async connect(transport: Transport, options?: RequestOptions): Promise<void> {
    await super.connect(transport, options);
    this.startPeriodicPing();
  }

  // Override close to stop ping
  override async close(): Promise<void> {
    this.stopPeriodicPing();
    await super.close();
  }
}

Requested Action

Implement the missing periodic ping functionality as specified in the MCP protocol, including:

  1. Automatic periodic ping scheduling
  2. Configurable ping frequency
  3. Proper lifecycle management (start with connection, stop with disconnection)

Labels

  • enhancement
  • protocol-compliance
  • ping

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions