Skip to content

Commit 39b426e

Browse files
committed
feat(McpAsyncClient): handle ping request
1 parent 9be29fe commit 39b426e

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

mcp/src/main/java/io/modelcontextprotocol/client/McpAsyncClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ public class McpAsyncClient {
175175
// Request Handlers
176176
Map<String, RequestHandler<?>> requestHandlers = new HashMap<>();
177177

178+
// Ping Request Handler
179+
requestHandlers.put(McpSchema.METHOD_PING, pingRequestHandler());
180+
178181
// Roots List Request Handler
179182
if (this.clientCapabilities.roots() != null) {
180183
requestHandlers.put(McpSchema.METHOD_ROOTS_LIST, rootsListRequestHandler());
@@ -486,6 +489,10 @@ private RequestHandler<McpSchema.ListRootsResult> rootsListRequestHandler() {
486489
return Mono.just(new McpSchema.ListRootsResult(roots));
487490
};
488491
}
492+
493+
private RequestHandler<Object> pingRequestHandler() {
494+
return params -> Mono.just(Map.of());
495+
}
489496

490497
// --------------------------
491498
// Sampling

mcp/src/test/java/io/modelcontextprotocol/client/McpAsyncClientResponseHandlerTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,32 @@ void testSamplingCreateMessageRequestHandlingWithNullHandler() {
349349
.hasMessage("Sampling handler must not be null when client capabilities include sampling");
350350
}
351351

352+
@Test
353+
void testPingMessageRequestHandling() {
354+
MockMcpClientTransport transport = initializationEnabledTransport();
355+
356+
McpAsyncClient asyncMcpClient = McpClient.async(transport).build();
357+
358+
// Simulate incoming ping request from server
359+
McpSchema.JSONRPCRequest pingRequest = new McpSchema.JSONRPCRequest(
360+
McpSchema.JSONRPC_VERSION,
361+
McpSchema.METHOD_PING,
362+
"ping-id",
363+
null
364+
);
365+
transport.simulateIncomingMessage(pingRequest);
366+
367+
// Verify response
368+
McpSchema.JSONRPCMessage sentMessage = transport.getLastSentMessage();
369+
assertThat(sentMessage).isInstanceOf(McpSchema.JSONRPCResponse.class);
370+
371+
McpSchema.JSONRPCResponse response = (McpSchema.JSONRPCResponse) sentMessage;
372+
assertThat(response.id()).isEqualTo("ping-id");
373+
assertThat(response.error()).isNull();
374+
assertThat(response.result()).isInstanceOf(Map.class);
375+
assertThat(((Map<?, ?>) response.result())).isEmpty();
376+
377+
asyncMcpClient.closeGracefully();
378+
}
379+
352380
}

0 commit comments

Comments
 (0)