summaryrefslogtreecommitdiff
path: root/servers/gitlab_glab/README.md
blob: 7a4bc5fb615642ddfdd4581c117d231b7354ef66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!-- ⚠️ DEPRECATION NOTICE: The `gitlab_glab` MCP server is deprecated and will no longer be maintained. Please migrate to the `gitlab_python` MCP server, which is the recommended and actively supported replacement. See `servers/gitlab_python/README.md` for details. -->
# GitLab CLI MCP Server

This MCP server provides integration with GitLab through the GitLab CLI (`glab`) tool. It allows LLM agents to interact with GitLab repositories and resources using the GitLab API.

## Features

- Check if the GitLab CLI is available and accessible
- Find GitLab projects by name and retrieve their IDs
- Search for GitLab issues with various filters
- Create new GitLab issues
- Get merge request diffs with automatic handling of large diffs
- Run CI/CD pipelines with support for variables and web mode
- More features to be added in the future

## Prerequisites

- Python 3.11 or higher
- GitLab CLI (`glab`) installed and accessible in the system PATH
- GitLab account with proper authentication set up via `glab auth login`

## Installation

```bash
# Clone the repository
git clone https://codeberg.org/knightdave/dawids-mcp-servers.git
cd dawids-mcp-servers
```

## Usage

### Running from repository root (recommended)

```bash
# Run with stdio transport (for MCP client integration)
uv --directory /path-to-repo/dawids-mcp-servers/servers/gitlab_glab run mcp-gitlab-glab --transport stdio

# Run with remote transport
uv --directory /path-to-repo/dawids-mcp-servers/servers/gitlab_glab run mcp-gitlab-glab --transport remote --host 0.0.0.0 --port 8080
```

Replace `/path-to-repo` with the actual path to where you cloned this repository.

### Alternative: Running from server directory

```bash
cd servers/gitlab_glab

# Run with stdio transport
uv run mcp-gitlab-glab --transport stdio

# Run with remote transport
uv run mcp-gitlab-glab --transport remote --host 0.0.0.0 --port 8080
```

## Available Tools

### check_glab_availability

Checks if the GitLab CLI tool is installed and accessible.

```python
result = use_mcp_tool(
    server_name="gitlab_glab",
    tool_name="check_glab_availability",
    arguments={
        "working_directory": "/path/to/current/directory"
    }
)
```

### find_project

Finds GitLab projects by name and returns their details.

```python
result = use_mcp_tool(
    server_name="gitlab_glab",
    tool_name="find_project",
    arguments={
        "project_name": "my-project",
        "working_directory": "/path/to/current/directory"
    }
)
```

The function returns a list of matching projects, each containing the following fields:
- `id`: The project ID
- `name`: The project name
- `path_with_namespace`: The project path with namespace
- `web_url`: The project web URL
- `description`: The project description

### search_issues

Search for GitLab issues with various filters.

```python
result = use_mcp_tool(
    server_name="gitlab_glab",
    tool_name="search_issues",
    arguments={
        "working_directory": "/path/to/current/directory",
        # Optional filters
        "author": "username",  # Filter by author
        "assignee": "username",  # Filter by assignee
        "closed": True,  # Get only closed issues
        "confidential": True,  # Filter by confidential issues
        "group": "group-name",  # Select a group or subgroup
        "issue_type": "issue",  # Filter by type: issue, incident, test_case
        "iteration": 123,  # Filter by iteration ID
        "label": ["bug", "critical"],  # Filter by labels
        "milestone": "v1.0",  # Filter by milestone
        "not_assignee": "username",  # Filter by not being assigned to
        "not_author": "username",  # Filter by not being authored by
        "not_label": ["wontfix"],  # Filter by lack of labels
        "page": 1,  # Page number (default: 1)
        "per_page": 30,  # Items per page (default: 30)
        "project": "group/project"  # Project path with namespace
    }
)
```

The function returns a dictionary containing:
- `issues`: A list of issues, each with:
  - `id`: The issue ID
  - `iid`: The internal issue ID
  - `title`: The issue title
  - `web_url`: The issue URL
  - `state`: The issue state (opened/closed)
  - `created_at`: Creation timestamp
  - `updated_at`: Last update timestamp
- `error`: Error message if the operation failed

### create_issue

Creates a new GitLab issue and returns its URL.

```python
result = use_mcp_tool(
    server_name="gitlab_glab",
    tool_name="create_issue",
    arguments={
        "title": "Issue title",
        "description": "Issue description",
        "working_directory": "/path/to/current/directory",
        # Optional parameters
        "labels": ["bug", "critical"],  # List of labels
        "assignee": ["username1", "username2"],  # List of usernames
        "milestone": "v1.0",  # Milestone title or ID
        "epic_id": 123,  # Epic ID
        "project": "group/project"  # Project path with namespace
    }
)
```

The function returns a dictionary containing:
- `url`: The URL of the created issue
- `error`: Error message if the operation failed

### get_mr_diff

Get the diff for a merge request using `glab mr diff` with optional content filtering.

```python
result = use_mcp_tool(
    server_name="gitlab_glab",
    tool_name="get_mr_diff",
    arguments={
        "working_directory": "/path/to/current/directory",
        # Optional parameters
        "mr_id": "123",  # MR ID or branch name (if None, uses current branch)
        "color": "never",  # Use color in diff output: always, never, auto (default: never)
        "raw": True,  # Use raw diff format (default: False)
        "repo": "group/project",  # Project path with namespace
        "max_size_kb": 100,  # Maximum size in KB before saving to temp file (default: 100)
        "filter_extensions": [".lock", ".log"]  # File extensions to exclude from diff (default: [".lock", ".log"])
    }
)
```

The function returns a dictionary containing:

- `diff`: The diff content (if small enough)
- `size_kb`: The size of the diff in KB
- `temp_file_path`: Path to temporary file if diff is too large (None otherwise)
- `diff_too_large`: Boolean indicating if diff was saved to temp file
- `max_size_kb`: The configured maximum size threshold
- `message`: Human-readable message about temp file creation (if applicable)
- `error`: Error message if the operation failed

**Content Filtering**: By default, this tool filters out changes to files with `.lock` and `.log` extensions to reduce noise in diffs. This includes:

- Lock files: `package-lock.json`, `yarn.lock`, `composer.lock`, etc.
- Log files: `debug.log`, `error.log`, `application.log`, etc.

You can customize the filtering by providing a `filter_extensions` list, or disable filtering entirely by passing an empty list `[]`.

**Note on Large Diffs**: To prevent overwhelming LLMs with extremely large diffs, this tool automatically saves diffs larger than `max_size_kb` (default: 100KB) to a temporary file and returns the file path instead of the content. This allows you to process large merge request diffs without hitting token limits.

### run_ci_pipeline

Run a CI/CD pipeline on GitLab using `glab ci run`.

```python
result = use_mcp_tool(
    server_name="gitlab_glab",
    tool_name="run_ci_pipeline",
    arguments={
        "working_directory": "/path/to/current/directory",
        # Optional parameters
        "branch": "main",  # Branch/ref to run pipeline on (if None, uses current branch)
        "variables": ["VAR1:value1", "VAR2:value2"],  # Variables in key:value format
        "variables_env": ["ENV1:envval1"],  # Environment variables in key:value format
        "variables_file": ["FILE1:file1.txt"],  # File variables in key:filename format
        "variables_from": "/path/to/vars.json",  # JSON file containing variables
        "web_mode": True,  # Enable web mode (sets CI_PIPELINE_SOURCE=web)
        "repo": "group/project"  # Project path with namespace
    }
)
```

The function returns a dictionary containing:
- `success`: Boolean indicating if the pipeline was created successfully
- `output`: The full command output from glab
- `branch`: The branch the pipeline was created on
- `web_mode`: Boolean indicating if web mode was used
- `pipeline_url`: The URL of the created pipeline (if found in output)
- `error`: Error message if the operation failed

**Branch Detection**: If no `branch` is specified, the tool will automatically detect the current git branch using `git branch --show-current`. If branch detection fails, the pipeline will be created without specifying a branch (uses GitLab's default behavior).

**Web Mode**: When `web_mode` is set to `True`, the tool adds `CI_PIPELINE_SOURCE:web` as an environment variable, which allows the pipeline to run with web-triggered behavior and access to manual pipeline features.

## Working Directory Context

All tools require a `working_directory` parameter that specifies the directory context in which the GitLab CLI commands should be executed. This ensures that commands are run in the same directory as the agent, maintaining proper context for repository operations.

The working directory should be the absolute path to the directory where you want the GitLab CLI commands to be executed. For example, if you're working with a GitLab repository cloned to `/home/user/projects/my-repo`, you would pass that path as the `working_directory` parameter.

## Development

### Running tests

```bash
cd servers/gitlab_glab
uv run pytest
```

### Running tests with coverage

```bash
cd servers/gitlab_glab
uv run pytest --cov=mcp_server_gitlab_glab
```

## License

This project is licensed under the MIT License - see the LICENSE file for details.