Skip to content
Merged
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
8 changes: 8 additions & 0 deletions src/ModelContextProtocol.Core/McpSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PingResult>(new PingResult()),
McpJsonUtilities.JsonContext.Default.JsonNode,
McpJsonUtilities.JsonContext.Default.PingResult);

LogSessionCreated(EndpointName, _sessionId, _transportKind);
}

Expand Down
9 changes: 0 additions & 9 deletions src/ModelContextProtocol.Core/Server/McpServerImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions tests/ModelContextProtocol.Tests/Client/McpClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -782,4 +782,16 @@ public async Task SetLoggingLevelAsync_WithRequestParams_NullThrows()
await Assert.ThrowsAsync<ArgumentNullException>("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);
}
}