A model on its own can only write text. It cannot multiply large numbers reliably, it knows nothing after its training cutoff or inside your company, and it cannot touch a database, a file or a web page. Tools fix all three. Through tools the agent computes exactly, looks up what it does not know, and acts on systems outside the conversation. This module is about the interface that makes that possible. It covers how a tool is described to a model, what happens between the model’s request and the result, how to design tools that a model uses well, and how the Model Context Protocol lets an agent use tools that someone else wrote and runs.

Notebooks

Notebook 3a: Function calling from scratch builds the tool layer by hand: the wire format, schemas generated from Python signatures, argument validation, a registry that turns every failure into an observation, a linter for tool definitions and a search tool sized for a token budget. Notebook 3b: MCP builds an MCP server, talks to it in memory, by hand over stdio and with the official SDK, then gives its tools to an agent.

What a tool is

Definition 3.1 (Tool).

A tool is a function that runs outside the model, offered to it as a definition: a name, a description, and a JSON Schema for its arguments. The model never runs a tool. It returns a tool call, which is a tool name plus arguments. Your code runs the tool and sends the result back as an observation.

The definition is everything the model knows about the tool. It never sees your code, so in Definition 3.1 the name and description must say what the tool does and when to use it, and the schema must say how to fill each argument. In practice tools fall into three groups. Information tools search, read and query. Computation tools run a calculator, a solver or code. Action tools send, write, buy and delete. Action tools are the ones that need permission checks (Module 12).

There have been two ways to teach a model to use tools. The first changes the model. Toolformer fine-tuned a model on text it annotated itself with calls to five APIs, keeping only the calls whose results made the following text easier to predict. Gorilla and ToolLLM fine-tuned models on thousands of real APIs. The second changes the description. Modern models are trained to read tool definitions at run time, so any function with a schema becomes usable without retraining. Every hosted model API now works this way, and so does this course.

The round trip

One tool call takes four moves. The model chooses a tool and writes the call. Your code checks the call and runs the tool. The result, or an error saying what went wrong, is appended to the conversation, and the model reads it on the next step.

model chooses a tool tool call name + JSON args your code validate args your code run the tool observation result or error invalid: the error says why

The formats differ by provider. In the OpenAI-style format the arguments arrive as a JSON string. In Anthropic’s they arrive as an object inside a tool_use block. Libraries such as LiteLLM translate between formats. What matters is that a tool call is model output, and model output can be wrong in every way: an unknown tool name, arguments that do not parse, a missing field, a value outside the allowed set.

Remark 3.1 (Arguments are untrusted input).

Treat a tool call the way a web server treats a form submission. Check the tool name, parse the arguments, and validate them against the schema before running anything. Then catch whatever the tool raises. Every failure goes back to the model as an observation, never as a crash. The model can only repair what the observation describes: KeyError: 'euros' says that something went wrong, while “from_currency must be one of USD, EUR, JPY, GBP” says how to fix it.

Notebook 3a builds this layer as a registry of about 40 lines, and as a before_tool callback that adds validation to agentlab.Agent. The rule in Remark 3.1 has a corollary: error messages are prompts, and they deserve the same care as the system prompt.

Designing tools a model can use

Most advice on tool design comes down to writing the definition for a reader who has only the definition. A useful test is to ask whether a new colleague could use the tool correctly from its definition alone.

  • Name the action and the object. reserve_table beats book, and search_kb beats search when there are several things to search. With tools from several servers, prefix names by source.
  • Say when to use it. The description is the model’s main clue for choosing among tools. Say what the tool returns, and when not to use it, if another tool is better.
  • Close open sets. If an argument takes one of a few values, make it an enum. A model cannot send "euro" if the schema lists EUR. Replace pairs of booleans that can contradict each other (on, off) with one enum.
  • Mark what is required. List required arguments, give the others defaults, and keep the count small. A tool with eight arguments is usually two tools.
  • Design the output too. Return what the model needs to decide its next step, in compact text, and say when a result was cut or is empty.

The last point has a price that is easy to miss. A tool result stays in the conversation, so it is paid for again on every later step. If a result of tokens arrives at step of a run of steps, it is re-sent with each of the remaining requests:

By equation , a 3,000-token search result at step 2 of a 20-step run costs 54,000 input tokens, eighteen times its own size. Prompt caching (Module 2) lowers the price of those tokens, and they still crowd the context. Notebook 3a measures three designs for the same search results. Full pages as JSON cost more than three times as much as titles, URLs and one-sentence snippets. The usual design is that middle one: enough to decide what to read, plus a second tool to read it in full.

How many tools? Every definition is sent with every request, so twenty tools with long descriptions can cost thousands of tokens per step before any work is done. They also give the model more ways to choose wrong. Keep the tool set small for each task. Merge tools that are always used together. When the catalogue is large, give the agent a tool that searches the catalogue and loads only the definitions it needs.

The Model Context Protocol

Tools written into your agent work for your agent only. Tools that others maintain, such as an issue tracker, a database or a browser, would otherwise need an adapter for every pair of agent and tool.

Definition 3.2 (Model Context Protocol).

The Model Context Protocol (MCP) is an open protocol between an AI application and the services it uses. The application runs one client per server. A server offers tools (called by the model), resources (read by the application) and prompts (chosen by the user). Messages are JSON-RPC 2.0, carried over stdio for a local server running as a subprocess or over Streamable HTTP for a remote one.

Anthropic introduced MCP in November 2024, and it has since been adopted across model providers and developer tools. Under Definition 3.2 a session has a short life cycle:

stepmessagewhat happens
handshakeinitialize, then the initialized notificationagree on a protocol version and exchange capabilities
discoverytools/listthe server returns each tool’s name, description and inputSchema
usetools/callthe server runs the tool and returns a list of content blocks
failurea result with isError: truethe tool failed; the message is meant for the model
endclose the transportfor stdio, closing the server’s stdin

The inputSchema is ordinary JSON Schema, so an MCP tool becomes a function-calling tool by wrapping it in the provider’s envelope. Notebook 3b does that with a small adapter class, so the agent cannot tell remote tools from local ones. One engineering detail matters in practice. A connection should stay open for the whole run, because starting a server process for every call is slow. The SDK’s transports, though, must be closed by the asyncio task that opened them. agentlab.mcp.MCPToolset therefore keeps the connection in one background task and hands it work through a queue.

MCP standardises how tools are found and called. It does not change what the model sees: the client still turns each tool into a definition in the provider’s format. Nor is it needed for a function that only your agent uses. MCP pays off when the same tools serve several applications, or when someone else maintains them.

Remark 3.2 (What a server can do to you).

Connecting a server means trusting it in three ways. A local server is a program running with your permissions, so run unfamiliar ones in a sandbox (Module 8) and pin their versions. Its tool descriptions are prompts written by someone else, inserted verbatim into your model’s context on every request, and they can change between sessions. Its results are untrusted text that may carry instructions aimed at your model (Module 12). The Python SDK passes a stdio server only a short list of environment variables, so your API keys reach it only if you pass them on purpose.

The checks that follow from Remark 3.2 are cheap. Read the full description of every tool before enabling it. Allowlist the tools a task needs rather than enabling a whole server. Log every call. These are the same habits as for any third-party dependency.

Measuring tool use

Function-calling benchmarks test the pieces of this module separately. The Berkeley Function Calling Leaderboard checks single calls, choices among several functions, parallel calls, and relevance detection: noticing that no offered tool fits, and calling none. ToolBench uses thousands of real APIs, and τ-bench adds a simulated user and checks the final state of a database. τ-bench also introduced the pass^k measure, the chance that all of tries succeed, which Module 10 uses for reliability. A model’s score on these benchmarks is a starting point. Your own tools, with your own descriptions, are what your agent depends on, and Module 10 shows how to test them.

Try this

  • Take a tool you have written or used, and run Notebook 3a’s lint_tool on its definition. Fix what it reports, then decide which warnings you disagree with and why.
  • Use equation to compare two designs for a ten-step research task. In the first, search returns full pages of about 2,000 tokens each, three per call, at steps 1 and 4. In the second, it returns snippets of about 50 tokens and the agent reads two full pages with read_page at steps 2 and 5.
  • In Notebook 3b, add a third tool to kb_server.py whose description tells the model to “always call this tool first”. Restart the toolset and print the descriptions. Which part of your agent would notice?

Readings