Skip to content

Register ping handler on McpClient to support server-initiated pings#1391

Open
Copilot wants to merge 2 commits intomainfrom
copilot/fix-register-ping-handler
Open

Register ping handler on McpClient to support server-initiated pings#1391
Copilot wants to merge 2 commits intomainfrom
copilot/fix-register-ping-handler

Conversation

Copy link
Contributor

Copilot AI commented Feb 25, 2026

Per the MCP spec, ping can be initiated by either party. McpClientImpl never registered a handler for the ping method, causing server→client pings to fail with -32601 MethodNotFound.

Changes

  • McpClientImpl.RegisterHandlers: Unconditionally registers a ping request handler that returns an empty PingResult, matching the existing ConfigurePing() behavior on the server side.
  • McpClientTests: Adds ServerCanPingClient test that sends a ping from the server to the connected client and asserts a successful response.
Original prompt

Bug

Per the MCP specification, ping requests may be initiated by either party (client→server or server→client), and the receiver must respond with an empty {} result. See: https://modelcontextprotocol.io/specification/2025-03-26/basic/utilities/ping

Currently, server→client pings fail with -32601 MethodNotFound because McpClientImpl never registers a handler for the ping method.

Root Cause

In src/ModelContextProtocol.Core/Server/McpServerImpl.cs, the constructor correctly calls ConfigurePing() (around line 96), which registers a handler via SetHandler(RequestMethods.Ping, ...).

In src/ModelContextProtocol.Core/Client/McpClientImpl.cs, the constructor calls RegisterHandlers() which only registers handlers for sampling/createMessage, roots/list, elicitation/create, and tasks/* methods. There is no ConfigurePing() call and no ping handler registered for the client side.

Fix

Register a ping request handler in McpClientImpl so that incoming ping requests from the server are automatically responded to, matching the behavior already present on the server side. The handler should be unconditionally registered (just like on the server side) — it simply returns a new PingResult.

In McpClientImpl.RegisterHandlers (or the constructor), add something equivalent to:

requestHandlers.Set(
    RequestMethods.Ping,
    (request, _, cancellationToken) => new ValueTask<PingResult>(new PingResult()),
    McpJsonUtilities.JsonContext.Default.JsonNode,
    McpJsonUtilities.JsonContext.Default.PingResult);

Please also add a test that verifies a server-initiated ping to the client succeeds (i.e., returns an empty result rather than throwing -32601).

Reference

This pull request was created from Copilot chat.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix missing ping request handler in McpClientImpl Register ping handler on McpClient to support server-initiated pings Feb 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ping from McpServer -> McpClient is not handled by McpClient.

2 participants