Topic 2 of 5 · 5 min read
Tool input schemas, output schemas and annotations
Goal: Write a tool definition with input and output schemas and honest annotations, and spot a schema that will make models call it wrongly.
A tool definition is the only documentation the model gets, so make the name, description, schema and annotations say exactly what the tool needs and does. The model never sees your code. It sees tools/list, then guesses how to call the tool from that text.
Check yourself · 1 of 3
A tool fails because the order does not exist. How should the server report it so the model can read and recover?
Why it matters
Most failed tool calls come from the definition, not the handler. A vague description, a free-text field that should be an enum or a missing required flag makes the model invent values. Nothing in the server throws an error, because the call is technically valid.
The parts
- Name and description. Say what the tool does, when to use it and when not to. Mention units, formats and an example value.
inputSchema. A JSON Schema object. Mark required fields. Useenumfor closed sets,patternorformatfor ids and dates,minimumandmaximumfor numbers, and adescriptionon every property.outputSchemaandstructuredContent. If you declare an output schema, return a JSON object instructuredContentthat matches it, so clients can validate and use it. The spec also asks you to return the same data as text incontentfor clients that do not read structured output.- Annotations. Hints such as
readOnlyHint,destructiveHint,idempotentHintandopenWorldHinthelp a client decide when to ask for confirmation. They are hints, not enforcement. Clients should not trust them from a server they do not trust, and you must still check permissions in the handler. - Errors. A failure of the tool's work, such as "order not found", is returned as a normal result with
isError: trueand a message the model can read and act on. Protocol errors, such as an unknown tool, use JSON-RPC errors.
Before and after
{
"name": "update",
"description": "Updates data",
"inputSchema": { "type": "object", "properties": { "id": {}, "value": {} } }
}
The model cannot tell what is updated, what id means or what value is. A fixed version:
{
"name": "set_order_status",
"description": "Change the status of one order. Use only after the user confirms. Example id: ORD-1042.",
"inputSchema": {
"type": "object",
"properties": {
"orderId": { "type": "string", "pattern": "^ORD-\\d+$" },
"status": { "type": "string", "enum": ["packed", "shipped", "cancelled"] }
},
"required": ["orderId", "status"],
"additionalProperties": false
},
"annotations": { "readOnlyHint": false, "destructiveHint": true, "idempotentHint": true }
}
What goes wrong
- Lying annotations. Marking a delete tool
readOnlyHint: truecan skip a confirmation the user would have wanted. - Throwing for expected failures. A raised exception becomes a protocol error the model may not be able to read. Return
isError: truewith a plain message instead. - Output that does not match
outputSchema. Strict clients reject it. - Huge free-text arguments. Models paste whole documents into a field described only as "input".
Examples
Read-only tool with an output schema
server.registerTool(
"get_order",
{
description: "Fetch one order by id. Read-only. Example: ORD-1042.",
inputSchema: { orderId: z.string().regex(/^ORD-\d+$/) },
outputSchema: { status: z.string(), totalCents: z.number().int() },
annotations: { readOnlyHint: true },
},
async ({ orderId }) => {
const order = await db.find(orderId);
if (!order) {
return { isError: true, content: [{ type: "text", text: `No order ${orderId}. Check the id with search_orders.` }] };
}
const out = { status: order.status, totalCents: order.totalCents };
return { structuredContent: out, content: [{ type: "text", text: JSON.stringify(out) }] };
}
);
The failure message names the next step, so the model can recover without asking the user.
Spotting a schema that invites bad calls
A tool called run_query takes one string, q, described as "the query". Models will send SQL, English or JSON depending on the day. Rename it, state the dialect and read-only scope in the description, and split free-form text from filters such as table (enum) and limit (integer, maximum 100).
Key points
- The model sees only the tool list, so the name, description and schema must carry everything it needs to call the tool correctly.
- Use enum, pattern, minimum, maximum and required fields so invalid calls are impossible instead of merely unlikely.
- When you declare an outputSchema, return matching structuredContent, and also keep a text version in content for older clients.
- Annotations such as readOnlyHint and destructiveHint are hints for clients, so the handler must still enforce permissions.
- Report expected tool failures as results with isError set to true and a message the model can act on.
Common mistakes
- Writing one-line descriptions such as "Updates data" and leaving every property undescribed.
- Using free-text strings where an enum, pattern or numeric range would block bad values.
- Treating annotations as security, instead of enforcing permissions in the handler.
- Throwing exceptions for expected failures instead of returning isError results with a helpful message.
Check yourself · 2 of 3
Which schema change best stops a model from sending status values such as "done" or "complete" to a tool that accepts only three?
Check yourself · 3 of 3
A tool deletes records but is annotated readOnlyHint true. What is the main risk?
Practice items, not counted towards your mastery.
Start here
- Build an MCP server (official tutorial) (opens in a new tab)DocsFree
Step-by-step server in Python, TypeScript and other SDKs, including the stdout logging rule.
- MCP SDKs (opens in a new tab)DocsFree
The list of official SDKs by language and their support tiers.
- MCP specification: Tools (opens in a new tab)StandardFree
The normative definition of tool schemas, outputSchema, structuredContent, annotations and isError.