MCP Tools
Independent educational resource, not an official Anthropic course. Facts about the API, prices and limits were checked against Anthropic's documentation on 15 November 2026 and the code was run against a local mock of the API, not the live service. Check the linked official pages before relying on a number, and test with your own key.
Tools are the part of MCP that matters most and the part that can do the most damage. A resource can only be read. A tool can change something. This lesson covers what a tool definition contains, how to write ones a model uses well, how errors should work, and what the specification says about keeping a person in the loop.
What you'll learn
- The fields of an MCP tool and what each is for.
- Tool annotations, and why they're hints rather than rules.
- Structured results and output schemas.
- The two kinds of error, and which one the model should see.
- Designing tools that survive a stateless protocol.
Before you start
Anatomy of a tool
From the specification, a tool definition includes:
name: unique within the server. It should be 1 to 128 characters using letters, digits, underscore, hyphen and dot, with no spaces.
title: an optional human-readable name for display.
description: what it does and when to use it. This is what the model reads, so write it like the descriptions in lesson 6.6.
inputSchema: JSON Schema for the arguments. For a tool with no parameters the spec recommends {"type": "object", "additionalProperties": false}.
outputSchema: optional JSON Schema for structured results.
annotations: optional hints about behaviour.
In Python you rarely write these by hand. The decorator builds the schema from your type hints and the description from your docstring:
@mcp.tool(title="Look up an order", annotations=ToolAnnotations(readOnlyHint=True, openWorldHint=False))
def lookup_order(order_id: str) -> OrderView:
"""Look up one order by id, for example A100. Returns status, carrier, tracking number and total.
Does not return payment details or the delivery address."""
We listed this tool through a client and checked that order_id is required, that the read-only hint is set, and that an output schema exists.
Annotations are hints
The annotation fields the SDK exposes are readOnlyHint, destructiveHint, idempotentHint and openWorldHint (plus an optional title). They help a host decide how to present a tool, for instance showing a confirmation for one that isn't read-only. The specification's warning is the part to remember: clients must consider tool annotations to be untrusted unless they come from trusted servers. A server that lies, or has a bug, can mark a destructive tool as read-only. As a server author, set them honestly. As a client author, make approval decisions from your own policy, as the bridge in lesson 7.3 does.
Structured results
A tool can return plain text content, structured data or both. If the function's return type is a TypedDict or a Pydantic model, the SDK builds an output schema and returns both a text copy and a structuredContent value. We found a few details by testing:
- A function annotated
-> dict with no fields returned text only. No structured content and no output schema. Use a TypedDict when you want a schema.
- A function returning a list had its result wrapped as
{"result": [...]} in the structured content.
- The specification says a tool that returns structured content should also return the serialized JSON as text for older clients, and the SDK did.
class OrderView(TypedDict):
order_id: str
status: str
carrier: str | None
tracking: str | None
total: str
Return only fields the model needs. The server's internal order record includes a customer id, but the OrderView doesn't, and a test checks that it never appears in the output.
Two kinds of error
The specification separates them clearly:
- Protocol errors are JSON-RPC errors for problems with the request itself: an unknown tool, a malformed request, a server fault. Models are less likely to fix these.
- Tool execution errors are results with
isError: true: an API failure, a validation problem, a business rule. These carry a message the model can act on, and clients should pass them to the model so it can correct itself.
In the Python SDK you raise ToolError with a message for the second kind. The SDK prefixes the text with "Error executing tool lookup_order:" and sets isError. We tested four failures: an unknown order, a badly formed id, a missing required argument and an out-of-range filter. All four came back as error results, not exceptions in the client, and the messages were specific. One behaviour to check in your own SDK version: calling a tool name that doesn't exist came back as an error result in the version we used, whereas the specification's example shows a protocol error. Handle both.
if o is None:
raise ToolError(f"No order {order_id}. Ask the customer to check the number.")
Write errors for the model: say what went wrong and what to try. Keep secrets, stack traces and internal hostnames out of them.
Validate everything
The specification lists what servers must do: validate all inputs, implement access controls, rate limit calls and sanitize outputs. The SDK checks types against the generated schema, but business rules are yours. Our server checks the order id format with a regular expression, caps limit between 1 and 20 and limits the reason text to 300 characters. A model can produce unexpected values, and so can anything that influenced the model.
Tools without sessions
Because the current protocol has no sessions, a server can't rely on "the connection I'm talking to" to remember a shopping cart. The specification's advice is to return an explicit handle from a creating tool and accept it as an argument later. If you do that, treat the handle as a name, not a credential: check the caller is allowed to use it on every call, make handles unguessable, give them an expiry and say so in the tool description. It's easy to get wrong, so when you can, prefer tools that take everything they need as arguments.
Keeping a person in the loop
The specification says that for trust and safety there should always be a human in the loop with the ability to deny tool invocations, and it suggests UI that shows which tools are exposed, clear indicators when one runs, and confirmation prompts. A tool server can't do this by itself, because it doesn't control the interface. That's why our propose_refund tool never pays anything: it queues a proposal for a person. When you design a tool, ask what the worst call looks like and whether a person sees it first.
Real-world use case (illustrative scenario)
This scenario is an invented example to show how the ideas fit together. It is not a case study and its details are not measured results.
A first version of a shop server has a manage_order(action, order_id) tool where action can be "lookup", "cancel" or "refund". It works in a demo. In review, someone notices that a read-only client can't be told apart from a destructive one, since one tool does both, so the host can't ask for confirmation only on the dangerous actions. They split it into lookup_order (read-only), list_orders (read-only) and propose_refund (queues for approval), each with its own annotations and its own permission.
Mistakes to avoid
- One tool with a mode parameter that mixes safe and dangerous actions.
- Returning whole internal records instead of the fields the model needs.
- Throwing generic errors that the model can't act on.
- Believing annotations enforce anything.
- Using handles as if they were authentication.
Security notes
A tool's inputs may be influenced by anything the model has read, including hostile text. Validate on the server regardless of what the client did, since your server may be used by other clients. Rate limit and size limit arguments, and never build file paths, SQL or shell commands directly from a model-supplied string.
Cost and speed
Each tool's name, description and schema are sent to the model on every request, so concise, clear descriptions pay for themselves. Keep results small: a list tool should have a limit and a sensible default, as ours does.
Key takeaways
- A tool is a name, a description, an input schema and optional output schema and annotations.
- Annotations are hints; clients must not trust them from untrusted servers.
- Use a TypedDict or model for structured output, and return only what's needed.
- Report problems the model can fix as tool execution errors, with a message it can act on.
- Validate on the server and keep risky actions behind a person.
Lesson recap: what a tool contains, which errors the model should see, and where safety lives.
Quick quiz
- Where does the model get information about when to use a tool?
- Why can't a client rely on
readOnlyHint alone?
- How do you give a Python tool an output schema?
- What's the difference between a protocol error and a tool execution error?
- Why is a state handle not a credential?
Answers and explanations
- From the tool's description (and its input schema). Write the description for the model.
- Annotations are untrusted unless the server is trusted. A wrong or malicious hint could cause a client to skip a needed confirmation.
- Annotate the return type with a TypedDict or a Pydantic model. A plain
dict gave no schema in our tests.
- A protocol error is a JSON-RPC error about the request itself, such as an unknown tool. A tool execution error is a normal result with
isError: true and a message the model can use to correct itself.
- Anyone who guesses or learns it could use it. The server must still check, on every call, that the caller is allowed to use that handle.
Official references and further reading
Related tutorials