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.
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
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.
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.
There was a problem hiding this comment.
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.
Which log is still at the error level? |
An exception thrown by the handler function (a tool implementation throwing an exception for example). |
Can you show me which one? It shouldn't be at the error level anymore, eg csharp-sdk/src/ModelContextProtocol/Shared/McpSession.cs Lines 717 to 718 in e190844 |
|
Ah, ok - so it's a warn. Then I don't know what other scenarios is an Exception. Other exceptions wasn't really the focus for this PR. I only wanted to avoid the error logs when clients ask for capabilities that are not available. See OP. Seems like you did some changes 2 weeks a go, while I open this PR 3 weeks ago. Maybe it's no longer an issue. I'll update and check. |
Yes, in response to your request that the logging level be lowered to warning from error. |
|
Okaydokay, did not know your PR was related. Looks better! Claude traffic generate logs with WRN instead of ERR, where my server has no resources defined. |

Motivation and Context
Some clients ignore the capabilities, and sends
<capability>/listrequests regardless (ex: Claude Desktop).ex: Claude Desktop polls for
/resources/listeven thoughresourcesis missing from thecapabilities. The C# SDK throws exceptions and logs as ERROR statements. This PR changes this to just return theMethodNotFounderror 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