Sub topic 10
Designing tools a model can use well
This chapter assumes you can write a basic MCP tool with FastMCP and know how type hints become a JSON Schema. Here the question is different: given a working tool, how do you make a model pick it at the right time and call it correctly?
What the model actually sees
The model never sees your code. It sees a tool's name, its description, and its input schema, and it chooses tools from those alone. Everything you want the model to know has to be in those three places.
Name tools for the task
Use a verb and a noun, and keep a consistent style across the server. get_service_owner and list_deploys read clearly. Names like query, run, or do_action leave the model guessing.
Prefer a small set of tools that match what people ask for over one tool per API endpoint. A model that must call five tools in order to answer "who owns billing?" will make more mistakes than one that calls a single tool.
Write descriptions as instructions
The docstring becomes the tool description. State what the tool does, what it returns, and when to use it instead of a neighbouring tool.
from typing import Annotated, Literal
from mcp.server.fastmcp import FastMCP
from pydantic import Field
mcp = FastMCP("team-tools")
DEPLOYS = {
"staging": ["v1.4.2", "v1.4.1", "v1.4.0"],
"production": ["v1.4.0", "v1.3.9"],
}
@mcp.tool()
def list_deploys(
environment: Literal["staging", "production"],
limit: Annotated[
int, Field(ge=1, le=20, description="How many versions to return")
] = 5,
) -> list[str]:
"""List the most recent deployed versions for an environment, newest first.
Use this to answer "what is running in production?" or "what shipped to
staging recently?". It returns version tags only, not deploy logs.
"""
return DEPLOYS[environment][:limit]
if __name__ == "__main__":
mcp.run()The last sentence of the docstring tells the model what the tool does not do. That prevents it from calling this tool when someone asks for logs.
Constrain inputs in the schema
Each constraint you put in the schema is one less thing the model can get wrong.
- Use
Literalfor fixed choices, so the model sees the allowed environments. - Use bounds such as
geandleso invalid numbers are rejected before your code runs. - Add a
descriptionto any parameter whose meaning is not obvious from its name. - Accept the identifiers people actually use, such as a short service name, rather than internal database IDs.
Shape outputs for the model
Return what the model needs to answer, not the raw API response. A 200-field JSON blob wastes context and hides the useful part.
- Return the relevant fields only, with clear names.
- Cap list sizes and say so, for example by returning a
limitparameter and a note when results were truncated. - Put the most important information first.
- Write error messages that say what to do next, such as
Unknown service: bilng. Known services: billing, search.
Common design mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Vague name such as `query` | Model calls it for the wrong requests | Name the task: `search_incidents` |
| Overlapping tools | Model picks inconsistently | Say in each description when to use it |
| Free-text parameter for a fixed set | Invalid values reach your code | Use `Literal` or an enum |
| Returning raw API payloads | Context fills with noise | Return only needed fields |
| Tool that does many unrelated things | Hard to describe and validate | Split by task |
Exercise
A tool named run takes a single string parameter cmd and returns the full JSON response from your deploy API. Name three changes that would make it easier for a model to use.
Show answer
Rename it for the task, for example list_deploys. Replace the free-text cmd with typed parameters, using Literal for fixed choices and bounds for numbers. Return only the fields the model needs, and add a description that says when to use the tool and what it does not return.