name: mcp-tool-schema-generator description: 为MCP工具输入参数生成JSON Schema定义。创建文档完善、AI可消费的模式,包含适当的类型、描述和验证规则。 allowed-tools: Read, Write, Edit, Bash, Glob, Grep
MCP工具模式生成器
为MCP工具生成全面的JSON Schema定义,优化AI消费,包含清晰的描述、类型约束和验证规则。
能力
- 为工具输入生成JSON Schema
- 从模式创建TypeScript类型
- 生成Zod验证模式
- 生成AI优化的描述
- 支持复杂的嵌套结构
- 处理枚举、联合类型和约束
使用场景
当您需要时调用此技能:
- 定义MCP工具的输入模式
- 创建类型安全的工具参数定义
- 为工具输入生成验证逻辑
- 为AI消费记录工具参数
输入参数
| 参数 | 类型 | 是否必需 | 描述 |
|---|---|---|---|
| toolName | 字符串 | 是 | MCP工具名称 |
| description | 字符串 | 是 | 工具描述 |
| parameters | 数组 | 是 | 参数定义列表 |
| outputFormat | 字符串 | 否 | json-schema、zod或typescript(默认:全部) |
| examples | 数组 | 否 | 文档的示例输入 |
参数定义结构
{
"parameters": [
{
"name": "path",
"type": "string",
"description": "要读取的文件路径",
"required": true,
"constraints": {
"pattern": "^[a-zA-Z0-9_/.-]+$",
"maxLength": 255
}
},
{
"name": "encoding",
"type": "string",
"description": "文件编码",
"required": false,
"default": "utf-8",
"enum": ["utf-8", "ascii", "base64"]
},
{
"name": "options",
"type": "object",
"description": "读取选项",
"required": false,
"properties": [
{ "name": "maxSize", "type": "number", "description": "最大读取字节数" },
{ "name": "follow", "type": "boolean", "description": "是否跟随符号链接" }
]
}
]
}
输出结构
schemas/
├── <toolName>/
│ ├── schema.json # JSON Schema定义
│ ├── schema.ts # TypeScript类型
│ ├── validation.ts # Zod验证模式
│ ├── examples.json # 示例输入
│ └── README.md # 模式文档
生成的代码模式
JSON Schema输出
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "mcp://tools/read_file/input",
"title": "read_file输入模式",
"description": "从文件系统读取文件内容",
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "要读取的文件路径。必须是有效的文件系统路径。",
"pattern": "^[a-zA-Z0-9_/.-]+$",
"maxLength": 255,
"examples": ["/home/user/document.txt", "./config.json"]
},
"encoding": {
"type": "string",
"description": "文件编码。文本文件默认为UTF-8。",
"enum": ["utf-8", "ascii", "base64"],
"default": "utf-8"
},
"options": {
"type": "object",
"description": "附加读取选项",
"properties": {
"maxSize": {
"type": "integer",
"description": "最大读取字节数。用于大文件。",
"minimum": 1,
"maximum": 10485760
},
"follow": {
"type": "boolean",
"description": "是否跟随符号链接",
"default": true
}
}
}
},
"required": ["path"],
"additionalProperties": false
}
TypeScript类型输出
/**
* read_file工具的输入参数
* @description 从文件系统读取文件内容
*/
export interface ReadFileInput {
/**
* 要读取的文件路径。必须是有效的文件系统路径。
* @pattern ^[a-zA-Z0-9_/.-]+$
* @maxLength 255
* @example "/home/user/document.txt"
*/
path: string;
/**
* 文件编码。文本文件默认为UTF-8。
* @default "utf-8"
*/
encoding?: 'utf-8' | 'ascii' | 'base64';
/**
* 附加读取选项
*/
options?: {
/**
* 最大读取字节数。用于大文件。
* @minimum 1
* @maximum 10485760
*/
maxSize?: number;
/**
* 是否跟随符号链接
* @default true
*/
follow?: boolean;
};
}
Zod验证输出
import { z } from 'zod';
/**
* read_file工具输入的Zod模式验证
*/
export const ReadFileInputSchema = z.object({
path: z
.string()
.regex(/^[a-zA-Z0-9_/.-]+$/, '无效的路径字符')
.max(255, '路径过长')
.describe('要读取的文件路径。必须是有效的文件系统路径。'),
encoding: z
.enum(['utf-8', 'ascii', 'base64'])
.optional()
.default('utf-8')
.describe('文件编码。文本文件默认为UTF-8。'),
options: z
.object({
maxSize: z
.number()
.int()
.min(1)
.max(10485760)
.optional()
.describe('最大读取字节数。用于大文件。'),
follow: z
.boolean()
.optional()
.default(true)
.describe('是否跟随符号链接'),
})
.optional()
.describe('附加读取选项'),
}).strict();
export type ReadFileInput = z.infer<typeof ReadFileInputSchema>;
支持的类型
| 类型 | JSON Schema | TypeScript | Zod |
|---|---|---|---|
| 字符串 | string | string | z.string() |
| 数字 | number | number | z.number() |
| 整数 | integer | number | z.number().int() |
| 布尔值 | boolean | boolean | z.boolean() |
| 数组 | array | T[] | z.array() |
| 对象 | object | interface | z.object() |
| 枚举 | enum | union | z.enum() |
| 空值 | null | null | z.null() |
| 联合类型 | oneOf | union | z.union() |
约束支持
| 约束 | 类型 | JSON Schema | Zod |
|---|---|---|---|
| minLength | 字符串 | minLength | .min() |
| maxLength | 字符串 | maxLength | .max() |
| pattern | 字符串 | pattern | .regex() |
| format | 字符串 | format | .email()/.url() |
| minimum | 数字 | minimum | .min() |
| maximum | 数字 | maximum | .max() |
| multipleOf | 数字 | multipleOf | .multipleOf() |
| minItems | 数组 | minItems | .min() |
| maxItems | 数组 | maxItems | .max() |
| uniqueItems | 数组 | uniqueItems | 自定义 |
AI优化的描述
该技能生成针对AI消费优化的描述:
- 明确目的:参数的作用
- 有效值:可接受的输入
- 示例:具体使用示例
- 约束:限制和验证规则
- 默认值:如果省略会发生什么
示例:
"要读取的文件路径。必须是现有文件的绝对或相对路径。
支持Unix和Windows路径格式。最大长度:255个字符。
示例:'/home/user/config.json'"
工作流程
- 解析参数 - 验证参数定义
- 推断类型 - 确定TypeScript/Zod类型
- 生成JSON Schema - 创建草案2020-12模式
- 生成TypeScript - 创建接口定义
- 生成Zod模式 - 创建验证代码
- 创建示例 - 生成有效的示例输入
- 文档模式 - 创建包含使用说明的README
应用的最佳实践
- JSON Schema草案2020-12合规性
- 严格模式(additionalProperties: false)
- 针对AI的全面描述
- 类型安全的TypeScript接口
- 使用Zod进行运行时验证
- 每个参数的示例输入
参考资料
- JSON Schema规范:https://json-schema.org/
- MCP工具文档:https://modelcontextprotocol.io/docs/tools
- Zod文档:https://zod.dev/
目标流程
- mcp-tool-implementation
- mcp-tool-documentation
- argument-parser-setup