使用 MCP 服务器进行 AI 驱动的 API 探索的类型安全 OpenAPI 客户端
SuFetch 结合了两个强大的工具:
使用 apiful 构建,用于类型安全的 OpenAPI 客户端。
# npm
npm install sufetch
# pnpm
pnpm add sufetch
# yarn
yarn add sufetch
# 全局安装
npm install -g sufetch
# 验证安装
sufetch-mcp --version
git clone https://github.com/productdevbook/sufetch.git
cd sufetch
pnpm install
pnpm build
import { createClient, cloud } from 'sufetch/hetzner'
// 创建一个类型化的客户端
const client = createClient({
baseURL: 'https://api.hetzner.cloud/v1',
headers: {
'Authorization': 'Bearer your-api-token'
}
}).with(cloud)
// 完全类型化的请求和响应
const servers = await client('/servers', {
method: 'GET' // ✅ 类型检查
})
// TypeScript 知道响应类型
console.log(servers.servers) // ✅ 自动完成工作
参见 支持的 API 获取所有可用的服务。
从端点中提取特定类型以实现最大类型安全性:
import type { HetznerCloud } from 'sufetch/hetzner'
// 提取请求体类型
type CreateServerBody = HetznerCloud<'/servers', 'post'>['request']
// 提取响应类型
type GetServerResponse = HetznerCloud<'/servers/{id}', 'get'>['response']
// 提取查询参数
type ListServersQuery = HetznerCloud<'/servers', 'get'>['query']
// 提取路径参数
type ServerPathParams = HetznerCloud<'/servers/{id}', 'get'>['path']
// 在函数中使用以确保类型安全
function processServer(server: GetServerResponse) {
console.log(server.server.id) // ✅ 完整自动完成
console.log(server.server.name) // ✅ 类型检查
}
function createServer(body: CreateServerBody) {
// TypeScript 强制正确的结构
return client('/servers', {
method: 'POST',
body // ✅ 类型安全
})
}
可用属性:
['request'] - 请求体类型['response'] - 成功响应(200/201)['query'] - 查询参数['path'] - 路径参数['responses'][status] - 特定状态码响应适用于所有 API:HetznerCloud、DigitalOcean、OryKratos、OryHydra。
参见下面的 MCP 服务器设置 部分。
SuFetch 当前包括:
| API | 描述 | 端点 | 导入 |
|---|---|---|---|
| DigitalOcean | 完整的云平台 API | 200+ | sufetch/digitalocean |
| Hetzner Cloud | 云基础设施管理 | 100+ | sufetch/hetzner |
| Ory Kratos | 身份与用户管理 | 50+ | sufetch/ory |
| Ory Hydra | OAuth 2.0 & OpenID Connect | 40+ | sufetch/ory |
想添加更多? 参见 添加新 API。
1. 安装(选择一种):
npm install -g sufetch # 全局
npx sufetch-mcp # 不需要安装
2. 配置:
<details> <summary><b>Claude Desktop</b>(点击展开)</summary>编辑配置文件:
~/Library/Application Support/Claude/claude_desktop_config.json%APPDATA%\Claude\claude_desktop_config.json{
"mcpServers": {
"sufetch": {
"command": "sufetch-mcp"
}
}
}
重启 Claude Desktop。
</details> <details> <summary><b>Claude Code CLI</b>(点击展开)</summary>claude mcp add --transport stdio --scope project sufetch -- sufetch-mcp
或者创建 .mcp.json:
{
"mcpServers": {
"sufetch": {
"command": "sufetch-mcp"
}
}
}
</details>
3. 测试: 让 Claude 执行:"使用 sufetch 列出可用的 API"
| 工具 | 描述 |
|---|---|
list_apis | 列出所有可用的 API |
get_api_info | 获取 API 元数据 |
search_endpoints | 按路径/方法/描述搜索 |
get_endpoint_details | 获取完整的端点规范 |
get_schema_details | 获取数据模式 |
generate_code_example | 生成 TypeScript 代码 |
get_quickstart | 获取 API 快速入门指南 |
mkdir -p openapi-specs/myapimyapi.json OpenAPI 规范apiful.config.ts 和 index.ts 从 openapi-specs/ory/ 作为模板pnpm build完成!您的 API 现在可以在 sufetch/myapi 和 MCP 服务器中使用。
参见 CLAUDE.md 获取详细说明。
</details>pnpm install # 安装
pnpm build # 构建
pnpm test # 测试
pnpm lint:fix # 校验
参见 CLAUDE.md 获取架构、构建流水线和贡献指南。
# 测试服务器是否正常工作
sufetch-mcp # 应输出: "SuFetch MCP 服务器正在 stdio 上运行"
# 检查配置
claude mcp list # 对于 Claude Code
cat .mcp.json # 检查文件是否存在
# 重启 Claude Desktop(如果使用桌面版)
</details>
<details>
<summary>构建问题?</summary>
rm -rf node_modules pnpm-lock.yaml dist
pnpm install && pnpm build
</details>
仍然卡住了? 打开一个问题,附上您的 Node 版本和错误信息。
欢迎贡献!参见 CONTRIBUTING.md。
git clone https://github.com/productdevbook/sufetch.git
cd sufetch
pnpm install && pnpm build
# 进行更改,运行 `pnpm test && pnpm lint:fix`
MIT © 2025