Calling an unknown tool logs a server-side Error "threw an unhandled exception"

Ouverte
#2,030 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
3/5
Temps estimé
1-2 jours
Accessibilité débutants
75/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
csharp

Piste de recherche

Start in src/ModelContextProtocol.Core/Server/McpServerImpl.cs, tracing the unknown-tool handler and the BuildInitialAlternateToolFilter and LogToolCallLifecycles paths that call ToolCallError. Compare this path with the existing Logs_Tool_Error_When_Tool_Throws_McpProtocolException test. Done means an unknown tool still returns JSON-RPC -32602 without an Error log, while an exception thrown by a tool remains logged at Error.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

Summary

When a client calls a tool that does not exist, the server answers correctly with JSON-RPC -32602 (Unknown tool: '…'), but it also writes a server-side Error with a stack trace:

fail: "no_such_tool" threw an unhandled exception.
ModelContextProtocol.McpProtocolException: Unknown tool: 'no_such_tool'

The cause is input from the client, not a fault of the server, yet it looks like a crash in logs, APM and alerting. Any client (or a model that hallucinates a tool name) can produce these Errors at will.

Where (2.4.0, unchanged on main)

src/ModelContextProtocol.Core/Server/McpServerImpl.cs

  • The default handler throws new McpProtocolException($"Unknown tool: '{name}'", McpErrorCode.InvalidParams) (line ~1603).
  • BuildInitialAlternateToolFilter (line ~1737) and LogToolCallLifecycles (line ~1787) call ToolCallError (LogLevel.Error, "threw an unhandled exception.") for every exception except cancellation and InputRequiredException, including this McpProtocolException, and then rethrow it as the protocol error.

Reproduction

Server with any tool, client calls tools/call with the name no_such_tool. Expected: -32602 to the client, no Error on the server. Actual: -32602 and the Error above.

Context

Logs_Tool_Error_When_Tool_Throws_McpProtocolException asserts that an McpProtocolException thrown by a tool is logged at Error, so I understand that is intended. The unknown-tool case is different: no tool ran, and the exception comes from the SDK's own validation of the request.

Proposal

Do not log ToolCallError when no tool was matched for the request (the SDK's own unknown-tool path), or log it at Debug/Information like ToolCallCompleted. The protocol error to the client stays as it is.

Workaround we use today: an incoming message filter (WithMessageFilters(f => f.AddIncomingFilter(...))) that answers tools/call for unknown names with -32602 before the tool pipeline runs. Happy to send a PR if the direction is acceptable.

Langage dominant
C#
Étoiles
4.6k
Forks
849
Merge moyen
1 j 16 h
PR mergées (30 j)
24

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de modelcontextprotocol/csharp-sdk

Toutes les issues de modelcontextprotocol/csharp-sdk

Issues similaires

Plus d'issues C#

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.