create-tool
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCreate xmcp Tool
创建xmcp工具
You are helping the user create a new xmcp tool. Follow this interactive workflow.
您正在协助用户创建一个全新的xmcp工具,请遵循以下交互式工作流程。
Step 1: Gather Information
步骤1:收集信息
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:
- Standard - For data queries, calculations, API calls (returns string/JSON)
- Template Literal (HTML) - For HTML widgets with external scripts
- React Component - For interactive stateful widgets (.tsx file)
-
Schema fields: What parameters should the tool accept? Ask for:
- Parameter name
- Type (string, number, boolean, enum, array, object)
- Description
- Whether it's optional
- Any validation rules
-
Annotations: Ask about behavior hints:
- Is it read-only? (doesn't modify environment)
- Is it destructive? (may delete or modify data)
- Is it idempotent? (repeated calls have same effect)
- Does it interact with external world? (openWorldHint)
-
Widget support (if Template Literal or React): Ask if they need:
- OpenAI tool invocation messages (invoking/invoked status)
- Widget accessibility (widgetAccessible)
- Widget-to-tool communication
在生成任何代码之前,请使用AskUserQuestion向用户询问以下问题:
-
工具名称(若未作为参数提供):工具应该命名为什么?(请使用短横线分隔式命名(kebab-case))
-
处理器类型:询问用户需要哪种处理器类型:
- 标准型 - 用于数据查询、计算、API调用(返回字符串/JSON)
- 模板字面量(HTML) - 用于包含外部脚本的HTML小部件
- React Component - 用于交互式有状态小部件(.tsx文件)
-
Schema字段:工具应接受哪些参数?请询问:
- 参数名称
- 类型(字符串、数字、布尔值、枚举、数组、对象)
- 描述
- 是否为可选参数
- 任何验证规则
-
注解:询问有关行为提示的信息:
- 它是否为只读?(不修改环境)
- 它是否具有破坏性?(可能删除或修改数据)
- 它是否具有幂等性?(重复调用效果相同)
- 它是否与外部系统交互?(openWorldHint)
-
小部件支持(若选择模板字面量或React类型):询问用户是否需要:
- OpenAI工具调用消息(调用中/已调用状态)
- 小部件可访问性(widgetAccessible)
- 小部件与工具的通信
Step 2: Generate the Tool
步骤2:生成工具
Create the tool file in with the appropriate extension:
src/tools/- for Standard and Template Literal handlers
.ts - for React Component handlers
.tsx
在目录下创建工具文件,并使用相应的扩展名:
src/tools/- 标准型和模板字面量处理器使用
.ts - React Component处理器使用
.tsx
File Location
文件位置
src/tools/{tool-name}.ts # or .tsx for Reactsrc/tools/{tool-name}.ts # 或React组件使用.tsxTool Structure Reference
工具结构参考
Every xmcp tool has three main exports:
typescript
// 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) { /* ... */ }每个xmcp工具都有三个主要导出项:
typescript
// 1. Schema(可选)- 使用Zod定义参数
export const schema = { /* ... */ };
// 2. 元数据(可选)- 工具配置
export const metadata: ToolMetadata = { /* ... */ };
// 3. 处理器(必填)- 默认导出函数
export default function handler(params) { /* ... */ }Quick Reference
快速参考
Essential Imports
必要导入
typescript
import { z } from "zod";
import { type InferSchema, type ToolMetadata } from "xmcp";typescript
import { z } from "zod";
import { type InferSchema, type ToolMetadata } from "xmcp";Handler Types Summary
处理器类型汇总
| Type | Use Case | File Extension |
|---|---|---|
| Standard (string) | Simple queries, calculations | |
| Standard (async) | API calls, database queries | |
| Template Literal | HTML widgets, iframes, scripts | |
| React Component | Interactive stateful UIs | |
| No Parameters | Tools that don't need input | |
| With Extra Args | Auth/context-dependent tools | |
| 类型 | 使用场景 | 文件扩展名 |
|---|---|---|
| 标准型(字符串) | 简单查询、计算 | |
| 标准型(异步) | API调用、数据库查询 | |
| 模板字面量 | HTML小部件、iframe、脚本 | |
| React Component | 交互式有状态UI | |
| 无参数 | 无需输入的工具 | |
| 带额外参数 | 依赖认证/上下文的工具 | |
Annotations Quick Reference
注解快速参考
| Annotation | Use When |
|---|---|
| Tool doesn't modify environment |
| Tool may delete/modify data |
| Repeated calls have same effect |
| Tool interacts with external world |
| 注解 | 使用场景 |
|---|---|
| 工具不修改环境 |
| 工具可能删除/修改数据 |
| 重复调用效果相同 |
| 工具与外部系统交互 |
Detailed Templates
详细模板
For complete code templates including:
- Schema patterns (simple, validation, enum, optional, object)
- Metadata patterns (basic, annotations, OpenAI widgets, MCP Apps UI)
- All handler type templates (A through G)
- Return value patterns
- Type imports reference
Read:
references/templates.md如需完整的代码模板,包括:
- Schema模式(简单、验证、枚举、可选、对象)
- 元数据模式(基础、注解、OpenAI小部件、MCP Apps UI)
- 所有处理器类型模板(A至G)
- 返回值模式
- 类型导入参考
请查阅:
references/templates.mdChecklist After Generation
生成后的检查清单
- File created in (or
src/tools/{tool-name}.ts).tsx - Schema uses for all parameters
.describe() - If using metadata, ensure it has descriptive and
namedescription - Appropriate annotations set based on tool behavior
- Handler matches the chosen type
- All imports are correct
Suggest running to verify the tool compiles correctly.
pnpm build- 文件已创建在(或
src/tools/{tool-name}.ts).tsx - Schema为所有参数使用了方法
.describe() - 若使用元数据,确保其包含描述性的和
namedescription - 根据工具行为设置了合适的注解
- 处理器与所选类型匹配
- 所有导入均正确
建议运行以验证工具是否能正确编译。
pnpm build