Are you the author? Sign in to claim
fe-testgen-mcp
基于 MCP 协议的前端单元测试生成服务,专注于 AI 分析和代码生成能力。通过 HTTP Streaming 暴露 MCP 工具,可与 n8n、Cursor、Claude 等客户端集成。
# 安装依赖
pnpm install
# 构建
pnpm build
# 启动服务
pnpm start
服务默认监听 http://localhost:3000/mcp
复制 env.example 为 .env 并填写必要配置:
# 必需:OpenAI API 密钥
OPENAI_API_KEY=your_api_key
# 可选:自定义模型
OPENAI_MODEL=gpt-4
# 可选:服务配置
HTTP_PORT=3000
HTTP_HOST=0.0.0.0 # 允许外部访问
分析代码变更,生成测试矩阵。
参数:
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
| rawDiff | string | ✅ | Unified diff 格式文本 |
| identifier | string | 唯一标识符(MR ID、commit hash) | |
| rules | string | test-strategy.md 内容 | |
| framework | string | 测试框架(vitest/jest) |
返回:功能清单、测试场景、统计信息
生成单元测试代码。
参数:
| 参数 | 类型 | 必需 | 说明 |
|---|---|---|---|
| rawDiff | string | ✅ | Unified diff 格式文本 |
| identifier | string | 唯一标识符 | |
| rules | string | test-strategy.md 内容 | |
| framework | string | 测试框架 | |
| scenarios | string[] | 指定场景类型 | |
| maxTests | number | 最大测试数量 |
返回:测试用例数组(包含文件路径、代码、场景类型)
GitLab/Phabricator Webhook
│
▼
读取 diff + rules 文件
│
▼
调用 MCP generate-tests
│
▼
写入测试文件 + 创建 MR
│
▼
发送通知(企微/飞书)
curl -X POST http://localhost:3000/mcp \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {
"name": "generate-tests",
"arguments": {
"rawDiff": "diff --git a/src/utils.ts ...",
"framework": "vitest"
}
}
}'
在项目中创建 .cursor/rules/test-strategy.md:
# 测试生成规则
## 测试框架配置
- **测试框架**:vitest
- **测试文件模式**:**/*.{test,spec}.{ts,tsx}
## Mock 策略
- 使用 vi.fn() 进行函数 mock
- HTTP 请求优先 mock fetch/axios
n8n 读取此文件内容,作为 rules 参数传给 MCP。
docker build -t fe-testgen-mcp .
docker run -d -p 3000:3000 \
-e OPENAI_API_KEY=xxx \
fe-testgen-mcp
Q: 如何提高生成结果的一致性?
A: 服务默认使用 temperature=0 和固定 seed=42,同时启用响应缓存。
Q: 如何指定测试框架?
A: 优先级:framework 参数 > rules 中解析 > 默认 vitest
Q: 需要 Node.js 版本?
A: Node.js 18+(推荐 20+)
Run Claude Code as an MCP server so any agent can delegate coding tasks to it
Browser automation using accessibility snapshots instead of screenshots
Google's universal MCP server supporting PostgreSQL, MySQL, MongoDB, Redis, and 10+ databases
Official GitHub integration for repos, issues, PRs, and CI/CD workflows