You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: document best practice for handling argument errors in MCP tools (#891)
Resolves#356
Clarify the two-tier error model:
- Recoverable tool errors: use CallToolResult with isError(true)
- Protocol-level errors: throw McpError / let exceptions propagate as JSON-RPC errors
Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
Copy file name to clipboardExpand all lines: docs/server.md
+39Lines changed: 39 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -795,3 +795,42 @@ Supported logging levels (in order of increasing severity): DEBUG (0), INFO (1),
795
795
## Error Handling
796
796
797
797
The SDK provides comprehensive error handling through the McpError class, covering protocol compatibility, transport communication, JSON-RPC messaging, tool execution, resource management, prompt handling, timeouts, and connection issues. This unified error handling approach ensures consistent and reliable error management across both synchronous and asynchronous operations.
798
+
799
+
### Error Handling in Tool Implementations
800
+
801
+
#### Two Tiers of Errors
802
+
803
+
MCP distinguishes between two categories of errors in tool execution:
804
+
805
+
**1. Tool-Level Errors (Recoverable by the LLM)**
806
+
807
+
Use `CallToolResult` with `isError(true)` for validation failures, missing arguments, or domain errors the LLM can act on and retry.
if (!emailAddress.matches("^[A-Za-z0-9+_.-]+@(.+)$")) {
812
+
returnCallToolResult.builder()
813
+
.content(List.of(newMcpSchema.TextContent("Invalid argument: 'email' must be a valid email address.")))
814
+
.isError(true)
815
+
.build();
816
+
}
817
+
```
818
+
819
+
The LLM receives this as part of the normal tool response and can self-correct in a subsequent interaction.
820
+
821
+
**2. Protocol-Level Errors (Unrecoverable)**
822
+
823
+
Uncaught exceptions from a tool handler are mapped to a JSON-RPC error response. Use this only for truly unexpected failures (e.g., infrastructure errors such as DB timeout), not for input validation.
824
+
825
+
```java
826
+
// This propagates as a JSON-RPC error — use sparingly
0 commit comments