Tools

Tools are functions that your LLM can actively call and decides when to use based on user requests. They enable AI models to perform actions such as writing to databases, calling external APIs, modifying files, or triggering other logic.

xmcp detects files under the /src/tools/ directory and registers them as tools. This path can be configured in the xmcp.config.ts file.

A tool file consists of three main exports:

  • Default: The tool handler function.
  • Schema (optional): The input parameters using Zod schemas.
  • Metadata (optional): The tool's identity and behavior hints. If omitted, the name is inferred from the file name and the description defaults to a placeholder.
src/tools/greet.ts

If you're returning a string or number only, you can shortcut the return value to be the string or number directly.

We encourage to use this shortcut for readability, and restrict the usage of the content array type only for complex responses, like images, audio or videos.

References

Schema (optional)

Defines the tool's input parameters with type validation:

  • Key: Parameter name.
  • Value: Zod schema with .describe() for documentation and tool inspection.
  • Purpose: Type validation and automatic parameter documentation.

Metadata (optional)

Defines the tool's identity and behavior hints:

  • Name: Unique identifier for the tool (defaults to file name if omitted)
  • Description: Brief explanation of what the tool does (defaults to placeholder if omitted)
  • Annotations: Behavioral hints for AI models and UIs

While metadata is optional, we strongly recommend providing a clear description. This helps LLMs discover and use your tools more efficiently, reducing token costs and improving accuracy.

Implementation (required)

The default export function that performs the actual work:

  • Parameters: Automatically typed from your schema using the built-in InferSchema.
  • Returns: MCP-compatible response with content array, or a string/number shortcut.
  • Async: Supports async operations for API calls, file I/O, etc.

On this page

One framework to rule them all

    Tools | xmcp Documentation