What happens when an agent calls a tool
How AI Works,
Most of what Claude Code does for me in a day is call tools. I also built Woolly, a local memory for AI tools with an MCP server, so I've written some of those tools myself. The one thing to know going in is that the model never runs any of them. It writes a request, some other program runs it, and the answer gets sent back in. This is that round trip, one step at a time.
I'm using the weather example from Anthropic's tool use docs the whole way through. It's small and it shows every piece.
What is a tool to an AI model?
A tool starts as a definition you send along with your request to the Messages API. Each one goes in a top level tools array and has three parts. A name, a description that says what it does and when to use it, and an input_schema. That last one is JSON Schema, a standard way to describe what arguments the tool takes.
{
"name": "get_weather",
"description": "Get the current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": { "type": "string" },
"unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
},
"required": ["location"]
}
}The name can be letters, digits, underscores and dashes, up to 128 characters. The docs call the description "by far the most important factor" in how well the model uses a tool, so a vague one really hurts.
What the model actually sees
The model never gets your code. The API takes your tool definitions and builds a special system prompt out of them. It starts with "In this environment you have access to a set of tools..." and then lists the definitions as JSON Schema. That text counts as input tokens, so every tool you add costs a little on every request.
How the model asks for a tool
Say you ask about the weather in San Francisco. The model can't look that up. What it can do is write out, in a fixed shape, which tool it wants and with what arguments. The response comes back like this.
{
"id": "msg_01Aq9w938a90dw8q",
"model": "claude-opus-5-5",
"stop_reason": "tool_use",
"role": "assistant",
"content": [
{
"type": "text",
"text": "I'll check the current weather in San Francisco for you."
},
{
"type": "tool_use",
"id": "toolu_01A09q90qw90lq917835lq9",
"name": "get_weather",
"input": { "location": "San Francisco, CA", "unit": "celsius" }
}
]
}Two things to look at. stop_reason is "tool_use", which means the model stopped on purpose because it wants something run. And the content has a text block, which often comes first, then a tool_use block with an id, the tool name and the input it picked. One response can hold more than one of those.
That's all the model did. It wrote some JSON, and nothing has run yet.
Who actually runs the tool?
Your code does. People call this part the client. In an agent like Claude Code, Claude Code itself is the client. It's the program wrapped around the model that sends the requests, reads the responses and does the real work. It sees the tool_use block, finds its own get_weather function and calls it with that input.
This is also where permission prompts live. When Claude Code asks before running something, that's the client deciding whether to carry out what the model asked for. The model can only ask.
There's one exception. Server tools like web_search, web_fetch and code_execution run on Anthropic's servers instead of on your machine. The model still doesn't run them. Something else does, it's just not your code.
Sending the tool result back
Once the tool finishes, the client sends a new request. It appends the assistant content exactly as it came back, then adds a user message with one tool_result block for each tool_use. The tool_use_id matches the id from before, so the model knows which answer goes with which call.
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": "toolu_01A09q90qw90lq917835lq9",
"content": "15 degrees"
}
]
}A few rules here. The tool_result blocks have to come first in that message. Put text before them and the API returns a 400. The same tools array gets sent again too. And if the tool fails, send the error back with is_error set to true and a useful message, so the model can try again.
A tool result is just more text going into the model. It can carry a prompt injection, which is text written to look like instructions. So it's data to read, not orders to follow.
The agent loop
That's one round trip. An agent is that round trip on repeat.
while (true) {
response = send(messages, tools)
if (response.stop_reason !== "tool_use") break
messages.push(assistant(response.content))
messages.push(user(runEachToolUse(response.content)))
}It keeps going while stop_reason is tool_use. It stops on end_turn, max_tokens, stop_sequence or refusal. end_turn is the normal one. The model is done and it's answering you.
Where does MCP fit in?
MCP is the Model Context Protocol. It's how a separate program, called an MCP server, offers its tools to a client like Claude Code.
A lot of people assume the model speaks MCP. It doesn't know MCP exists. MCP sits between the client and the server, and the model only ever sees ordinary tool definitions.
The client asks the server for its tools with tools/list, over JSON-RPC 2.0. Each tool comes back with a name, description and inputSchema, plus optional title, outputSchema and annotations. The client turns those into normal API tool definitions. Watch the spelling. MCP uses inputSchema in camelCase and the Anthropic API uses input_schema.
When the model emits tool_use for one of them, the client sends tools/call to the server.
// request params
{ "name": "get_weather", "arguments": { "location": "New York" } }
// result
{
"content": [{ "type": "text", "text": "Current weather in New York:..." }],
"isError": false
}The client wraps that content and isError up as a tool_result. Same round trip as before, with one more hop.
The spec calls tools model-controlled, meaning the model picks when to use them. It also says there should be a human in the loop who can deny a call. That's the permission prompt again.
What MCP tools look like in Claude Code
Claude Code names MCP tools mcp__<server>__<tool>. So my Woolly tools show up as mcp__woolly__recall, mcp__woolly__remember, mcp__woolly__decide, mcp__woolly__handoff and a few more. When Claude calls recall, it writes a tool_use block with that name. Claude Code sends tools/call to the Woolly server, and Woolly's answer comes back as a tool_result.
Two more details. Tool search is on by default, so MCP tool definitions are deferred and only loaded when they're needed. That matters, since every definition costs input tokens. And MCP output warns at 10,000 tokens and is capped at 25,000 by default. You can change the cap with MAX_MCP_OUTPUT_TOKENS.