光标测试MCP–AI测试工程师
许可证: 麻省理工学院\
Node.js版本: >=18.0.0
一种模型上下文协议(MCP)服务器,使Cursor IDE中的AI助手能够通过自动测试框架检测、执行和分析来管理和理解项目测试。
特性
- 框架检测:自动检测测试框架(Jest、Vitest、Mocha、pytest)
- 测试执行:运行超时和输出截断的测试
- 覆盖率分析:阅读和分析报道报告
- 测试骨架生成:为不同框架生成测试文件骨架
- 输出摘要:分析和构造测试输出以了解AI
- 错误处理:具有详细错误消息的稳健错误处理
快速开始
安装```bash
git clone https://github.com/your-username/cursor-test-mcp.git
cd cursor-test-mcp
npm install
npm run build
或者使用安装脚本:```bash
./install.sh
手动配置
创建或编辑 ~/.cursor/mcp.json 对于全局配置:```json
{
"mcpServers": {
"cursor-test-mcp": {
"command": "node",
"args": ["/path/to/cursor-test-mcp/dist/index.js"],
"env": {
"MCP_MODE": "stdio",
"LOG_LEVEL": "info",
"PROJECT_ROOT": "/optional/project/path"
}
}
}
}
> **备注**\
> 如果您已经定义了其他MCP服务器,只需添加 `cursor-test-mcp` 进入现有 `mcpServers` 对象。
安装后:
1. 重新启动游标
1. 启用 **`cursor-test-mcp`** 在MCP设置中
## 可用工具
### 框架检测
|工具|说明|
|------|-------------|
| `test_detect_framework` |检测测试框架和默认测试命令|
### 测试执行
|工具|说明|
|------|-------------|
| `test_run` |运行测试并返回汇总结果|
### 覆盖率分析
|工具|说明|
|------|-------------|
| `test_read_coverage` |阅读覆盖率摘要并分析覆盖率最低的文件|
### 测试生成
|工具|说明|
|------|-------------|
| `test_generate_skeleton` |为给定主题生成测试文件骨架|
### 产出分析
|工具|说明|
|------|-------------|
| `test_summarize_output` |解析和构造原始测试输出|
## 工具详细信息
### `test_detect_framework`
检测当前项目中最可能的测试框架和默认测试命令。
**输入:**```json
{
"projectPath": "string (optional, absolute path)"
}
行为:
- 如果
projectPath未提供: - 第一次检查
PROJECT_ROOT环境变量 - 回落到
process.cwd()(当前工作目录) - 分析
package.json、锁定文件和pytest配置文件 - 检测Jest/Vitest/Mocha/pytest或通用
npm test
示例提示:
- “检测测试框架,并向我显示此项目的默认测试命令。”
- “这个项目使用什么测试框架,命令是什么?”
test_run
运行项目的测试并返回汇总结果。
输入:```json
{
"projectPath": "string (optional, absolute path)",
"command": "string (optional, e.g. 'npm test', 'pytest -q')",
"timeoutSeconds": "number (optional, default 180, max 3600)",
"maxOutputLength": "number (optional, default 8000, max 200000)"
}
**行为:**
- 如果 `command` 不提供,自动检测并使用默认测试命令
- 将输出截断到最后 `maxOutputLength` 字符(针对AI消费进行了优化)
- 超时时终止命令并返回“超时”信息
**示例提示:**
- 运行此项目的测试,总结输出,并解释失败的可能根本原因
- “运行所有测试,总结最近的输出,并分析哪些测试可能爆炸。
### `test_read_coverage`
读取JSON覆盖率摘要,并返回总体覆盖率和最低覆盖率文件。
**输入:**```json
{
"projectPath": "string (optional, absolute path)",
"coverageFile": "string (optional, e.g. 'coverage/coverage-summary.json')",
"topNLowestFiles": "number (optional, default 10, max 50)"
}
行为:
- 如果
coverageFile未提供,尝试常见的Jest/Vitest位置 - 从以下位置读取行/语句/函数/分支覆盖率
total领域 - 返回第一个
topNLowestFiles行覆盖率最低的文件
示例提示:
- “阅读Jest覆盖率摘要,告诉我哪5个文件的行覆盖率最低。”
- “阅读覆盖范围摘要,并说出覆盖范围最低的5个文件。
test_generate_skeleton
为给定的主题和框架样式生成测试文件骨架(describe/it block)。
输入:```json
{
"framework": "string (optional, 'jest' | 'vitest' | 'mocha', default: 'jest')",
"testFilePath": "string (required, e.g. 'src/utils/math.test.ts')",
"subjectName": "string (required, e.g. 'sum' or 'ImageProcessor')",
"description": "string (optional, high-level description)"
}
**行为:**
- 生成 `describe` / `it` 或 `assert` 基于框架的骨架
- 在描述块中包括文件路径和主题名称
- 以字符串形式返回骨架(文件写入决定权留给用户)
**示例提示:**
- “为src/utils/math.ts生成一个Jest测试框架,重点是求和函数。”
- "`src/utils/math.ts` 内部 `sum` 为其功能生成Jest测试骨架。 ”
### `test_summarize_output`
解析原始测试输出(Jest/Vitest/Mocha/Pytest风格),并返回失败测试的结构化摘要和高级统计数据。
**输入:**```json
{
"rawOutput": "string (required, raw test output or CI log chunk)",
"maxFailures": "number (optional, default 50, max 200)"
}
行为:
- 从中提取文件名
FAIL ...线条 - 从中提取Jest/Vitest故障名称
● suite › test线条 - 捕获
Test Suites:和Tests:摘要行 - 猜测框架类型(
frameworkGuess现场) - 返回失败列表
file,title,以及rawBlock对于每个错误
示例提示:
- “给定这个Jest输出,按文件对失败的测试进行分组,并简要解释每个失败。”
- “按照这个Jest输出的文件对测试进行分组,并简要解释为什么每个测试都会爆炸。
使用示例
检测框架```
"Detect the test framework and show me the default test command for this project."
"Bu projede hangi test framework'ü kullanılıyor?"
### 运行测试```
"Run all tests now and summarize the failing ones for me."
"Şimdi testleri çalıştır, eğer hata varsa bana kısa bir özet çıkar."
阅读报道```
"Read the coverage report and show files with lowest coverage."
"Coverage raporunu oku ve en düşük coverage'a sahip dosyaları göster."
### 生成测试骨架```
"Generate a Jest test skeleton for src/utils/math.ts focusing on the sum function."
"`src/utils/math.ts` içindeki `sum` fonksiyonu için Jest test iskeleti üret."
CLI命令```bash
node dist/index.js --help # Show help
node dist/index.js # Start MCP server (used by Cursor)
## 项目结构```
cursor-test-mcp/
├── src/
│ ├── index.ts # Entry point with CLI
│ ├── server.ts # MCP Server implementation
│ ├── tools/
│ │ └── test-tools.ts # All test-related tools
│ └── utils/
│ └── logger.ts # Logging utility
├── dist/ # Compiled JavaScript
├── package.json
├── tsconfig.json
├── install.sh # Installation script
├── LICENSE
└── README.md
支持的框架
这 test_detect_framework 该工具支持以下检测:
Node.js:
- 杰斯特(via
jest或ts-jest依赖关系) - Vitest(通过
vitest依赖性) - 摩卡(via
mocha依赖性) - 通用npm/yarn/pnpm测试脚本
python
- pytest(通过
pytest.ini,pyproject.toml,或requirements.txt)
包装经理:
- npm(默认)
- 纱线(通过以下方式检测
yarn.lock或.yarnrc) - pnpm(通过检测
pnpm-lock.yaml)
错误处理
服务器包括强大的错误处理功能:
- 超时管理:测试命令的可配置超时(默认180秒,最大3600秒)
- 输出截断:长输出被截断以防止AI上下文溢出
- 详细错误:错误消息包括解决提示
- 优雅关闭:正确清理信号情报/信号机信号
- 框架检测回退:如果检测失败,则返回“未知”框架
环境变量
| 变量 | 描述 | 必填 |
|---|---|---|
LOG_LEVEL |
日志级别(debug, info, warn, error,默认值: info) |
没有 |
PROJECT_ROOT |
默认项目根目录(可选) | 否 |
MCP_MODE |
MCP传输模式(应为 stdio 光标) |
否 |
如果 PROJECT_ROOT 设置后,测试将在该目录中运行 projectPath 未提供。
贡献
欢迎投稿!请随时提交拉取请求。
- 复刻仓库
- 创建功能分支(
git checkout -b feature/amazing-feature) - 提交您的更改(
git commit -m 'Add some amazing feature') - 推到分支(
git push origin feature/amazing-feature) - 打开拉取请求
开发说明
- 语言:TypeScript
- MCP-SDK:
@modelcontextprotocol/sdk - 入口点:
src/index.ts→src/server.ts - 工具:
src/tools/test-tools.ts - 日志记录器:
src/utils/logger.ts
贡献:
- 克隆存储库或在现有目录中工作
- 根据您的测试/覆盖场景添加新的MCP工具(
test_*) - 构建于
npm run build并在Cursor中进行测试
参考验证场景
此MCP在一个复杂的Jest项目上进行了手动验证,包括:
- 多个测试文件(
tests/math.test.ts,tests/userService.test.ts) - 成功和故意失败的测试
- 异步测试和域错误(例如。,
USER_INACTIVE,USER_NOT_ADMIN) - 测试
--coverage生成的coverage/coverage-summary.json
工具结果:
test_detect_framework→ 正确检测到Jest+npm test命令test_run→ 正确携带2条失败/13条通过/15条信息和错误消息test_read_coverage→ 返回与Jest控制台表匹配的全局覆盖率和最低覆盖率文件test_summarize_output→ 按文件和测试名称正确分组失败的测试
该场景可作为参考,以证明MCP工具在现实和具有挑战性的项目中的表现符合预期。
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
致谢
- Alican Kiraz -MCP生态系统的启示
- n8n -工作流自动化平台(供MCP模式参考)
- Anthropic -模型上下文协议
- 光标 -AI驱动的IDE
