← Back to ToolBench
Utilities
Error Reporting
Structured error codes for meaningful failure handling
What it does
MCP uses JSON-RPC error codes for structured error reporting. Servers should return specific error codes (e.g., InvalidParams, MethodNotFound, InternalError) along with human-readable messages. MCP also defines custom error codes like RequestTimeout. Structured errors help clients and LLMs understand what went wrong and how to recover, rather than just seeing a generic failure.
How it works
Example implementation
import { McpError, ErrorCode } from "@modelcontextprotocol/sdk/types.js";
server.tool("get_user", { id: z.string() }, async ({ id }) => {
const user = await db.findUser(id);
if (!user) {
throw new McpError(ErrorCode.InvalidParams, `User ${id} not found`);
}
return { content: [{ type: "text", text: JSON.stringify(user) }] };
});