Create a new xmcp tool with schema, metadata, and handler. Use when the user wants to add a new tool to their xmcp project.
You are helping the user create a new xmcp tool. Follow this interactive workflow.
Before generating any code, ask the user these questions using AskUserQuestion:
Tool name (if not provided as argument): What should the tool be named? (Use kebab-case)
Handler type: Ask which handler type they need:
Schema fields: What parameters should the tool accept? Ask for:
Annotations: Ask about behavior hints:
Widget support (if Template Literal or React): Ask if they need:
Create the tool file in src/tools/ with the appropriate extension:
.ts for Standard and Template Literal handlers.tsx for React Component handlerssrc/tools/{tool-name}.ts # or .tsx for React
Every xmcp tool has three main exports:
// 1. Schema (optional) - Define parameters with Zod
export const schema = { /* ... */ };
// 2. Metadata (optional) - Tool configuration
export const metadata: ToolMetadata = { /* ... */ };
// 3. Handler (required) - Default export function
export default function handler(params) { /* ... */ }
import { z } from "zod";
import { type InferSchema, type ToolMetadata } from "xmcp";
| Type | Use Case | File Extension |
|---|---|---|
| Standard (string) | Simple queries, calculations | .ts |
| Standard (async) | API calls, database queries | .ts |
| Template Literal | HTML widgets, iframes, scripts | .ts |
| React Component | Interactive stateful UIs | .tsx |
| No Parameters | Tools that don't need input | .ts |
| With Extra Args | Auth/context-dependent tools | .ts |
| Annotation | Use When |
|---|---|
readOnlyHint: true |
Tool doesn't modify environment |
destructiveHint: true |
Tool may delete/modify data |
idempotentHint: true |
Repeated calls have same effect |
openWorldHint: true |
Tool interacts with external world |
For complete code templates including:
Read: references/templates.md
src/tools/{tool-name}.ts (or .tsx).describe() for all parametersname and descriptionSuggest running pnpm build to verify the tool compiles correctly.