-
Notifications
You must be signed in to change notification settings - Fork 247
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
base: main
Are you sure you want to change the base?
Handle requests for capabilty, when capability not present #290
Conversation
This part seems good. I agree that the server should not emit error logs just because a client is behaving improperly.
This seems problematic. Have you tried calling csharp-sdk/src/ModelContextProtocol/Shared/McpSession.cs Lines 159 to 165 in 0c9e91f
|
ba89760
to
92f7112
Compare
Ah, ok! Thanks. So it might be better to do respond with an I cannot throw, since that's tied together with the problematic error log level.
+++ await SendMessageAsync(new JsonRpcError
+++ {
+++ Id = request.Id,
+++ JsonRpc = "2.0",
+++ Error = detail,
+++ RelatedTransport = request.RelatedTransport,
+++ }, cancellationToken).ConfigureAwait(false);
--- await _transport.SendMessageAsync(new JsonRpcNotification
---{
--- JsonRpc = "2.0",
--- Method = NotificationMethods.LoggingMessageNotification,
--- Params = new JsonObject
--- {
--- ["message"] = $"{EndpointName} lacks capability to do {request.Method}",
--- ["requestId"] = request.Id.ToString(),
--- ["type"] = nameof(BadRequest)
--- }
---}, cancellationToken).ConfigureAwait(false);
---return;
No, I did not do a manual regression test. All automated tests pass when I do |
92f7112
to
21833b9
Compare
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`
21833b9
to
91959fa
Compare
Updated the code to respond with |
How does this surface now if the client calls something like |
Not sure I can assert that the log level is reduced, but I have not changed any behaviour. Is there a missing test from before, I'll happily add it. Where do you want it? Before the change:
After the change
|
Code = (int) McpErrorCode.MethodNotFound, | ||
}, | ||
RelatedTransport = request.RelatedTransport | ||
}, cancellationToken).ConfigureAwait(false); |
There was a problem hiding this comment.
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?
csharp-sdk/src/ModelContextProtocol/Shared/McpSession.cs
Lines 135 to 166 in e190844
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?
There was a problem hiding this comment.
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.
Which log is still at the error level? |
Motivation and Context
Some clients ignore the capabilities, and sends
<capability>/list
requests regardless (ex: Claude Desktop).ex: Claude Desktop polls for
/resources/list
even thoughresources
is missing from thecapabilities
. The C# SDK throws exceptions and logs as ERROR statements. This PR changes this to just return theMethodNotFound
error code over JSON-RPC, and log as a warning instead of error.Relevant discussion: #74 (comment)
How Has This Been Tested?
Use Claude Desktop
Ran all tests
Breaking Changes
No
Types of changes
Checklist
Additional context