feat: comprehensive refactor of index.js and documentation update

This commit is contained in:
zhangguoyun committed 2026-01-15 11:09:13 +08:00
1 parent 1ea6e0e9f0
commit dd1d86706c
12 files changed
+4520 -68

No files matched your search

+51 -5
View File
@@ -38,30 +38,76 @@ npm install
## 🚀 启动服务
### 命令行选项
```bash
node index.js [选项]
选项:
-t, --transport <type> 传输方式 (stdio|http) [默认: stdio]
-p, --port <number> HTTP 服务器端口号 [默认: 8889]
-h, --help 显示帮助信息
```
### 1. Stdio 模式 (默认)
适用于 Claude Desktop 或作为子进程运行的场景。
适用于 Claude Desktop、Cursor 或作为子进程运行的场景。
```bash
# 需确保环境变量已设置
export APIFOX_PROJECT_ID=你的项目ID
export APIFOX_TOKEN=你的Token
# 默认使用 stdio 模式
node index.js
# 或显式指定
node index.js --transport stdio
```
**特点:**
- 通过标准输入/输出与客户端通信
- 适合本地集成 (Claude Desktop, Cursor 等)
- 日志输出到 stderr,不干扰 JSON-RPC 通信
### 2. HTTP (SSE) 模式
适用于远程部署或需要 HTTP 访问的场景。
适用于远程部署、调试或需要 HTTP 访问的场景。
```bash
node index.js --transport http --port 8889
# 使用默认端口 8889
node index.js --transport http
# 或指定自定义端口
node index.js --transport http --port 3000
```
* **Endpoint**: `http://localhost:8889/sse`
* **Health Check**: `http://localhost:8889/health`
**HTTP 端点:**
- **MCP 服务**: `http://localhost:8889/sse` (Server-Sent Events)
- **MCP 消息**: `http://localhost:8889/messages`
- **健康检查**: `http://localhost:8889/health`
**HTTP 请求头支持:**
- `Authorization`: Bearer token (可选,覆盖环境变量 `APIFOX_TOKEN`)
- `x-apifox-project-id`: 项目 ID (可选,覆盖环境变量 `APIFOX_PROJECT_ID`)
**特点:**
- 支持 CORS,可跨域访问
- 支持动态认证 (通过请求头传递 token 和 projectId)
- 提供健康检查端点
- 详细的请求日志
### 环境变量验证
服务启动时会自动验证必需的环境变量:
- `APIFOX_TOKEN` (必需): 如未设置,服务将拒绝启动并提示配置方法
- `APIFOX_PROJECT_ID` (可选): 可通过请求头动态指定
### 查看帮助信息
```bash
node index.js --help
```
## 💻 客户端集成示例