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 +}