What are Tools?
Tools are the core primitives in MCP that allow AI models to perform actions. They represent callable functions that models can invoke with structured arguments to accomplish tasks like fetching data, performing calculations, or interacting with external services. In xmcp, tools are automatically registered from thesrc/tools/ directory using file-system routing.
Tool Structure
Every tool consists of three main exports:1. Metadata Export
Themetadata export defines the tool’s identity and behavior hints:
ToolMetadata Type
TheToolMetadata interface from ~/workspace/source/packages/xmcp/src/types/tool.ts:19-32 includes:
Unique identifier for the tool
Human-readable description of what the tool does
Optional hints about tool behavior:
title- Human-readable title for the toolreadOnlyHint- If true, the tool does not modify its environmentdestructiveHint- If true, the tool may perform destructive updatesidempotentHint- If true, repeated calls with same args have no additional effectopenWorldHint- If true, tool interacts with external entities
Metadata for the tool. Supports nested OpenAI metadata and other vendor extensions:
2. Schema Export
Theschema export defines the tool’s input parameters using Zod:
3. Default Export (Handler)
The default export is the function that executes when the tool is called:Real Examples
Basic Tool: Greet
From~/workspace/source/examples/http-transport/src/tools/greet.ts:1-25:
Async Tool: Hash String
From~/workspace/source/examples/http-transport/src/tools/hash-string.ts:1-39:
Tool with Extra Arguments
From~/workspace/source/examples/template-config/src/tools/extra-arguments.ts:1-24:
The second parameter in tool handlers provides access to
ToolExtraArguments, which includes:signal- AbortSignal for request cancellationauthInfo- Validated access token informationsessionId- Transport session IDrequestId- JSON-RPC request IDrequestInfo- HTTP request headerssendNotification- Send notificationssendRequest- Send requests to the client
Complex Tool: Create User
From~/workspace/source/examples/with-nestjs/src/tools/create-user.ts:1-55:
Creating Tools with CLI
Use the xmcp CLI to quickly scaffold a new tool:src/tools/my-tool.ts with the basic structure:
You can create tools in nested directories:This creates
src/tools/api/users.ts.Return Values
Tools can return:- Simple strings - Will be wrapped in MCP content format
- MCP Content objects - For structured responses:
Best Practices
Use Descriptive Names
Choose clear, action-oriented names like
create-user, fetch-data, or calculate-price.Add Tool Annotations
Use annotations to help AI models understand tool behavior:
- Set
readOnlyHint: truefor tools that don’t modify state - Set
destructiveHint: truefor tools that delete or modify data - Set
idempotentHint: truefor tools that are safe to retry
Validate Inputs
Use Zod’s validation features like
.min(), .max(), .email() to ensure inputs are valid.Handle Errors Gracefully
Return structured error responses:
Next Steps
Building Prompts
Learn how to create reusable prompt templates
Building Resources
Create static or dynamic resources for context