使用说明
MCP GitLab内部部署
一个MCP(模型上下文协议)服务器,使Claude能够与您的GitLab本地实例进行交互。此服务器提供18个工具,用于直接从Claude管理项目、问题、合并请求和CI/CD管道。
先决条件
- Python 3.10+
- 紫外线 包管理器
- GitLab个人访问令牌
安装
-
克隆存储库:
git clone https://innersource.soprasteria.com/i2s-ics-do/dps_mcp_local_gitlab.git cd dps_mcp_local_gitlab -
安装依赖项:
uv sync
GitLab令牌设置
在GitLab实例上创建个人访问令牌:
- 首选
https://your-gitlab.com/-/user_settings/personal_access_tokens - 创建具有适当作用域的新令牌:
| 用例 | 所需范围 |
|---|---|
| 完全访问(读+写) | api |
| 只读访问 | read_api + read_repository |
配置Claude代码
选项1:CLI命令(推荐)
使用命令行将MCP服务器添加到Claude代码中:
claude mcp add gitlab \
--transport stdio \
--env GITLAB_URL=https://your-gitlab.com \
--env GITLAB_TOKEN=glpat-xxxxxxxxxxxx \
-- uv --directory /path/to/dps_mcp_local_gitlab run mcp-gitlab
替换:
https://your-gitlab.com使用您的GitLab实例URLglpat-xxxxxxxxxxxx使用您的个人访问令牌/path/to/dps_mcp_local_gitlab具有此存储库的绝对路径
选项2:项目配置文件(.mcp.json)
创建一个 .mcp.json 项目根目录中的文件,与团队共享配置:
{
"mcpServers": {
"gitlab": {
"command": "uv",
"args": ["--directory", "/path/to/dps_mcp_local_gitlab", "run", "mcp-gitlab"],
"env": {
"GITLAB_URL": "${GITLAB_URL}",
"GITLAB_TOKEN": "${GITLAB_TOKEN}"
}
}
}
}
然后在shell中设置环境变量或 .env 文件:
export GITLAB_URL=https://your-gitlab.com
export GITLAB_TOKEN=glpat-xxxxxxxxxxxx
选项3:Claude桌面配置
编辑您的Claude Desktop配置文件:
- macOS:
~/Library/Application Support/Claude/claude_desktop_config.json - 视窗:
%APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"gitlab": {
"command": "uv",
"args": ["--directory", "/path/to/dps_mcp_local_gitlab", "run", "mcp-gitlab"],
"env": {
"GITLAB_URL": "https://your-gitlab.com",
"GITLAB_TOKEN": "glpat-xxxxxxxxxxxx"
}
}
}
}
有用的Claude代码命令
claude mcp list # List all configured MCP servers
claude mcp get gitlab # View gitlab MCP configuration details
claude mcp remove gitlab # Remove the gitlab MCP server
SSL配置
对于自签名证书,请添加 GITLAB_SSL_VERIFY 环境变量:
claude mcp add gitlab \
--transport stdio \
--env GITLAB_URL=https://your-gitlab.com \
--env GITLAB_TOKEN=glpat-xxxxxxxxxxxx \
--env GITLAB_SSL_VERIFY=false \
-- uv --directory /path/to/dps_mcp_local_gitlab run mcp-gitlab
可用工具
项目和存储库(6个工具)
| 工具 | 说明 |
|---|---|
list_projects |
列出可访问的项目 |
get_project |
获取项目详细信息 |
list_branches |
列出项目中的分支 |
list_commits |
列表提交 |
get_file |
从存储库中读取文件 |
list_repository_tree |
列出文件和目录 |
问题(4个工具)
| 工具 | 说明 |
|---|---|
list_issues |
列出项目问题 |
get_issue |
获取问题详细信息 |
create_issue |
创建新问题 |
add_issue_comment |
向问题添加评论 |
合并请求(4个工具)
| 工具 | 说明 |
|---|---|
list_merge_requests |
列出合并请求 |
get_merge_request |
获取MR详细信息 |
get_merge_request_changes |
查看MR差异 |
add_mr_comment |
向MR添加评论 |
管道CI/CD(4个工具)
| 工具 | 说明 |
|---|---|
list_pipelines |
列出CI/CD管道 |
get_pipeline |
获取管道详细信息 |
list_pipeline_jobs |
列出管道中的作业 |
get_job_log |
查看作业日志 |
用法示例
配置后,您可以让Claude与您的GitLab实例进行交互:
- “列出我的所有GitLab项目”
- “向我展示我的团队/我的项目中的未决问题”
- “获取合并请求的差异!42在项目我的团队/我的项目中”
- “为什么123号管道在我的团队/项目中失败了?”
- “在我的团队/我的项目中创建一个题为“修复登录错误”的问题”
故障排除
“GITLAB_URL环境变量是必需的”
添加MCP服务器时,请确保已正确配置环境变量。
认证失败
验证您的个人访问令牌是否有效,是否具有所需的范围。
SSL证书错误
如果使用自签名证书,请设置 GITLAB_SSL_VERIFY=false 在您的MCP配置中。