Skip to content

Commit c1bd65f

Browse files
Copilotstephentoub
andcommitted
Register ping handler in McpClientImpl so server-initiated pings succeed
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
1 parent b4037bf commit c1bd65f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/ModelContextProtocol.Core/Client/McpClientImpl.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ private void RegisterHandlers(McpClientOptions options, NotificationHandlers not
8585
notificationHandlers.RegisterRange(notificationHandlersFromOptions);
8686
}
8787

88+
requestHandlers.Set(
89+
RequestMethods.Ping,
90+
(request, _, cancellationToken) => new ValueTask<PingResult>(new PingResult()),
91+
McpJsonUtilities.JsonContext.Default.JsonNode,
92+
McpJsonUtilities.JsonContext.Default.PingResult);
93+
8894
if (taskStatusHandler is not null)
8995
{
9096
notificationHandlers.Register(

tests/ModelContextProtocol.Tests/Client/McpClientTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,4 +782,16 @@ public async Task SetLoggingLevelAsync_WithRequestParams_NullThrows()
782782
await Assert.ThrowsAsync<ArgumentNullException>("requestParams",
783783
() => client.SetLoggingLevelAsync((SetLevelRequestParams)null!, TestContext.Current.CancellationToken));
784784
}
785+
786+
[Fact]
787+
public async Task ServerCanPingClient()
788+
{
789+
await using McpClient client = await CreateMcpClientForServer();
790+
791+
var pingRequest = new JsonRpcRequest { Method = RequestMethods.Ping };
792+
var response = await Server.SendRequestAsync(pingRequest, TestContext.Current.CancellationToken);
793+
794+
Assert.NotNull(response);
795+
Assert.NotNull(response.Result);
796+
}
785797
}

0 commit comments

Comments
 (0)