Tools That Change Data
A tool that changes data, such as one that books a seat or saves a note, is only useful if a model calls it at the right moment with the right arguments. The model never reads your code. It decides from a short description of each tool that the server publishes: a name, some text and a schema of the arguments. This page explains how that description is built from a Python function, how to write it so the right call is the obvious one, and how a tool can return data that a program reads directly instead of parsing a sentence.
What the model decides from
In MCP, tools are model-controlled. The user asks for something in plain words. The model, running inside the host application, then decides whether a tool would help, which one, and with which arguments. Nobody wires a button to the tool in advance. The specification adds that there should always be a person who can deny a call, so a host usually lets the user approve a call before it runs. Still, the choice of the call itself comes from the model.
To make that choice, the model has only what the server lists for each tool when a client sends tools/list. Only two fields are required: the name and the inputSchema. The others are optional, and the most important of them is the description. The body of the function is never sent. If the name is vague or the description leaves out what the tool changes, the model has no other way to find out.
- Python functionThe function
The SDK reads the name, the docstring and the type hints. The body runs on the server when the tool is called and is never shown to anyone.
- def reserve_book(...)
- docstring
- type hints
- body (stays on the server)
- Tool listingWhat tools/list returns
One entry like this per tool. It is everything the client, and through it the model, knows about the tool before calling it.
- name required
- inputSchema required
- description optional
- title optional
- outputSchema optional
- annotations optional
- The model
- which tool?
- which arguments?
- Python function to Tool listing: the SDK builds
- Tool listing to The model: the host passes on
In the 2.x line of the official mcp Python SDK, the server class is MCPServer. The 1.x line and many older tutorials call it FastMCP, which is also the name of a separate framework. On a server object named mcp, the decorator @mcp.tool() fills the listing for you. The function name becomes the tool name, the docstring becomes the description, and the type hints become the input schema. The tool itself can be an ordinary def function; the SDK makes sure the server can still answer other requests while it runs.
True or false?
A model can look at the code of a tool to work out what it does before calling it.
False. Only the listing is sent: the name, the description, the schemas and a few optional fields. The function body never leaves the server.
Two tools with the same code but different descriptions can be called very differently by a model.
True. The description is part of what the model decides from, and the code is not, so changing the text changes the decision.
Input schemas from parameters
The model needs to know which arguments a tool takes, what type each one is and which ones it must supply. MCP carries this as an input schema: a small document in JSON Schema, a standard format for describing the shape of JSON data. The arguments always form a JSON object, so the schema says "type": "object", lists each argument under properties with its type, and names the arguments that must be present in required.
You do not write this document by hand. A type hint after a parameter name, such as isbn: str, tells Python and the SDK what type the value has, and the SDK turns each hint into a property. A parameter without a default becomes required. A parameter with a default becomes optional, and the default is written into the schema, so a caller that leaves it out gets the same value Python would use.
from mcp.server import MCPServer
mcp = MCPServer("Library")
@mcp.tool()
def reserve_book(isbn: str, copies: int = 1) -> str:
"""Reserve copies of a book for the current reader."""
...{
"type": "object",
"properties": {
"isbn": {"title": "Isbn", "type": "string"},
"copies": {"default": 1, "title": "Copies", "type": "integer"}
},
"required": ["isbn"],
"title": "reserve_bookArguments"
}Python's str becomes the JSON Schema type string, int becomes integer, float becomes number and bool becomes boolean. The schema is also a promise the SDK keeps: if a client sends "copies": "two", the SDK rejects the call before the function runs, so the body can trust that copies is an integer.
The SDK also adds a "title" to each property, such as "title": "Isbn", and one for the whole schema. These titles only repeat the names, so the model gets nothing new from them. The SDK writes no $schema key, and MCP then reads the schema as JSON Schema 2020-12.
True or false?
Giving a parameter a default value removes it from the required list of the input schema.
True. The SDK lists in required only the parameters without a default, and writes the default into the property.
A model will always send every argument listed under properties.
False. Only the arguments in required must be present. An optional one may be left out, and the function then receives its default.
Writing names and descriptions for the model
Since the model decides from text alone, the name, the description and the parameter descriptions are the interface of the tool, as much as its code is. A useful test is to describe the tool the way you would to a new colleague who cannot see the code: what it does, when to use it, what it changes, and what each argument means and looks like.
The name should say the action and the thing it acts on, such as reserve_book or cancel_reservation, so that two tools on one server cannot be confused. The specification asks for names of 1 to 128 characters using only ASCII letters, digits, underscores, hyphens and dots, with no spaces, and unique within the server.
The description says what happens, especially what data changes, and what comes back.
Each parameter needs a name that leaves no doubt (isbn or reservation_id, not book or id) and, where the format matters, a description of its own.
| Part | Leaves the model guessing | Makes the right call obvious |
|---|---|---|
| Name | update | cancel_reservation |
| Description | Updates a reservation. | Cancel a reservation and return its copies to the shelf. Returns the cancelled reservation. |
| Parameter | id: str | reservation_id: int, described as the number returned by reserve_book |
In the SDK, a description for one parameter goes on its type hint, with two helpers. Annotated, from Python's typing module, attaches extra information to a type without changing the type. Field, from the Pydantic library the SDK is built on, is that extra information: it carries the description. The model reads these texts alongside the docstring.
from typing import Annotated
from pydantic import Field
@mcp.tool()
def reserve_book(
isbn: Annotated[str, Field(description="The 13-digit ISBN, digits only.")],
copies: Annotated[int, Field(description="How many copies to reserve.")] = 1,
) -> str:
"""Reserve copies of a book for the current reader.
Creates a new reservation. Use search_books first to find the ISBN.
"""
...The type stays str and int, so the schema keeps its types and gains a "description" on each property. If you need a tool name or description that differs from the function, @mcp.tool(name=..., description=...) overrides them.
True or false?
Renaming a parameter from `id` to `reservation_id` can change how often a model calls the tool correctly, even if nothing else changes.
True. The parameter name is part of the schema the model reads, and an unambiguous name tells it which value belongs there.
A short docstring is best, because the model already knows what a tool named `reserve_book` does.
False. The name suggests the action but not what it changes, what it returns or when to prefer it over a similar tool. The description is where those go.
Structured output: results a program can read
When a tool runs, the result goes back to two kinds of readers. The model reads text. The application around it, or a test, or a script, wants data: the number of the new reservation, without picking it out of a sentence. MCP gives each reader its own part of the result. content holds blocks such as text; in the SDK's words, it is the only part of the result the model sees. structuredContent holds the same result as a JSON value for programs.
- Returned value Reservation(...)
- content JSON as text
- structuredContent the same data as JSON
- The model
- A program
- Returned value to content
- Returned value to structuredContent
- content to The model: reads
- structuredContent to A program: reads
The shape of that data is declared the same way as the arguments, with a type hint, this time the return type after ->. The SDK turns it into an output schema, published in the listing as outputSchema, and fills both parts of every result.
A common way to describe a record is a class. A class is a template for objects: the code below defines one named Reservation, and Reservation(...) creates an object from it. When the class is based on Pydantic's BaseModel, its attributes, each with a type hint, are the fields of the record.
from pydantic import BaseModel
class Reservation(BaseModel):
reservation_id: int
isbn: str
copies: int
@mcp.tool()
def reserve_book(isbn: str, copies: int = 1) -> Reservation:
"""Reserve copies of a book. Returns the new reservation."""
reservation_id = save_reservation(isbn, copies) # stores it, returns the new id
return Reservation(reservation_id=reservation_id, isbn=isbn, copies=copies){
"properties": {
"reservation_id": {"title": "Reservation Id", "type": "integer"},
"isbn": {"title": "Isbn", "type": "string"},
"copies": {"title": "Copies", "type": "integer"}
},
"required": ["reservation_id", "isbn", "copies"],
"title": "Reservation",
"type": "object"
}{
"resultType": "complete",
"content": [
{
"type": "text",
"text": "{\"reservation_id\": 41, \"isbn\": \"9780441013593\", \"copies\": 1}"
}
],
"structuredContent": {
"reservation_id": 41,
"isbn": "9780441013593",
"copies": 1
}
}The first field, resultType, only says that this is the final result of the call. Every result in the current version of the protocol carries it, and the SDK fills it in. The text block holds the same object serialized as JSON, because the specification asks a tool that returns structured content to include that text too, for clients that only read content. A program then reads structuredContent["reservation_id"] and gets the integer 41. In the Python client the two parts are the attributes content and structured_content of the result: the SDK spells names in snake_case in Python and in camelCase on the wire.
The output schema is a contract in both directions. Before a result leaves the server, the SDK validates the returned value against it; a value that does not fit comes back as an error result instead of wrong data, with the details in the server log. A client should validate what it receives against the same schema.
A return type that is not a record still gets a schema. For -> int or -> str the SDK wraps the value in an object with one key, so a tool returning 17 gives {"result": 17} as structured content and "17" as text. Lists are wrapped the same way. One case fails quietly: a plain class without type-hinted attributes gives no output schema and no structured content, only text.
True or false?
Structured output means the model is forced to answer in JSON that matches a schema.
False. structuredContent is data the server produces from what the function returns. The model is not involved in creating it.
A tool annotated `-> int` returns structured content too.
True. The SDK wraps a plain value under a result key, so the output schema is an object with one integer field and the structured content is, for example, {"result": 17}.
If a function returns a value that does not match its output schema, the client receives the value anyway and has to notice the mismatch.
False. The SDK checks the value before sending it, and a mismatch reaches the client as an error result rather than as data.