I'm uncertain about the recommended approach for handling errors in MCP, especially when it comes to missing or invalid arguments.
Specifically:
Should we throw an exception, or should we catch the error and return it as part of the McpSchema.CallToolResult so the LLM can understand what went wrong and possibly correct the request in a future interaction?
Example Scenario:
I have a tool that expects two parameters: first_name and last_name. It returns a simple greeting message.
If the request includes only first_name and last_name is null, how should the error be handled?
Option 1 — Throw an exception:
catch (Exception e) {
throw new McpError(e);
}
Option 2 — Catch and return the error message:
catch (Exception e) {
contents.add(
new McpSchema.TextContent("Error: " + e.getMessage())
);
}
Which approach aligns better with MCP's error-handling philosophy and the intended LLM experience?
I'm uncertain about the recommended approach for handling errors in MCP, especially when it comes to missing or invalid arguments.
Specifically:
Should we throw an exception, or should we catch the error and return it as part of the
McpSchema.CallToolResultso the LLM can understand what went wrong and possibly correct the request in a future interaction?Example Scenario:
I have a tool that expects two parameters:
first_nameandlast_name. It returns a simple greeting message.If the request includes only
first_nameandlast_nameis null, how should the error be handled?Option 1 — Throw an exception:
Option 2 — Catch and return the error message:
Which approach aligns better with MCP's error-handling philosophy and the intended LLM experience?