Sub topic 8
Following a tool call from prompt to result
This chapter assumes you know the host, client and server roles, the three primitives, and the basic shape of JSON-RPC messages. It also assumes you have a server configured in your assistant, as covered in the previous chapter. Here you will trace one request from your prompt to the final answer.
The setup
Suppose you have the calculator server from the examples running, with a single tool named add. You type this prompt into the assistant:
> What is 2 plus 3?
You can't see most of what happens next. Knowing the steps helps you debug a tool that never fires or returns the wrong thing.
The full round trip
Step 1: Discovery happened earlier
When the host started the server, its client sent tools/list. The server answered with each tool's name, description and input schema. The host stored these definitions. This is why a server added mid-session often needs a restart or reload before its tools appear.
Step 2: The prompt goes to the model with tool definitions
The host sends your prompt to the model along with the tool definitions. The model does not run anything. It only reads the descriptions and decides whether a tool would help. The description and schema are the model's only evidence about what add does, so a vague docstring leads to wrong or missing calls.
Step 3: The model asks for a call
If the model decides to use add, it replies with a structured request instead of plain text. It names the tool and supplies arguments, here a as 2 and b as 3. This is a request to the host, not an action.
Step 4: The host decides, and the client sends
The host is where permission is enforced. Many assistants show an approval prompt at this point. If you approve, the client sends this request to the server:
{"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}The id lets the client match the response to this request.
Step 5: The server runs the code and replies
The server runs the add function and returns a result with content blocks. A typical response looks like this:
{"jsonrpc": "2.0", "id": 1, "result": {"content": [{"type": "text", "text": "5"}], "isError": false}}Step 6: The model uses the result
The host adds the tool result to the conversation and calls the model again. The model now writes the final answer for you. A single prompt can trigger several of these loops if the model needs more than one call.
Two kinds of failure
Errors show up in two different places, and the difference matters when debugging.
| Failure | Where it appears | Example |
|---|---|---|
| Protocol error | JSON-RPC `error` field | Unknown tool name, malformed arguments |
| Tool execution error | `result` with `isError: true` | The API behind the tool returned a 500 |
A tool execution error is given to the model as text, so it can read the message, correct its arguments and retry. A protocol error usually signals a bug in the client or the server.
Common mistakes
- Vague descriptions. The model skips the tool or picks the wrong one.
- Wrong schema types. If the schema says an integer and the model sends a string, the server may reject the call.
- Stdout pollution. On stdio, a stray
printwrites to the same stream as protocol messages and corrupts them. Log to stderr instead. - Forgetting the restart. Changes to a server's code or tool list are not seen until the host reconnects.
Exercise
A tool call fails because the weather API behind your server returned a 503 error. Which kind of failure is this, and how does the model find out?
Show answer
It is a tool execution error. The server returns a normal result with isError set to true and a text message describing the failure. The host passes that text to the model, which can explain the problem to the user or retry.
Exercise
At which step does the assistant's approval prompt usually appear, and why does that placement matter?
Show answer
It appears in step 4, after the model requests a call and before the client sends tools/call. The model only proposes the call, so the host is the one place where you can block it before any code runs.