克劳德代码评审-MCP
一个 MCP(模型上下文协议)服务器,使用 OpenAI、Google 和 Anthropic 模型提供代码审查功能。它作为代码审查的“第二意见”工具,可与任何 MCP 客户端(包括 Claude Code、Claude Desktop、Cursor 和 Windsurf)配合使用。
特征
- 多提供商支持:利用 OpenAI、Google 的 Gemini 和 Anthropic 的 Claude 模型进行代码审查
- 两种评审类型:选择结构化评审(包含分类反馈)或自由形式的叙述评审
- 上下文感知:包括项目结构、相关文件、提交消息和依赖项,以便进行更相关的审查
- 智能代码处理:自动检测编程语言、处理大文件并适当格式化输出
- 强大的错误处理:包括 API 故障的重试逻辑和优雅的错误恢复
- MCP 兼容:可与任何 MCP 客户端(Claude Code、Claude Desktop、Cursor、Windsurf)配合使用
- 轻松设置:通过环境变量进行简单配置
安装
全局安装
npm install -g claude-code-review-mcp
与 npx 一起使用(无需安装)
# Set environment variables separately
export OPENAI_API_KEY=<key>
npx -y claude-code-review-mcp
# Or use inline environment setting
OPENAI_API_KEY=<key> npx -y claude-code-review-mcp
# Or with Google API key
GOOGLE_API_KEY=<key> npx -y claude-code-review-mcp
# Or with Anthropic API key
ANTHROPIC_API_KEY=<key> npx -y claude-code-review-mcp
# Or use multiple API keys for more model options
OPENAI_API_KEY=<key> GOOGLE_API_KEY=<key> ANTHROPIC_API_KEY=<key> npx -y claude-code-review-mcp
配置
服务器至少需要以下 API 密钥之一:
OPENAI_API_KEY
:您的 OpenAI API 密钥GOOGLE_API_KEY
:您的 Google Gemini API 密钥ANTHROPIC_API_KEY
:您的 Anthropic API 密钥
可选配置:
PORT
:服务器端口(默认:动态 - 将选择可用端口)HOST
:服务器主机(默认值:127.0.0.1)LOG_LEVEL
:日志级别(0=DEBUG、1=INFO、2=WARN、3=ERROR;默认值:1)
可用型号
OpenAI 模型(需要 OPENAI_API_KEY)
gpt-4.1
- OpenAI GPT-4.1o4-mini
——OpenAI O4 Minio3-mini
——OpenAI O3 Mini
Google 模型(需要 GOOGLE_API_KEY)
gemini-2.5-pro-preview-05-06
- Google Gemini 2.5 Progemini-2.5-flash-preview-04-17
- Google Gemini 2.5 Flash
人择模型(需要 ANTHROPIC_API_KEY)
claude-3-opus-20240229
- 人类学克劳德 3 作品claude-3-sonnet-20240229
- 人性的克劳德 3 首十四行诗claude-3-haiku-20240307
- 人类学克劳德 3 俳句
可用工具
MCP 服务器提供了三种工具:
1. reviewCodeStructured
提供详细、结构化的代码审查,包括以下部分:
- 总体摘要
- 代码质量(优势和劣势)
- 错误(严重程度和建议的修复)
- 改进建议
- 安全问题(如果有)
以自由格式的文本格式提供叙述性代码审查,适合一般印象和对话反馈。
3. 列表模型
根据提供的 API 密钥列出所有可用的模型,包括模型 ID 和人类可读的名称。
与 Claude Code 集成
要将此 MCP 服务器添加到 Claude 代码:
# Use environment variables properly (recommended approach)
claude mcp add code-review -s user -e OPENAI_API_KEY=<key> -e GOOGLE_API_KEY=<key> -e ANTHROPIC_API_KEY=<key> -- npx -y claude-code-review-mcp
# Alternative: Export the variables before adding the MCP
export OPENAI_API_KEY=<key>
export GOOGLE_API_KEY=<key>
export ANTHROPIC_API_KEY=<key>
claude mcp add code-review -s user -- npx -y claude-code-review-mcp
您还可以通过在.claude/commands/review-with.md
创建文件来创建自定义斜线命令:
I'll review your code using alternative LLM models. Model to use: $ARGUMENTS
Claude Code 支持自定义斜线命令,您可以创建这些命令以便轻松与 MCP 服务器交互。在项目的.claude/commands/
目录中创建这些命令,即可启用强大的代码审查工作流程。
基本设置
首先,如果命令目录不存在,则创建它:
mkdir -p .claude/commands
模型列表命令
创建一个命令来列出可用的模型:
# Create the list-review-models.md file
cat > .claude/commands/list-review-models.md << 'EOF'
I'll check which alternative code review models are available through our MCP server.
First, I'll use the MCP server to list all available models for code review.
After that, I'll present the models in a clear table format with:
- Model ID (what you'll use when requesting a review)
- Provider (OpenAI or Google)
- Description (size and capabilities)
- Speed (relative performance)
This will help you choose the right model for your code review needs.
EOF
基本代码审查命令
创建一个接受模型名称的简单审查命令:
# Create the review-with.md file
cat > .claude/commands/review-with.md << 'EOF'
I'll review the code I've just worked on using an alternative LLM model to provide a second opinion.
First, I'll identify the code changes or file you want reviewed. If you don't specify a file, I'll look at recent changes.
Then, I'll send this code to be reviewed by the specified model through our MCP server.
Available models (run /project:list-review-models to see all options):
- OpenAI models (if configured): "gpt-4.1", "o4-mini", "o3-mini"
- Google models (if configured): "gemini-2.5-pro-preview-05-06", "gemini-2.5-flash-preview-04-17"
Model to use (leave blank for default): $ARGUMENTS
EOF
结构化审查指挥部
创建一个专门用于结构化评论的命令:
# Create the structured-review.md file
cat > .claude/commands/structured-review.md << 'EOF'
I'll perform a structured code review using an alternative LLM model.
This review will be organized into clear sections:
1. Overall summary
2. Code quality assessment (strengths and weaknesses)
3. Potential bugs with severity ratings (Low/Medium/High)
4. Specific improvement suggestions
5. Security considerations (if applicable)
If you don't specify a model, I'll use the default available model.
Model to use (optional): $ARGUMENTS
EOF
自由格式审阅命令
为叙述式评论创建命令:
# Create the freeform-review.md file
cat > .claude/commands/freeform-review.md << 'EOF'
I'll provide a conversational, narrative-style code review using an alternative LLM model.
This will be a more holistic assessment of your code with flowing paragraphs rather than structured categories. This style works well for:
- General impressions
- High-level feedback
- More nuanced commentary on code style and approach
If you don't specify a model, I'll use the default available model.
Model to use (optional): $ARGUMENTS
EOF
审查特定文件命令
创建一个命令来审查特定文件:
# Create the review-file.md file
cat > .claude/commands/review-file.md << 'EOF'
I'll review a specific file using an alternative LLM model.
Please provide the file path to review and optionally the model to use.
Format: [file_path] [model_name]
For example:
- "src/utils.js gemini-2.5-pro-preview-05-06" - Reviews utils.js with Gemini Pro
- "lib/auth.ts" - Reviews auth.ts with the default model
Input: $ARGUMENTS
EOF
特定焦点的审查命令
创建专门的审核命令:
# Create security review command
cat > .claude/commands/security-review.md << 'EOF'
I'll perform a security-focused code review using an alternative LLM model.
This review will specifically examine:
- Potential security vulnerabilities
- Input validation issues
- Authentication/authorization flaws
- Data protection concerns
- Injection vulnerabilities
- Secure coding best practices
If you don't specify a model, I'll use a model recommended for security analysis.
Model to use (optional): $ARGUMENTS
EOF
# Create performance review command
cat > .claude/commands/performance-review.md << 'EOF'
I'll perform a performance-focused code review using an alternative LLM model.
This review will specifically examine:
- Algorithm efficiency
- Memory usage
- Unnecessary computations
- Loop optimizations
- Data structure choices
- Caching opportunities
- Async/parallel processing considerations
If you don't specify a model, I'll use a model that's good at performance analysis.
Model to use (optional): $ARGUMENTS
EOF
综合项目审查指挥部
创建一个用于审查具有完整项目上下文的代码的命令:
# Create the project-review.md file
cat > .claude/commands/project-review.md << 'EOF'
I'll perform a comprehensive code review with full project context using an alternative LLM model.
This review will:
1. Analyze the code structure and organization
2. Consider related files and dependencies
3. Evaluate consistency with project patterns
4. Assess integration with existing components
5. Check alignment with project architecture
I'll gather project context, including directory structure and related files, to ensure a thorough, context-aware review.
Format: [file_to_review] [model_name]
Example: "src/components/Button.jsx gemini-2.5-pro-preview-05-06"
Input: $ARGUMENTS
EOF
前后审查命令
创建一个命令来比较代码变化:
# Create the diff-review.md file
cat > .claude/commands/diff-review.md << 'EOF'
I'll review the changes you've made to a file using an alternative LLM model.
This will:
1. Identify what was changed between versions
2. Evaluate if the changes address the intended purpose
3. Check for any new issues introduced
4. Suggest potential improvements to the changes
I'll need to know which file to examine. If you've been working on a file with Claude Code, I'll automatically find the changes.
Model to use (optional): $ARGUMENTS
EOF
使用自定义斜线命令
创建这些命令后,您可以在 Claude Code 中输入/project:
后跟命令名称来使用它们。例如:
/project:list-review-models
/project:review-with gemini-2.5-pro-preview-05-06
/project:structured-review o4-mini
/project:security-review
/project:review-file src/utils.js gemini-2.5-flash-preview-04-17
自定义命令的提示
- 命令发现:在 Claude Code 中输入
/project:
来查看可用命令列表 - 默认模型:如果您未指定模型,则命令将使用默认模型(如果可用,通常为 o4-mini)
- 多重评论:您可以通过使用不同的模型运行评论来获得多种视角
- 项目背景:对于最相关的评论,使用包含项目背景的命令
- 专业焦点:当您特别关注安全性、性能等时,请使用特定焦点的命令。
示例工作流程
典型的工作流程可能如下所示:
- 使用 Claude Code 编写代码
- 运行
/project:list-review-models
查看可用选项 - 运行
/project:structured-review gemini-2.5-pro-preview-05-06
以获取来自 Google 模型的结构化评论 - 与克劳德的建议进行比较
- 从两个角度进行改进
- 运行
/project:diff-review
来查看更改
这些自定义命令使 Claude Code 和 claude-code-review-mcp 服务器能够顺利集成,为您的代码提供宝贵的“第二意见”。
示例用法
启动 MCP 服务器
# Start with OpenAI API key (using exports, recommended)
export OPENAI_API_KEY=<key>
npx -y claude-code-review-mcp
# Or with inline environment variables
OPENAI_API_KEY=<key> npx -y claude-code-review-mcp
# Start with Google Gemini API key
export GOOGLE_API_KEY=<key>
npx -y claude-code-review-mcp
# Start with Anthropic Claude API key
export ANTHROPIC_API_KEY=<key>
npx -y claude-code-review-mcp
# Use multiple API keys for more model options
export OPENAI_API_KEY=<key>
export GOOGLE_API_KEY=<key>
export ANTHROPIC_API_KEY=<key>
npx -y claude-code-review-mcp
# Use custom port and host
export OPENAI_API_KEY=<key>
export PORT=8080
export HOST=0.0.0.0
npx -y claude-code-review-mcp
与 MCP 客户端一起使用
服务器启动后,您可以使用服务器的 URL 从任何 MCP 客户端(例如 Claude Code、Claude Desktop、Cursor 或 Windsurf)连接到服务器。服务器会在启动日志中显示实际的 URL 和端口(使用动态分配的端口以避免冲突)。
输入模式
所有审查工具均接受以下输入:
{
code: string; // Required: The code to review
filename?: string; // Optional: The filename with extension
language?: string; // Optional: Programming language
model: string; // Required: Model ID to use for review
projectContext?: { // Optional: Additional context
projectStructure?: string; // Directory structure
relatedFiles?: Array<{ // Related code files
name: string; // Filename with path
language?: string; // File language
content: string; // File content
}>;
commitMessage?: string; // Purpose of changes
dependencies?: Record<string, string>; // Project dependencies
}
}
输出架构
结构化评审输出
{
review?: { // Present on success
summary: string; // Overall assessment
quality: {
strengths: string[]; // Good aspects
weaknesses: string[]; // Areas for improvement
};
bugs: Array<{
description: string; // Issue description
severity: "Low" | "Medium" | "High"; // Impact level
suggestion: string; // How to fix
}>;
improvements: string[]; // Enhancement suggestions
securityIssues?: string[]; // Security concerns if any
};
modelUsed: string; // Human-readable model name
error?: string; // Present on error
availableModels?: Record<string, string>; // Present on error or listModels
}
自由格式审阅输出
{
reviewText?: string; // Present on success
modelUsed: string; // Human-readable model name
error?: string; // Present on error
availableModels?: Record<string, string>; // Present on error or listModels
}
列出模型输出
{
availableModels: Record<string, string>; // Model ID to name mapping
modelUsed: string; // Always "None" for this tool
}
MCP 客户端集成
克劳德·科德
- 添加 MCP 服务器:
claude mcp add code-review -s user npx -y claude-code-review-mcp
- 在克劳德代码中的使用:
/code-review:reviewCodeStructured --model o4-mini
克劳德桌面
在 Claude Desktop 设置中,按如下方式配置 MCP:
"claude-code-review-mcp": {
"command": "npx",
"args": ["-y", "claude-code-review-mcp"],
"env": {
"OPENAI_API_KEY": "your-openai-key",
"GOOGLE_API_KEY": "your-google-key",
"ANTHROPIC_API_KEY": "your-anthropic-key"
}
}
该服务器使用先进的 JSON 输出清理技术,与所有 MCP 客户端(包括 Claude Desktop)完全兼容。
光标和风帆冲浪
遵循客户端的特定 MCP 配置指南,使用相同的命令和环境变量。
故障排除
API 密钥问题
- “Model X 不可用” :确保您已为该模型提供了适当的 API 密钥。
- 未提供 API 密钥:您必须提供 OPENAI_API_KEY、GOOGLE_API_KEY 或 ANTHROPIC_API_KEY 中的至少一个。
- 建议型号:如果您请求的型号不可用,服务器将建议替代型号。
速率限制和 API 错误
- 如果您遇到速率限制或 API 错误,错误消息将会指出该问题。
- 如果一个提供商遇到问题,请考虑使用不同的模型。
安全注意事项
- API 密钥永远不会被记录或暴露
- 为保护隐私,代码内容仅被最低限度记录
- 保持最小依赖性以减少安全面
- 请求处理包括输入验证和清理
- 错误消息旨在避免泄露敏感信息
兼容性
- 需要 Node.js 18.0.0 或更高版本
- 适用于 Linux、macOS 和 Windows(如有必要,可通过 WSL)
- 与所有 MCP 客户端兼容(Claude Code、Claude Desktop、Cursor、Windsurf)
- 优雅地处理大型代码文件和项目上下文
- 暂时性 API 故障的自动重试机制
发展
# Install dependencies
npm install
# Start in development mode
npm run dev
# Build for production
npm run build
# Start in production mode
npm run start
执照
麻省理工学院
贡献者