类型安全API客户端工具

SuFetch是一款结合类型安全API客户端和MCP服务器的工具,用于从OpenAPI规范生成完全类型化的TypeScript客户端,并允许AI助手探索API和生成代码。

  • by productdevbook
  • TypeScript
  • 本地部署
  • 19 星标
  • 更新于
在 GitHub 查看 productdevbook/sufetch

使用说明

SuFetch

带MCP服务器的类型安全的OpenAPI客户端,用于AI驱动的API探索

目录


SuFetch是什么?

SuFetch结合了两个强大的工具:

  1. 类型安全的API客户端 -根据OpenAPI规范生成全类型TypeScript客户端
  2. MCP服务器 -让AI助手(如Claude)探索您的API并生成代码

内置 阿匹富 对于类型安全的OpenAPI客户端。

特性

  • ✨ 完全类型安全 -所有API调用的自动完成和类型检查
  • 🤖 MCP集成 -AI助手可以为您的API探索和生成代码
  • 🔄 自动发现 -自动服务检测和类型生成
  • 🛠️ 现代堆栈 -TypeScript 5.7,ESNext,严格模式
  • 🧪 测试良好 -76+次测试,覆盖率>60%

安装

用于使用API客户端

# npm
npm install sufetch

# pnpm
pnpm add sufetch

# yarn
yarn add sufetch

用于MCP服务器(全局)

# Install globally
npm install -g sufetch

# Verify installation
sufetch-mcp --version

为了发展

git clone https://github.com/productdevbook/sufetch.git
cd sufetch
pnpm install
pnpm build

快速开始

使用类型安全的API客户端

import { createClient, cloud } from 'sufetch/hetzner'

// Create a typed client
const client = createClient({
  baseURL: 'https://api.hetzner.cloud/v1',
  headers: {
    'Authorization': 'Bearer your-api-token'
  }
}).with(cloud)

// Fully typed requests and responses
const servers = await client('/servers', {
  method: 'GET'  // ✅ Type-checked
})

// TypeScript knows the response type
console.log(servers.servers)  // ✅ Autocomplete works

看 支持的API 对于所有可用的服务。

高级字体安全的字体助手

从端点提取特定类型以获得最大类型安全性:

import type { HetznerCloud } from 'sufetch/hetzner'

// Extract request body type
type CreateServerBody = HetznerCloud<'/servers', 'post'>['request']

// Extract response type
type GetServerResponse = HetznerCloud<'/servers/{id}', 'get'>['response']

// Extract query parameters
type ListServersQuery = HetznerCloud<'/servers', 'get'>['query']

// Extract path parameters
type ServerPathParams = HetznerCloud<'/servers/{id}', 'get'>['path']

// Use in functions for type safety
function processServer(server: GetServerResponse) {
  console.log(server.server.id)    // ✅ Full autocomplete
  console.log(server.server.name)  // ✅ Type-checked
}

function createServer(body: CreateServerBody) {
  // TypeScript enforces correct structure
  return client('/servers', {
    method: 'POST',
    body  // ✅ Type-safe
  })
}

可用属性:

  • ['request'] -请求正文类型
  • ['response'] -成功响应(200/201)
  • ['query'] -查询参数
  • ['path'] -路径参数
  • ['responses'][status] -特定状态代码响应

适用于所有API: HetznerCloud, DigitalOcean, OryKaratos, OryHydra.

使用AI助手(MCP)

看 MCP服务器设置 下面的部分。

支持的API

SuFetch目前包括:

API 描述 端点 导入
数字海洋 完整的云平台API 200+ sufetch/digitalocean
Hetzner 云 云基础设施管理 100+ sufetch/hetzner
奥里·奎托斯 身份和用户管理 50+ sufetch/ory
Ory Hydra OAuth 2.0和OpenID连接 40+ sufetch/ory

想添加更多吗? 看 添加新API.

MCP服务器设置

快速设置

1.安装(选择一个):

npm install -g sufetch  # Global
npx sufetch-mcp         # No install

2.配置:

Claude Desktop (click to expand)

编辑配置文件:

  • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
  • 窗户: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "sufetch": {
      "command": "sufetch-mcp"
    }
  }
}

重新启动克劳德桌面。

Claude Code CLI (click to expand)
claude mcp add --transport stdio --scope project sufetch -- sufetch-mcp

或创建 .mcp.json:

{
  "mcpServers": {
    "sufetch": {
      "command": "sufetch-mcp"
    }
  }
}

3.测试: 问Claude:“使用sufetch列出可用的API”

可用的MCP工具

工具 说明
list_apis 列出所有可用的API
get_api_info 获取API元数据
search_endpoints 按路径/方法/描述搜索
get_endpoint_details 获取完整的端点规格
get_schema_details 获取数据模式
generate_code_example 生成TypeScript代码
get_quickstart 获取API快速启动指南

添加新API

Click to see how to add your own OpenAPI specs
  1. 创建目录: mkdir -p openapi-specs/myapi
  2. 添加您的 myapi.json OpenAPI规范
  3. 复制 apiful.config.ts 和 index.ts 从 openapi-specs/ory/ 作为模板
  4. 跑 pnpm build

完成!您的API现在可用作 sufetch/myapi 在MCP服务器中。

看 CLAUDE.md 详细说明。

发展

pnpm install  # Install
pnpm build    # Build
pnpm test     # Test
pnpm lint:fix # Lint

看 CLAUDE.md 架构、构建管道和贡献指南。

故障排除

MCP Server not showing?
# Test server works
sufetch-mcp  # Should output: "SuFetch MCP server running on stdio"

# Check config
claude mcp list  # For Claude Code
cat .mcp.json    # Check file exists

# Restart Claude Desktop (if using Desktop)
Build issues?
rm -rf node_modules pnpm-lock.yaml dist
pnpm install && pnpm build

还在卡住吗? 打开一个问题 包含您的节点版本和错误消息。

贡献

欢迎投稿!看 贡献.md.

git clone https://github.com/productdevbook/sufetch.git
cd sufetch
pnpm install && pnpm build
# Make changes, run `pnpm test && pnpm lint:fix`

链接

许可证

麻省理工学院©2025


内置 阿匹富 · 主控程序