From 91959fab198abd1553acf849cf6162533858bc3c Mon Sep 17 00:00:00 2001 From: John Korsnes Date: Fri, 11 Apr 2025 22:43:04 +0200 Subject: [PATCH] Handle requests for capability, when capability not present Will also lower log level from err to wrn Scenario: ex: Claude Desktop polls for `/resources/list` even though `resources` is missing from the `capabilities` --- src/ModelContextProtocol/Shared/McpSession.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/ModelContextProtocol/Shared/McpSession.cs b/src/ModelContextProtocol/Shared/McpSession.cs index 56f0674c..56d3791b 100644 --- a/src/ModelContextProtocol/Shared/McpSession.cs +++ b/src/ModelContextProtocol/Shared/McpSession.cs @@ -298,7 +298,18 @@ private void HandleMessageWithId(JsonRpcMessage message, JsonRpcMessageWithId me if (!_requestHandlers.TryGetValue(request.Method, out var handler)) { LogNoHandlerFoundForRequest(EndpointName, request.Method); - throw new McpException($"Method '{request.Method}' is not available.", McpErrorCode.MethodNotFound); + await SendMessageAsync(new JsonRpcError + { + Id = request.Id, + JsonRpc = "2.0", + Error = new JsonRpcErrorDetail + { + Message = $"{EndpointName} lacks capability to do {request.Method}", + Code = (int) McpErrorCode.MethodNotFound, + }, + RelatedTransport = request.RelatedTransport + }, cancellationToken).ConfigureAwait(false); + return null; } LogRequestHandlerCalled(EndpointName, request.Method); @@ -764,4 +775,4 @@ private static TimeSpan GetElapsed(long startingTimestamp) => [LoggerMessage(Level = LogLevel.Trace, Message = "{EndpointName} sending message. Message: '{Message}'.")] private partial void LogSendingMessageSensitive(string endpointName, string message); -} \ No newline at end of file +}