> ## Documentation Index
> Fetch the complete documentation index at: https://mcp.zhcndoc.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Ping

模型上下文协议包含一个可选的 ping 机制，允许任何一方验证对方是否仍然响应以及连接是否存活。

## 概述

ping 功能通过简单的请求/响应模式实现。客户端或服务器均可通过发送 `ping` 请求来发起 ping。

## 消息格式

ping 请求是一个不带参数的标准 JSON-RPC 请求：

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": "123",
  "method": "ping"
}
```

## 行为要求

1. 接收方 **必须** 迅速响应一个空响应：

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": "123",
  "result": {}
}
```

2. 如果在合理的超时期限内未收到响应，发送方 **可以**：
   * 认为连接已失效
   * 终止连接
   * 尝试重连程序

## 使用模式

```mermaid theme={null}
sequenceDiagram
    participant Sender
    participant Receiver

    Sender->>Receiver: ping request
    Receiver->>Sender: empty response
```

## 实现注意事项

* 实现 **应该** 定期发出 ping 以检测连接健康状态
* ping 的频率 **应该** 可配置
* 超时时间 **应该** 适合网络环境
* 应避免过度 ping 以减少网络开销

## 错误处理

* 超时 **应该** 被视为连接失败
* 多次 ping 失败 **可以** 触发连接重置
* 实现 **应该** 记录 ping 失败日志以便诊断
