API类型MCP服务器
一个模型上下文协议(MCP)服务器,查询API并从响应中自动生成TypeScript类型。非常适合希望快速获取外部API类型定义的前端开发人员。
特性
- 🚀 查询任何REST API端点
- 🎯 根据JSON响应自动生成TypeScript接口
- 🔍 支持所有HTTP方法(GET、POST、PUT、DELETE、PATCH)
- 🛡️ 请求验证和错误处理
- 📊 API响应结构分析
- 🔧 自定义接口命名
- ✅ 根据预期结构进行响应验证
安装
- 克隆项目:
git clone https://github.com/tyzrex/Api-Response-Types-MCP-Server api-types-mcp-server
cd api-types-mcp-server- 安装依赖项:
pnpm install- 构建项目:
pnpm run build设置
- 将服务器添加到MCP配置文件中(通常在Claude Desktop设置中):
{
"mcpServers": {
"api-types": {
"command": "node",
"args": ["./dist/server.js"],
"cwd": "/absolute/path/to/api-types-mcp-server"
}
}
}- 重新启动Claude Desktop以加载服务器。
可用工具
1.query_api
查询API终结点并根据响应生成TypeScript类型。
参数:
url(必需):API端点URLmethod(可选):HTTP方法(默认:GET)headers(可选):请求标头body(可选):请求POST/PUT/PATCH的正文interfaceName(可选):自定义接口名称
示例用法:
Query the JSONPlaceholder API: https://jsonplaceholder.typicode.com/posts/12.generate_types_from_json
从JSON对象生成TypeScript类型。
参数:
json(必填):用于生成类型的JSON对象interfaceName(可选):自定义接口名称
3.validate_api_response
验证API响应结构并与预期格式进行比较。
参数:
url(必需):API端点URLexpectedStructure(可选):预期响应结构
使用示例
API基本查询
Use the query_api tool to get types for: https://api.github.com/users/octocat自定义头
Query https://api.example.com/data with Authorization header: Bearer your-token带正文的POST请求
Make a POST request to https://api.example.com/users with body:
{
"name": "John Doe",
"email": "john@example.com"
}从JSON生成类型
Generate TypeScript types for this JSON:
{
"id": 1,
"name": "Product",
"price": 29.99,
"inStock": true,
"tags": ["electronics", "gadget"]
}生成的输出
服务器生成干净的TypeScript接口,如:
interface UserResponse {
id: number;
name: string;
email: string;
address: {
street: string;
city: string;
zipcode: string;
};
posts: Post[];
}