Skip to content
Back to log

How to write an MCP tool definition

Chainabit's Chao · · 7 min read

In short

An MCP tool definition makes clear what a tool does, what inputs it expects, and how its output is structured. Explore the fields and evaluation criteria with examples from Model Context Protocol and Anthropic.

Model Context Protocol defines each tool with a unique name and metadata that describes its schema. According to Anthropic, tool descriptions and definitions are loaded into the agent’s context and can guide how it calls tools. The definition includes the name and a short description, as well as the expected inputs, outputs, and fields that describe the tool’s behavior.

Define the tool’s purpose

Anthropic recommends giving each tool a clear, distinct purpose. Tools should help the agent break a task into smaller pieces and solve them. Too many tools or tools with overlapping functions can make it harder for an agent to choose an approach. Tools with unclear purposes can also leave the agent unsure which one to use.

Anthropic’s address book example uses search_contacts or message_contact in place of list_contacts. Having the agent read through a list of every contact can fill its limited context with irrelevant information. The search_contacts tool is designed to find the relevant contact.

A tool can combine several operations or API calls behind the scenes. Anthropic gives the example of schedule_event, which finds a suitable time and creates an event in place of separate list_users, list_events, and create_event operations. This lets the tool handle a common multistep task in a single call.

In another example, search_logs returns the relevant log lines along with the surrounding context. The get_customer_context tool brings together relevant customer information in place of separate get_customer_by_id, list_transactions, and list_notes tools. Anthropic also discusses how tool names can reflect the natural boundaries between tasks.

Write the name and description

In a Model Context Protocol tool definition, name is the tool’s unique identifier. The title field is an optional human-readable display name. The description field explains what the tool does in human-readable terms. The inputSchema defines the expected parameters, and the optional outputSchema defines the expected output structure. The optional annotations field describes aspects of the tool’s behavior.

Anthropic recommends writing the description as if you were explaining the tool to someone who just joined the team. Authors may take for granted details such as special query formats, the meaning of domain-specific terms, or how the entities used by the tool relate to one another. Anthropic recommends spelling out those details.

Related tools can be grouped under shared prefixes. Anthropic gives asana_search and jira_search as examples that start with the service name. More granular groupings use names such as asana_projects_search and asana_users_search. Anthropic notes that MCP clients sometimes group tools themselves.

According to Anthropic, prefixes and suffixes can affect evaluation results, and their effects vary by LLM. Anthropic recommends choosing a naming convention based on the results of your own evaluations. The tool’s name and how it relates to other tools can influence which tool the agent chooses.

Define inputs clearly

In Model Context Protocol, inputSchema is a JSON Schema that defines the expected parameters. In MCP’s weather example, the tool is called get_weather and takes a location field as input. The schema sets the type of location to string. The field’s description specifies that it expects a city name or postal code, and the required list includes location.

In the same example, the tool’s own description says that it retrieves weather information for a location. The location field’s description explains the expected input. The tool description and parameter description appear at different levels of the definition.

Anthropic recommends describing the expected inputs and outputs clearly and enforcing them with strict data models. It emphasizes using unambiguous parameter names. Its example uses user_id in place of user.

The Model Context Protocol flight search example also defines its parameters in separate fields. For searchFlights, the origin and destination fields are both defined as string. Each field has its own description, and both appear in the required list. The tool’s description explains that it searches for flights, while the parameter descriptions identify the departure and arrival locations.

Define outputs and error reporting

In MCP, outputSchema is an optional JSON Schema that defines the expected output structure. When a tool provides this schema, the server must return structured results that conform to it. Model Context Protocol recommends that the client also validate structured results against the schema.

Turn this insight into action.

Use the mobile app to continue with guided workflows.

In MCP, structured content is returned as a JSON object in the result’s structuredContent field. For backward compatibility, the standard recommends that a tool returning structured content also include the serialized JSON in a TextContent block. An output schema can provide type information and help the client and LLM process the returned data.

In the Model Context Protocol output schema example, get_weather_data defines the temperature, conditions, and humidity fields. The schema uses number for temperature and humidity and string for the description of conditions. All three fields appear in the required list. The example result shows these fields in both the structured content and the serialized text.

Anthropic recommends highlighting information relevant to the current context in tool responses. It compares technical identifiers such as uuid, 256px_image_url, and mime_type with fields such as name, image_url, and file_type. Anthropic says the latter fields are more likely to directly inform the agent’s next actions and responses.

Some workflows require both human-readable names and technical identifiers. Anthropic illustrates this with an example where search_user(name=’jane’) can be followed by send_message(id=12345). In the same approach, a response_format enum parameter can let the agent request a concise or detailed response.

In Anthropic’s example, a detailed response includes identifiers such as thread_ts, channel_id, and user_id that may be needed in later calls. A concise response returns the thread content and leaves out the identifiers. The choice of XML, JSON, or Markdown can also affect evaluation results. Anthropic notes that the best format depends on the task and the agent.

For responses that can take up too much context, Anthropic recommends using one or more of pagination, range selection, filtering, or truncation. When a response is truncated, it can include guidance to help the agent proceed. One example is to suggest smaller, targeted searches in place of a broad search when looking for information.

Error messages are also part of the information the agent receives. For issues such as input validation errors, Anthropic recommends responses that clearly spell out concrete fixes in place of cryptic error codes or tracebacks. Error responses can also include examples of correctly formatted tool inputs.

Model Context Protocol distinguishes between protocol errors and tool execution errors. Unknown tools, invalid arguments, and server errors are examples of protocol errors. API errors, invalid input data, and business logic errors can be reported in the tool result with isError: true.

Evaluate the definition

Anthropic recommends testing tools locally first, then evaluating them with tasks based on real-world use. Each evaluation prompt can be paired with a verifiable answer or outcome. Verification can be as simple as comparing the text of the expected answer with the generated answer.

Anthropic recommends avoiding overly strict validators that reject correct answers because of formatting, punctuation, or valid alternative wording. You can also specify which tools the agent is expected to use for a given prompt and answer pair. Anthropic cautions against prescribing a strategy too narrowly, since a task may have several valid solutions.

Anthropic recommends examining where the agent struggled and reviewing raw conversation logs. These logs include tool calls and tool responses. The logs can reveal behavior that the agent’s feedback leaves implicit. Conflicting tool descriptions and confusing schemas are among the issues that can be addressed during evaluation.

According to Anthropic, frequent tool errors caused by invalid parameters can indicate a need for clearer descriptions or better examples. In addition to overall accuracy, Anthropic recommends tracking metrics such as tool errors, call counts, and task duration. Tracing tool calls can reveal common workflows agents use.

Trust boundaries and security

Anthropic says annotations on MCP server tools can help identify tools that need access to the outside world or make destructive changes. Model Context Protocol says clients should trust tool annotations only when they come from trusted servers.

Model Context Protocol requires servers to validate all tool inputs, enforce access controls, and sanitize tool outputs. The standard also recommends that clients ask for user confirmation for sensitive operations and show users the tool inputs before calling the server.

Ready to start?

Download the app and continue from your phone.

End of Entry