Topic 4 of 5 · 5 min read
Actionable tool errors agents can recover from
Goal: Rewrite a set of tool error messages so an agent can correct its call without asking the user.
Treat every tool error as the next prompt: it should say what was wrong, which value caused it, and what to try instead. When a call fails, the agent has only the error text to decide its next move.
Check yourself · 1 of 3
An agent calls a tool with a project name that does not exist. Which error message helps it most?
Why it matters
A message such as Error 500 or a Python stack trace tells the agent nothing it can use. It will retry the same call, give up, or ask the user for help they cannot give. Specific, actionable errors let the agent fix a bad argument on its own. Anthropic's guide recommends errors that clearly communicate specific and actionable improvements, rather than opaque codes or tracebacks.
How to do it
- Report tool errors inside the result. MCP separates protocol errors (unknown tool, malformed request) from tool execution errors. For failures the model can act on, return a normal result with
isError: trueand a text explanation, so the model sees it. - Name the field and the value. "
due_datewasnext Friday". - State the rule. "Expected ISO date, for example 2026-12-04."
- Suggest the next step. "Call
projects_listto find valid project names" or "Did you meanQ4-Launch?" - Say whether to retry. Distinguish "fix the input" from "temporary, try again in 30 seconds" from "permission missing, ask the user".
- Keep internals out. No stack traces, file paths, SQL or secrets. Log those server-side and return a short reference id.
Before and after
{ "isError": true, "content": [{ "type": "text", "text": "Error: invalid input" }] }
becomes
{
"isError": true,
"content": [{
"type": "text",
"text": "project 'Q4 launch' was not found. Project names are case-sensitive and use hyphens. Closest matches: 'Q4-Launch', 'Q4-Launch-EU'. Use projects_list to see all names."
}]
}
The agent can retry with Q4-Launch in the same turn.
What goes wrong
- Leaking internals. A stack trace gives attackers and the model details they should not have, and it wastes tokens.
- Same error for different faults. "Bad request" for a missing field, a wrong format and a missing permission sends the agent down the wrong path.
- Validation after side effects. Check input before doing anything, so a failed call changes nothing and can be retried safely.
Examples
Enum mismatch
A call sets status="done" on a ticket. Bad: "Validation failed." Good: "status must be one of open, pending, resolved, closed. You sent 'done'. Use 'resolved' to mark a ticket as finished."
Permission versus input
A delete call is refused. The message says "You do not have permission to delete project Q4-Launch. This is not a problem with the input; ask the user to request access from an admin." The agent stops retrying and tells the user.
Key points
- An error message is the agent's only guide to its next call, so it must name the field, the problem and a fix.
- Return tool execution failures as results with
isError: trueso the model can see and act on them. - Say whether to fix the input, retry later, or ask the user for access.
- Never return stack traces, internal paths, queries or secrets; log them server-side and give a reference id.
- Validate before any side effect, so a rejected call leaves nothing half-done.
Common mistakes
- Returning "Something went wrong" with no field, cause or next step.
- Passing the exception text or stack trace straight through to the model.
- Using one generic error for input, permission and outage problems.
- Doing part of the work and then failing validation, leaving partial changes.
Check yourself · 2 of 3
A tool hits a failure the model could fix by changing its arguments. How should the server report it in MCP?
Check yourself · 3 of 3
Why should a tool keep stack traces out of its error results?
Practice items, not counted towards your mastery.
Start here
- Writing effective tools for AI agents (Anthropic) (opens in a new tab)ArticleFree
Anthropic's field guide: which tools to build, namespacing, token-efficient responses, prompt-engineered descriptions and eval-driven iteration.
- MCP specification: Tools (opens in a new tab)StandardFree
The protocol definition of tool listings, input and output schemas, structured content and the isError flag.
- Introducing advanced tool use (Anthropic) (opens in a new tab)ArticleFree
Techniques for large tool sets, such as loading tools on demand and calling them from code, and why they matter for context cost.