# YAPI MCP Server
[English](./README.md) | 中文
[](https://opensource.org/licenses/MIT)
一个用于 [YAPI](https://github.com/YMFE/yapi) 的 [MCP (Model Context Protocol)](https://modelcontextprotocol.io/) 服务器,让 AI 助手能够管理 API 文档。
## 功能
| 工具 | 描述 | 对应 API |
|------|------|----------|
| `yapi_list_projects` | 列出所有已配置的项目 | - |
| `yapi_run_auto_test` | 运行自动化测试 | `/api/open/run_auto_test` |
| `yapi_import_data` | 导入接口数据 | `/api/open/import_data` |
| `yapi_interface_add` | 新增接口 | `/api/interface/add` |
| `yapi_interface_save` | 保存接口 | `/api/interface/save` |
| `yapi_interface_up` | 更新接口 | `/api/interface/up` |
| `yapi_interface_get` | 获取接口详情 | `/api/interface/get` |
| `yapi_interface_list` | 获取接口列表 | `/api/interface/list` |
| `yapi_interface_list_menu` | 获取接口菜单 | `/api/interface/list_menu` |
| `yapi_interface_add_cat` | 新增接口分类 | `/api/interface/add_cat` |
| `yapi_interface_get_cat_menu` | 获取所有分类 | `/api/interface/getCatMenu` |
## 安装
克隆本仓库后在项目目录安装依赖并构建:
```bash
git clone <仓库地址>
cd yapi-mcp
npm install
npm run build
```
## 配置
### 环境变量
| 变量 | 必填 | 说明 |
|------|------|------|
| `YAPI_BASE_URL` | 是 | YAPI 服务器地址,如 `https://yapi.example.com` |
| `YAPI_PROJECTS` | 是 | 项目配置,格式:`项目ID1:token1,项目ID2:token2` |
### YAPI_PROJECTS 格式
```
项目ID1:token1,项目ID2:token2
```
示例:
```
123:abc123token,456:def456token
```
Token 可以在 YAPI 项目设置页面获取。
## 在 Cursor 中使用
编辑 MCP 配置文件:
### macOS/Linux
编辑 `~/.cursor/mcp.json`,将 `/path/to/yapi-mcp` 替换为你的项目实际路径:
```json
{
"mcpServers": {
"yapi": {
"command": "node",
"args": ["/path/to/yapi-mcp/dist/index.js"],
"env": {
"YAPI_BASE_URL": "https://your-yapi-server.com",
"YAPI_PROJECTS": "123:your-token-here,456:another-token"
}
}
}
}
```
### Windows
编辑 `%APPDATA%\Cursor\mcp.json`,将路径改为你的项目实际路径(如 `C:\\Users\\你的用户名\\yapi-mcp\\dist\\index.js`):
```json
{
"mcpServers": {
"yapi": {
"command": "node",
"args": ["C:\\path\\to\\yapi-mcp\\dist\\index.js"],
"env": {
"YAPI_BASE_URL": "https://your-yapi-server.com",
"YAPI_PROJECTS": "123:your-token-here"
}
}
}
}
```
## 在 Claude Desktop 中使用
### macOS
编辑 `~/Library/Application Support/Claude/claude_desktop_config.json`:
将 `/path/to/yapi-mcp` 替换为你的项目实际路径:
```json
{
"mcpServers": {
"yapi": {
"command": "node",
"args": ["/path/to/yapi-mcp/dist/index.js"],
"env": {
"YAPI_BASE_URL": "https://your-yapi-server.com",
"YAPI_PROJECTS": "123:your-token-here"
}
}
}
}
```
### Windows
编辑 `%APPDATA%\Claude\claude_desktop_config.json`,将路径改为你的项目实际路径,配置同上。
## 工具使用示例
### 列出所有项目
```json
{
"name": "yapi_list_projects",
"arguments": {}
}
```
### 获取接口列表(使用默认项目)
```json
{
"name": "yapi_interface_list",
"arguments": {
"page": 1,
"limit": 20
}
}
```
### 获取接口列表(指定项目)
```json
{
"name": "yapi_interface_list",
"arguments": {
"project": 456,
"page": 1,
"limit": 20
}
}
```
### 新增接口
```json
{
"name": "yapi_interface_add",
"arguments": {
"catid": 456,
"title": "获取用户信息",
"path": "/api/user/info",
"method": "GET",
"desc": "根据用户 ID 获取详细信息"
}
}
```
### 获取接口详情
```json
{
"name": "yapi_interface_get",
"arguments": {
"id": 789
}
}
```
### 运行自动化测试
```json
{
"name": "yapi_run_auto_test",
"arguments": {
"project": 456,
"env_name": "dev"
}
}
```
## 开发
```bash
# 克隆仓库
cd yapi-mcp
# 安装依赖
npm install
# 开发模式
npm run dev
# 构建
npm run build
# 运行
npm start
```
## 安全提示
- 不要将 YAPI Token 提交到版本控制
- 使用环境变量存储敏感配置
- 生产环境建议使用密钥管理工具
## 许可证
MIT
## 贡献
欢迎提交 Pull Request!