关于 test-coverage-mcp
Test Coverage MCP 是一个基于 LCOV 文件分析测试覆盖率的 MCP 服务,使编码和测试类 AI Agent 能够感知覆盖率变化。它以紧凑的 JSON 返回结果,减少解析大型 LCOV 文件所消耗的 token。
主要功能
- 01查询项目整体行与分支覆盖率
- 02查询单个文件的覆盖率
- 03记录会话基线并比较覆盖率变化
- 04返回紧凑 JSON 以节省 token
- 05支持 SF、DA、BRDA 等 LCOV 格式
适用场景
- 01编码 Agent 在修改代码后检查覆盖率是否下降
- 02测试 Agent 查找尚未覆盖的文件
- 03在一次会话内跟踪覆盖率的提升或回退
MCP 相关
该项目是一个 MCP 服务,向 MCP 客户端暴露 coverage_summary、coverage_file_summary、start_recording 和 get_diff_since_start 等覆盖率工具。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
4收录的工具列表 · 更新于 2 周前
此工具无需参数。
详细介绍
Test Coverage MCP
让你的 Agent 在为你编写代码时了解覆盖率
“嗨,我是一个编码 Agent。我刚做了个炫酷的新功能……但糟糕,我把覆盖率降低了 🤓。我怎么会知道呢?”
“嗨,我是一个测试 Agent。我的任务是为一些代码编写测试,但我怎么知道哪些部分还没有覆盖?😳”
让你的编码 Agent 和测试 Agent 拥有「视野」:这个 MCP 服务可为任何编程语言提供即时、可靠且节省令牌的测试覆盖率数据(基于 LCOV)。
__
🚀 刚刚发布(2025 年 11 月)!最近我花了很多时间打磨这个库。如果你觉得它有用,点个 ⭐ 有助于让其他开发者注意到这个项目。
问题
当 AI 编码 Agent 在没有合适覆盖率工具的情况下处理你的代码时,会面临三个关键问题:
- 覆盖率盲区 - 它们无法判断自己的更改是提升了还是降低了测试覆盖率。
- 浪费令牌 - 它们要耗费数千个令牌来解析庞大的 LCOV 文件(有些超过 10 MB)。
- 脚本不可靠 - 它们会临时编写自定义解析脚本,但往往会失败或产生错误结果。
解决方案
这个 MCP 服务通过以下功能解决全部三个问题:
- 覆盖率感知 - Agent 可以随时通过简单的工具调用检查覆盖率。
- 节省令牌 - 只需 <100 个令牌即可获取覆盖率摘要,而不是数千个。
- 准确性 - 生产级 LCOV 解析,可处理各种格式差异。
- 基线跟踪 - 无需在内存中保留状态,即可衡量会话期间的覆盖率进展。
测试覆盖率:本项目的测试覆盖率为 95%,我们的目标是尽快达到 100%。
两种主要工作流
1. 查询覆盖率摘要
查询整个项目或特定文件的覆盖率:
// Get overall project coverage
coverage_summary({ lcovPath: "./coverage/lcov.info" });
// Returns: { linesCoveragePercentage: 87.5, branchesCoveragePercentage: 82.1 }
// Get coverage for specific files
coverage_file_summary({
lcovPath: "./coverage/lcov.info",
filePath: "src/utils/parser.ts",
});
// Returns: { path: "src/utils/parser.ts", linesCoveragePercentage: 92.0, branchesCoveragePercentage: 88.5 }
2. 跟踪会话进度的基线
在会话开始时建立基线,然后衡量进度:
// At session start - record current coverage as baseline
start_recording({ lcovPath: "./coverage/lcov.info" });
// Returns: "Recording started"
// ... agent writes code and tests ...
// Check coverage impact
get_diff_since_start({ lcovPath: "./coverage/lcov.info" });
// Returns: { linesPercentageImpact: +2.3, branchesPercentageImpact: +1.8 }
为什么要跟踪基线? 没有基线,Agent 就需要在整个会话期间把初始覆盖率保存在其有状态记忆中,占用宝贵的上下文窗口空间。
安装
npm install -g test-coverage-mcp
配置
将此 MCP 服务添加到 AI 编码工具的配置中:
Claude Desktop(Claude Code)
macOS:编辑 ~/Library/Application Support/Claude/claude_desktop_config.json
Windows:编辑 %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"test-coverage": {
"command": "npx",
"args": ["-y", "test-coverage-mcp"]
}
}
}
更新后,重启 Claude Desktop。
Cursor IDE
在项目根目录中创建或编辑 .cursor/mcp.json:
{
"mcpServers": {
"test-coverage": {
"command": "npx",
"args": ["-y", "test-coverage-mcp"]
}
}
}
GitHub Copilot(VS Code)
在工作区中创建或编辑 .vscode/mcp.json:
{
"servers": {
"test-coverage": {
"command": "npx",
"args": ["-y", "test-coverage-mcp"]
}
}
}
需要 VS Code 1.99+ 或 Visual Studio 17.14+。企业用户需要启用“MCP servers in Copilot”策略。
Windsurf(Codeium IDE)
macOS:编辑 ~/.codeium/windsurf/mcp_config.json
Windows:编辑 %APPDATA%\Codeium\Windsurf\mcp_config.json
Linux:编辑 ~/.codeium/windsurf/mcp_config.json
{
"mcpServers": {
"test-coverage": {
"command": "npx",
"args": ["-y", "test-coverage-mcp"]
}
}
}
或者使用图形界面:Settings → Advanced Settings → Cascade → Add Server
可用工具
coverage_summary
从 LCOV 文件获取整个项目的覆盖率。
输入:
{
lcovPath?: string // Optional. Defaults to "./coverage/lcov.info"
}
输出:
{
linesCoveragePercentage: number, // 0-100
branchesCoveragePercentage: number // 0-100
}
示例:
coverage_summary({ lcovPath: "./coverage/lcov.info" });
// { linesCoveragePercentage: 87.5, branchesCoveragePercentage: 82.1 }
coverage_file_summary
获取特定文件的覆盖率。
输入:
{
lcovPath?: string, // Optional. Defaults to "./coverage/lcov.info"
filePath: string // Required. Path to the file
}
输出:
{
path: string,
linesCoveragePercentage: number, // 0-100
branchesCoveragePercentage: number // 0-100
}
示例:
coverage_file_summary({
lcovPath: "./coverage/lcov.info",
filePath: "src/utils/parser.ts",
});
// { path: "src/utils/parser.ts", linesCoveragePercentage: 92.0, branchesCoveragePercentage: 88.5 }
start_recording
将当前覆盖率记录为基线,以供之后比较。
输入:
{
lcovPath: string; // Required. Path to LCOV file to record
}
输出:
"Recording started";
示例:
start_recording({ lcovPath: "./coverage/lcov.info" });
// "Recording started"
get_diff_since_start
将当前覆盖率与已记录的基线进行比较。
输入:
{
lcovPath: string; // Required. Path to current LCOV file
}
输出:
{
linesPercentageImpact: number, // Positive = improvement, negative = regression
branchesPercentageImpact: number // Positive = improvement, negative = regression
}
示例:
get_diff_since_start({ lcovPath: "./coverage/lcov.info" });
// { linesPercentageImpact: +2.3, branchesPercentageImpact: +1.8 }
使用示例
示例 1:开始工作前检查覆盖率
Agent: "Let me check the current test coverage before I start working"
[Uses coverage_summary tool]
Agent: "Current coverage is 87.5% lines and 82.1% branches. I'll aim to maintain or improve this."
示例 2:开发期间跟踪覆盖率变化
Agent: "I'll record the baseline coverage first"
[Uses start_recording tool]
Agent: "Now I'll add the new authentication feature with tests"
[Writes code and tests]
Agent: "Let me check the coverage impact"
[Uses get_diff_since_start tool]
Agent: "Great! Coverage increased by 2.3% for lines and 1.8% for branches."
示例 3:验证特定文件的覆盖率
Agent: "Let me check coverage for the file I just modified"
[Uses coverage_file_summary with filePath: "src/auth/validator.ts"]
Agent: "The validator.ts file now has 95% line coverage and 92% branch coverage."
工作原理
此 MCP 服务:
- 解析 LCOV 文件,使用适用于生产环境的解析器处理所有 LCOV 格式变体
- 计算覆盖率,包括整个项目或单个文件的覆盖率百分比
- 存储基线,将基线存储在临时目录中,以便按会话跟踪
- 返回精简的 JSON 响应,最大限度地减少 Token 消耗
LCOV 格式支持
此服务支持所有标准 LCOV 文件格式,包括:
- 包含摘要部分的文件(
SF:、end_of_record) - 仅包含逐行数据的文件(
DA:条目) - 包含分支覆盖率数据的文件(
BRDA:、BRF:、BRH:) - 同一文件中包含混合格式的文件
故障排除
“未找到 LCOV 文件”
- 确保你已先运行启用覆盖率的测试套件
- 检查 LCOV 文件的路径是否正确(相对路径以当前工作目录为基准解析)
- 默认路径为
./coverage/lcov.info
“未找到文件的覆盖率数据”
- 确认文件路径与 LCOV 文件中显示的路径完全一致
- 某些测试框架使用绝对路径,其他框架则使用相对路径
“未找到基线记录”
- 调用
get_diff_since_start前必须先调用start_recording - 基线存储在临时存储中,系统重启时会清除
开发
# Install dependencies
npm install
# Build
npm run build
# Run tests (with coverage!)
npm test
# Run linter
npm run lint
# Test with MCP inspector
npm run inspect
参与贡献
欢迎贡献!请随时提交 Pull Request。
许可证
MIT © Yoni Goldberg
链接
改进建议
- coverage_file_summary 返回的嵌套属性也以扁平形式声明
- 开始记录会覆盖其他会话的文件
- 改进记录命名——setSessionBaseline、getDiffSinceBaseline
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。