Skip to main content
Glama
ffpy

GitLab MCP Code Review

by ffpy
README.md
# GitLab MCP 代码审查工具

[![Python 3.10+](https://img.shields.io/badge/python-3.10+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

> 本项目 fork 自 [cayirtepeomer/gerrit-code-review-mcp](https://github.com/cayirtepeomer/gerrit-code-review-mcp) 并为 GitLab 集成进行了适配。

一个用于将 Claude 等 AI 助手与 GitLab 的合并请求集成的 MCP (Model Context Protocol) 服务器。这使得 AI 助手可以通过 GitLab API 直接审查代码变更。

## 功能

- **完整的合并请求分析**: 获取合并请求的全部详情,包括差异、提交和评论
- **文件特定的差异**: 分析合并请求中特定文件的变更
- **版本比较**: 比较不同的分支、标签或提交
- **审查管理**: 添加评论、批准或取消批准合并请求
- **项目概览**: 获取项目中的所有合并请求列表

## 安装

### 先决条件

- Python 3.10+
- uv
- 具有 API 范围 (read_api, api) 的 GitLab 个人访问令牌
- 用于 MCP 集成的 [Cursor IDE](https://cursor.sh/) 或 [Claude 桌面应用](https://claude.ai/desktop)

## Cursor IDE 集成

克隆此仓库:
```bash
git clone https://gitea.ffpy.site/ffpy/gitlab-mcp-code-review.git
cd gitlab-mcp-code-review
```

要将此 MCP 与 Cursor IDE 一起使用,请将以下配置添加到你的 `~/.cursor/mcp.json` 文件中:

```json
{
  "mcpServers": {
    "gitlab-mcp-code-review": {
      "command": "uv",
      "args": [
        "--directory",
        "/path/to/your/gitlab-mcp-code-review",
        "run",
        "server.py"
      ],
      "env": {
        "GITLAB_HOST": "gitlab.com",
        "GITLAB_TOKEN": "xxx"
      }
    }
  }
}
```

- 将 `/path/to/your/gitlab-mcp-code-review` 替换为你克隆仓库的实际路径。
- 将 `GITLAB_HOST` 修改为你的Gitlab地址
- 将 `GITLAB_TOKEN` 修改为你的AccessToken

## 环境变量

### 必需的环境变量

- `GITLAB_HOST`: GitLab 实例的主机名(默认: gitlab.com)
- `GITLAB_TOKEN`: GitLab 个人访问令牌(需要 api 和 read_api 权限)

### 可选的环境变量(用于代码审查规范功能)

如果你想使用 `fetch_code_review_rules` 工具从远程服务器获取团队的代码审查规范,需要配置以下环境变量:

- `CODE_REVIEW_SSH_HOST`: SSH 服务器地址
- `CODE_REVIEW_SSH_PORT`: SSH 端口(默认: 22)
- `CODE_REVIEW_SSH_USERNAME`: SSH 用户名
- `CODE_REVIEW_SSH_PASSWORD`: SSH 密码
- `CODE_REVIEW_RULE_FILE`: 规范文件在服务器上的绝对路径(例如: /home/user/code-review-rules.md)

配置示例:
```json
{
  "mcpServers": {
    "gitlab-mcp-code-review": {
      "command": "uv",
      "args": ["--directory", "/path/to/your/gitlab-mcp-code-review", "run", "server.py"],
      "env": {
        "GITLAB_HOST": "gitlab.com",
        "GITLAB_TOKEN": "xxx",
        "CODE_REVIEW_SSH_HOST": "your-server.com",
        "CODE_REVIEW_SSH_PORT": "22",
        "CODE_REVIEW_SSH_USERNAME": "your-username",
        "CODE_REVIEW_SSH_PASSWORD": "your-password",
        "CODE_REVIEW_RULE_FILE": "/path/to/code-review-rules.md"
      }
    }
  }
}
```

## 可用工具

MCP 服务器提供以下工具用于与 GitLab 交互:

| 工具 | 描述 |
|---|---|
| `fetch_code_review_rules` | 通过 SSH 获取团队的代码审查规范文件 |
| `fetch_merge_request` | 获取有关合并请求的完整信息 |
| `compare_versions` | 比较不同的分支、标签或提交 |
| `add_merge_request_comment` | 向合并请求添加评论 |
| `approve_merge_request` | 批准合并请求 |
| `unapprove_merge_request` | 取消批准合并请求 |
| `get_project_merge_requests` | 获取项目的合并请求列表 |

## 故障排除

如果遇到问题:

1.  验证你的 GitLab 令牌是否具有适当的权限 (api, read_api)
2.  确保你的 MCP 配置路径正确
3.  使用以下命令测试连接:`curl -H "Private-Token: your-token" https://gitlab.com/api/v4/projects`

## 许可证

本项目根据 MIT 许可证授权 - 有关详细信息,请参阅 [LICENSE](LICENSE) 文件。

TDQS

A3.5/5.0

Scored across 12 tools

Disambiguation4/5

Most tools have distinct purposes focused on merge request management and code review workflows, with clear boundaries between actions like adding comments, approving, and comparing versions. However, there is some potential overlap between 'add_merge_request_comment' and 'add_merge_request_discussion' as both involve commenting, though the latter is more specific to file positions.

Naming Consistency5/5

Tool names follow a consistent verb_noun pattern throughout, such as 'add_merge_request_comment', 'approve_merge_request', and 'fetch_code_review_rules'. All tools use snake_case and maintain a predictable structure, making them easy to parse and understand.

Tool Count5/5

With 12 tools, the set is well-scoped for a GitLab code review server, covering essential operations like fetching merge requests, managing discussions, approvals, and project searches. Each tool serves a clear purpose without redundancy, fitting the domain appropriately.

Completeness4/5

The toolset provides comprehensive coverage for code review tasks, including fetching rules, listing and fetching merge requests, adding and managing discussions, and handling approvals. A minor gap is the lack of tools for directly modifying merge requests (e.g., updating or merging them), but core review workflows are well-supported.

Maintenance

ActivityInactive
ResponsivenessNo issues