diff --git a/src/ModelContextProtocol.Core/McpSessionHandler.cs b/src/ModelContextProtocol.Core/McpSessionHandler.cs index 4d9cb01ba..b2f94fb28 100644 --- a/src/ModelContextProtocol.Core/McpSessionHandler.cs +++ b/src/ModelContextProtocol.Core/McpSessionHandler.cs @@ -132,6 +132,14 @@ public McpSessionHandler( _incomingMessageFilter = incomingMessageFilter ?? (next => next); _outgoingMessageFilter = outgoingMessageFilter ?? (next => next); _logger = logger; + + // Per the MCP spec, ping may be initiated by either party and must always be handled. + _requestHandlers.Set( + RequestMethods.Ping, + (request, _, cancellationToken) => new ValueTask(new PingResult()), + McpJsonUtilities.JsonContext.Default.JsonNode, + McpJsonUtilities.JsonContext.Default.PingResult); + LogSessionCreated(EndpointName, _sessionId, _transportKind); } diff --git a/src/ModelContextProtocol.Core/Server/McpServerImpl.cs b/src/ModelContextProtocol.Core/Server/McpServerImpl.cs index 39feae5d6..49fadb5e4 100644 --- a/src/ModelContextProtocol.Core/Server/McpServerImpl.cs +++ b/src/ModelContextProtocol.Core/Server/McpServerImpl.cs @@ -91,7 +91,6 @@ public McpServerImpl(ITransport transport, McpServerOptions options, ILoggerFact ConfigureLogging(options); ConfigureCompletion(options); ConfigureExperimental(options); - ConfigurePing(); // Register any notification handlers that were provided. if (options.Handlers.NotificationHandlers is { } notificationHandlers) @@ -204,14 +203,6 @@ public override async ValueTask DisposeAsync() await _sessionHandler.DisposeAsync().ConfigureAwait(false); } - private void ConfigurePing() - { - SetHandler(RequestMethods.Ping, - async (request, _) => new PingResult(), - McpJsonUtilities.JsonContext.Default.JsonNode, - McpJsonUtilities.JsonContext.Default.PingResult); - } - private void ConfigureInitialize(McpServerOptions options) { _requestHandlers.Set(RequestMethods.Initialize, diff --git a/tests/ModelContextProtocol.Tests/Client/McpClientTests.cs b/tests/ModelContextProtocol.Tests/Client/McpClientTests.cs index 32e04da60..9a99f597c 100644 --- a/tests/ModelContextProtocol.Tests/Client/McpClientTests.cs +++ b/tests/ModelContextProtocol.Tests/Client/McpClientTests.cs @@ -782,4 +782,16 @@ public async Task SetLoggingLevelAsync_WithRequestParams_NullThrows() await Assert.ThrowsAsync("requestParams", () => client.SetLoggingLevelAsync((SetLevelRequestParams)null!, TestContext.Current.CancellationToken)); } + + [Fact] + public async Task ServerCanPingClient() + { + await using McpClient client = await CreateMcpClientForServer(); + + var pingRequest = new JsonRpcRequest { Method = RequestMethods.Ping }; + var response = await Server.SendRequestAsync(pingRequest, TestContext.Current.CancellationToken); + + Assert.NotNull(response); + Assert.NotNull(response.Result); + } }