Skip to content

Handle requests for capabilty, when capability not present #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
15 changes: 13 additions & 2 deletions src/ModelContextProtocol/Shared/McpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this achieve that's not already achieved when the caller a frame or two up catches the exception and similarly sends back a JsonRpcError?

catch (Exception ex)
{
// Only send responses for request errors that aren't user-initiated cancellation.
bool isUserCancellation =
ex is OperationCanceledException &&
!cancellationToken.IsCancellationRequested &&
combinedCts?.IsCancellationRequested is true;
if (!isUserCancellation && message is JsonRpcRequest request)
{
LogRequestHandlerException(EndpointName, request.Method, ex);
JsonRpcErrorDetail detail = ex is McpException mcpe ?
new()
{
Code = (int)mcpe.ErrorCode,
Message = mcpe.Message,
} :
new()
{
Code = (int)McpErrorCode.InternalError,
Message = "An error occurred.",
};
await SendMessageAsync(new JsonRpcError
{
Id = request.Id,
JsonRpc = "2.0",
Error = detail,
RelatedTransport = request.RelatedTransport,
}, cancellationToken).ConfigureAwait(false);
}

Is the sole purpose here to avoid an exception try/catch, or are you trying to achieve something else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think part of the goal is to get rid of the LogRequestHandlerException warning log, but I agree that instead special-casing McpExceptions with McpErrorCode.MethodNotFound in the existing catch to emit a lower-severity log makes more sense.

Copy link
Author

@johnkors johnkors Apr 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, lower the level from err to wrn is/was the intent.

Another option is to pass something in the Exception or fine-grained exception handling, but not sure how much you like passing state and using Exceptions for control flow.

Happy to change it.

return null;
}

LogRequestHandlerCalled(EndpointName, request.Method);
Expand Down Expand Up @@ -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);
}
}