Skip to content

Commit 1cf6deb

Browse files
authored
docs: document the ping utility with examples (#1106)
Add a Ping section to the README covering both directions of the utility: constructing and sending a PingRequest via send_request, and the automatic default handler response (with an override example for custom liveness logic). Links to the MCP ping spec. Ping was the only non-experimental feature lacking user-facing documentation and an example.
1 parent 00bcf13 commit 1cf6deb

1 file changed

Lines changed: 49 additions & 0 deletions

File tree

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,55 @@ impl ServerHandler for MyServer {
11511151
}
11521152
```
11531153

1154+
### Ping
1155+
1156+
Either side can send a `ping` request to check that its counterpart is still
1157+
responsive and the connection is alive. A ping carries no parameters and the
1158+
receiver replies with an empty result. Because pings can flow in both
1159+
directions, `rmcp` handles them symmetrically:
1160+
1161+
- **Sending a ping** — construct a `PingRequest` and send it over the peer.
1162+
A client pings the server with `ClientRequest::PingRequest`; a server pings
1163+
the client with `ServerRequest::PingRequest`. `send_request` resolves once the
1164+
empty response arrives, so a returned `Ok` confirms the peer is reachable:
1165+
1166+
```rust
1167+
use rmcp::model::{PingRequest, ServerRequest};
1168+
1169+
// From a server, ping the connected client to verify it is still alive.
1170+
context.peer
1171+
.send_request(ServerRequest::PingRequest(PingRequest::default()))
1172+
.await?;
1173+
```
1174+
1175+
```rust
1176+
use rmcp::model::{ClientRequest, PingRequest};
1177+
1178+
// From a client, ping the server. `running` is the value returned by serve().
1179+
running
1180+
.send_request(ClientRequest::PingRequest(PingRequest::default()))
1181+
.await?;
1182+
```
1183+
1184+
- **Responding to a ping**`rmcp` answers incoming pings automatically. The
1185+
default `ping` method on `ServerHandler` and `ClientHandler` returns an empty
1186+
result, so no code is required. Override it only if you want to run custom
1187+
logic (for example, health checks) when a ping arrives:
1188+
1189+
```rust
1190+
impl ServerHandler for MyServer {
1191+
async fn ping(
1192+
&self,
1193+
_context: RequestContext<RoleServer>,
1194+
) -> Result<(), McpError> {
1195+
// Custom liveness logic here, if any.
1196+
Ok(())
1197+
}
1198+
}
1199+
```
1200+
1201+
**MCP Spec:** [Ping](https://modelcontextprotocol.io/specification/2025-11-25/basic/utilities/ping)
1202+
11541203
### Initialized notification
11551204

11561205
Legacy clients send `initialized` after the `initialize` handshake completes.

0 commit comments

Comments
 (0)