Skip to content
mentoor

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?

Choose one answer

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

  1. 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: true and a text explanation, so the model sees it.
  2. Name the field and the value. "due_date was next Friday".
  3. State the rule. "Expected ISO date, for example 2026-12-04."
  4. Suggest the next step. "Call projects_list to find valid project names" or "Did you mean Q4-Launch?"
  5. Say whether to retry. Distinguish "fix the input" from "temporary, try again in 30 seconds" from "permission missing, ask the user".
  6. 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: true so 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?

Choose one answer

Check yourself · 3 of 3

Why should a tool keep stack traces out of its error results?

Choose one answer

Practice items, not counted towards your mastery.

Start here