API类型生成服务

一个通过查询API并自动从响应生成TypeScript类型的模型上下文协议(MCP)服务器,适用于需要快速获取外部API类型定义的前端开发人员。

  • by tyzrex
  • TypeScript
  • 本地部署
  • 更新于
在 GitHub 查看 tyzrex/api-response-types-mcp-server

使用说明

API类型MCP服务器

一个模型上下文协议(MCP)服务器,查询API并从响应中自动生成TypeScript类型。非常适合希望快速获取外部API类型定义的前端开发人员。

特性

  • 🚀 查询任何REST API端点
  • 🎯 根据JSON响应自动生成TypeScript接口
  • 🔍 支持所有HTTP方法(GET、POST、PUT、DELETE、PATCH)
  • 🛡️ 请求验证和错误处理
  • 📊 API响应结构分析
  • 🔧 自定义接口命名
  • ✅ 根据预期结构进行响应验证

安装

  1. 克隆项目:
git clone https://github.com/tyzrex/Api-Response-Types-MCP-Server api-types-mcp-server
cd api-types-mcp-server
  1. 安装依赖项:
pnpm install
  1. 构建项目:
pnpm run build

设置

  1. 将服务器添加到MCP配置文件中(通常在Claude Desktop设置中):
{
  "mcpServers": {
    "api-types": {
      "command": "node",
      "args": ["./dist/server.js"],
      "cwd": "/absolute/path/to/api-types-mcp-server"
    }
  }
}
  1. 重新启动Claude Desktop以加载服务器。

可用工具

1.query_api

查询API终结点并根据响应生成TypeScript类型。

参数:

  • url (必需):API端点URL
  • method (可选):HTTP方法(默认:GET)
  • headers (可选):请求标头
  • body (可选):请求POST/PUT/PATCH的正文
  • interfaceName (可选):自定义接口名称

示例用法:

Query the JSONPlaceholder API: https://jsonplaceholder.typicode.com/posts/1

2.generate_types_from_json

从JSON对象生成TypeScript类型。

参数:

  • json (必填):用于生成类型的JSON对象
  • interfaceName (可选):自定义接口名称

3.validate_api_response

验证API响应结构并与预期格式进行比较。

参数:

  • url (必需):API端点URL
  • expectedStructure (可选):预期响应结构

使用示例

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[];
}