Skip to content

feat(McpAsyncClient): Handle ping request #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ public class McpAsyncClient {
// Request Handlers
Map<String, RequestHandler<?>> requestHandlers = new HashMap<>();

// Ping Request Handler
requestHandlers.put(McpSchema.METHOD_PING, pingRequestHandler());

// Roots List Request Handler
if (this.clientCapabilities.roots() != null) {
requestHandlers.put(McpSchema.METHOD_ROOTS_LIST, rootsListRequestHandler());
Expand Down Expand Up @@ -487,6 +490,13 @@ private RequestHandler<McpSchema.ListRootsResult> rootsListRequestHandler() {
};
}

// --------------------------
// Ping - The receiver MUST respond promptly with an empty response
// --------------------------
private RequestHandler<Object> pingRequestHandler() {
return params -> Mono.just(Map.of());
}

// --------------------------
// Sampling
// --------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,28 @@ void testSamplingCreateMessageRequestHandlingWithNullHandler() {
.hasMessage("Sampling handler must not be null when client capabilities include sampling");
}

@Test
void testPingMessageRequestHandling() {
MockMcpClientTransport transport = initializationEnabledTransport();

McpAsyncClient asyncMcpClient = McpClient.async(transport).build();

// Simulate incoming ping request from server
McpSchema.JSONRPCRequest pingRequest = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION,
McpSchema.METHOD_PING, "ping-id", null);
transport.simulateIncomingMessage(pingRequest);

// Verify response
McpSchema.JSONRPCMessage sentMessage = transport.getLastSentMessage();
assertThat(sentMessage).isInstanceOf(McpSchema.JSONRPCResponse.class);

McpSchema.JSONRPCResponse response = (McpSchema.JSONRPCResponse) sentMessage;
assertThat(response.id()).isEqualTo("ping-id");
assertThat(response.error()).isNull();
assertThat(response.result()).isInstanceOf(Map.class);
assertThat(((Map<?, ?>) response.result())).isEmpty();

asyncMcpClient.closeGracefully();
}

}