Sub topic 6
Tools: actions the model can invoke
This chapter assumes you know the host, client, and server roles, the JSON-RPC message shapes, and the initialization handshake from earlier chapters. You should also be able to read short Python functions.
What a tool is
A tool is a function that a server exposes so the model can call it. Examples are querying a database, creating a ticket, or adding two numbers. Tools are model-controlled. The model decides when a tool is useful, and the host runs the call through its client. Resources and prompts differ here: the application or the user controls those.
A server that supports tools declares a tools capability during initialization. The client should not send tool requests to a server that did not declare it.
The tool lifecycle
A tool call has two steps. First, the client discovers tools with tools/list. Second, it invokes one with tools/call. The host passes the discovered definitions to the model, and the model picks one.
What a definition contains
Each tool in a tools/list response has these fields:
| Field | Purpose |
|---|---|
| `name` | Unique identifier the model uses to call the tool |
| `description` | Plain-language text that tells the model when to use it |
| `inputSchema` | A JSON Schema describing the arguments |
The model reads the description and schema to decide how to call the tool. Vague descriptions lead to wrong or missed calls.
Calling a tool
A tools/call request names the tool and supplies arguments. The server answers with the same id and a content list.
{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "add", "arguments": {"a": 2, "b": 3}}}
{"jsonrpc": "2.0", "id": 2, "result": {"content": [{"type": "text", "text": "5"}], "isError": false}}The content list can hold several items. Text is the most common type. Other types, such as images, also exist.
Defining a tool in Python
With the official Python SDK, a decorator turns a function into a tool. FastMCP builds the inputSchema from the type hints and takes the description from the docstring.
from mcp.server.fastmcp import FastMCP
mcp = FastMCP("demo")
@mcp.tool()
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
if __name__ == "__main__":
mcp.run(transport="stdio")The schema generated for add requires two integers, a and b. A call with a string for a is rejected before your function runs.
Two kinds of errors
Tool failures are reported in two different ways, and mixing them up is a common mistake.
- Protocol errors are standard JSON-RPC errors. They cover an unknown tool name, malformed arguments, or a server fault. The response has an
errorfield instead ofresult. - Tool execution errors happen when the tool ran but failed, for example an API returned a 404. The response is a normal
resultwith"isError": trueand a message incontent.
Return execution errors inside the result. The model can read the message and try again with different arguments. A protocol error usually does not reach the model in a useful form.
{"jsonrpc": "2.0", "id": 3, "result": {"content": [{"type": "text", "text": "City not found: Atlantis"}], "isError": true}}Common mistakes
- Writing a one-word description such as "search". Say what is searched and what the result looks like.
- Exposing too many tools. Each definition uses context in the model's prompt, and a long list makes selection less reliable.
- Trusting arguments. The model produces them, so validate them as you would any untrusted input.
- Running destructive actions with no confirmation. Hosts should show the user which tool is about to run, a point the security chapter covers.
Exercise
A tool calls an external weather API, which responds that the city does not exist. Should the server return a JSON-RPC error or a result with isError: true, and why?
Show answer
It should return a result with isError: true and a clear message such as "City not found". The tool ran correctly and the failure is part of its outcome. The model sees the message and can correct the city name and retry. A JSON-RPC error is for protocol problems like an unknown tool or invalid arguments.
Exercise
Which part of a tool definition does the model rely on to decide when to use the tool, and which part constrains the arguments?
Show answer
The description tells the model when to use the tool. The inputSchema, a JSON Schema, constrains the arguments.