关于 mcp-code-sanitizer
mcp-code-sanitizer 是面向开发者的 MCP 代码审查服务,使用 Groq 检查代码和文件中的错误与安全问题。它还可比较版本、生成测试和 HTML 报告,并支持自动审查拉取请求。
主要功能
- 01分析代码错误与安全问题
- 02比较版本并检测回归
- 03按经验水平解释代码
- 04生成 pytest、jest 和 go test 测试
- 05分析文件并创建 HTML 报告
- 06通过 GitHub Action 审查拉取请求
适用场景
- 01合并前检查代码漏洞
- 02比较重构前后的代码
- 03生成边界与安全测试
- 04在拉取请求中发布结构化审查意见
MCP 相关
向 MCP 客户端提供代码分析、比较、解释、测试生成、文件分析、报告和缓存工具。它使用 Groq API,并需要 GROQ_API_KEY。
配置
本地服务的配置在各客户端中通用,复制后粘贴到客户端的 MCP 配置文件即可。
工具
7收录的工具列表 · 更新于 2 周前
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| code | string | 是 | 要检查的代码片段。 |
| context | string | 否 | 可选描述——代码的作用及其来源。 |
| language | string | 否 | 编程语言(python、javascript、go、rust 等)。 |
详细介绍
mcp-code-sanitizer
为 Claude Desktop、Cursor、VS Code 和 Claude Code CLI 提供严格的 AI 代码审查。 由 Groq(免费 API)提供支持,可发现错误、漏洞和安全问题。
Claude / Cursor / VS Code ──MCP──► code-sanitizer ──REST──► Groq API
(server.py) (llama-3.3-70b)
功能
| 工具 | 功能 |
|---|---|
analyze_code | 严格审查——错误、安全问题,评分 0–100 |
compare_code | 比较两个版本,检测回归问题,建议合并或 request_changes |
explain_code | 面向初级/中级/高级受众,逐步讲解代码 |
generate_tests | 生成 pytest / jest / go test 测试——涵盖正常路径、边界情况和安全性 |
analyze_file | 通过并行分块分析磁盘上的整个文件 |
generate_report | 根据任意分析结果生成 HTML 报告 |
cache_info | 查看缓存统计信息并清除缓存 |
输出示例
{
"summary": "Critical SQL injection and secret exposed in logs",
"score": 23,
"issues": [
{
"severity": "critical",
"line": 2,
"title": "SQL Injection",
"description": "f-string directly interpolates user_id into query",
"fix": "cursor.execute('SELECT * FROM users WHERE id = %s', (user_id,))"
}
],
"warnings": [{"title": "No exception handling", "description": "..."}],
"suggestions": ["Consider using an ORM instead of raw SQL"]
}
安装
前置条件: 在 console.groq.com/keys 获取免费的 Groq API 密钥——无需信用卡。
Claude Code CLI
claude mcp add code-sanitizer -e GROQ_API_KEY=gsk_your_key -- uvx mcp-code-sanitizer
Claude Desktop
| 操作系统 | 配置文件 |
|---|---|
| macOS | ~/Library/Application Support/Claude/claude_desktop_config.json |
| Windows | %APPDATA%\Claude\claude_desktop_config.json |
| Linux | ~/.config/Claude/claude_desktop_config.json |
{
"mcpServers": {
"code-sanitizer": {
"command": "uvx",
"args": ["mcp-code-sanitizer"],
"env": {
"GROQ_API_KEY": "gsk_your_key_here"
}
}
}
}
Cursor
在项目中创建 .cursor/mcp.json(或在全局路径 ~/.cursor/mcp.json 创建):
{
"mcpServers": {
"code-sanitizer": {
"command": "uvx",
"args": ["mcp-code-sanitizer"],
"env": {
"GROQ_API_KEY": "gsk_your_key_here"
}
}
}
}
VS Code
需要 VS Code 1.99+ 和 GitHub Copilot。在项目中创建 .vscode/mcp.json:
{
"servers": {
"code-sanitizer": {
"command": "uvx",
"args": ["mcp-code-sanitizer"],
"env": {
"GROQ_API_KEY": "gsk_your_key_here"
}
}
}
}
也可以通过 Ctrl+Shift+P → "MCP: Add Server" 全局添加。
没有
uvx? 使用pip install uv安装,然后运行上述命令。
手动安装(备选方案)
如果你更喜欢克隆仓库:
git clone https://github.com/notasandy/mcp-code-sanitizer
cd mcp-code-sanitizer
pip install -r requirements.txt
cp .env.example .env # add your GROQ_API_KEY
python server.py
然后将客户端配置指向:
{
"command": "python",
"args": ["/full/path/to/server.py"],
"env": { "GROQ_API_KEY": "gsk_your_key_here" }
}
GitHub Action — 自动审查 PR
只需 5 行,即可为任意仓库添加 AI 代码审查。 此 Action 会在每个 PR 上发布结构化评论,包含评分、问题和修复建议。
# .github/workflows/ai-review.yml
name: AI Code Review
on:
pull_request:
types: [opened, synchronize]
permissions:
contents: read
pull-requests: write
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: notasandy/mcp-code-sanitizer@v1
with:
groq_api_key: ${{ secrets.GROQ_API_KEY }}
将 GROQ_API_KEY 添加到仓库机密 → Settings → Secrets → Actions。
此 Action 会自动:
- 仅审查发生更改的文件(每个 PR 最多 10 个)
- 在 PR 评论中发布评分和结构化问题列表
- 如果发现严重问题,则使检查失败
在聊天中使用
连接后,只需用自然语言提出需求:
Review this code for vulnerabilities:
def get_user(user_id):
query = f"SELECT * FROM users WHERE id = {user_id}"
return db.execute(query)
也可以显式调用工具:
analyze_file /path/to/my_script.py
generate_tests for this function: ...
compare_code — before vs after refactor, did it get better?
generate_report and save to /tmp/report.html
架构
mcp-code-sanitizer/
├── server.py # FastMCP entry point
├── config.py # Constants — keys, limits, extension map
├── groq_client.py # Async Groq client with auto-retry on 429
├── cache.py # In-memory LRU cache with TTL
├── prompts.py # System prompts for all tools
└── tools/
├── analyze.py # analyze_code
├── compare.py # compare_code
├── explain.py # explain_code
├── tests.py # generate_tests
├── file_tool.py # analyze_file — chunking + parallel analysis
├── cache_tool.py # cache_info
└── report.py # generate_report — HTML output
配置
所有设置都可通过 .env 或环境变量进行:
| 变量 | 默认值 | 说明 |
|---|---|---|
GROQ_API_KEY | — | 必需。 在 console.groq.com 获取 |
GROQ_MODEL | llama-3.3-70b-versatile | 要使用的 Groq 模型 |
CACHE_TTL | 3600 | 缓存有效期(秒) |
CACHE_MAX | 200 | 缓存条目数上限 |
可用的 Groq 模型
| 模型 | 速度 | 质量 |
|---|---|---|
llama-3.3-70b-versatile | 快 | 最佳(默认) |
llama-3.1-8b-instant | 最快 | 良好 |
mixtral-8x7b-32768 | 快 | 很好 |
参与贡献
欢迎提交 PR 和 Issues。我们最期待:
- 支持其他大模型提供商(OpenAI、Anthropic)
- 新工具:依赖项审计、复杂度评分、文档字符串生成器
- 改进提示词并支持更多语言
许可证
MIT — 随你使用。如果能点个星标,我们将不胜感激。
链接
评论
一个开放协议,规定了 AI 应用如何以统一方式连接外部工具与数据源。客户端负责与模型对话,MCP 服务负责提供工具、资源和提示模板,两者通过标准消息通信,同一个服务可以被不同客户端复用。