api2mcp 0.3.1 → 0.4.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,11 +8,28 @@
8
8
  - 📝 **完整支持**: 支持 OpenAPI 3.x 规范,包括路径参数、查询参数、请求体等
9
9
  - 🚀 **简单易用**: 通过 CLI 或配置文件快速启动
10
10
  - 🔌 **MCP 兼容**: 完全兼容 MCP 协议,可在 Claude Desktop 等 MCP 客户端中使用
11
+ - 🎯 **按需模式**: 针对大型 OpenAPI 文档,提供按需发现和调用 API 的模式
11
12
 
12
13
  ## 安装
13
14
 
14
15
  ```bash
15
- # 使用 pnpm
16
+ # 推荐使用 npx,无需安装
17
+ npx api2mcp --url https://petstore3.swagger.io/api/v3/openapi.json
18
+
19
+ # 或全局安装
20
+ npm install -g api2mcp
21
+ # 或
22
+ pnpm add -g api2mcp
23
+ ```
24
+
25
+ ## 从源码构建
26
+
27
+ ```bash
28
+ # 克隆仓库
29
+ git clone https://github.com/shenjingnan/api2mcp.git
30
+ cd api2mcp
31
+
32
+ # 安装依赖
16
33
  pnpm install
17
34
 
18
35
  # 构建
@@ -21,23 +38,40 @@ pnpm build
21
38
 
22
39
  ## 使用方法
23
40
 
24
- ### CLI
41
+ ### npx 快速使用(推荐)
42
+
43
+ 无需安装,直接使用 npx 运行:
25
44
 
26
45
  ```bash
27
46
  # 基本使用
28
- api2mcp --url https://petstore3.swagger.io/api/v3/openapi.json
47
+ npx api2mcp --url https://petstore3.swagger.io/api/v3/openapi.json
29
48
 
30
49
  # 指定基础 URL
31
- api2mcp --url ./openapi.json --base-url https://api.example.com
50
+ npx api2mcp --url ./openapi.json --base-url https://api.example.com
32
51
 
33
52
  # 带认证头
34
- api2mcp --url https://api.example.com/openapi.json --headers '{"Authorization":"Bearer xxx"}'
53
+ npx api2mcp --url https://api.example.com/openapi.json --headers '{"Authorization":"Bearer xxx"}'
35
54
 
36
55
  # 带工具前缀
37
- api2mcp --url https://api.example.com/openapi.json --prefix myapi
56
+ npx api2mcp --url https://api.example.com/openapi.json --prefix myapi
38
57
 
39
58
  # 调试模式
40
- api2mcp --url https://api.example.com/openapi.json --debug
59
+ npx api2mcp --url https://api.example.com/openapi.json --debug
60
+ ```
61
+
62
+ ### CLI
63
+
64
+ 如果需要全局安装:
65
+
66
+ ```bash
67
+ # 使用 pnpm 全局安装
68
+ pnpm add -g api2mcp
69
+
70
+ # 或使用 npm 全局安装
71
+ npm install -g api2mcp
72
+
73
+ # 然后直接运行
74
+ api2mcp --url https://petstore3.swagger.io/api/v3/openapi.json
41
75
  ```
42
76
 
43
77
  ### CLI 参数
@@ -49,6 +83,7 @@ api2mcp --url https://api.example.com/openapi.json --debug
49
83
  | `--timeout` | `-t` | 请求超时时间 (毫秒) |
50
84
  | `--headers` | `-h` | 自定义请求头 (JSON 字符串) |
51
85
  | `--prefix` | `-p` | 工具名前缀 |
86
+ | `--mode` | `-m` | 工作模式:`default`(默认)或 `ondemand`(按需) |
52
87
  | `--debug` | `-d` | 启用调试模式 |
53
88
 
54
89
  ### 配置文件
@@ -68,10 +103,61 @@ api2mcp --url https://api.example.com/openapi.json --debug
68
103
  "headers": {
69
104
  "Authorization": "Bearer your-token"
70
105
  },
71
- "toolPrefix": "myapi"
106
+ "toolPrefix": "myapi",
107
+ "mode": "default"
72
108
  }
73
109
  ```
74
110
 
111
+ ## 工作模式
112
+
113
+ api2mcp 支持两种工作模式:
114
+
115
+ ### 默认模式 (default)
116
+
117
+ 将所有 API 直接转换为 MCP 工具。适合 API 数量较少的场景。
118
+
119
+ ```bash
120
+ npx api2mcp --url https://api.example.com/openapi.json --mode default
121
+ ```
122
+
123
+ ### 按需模式 (ondemand)
124
+
125
+ 当 OpenAPI 文档包含大量端点(如数百或数千个)时,按需模式提供更好的体验:
126
+
127
+ - **减少上下文占用**: 不会预先注册所有工具,节省 LLM 上下文空间
128
+ - **按需发现**: LLM 可以搜索和浏览可用的 API
129
+ - **直接执行**: 找到需要的 API 后直接调用
130
+
131
+ ```bash
132
+ npx api2mcp --url https://api.example.com/openapi.json --mode ondemand
133
+ ```
134
+
135
+ #### 按需模式提供的工具
136
+
137
+ | 工具名 | 功能 |
138
+ |--------|------|
139
+ | `api_list` | 分页浏览所有 API,支持按标签过滤 |
140
+ | `api_search` | 模糊搜索 API(名称、摘要、描述、路径) |
141
+ | `api_detail` | 获取 API 的详细信息和参数 Schema |
142
+ | `api_execute` | 直接执行 API 调用 |
143
+
144
+ #### 使用示例
145
+
146
+ ```
147
+ 用户: 我需要查询用户信息
148
+
149
+ LLM 调用流程:
150
+ 1. api_search(query="user") → 找到相关 API
151
+ 2. api_detail(id="get_user") → 查看参数要求
152
+ 3. api_execute(operationId="get_user", parameters={userId: 123}) → 执行调用
153
+ ```
154
+
155
+ #### 推荐场景
156
+
157
+ - OpenAPI 文档包含 100+ 个端点
158
+ - API 数量众多但通常只需要少数几个
159
+ - 希望减少 MCP 客户端的加载时间
160
+
75
161
  ### 环境变量
76
162
 
77
163
  | 环境变量 | 说明 |
@@ -84,7 +170,7 @@ api2mcp --url https://api.example.com/openapi.json --debug
84
170
 
85
171
  ### 在 Claude Desktop 中使用
86
172
 
87
- 在 Claude Desktop 配置文件中添加:
173
+ 在 Claude Desktop 配置文件中添加:
88
174
 
89
175
  **macOS**: `~/Library/Application Support/Claude/claude_desktop_config.json`
90
176
  **Windows**: `%APPDATA%\Claude\claude_desktop_config.json`
@@ -93,13 +179,15 @@ api2mcp --url https://api.example.com/openapi.json --debug
93
179
  {
94
180
  "mcpServers": {
95
181
  "api2mcp": {
96
- "command": "node",
97
- "args": ["/path/to/api2mcp/bin/api2mcp.js", "--url", "https://api.example.com/openapi.json"]
182
+ "command": "npx",
183
+ "args": ["-y", "api2mcp", "--url", "https://api.example.com/openapi.json"]
98
184
  }
99
185
  }
100
186
  }
101
187
  ```
102
188
 
189
+ > **注意**: 使用 `-y` 参数可以自动确认 npx 的安装提示,避免交互式确认。
190
+
103
191
  ## 配置优先级
104
192
 
105
193
  配置加载优先级 (从高到低):