# Tell the AI when a tool call failed

29 November 2026 · AI in practice · by Steen Kramer Jensen, sysapp ApS · https://www.sysapp.dk/notes/tell-the-ai-when-a-tool-call-failed.html

> An MCP tool that returns its error as ordinary text reports a success. Mark failures as errors, and put enough in the message for the assistant to correct the call.

An AI assistant with tools reads whatever a tool returns. If the tool catches its own error and returns a polite sentence such as "could not fetch data", the call is reported as a success. The assistant has to work out from the wording that something went wrong.

Sometimes it does. Sometimes it treats the sentence as data, or fills the gap with a guess.

## Mark the failure as a failure

An MCP tool result carries a flag, `isError`. When it is set, the client and the model know the call failed and can retry, change the arguments or tell the user.

So do not return error text from a tool. Raise an error, and let the server set the flag.

## Example: my energy data server

The MCP server in front of my energy data API is a thin wrapper. Every tool makes one GET request through one shared function, and that function is the only place errors are handled:

```python
try:
    r = await client().get(path, params=q, headers=headers)
except httpx.HTTPError as e:
    raise ToolError(f"Network error calling {path}: {e.__class__.__name__}: {e}") from e
if r.status_code >= 400:
    raise ToolError(f"HTTP {r.status_code} from {path}: {r.text[:2000]}")
```

(The messages are in Danish in the real code.)

Three details matter here:

- The message carries the status code, the endpoint and the first 2000 characters of the API's own answer. If the API rejects a parameter, the assistant can read why and correct the call.
- `ToolError` is for failures I expected. The Python SDK treats any other exception as a crash: the model only sees "Error executing tool" and the tool name, and the traceback goes to the server log. Nothing internal leaks into the conversation.
- A cut-off answer is a hidden failure too. Answers over 60,000 characters are shortened, and a note at the end says so and tells the model to narrow the date range, pick fewer fields or paginate.

## What to check in your own tools

- Does any tool return a string that starts with "Error"? Raise it instead.
- Does the error message say what failed and what to change?
- If you shorten or filter a result, does the result say so?
- Have you called a tool with a wrong argument on purpose and looked at what the assistant receives?

An assistant can only handle the failures it is told about.

---
The first draft of this note was written by AI (Claude) from Steen's own notes and project material. Steen Kramer Jensen edited and approved it before it was published.
