Skip to main content
Glama
GitLab MR MCP

GitLab MR MCP

by aminpt
README.md
# GitLab MR MCP

A local, **read-only** Model Context Protocol (MCP) server that lets MCP-compatible clients such as Cursor inspect GitLab projects and Merge Requests through the GitLab REST API.

It can list accessible projects and MRs, retrieve MR metadata, discussions, diffs, and commits, and search MRs. It does not create comments, modify files, approve, close, or merge MRs.

## Features

- Discover projects visible to the configured GitLab token
- List open, closed, merged, or all Merge Requests
- Filter MRs by author, reviewer, or search text
- Retrieve MR metadata, discussion threads, diffs, and commits
- Search MR titles/descriptions
- Use GitLab.com or a self-managed GitLab instance

## Requirements

- Python 3.10+
- An account on a GitLab instance
- A Personal Access Token with the `read_api` scope
- An MCP-compatible client that supports local `stdio` servers (for example, Cursor)

## Installation

```bash
git clone https://github.com/YOUR_USERNAME/gitlab-mr-mcp.git
cd gitlab-mr-mcp
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install -r requirements.txt
cp .env.example .env
```

Edit `.env` and set your own GitLab instance URL and token:

```dotenv
GITLAB_URL=https://gitlab.example.com
GITLAB_TOKEN=glpat-your_personal_access_token
GITLAB_MAX_PROJECTS=500
GITLAB_MAX_PAGES=20
GITLAB_TIMEOUT=30
```

Create the token in your GitLab user settings and grant **only** `read_api` unless you have a specific reason to do otherwise. The token can access only resources that your GitLab account is permitted to see.

Protect the local secrets file:

```bash
chmod 600 .env
```

**Never commit or share `.env`.** The repository's `.gitignore` excludes it, but always verify before pushing.

## Verify your credentials

With the virtual environment activated:

```bash
set -a
source .env
set +a
curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GITLAB_URL/api/v4/user"
```

A JSON response containing your GitLab username indicates that authentication works. `401` usually indicates an invalid/expired token or insufficient token scope; `403` indicates a permission or instance-policy issue.

## Configure Cursor

Find the absolute path to this project:

```bash
pwd
```

Add the following server entry to Cursor's MCP configuration (commonly `~/.cursor/mcp.json`). Replace `/ABSOLUTE/PATH/TO/gitlab-mr-mcp` with the path printed by `pwd`:

```json
{
  "mcpServers": {
    "gitlab-mr-mcp": {
      "command": "/ABSOLUTE/PATH/TO/gitlab-mr-mcp/.venv/bin/python",
      "args": [
        "/ABSOLUTE/PATH/TO/gitlab-mr-mcp/server.py"
      ],
      "env": {
        "GITLAB_URL": "https://gitlab.example.com"
      }
    }
  }
}
```

The server loads `GITLAB_TOKEN` from the `.env` file located beside `server.py`; do not put the token in the shared MCP JSON configuration. Restart or reload the MCP server in Cursor after saving the configuration.

## Available tools

| Tool | Purpose |
|---|---|
| `list_projects(search="", limit=100)` | List projects where the token's user is a member |
| `list_merge_requests(project="", state="opened", author_username="", reviewer_username="", search="", limit=50)` | List MRs in one project or across discovered membership projects |
| `get_merge_request(project, iid)` | Retrieve MR metadata and description |
| `get_mr_discussions(project, iid, limit=200)` | Retrieve discussion threads and notes, including resolution metadata when returned by GitLab |
| `get_mr_diffs(project, iid, limit=100)` | Retrieve changed file paths and diff patches |
| `get_mr_commits(project, iid, limit=100)` | Retrieve commits belonging to an MR |
| `search_merge_requests(query, state="all", project="", limit=50)` | Search MRs by title/description text |

For `project`, use either the numeric project ID or its full path, such as `group/subgroup/project`. Valid `state` values are `opened`, `closed`, `merged`, and `all`.

## Example prompts

```text
Use gitlab-mr-mcp to list the projects I can access.
```

```text
List the 30 most recently updated open merge requests across my accessible projects.
Group them by project.
```

```text
For MR !123 in project group/subgroup/project, retrieve its metadata,
discussions, and diff. Summarize unresolved review comments.
Do not modify anything.
```

## Configuration and limitations

- **Read-only:** the implementation issues GitLab `GET` requests only.
- **Project discovery:** project discovery currently uses GitLab's `membership=true` filter. It may not include every project visible to a user through inherited group access or other visibility rules.
- **Pagination:** results use GitLab's `page` / `per_page` pagination and are bounded by `GITLAB_MAX_PAGES`, `GITLAB_MAX_PROJECTS`, and per-tool limits.
- **Large diffs:** GitLab may limit or truncate very large diff responses.
- **GitLab versions:** endpoint availability and returned fields can vary by GitLab version and instance configuration.
- **Permissions:** the token cannot bypass the user's GitLab permissions or instance policies.

## Security

- Use a dedicated token with the minimum required scope (`read_api`).
- Keep `.env` private and out of version control.
- Do not paste tokens into prompts, issues, logs, screenshots, or shared configuration files.
- Revoke and replace a token immediately if it is accidentally exposed.

## License

This project is distributed under the MIT License. See [LICENSE](LICENSE).