-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
288 lines (266 loc) · 10 KB
/
index.js
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { Gitlab } from "@gitbeaker/rest";
import { exec } from "child_process";
import { promisify } from "util";
import { z } from "zod";
import _ from "lodash";
// Promisify exec for async usage
const execAsync = promisify(exec);
// Initialize GitLab API Client
const gitlabToken = process.env.MR_MCP_GITLAB_TOKEN;
if (!gitlabToken) {
console.error("Error: MR_MCP_GITLAB_TOKEN environment variable is not set.");
}
const api = new Gitlab({
token: gitlabToken,
});
// Helper function to format errors for MCP responses
const formatErrorResponse = (error) => ({
content: [{ type: "text", text: `Error: ${error.message} - ${error.cause?.description || "No additional details"}` }],
isError: true,
});
// Initialize the MCP server
const server = new McpServer({
name: "GitlabMrMCP",
version: "1.0.0",
});
// --- Merge Request Tools ---
server.tool(
"get_projects",
"Get a list of projects with id, name, description, web_url and other useful information.",
{
verbose: z.boolean().default(false).describe("By default a filtered version is returned, suitable for most cases. Only set true if more information is needed."),
},
async ({ verbose }) => {
try {
const projects = await api.Projects.all({ membership: true });
const filteredProjects = verbose ? projects : projects.map(project => ({
id: project.id,
description: project.description,
name: project.name,
path: project.path,
path_with_namespace: project.path_with_namespace,
web_url: project.web_url,
default_branch: project.default_branch,
}));
const projectsText = Array.isArray(filteredProjects) && filteredProjects.length > 0
? JSON.stringify(filteredProjects, null, 2)
: "No projects found.";
return {
content: [{ type: "text", text: projectsText }],
};
} catch (error) {
return formatErrorResponse(error);
}
}
);
server.tool(
"list_open_merge_requests",
"Lists all open merge requests in the project",
{
project_id: z.number().describe("The project ID of the merge request"),
verbose: z.boolean().default(false).describe("By default a filtered version is returned, suitable for most cases. Only set true if more information is needed."),
},
async ({ verbose, project_id }) => {
try {
const mergeRequests = await api.MergeRequests.all({ projectId: project_id, state: 'opened' });
const filteredMergeRequests = verbose ? mergeRequests : mergeRequests.map(mr => ({
iid: mr.iid,
project_id: mr.project_id,
title: mr.title,
description: mr.description,
state: mr.state,
web_url: mr.web_url,
}));
return {
content: [{ type: "text", text: JSON.stringify(filteredMergeRequests, null, 2) }],
};
} catch (error) {
return formatErrorResponse(error);
}
}
);
server.tool(
"get_merge_request_details",
"Get details about a specific merge request of a project like title, source-branch, target-branch, web_url, ...",
{
project_id: z.number().describe("The project ID of the merge request"),
merge_request_iid: z.number().describe("The internal ID of the merge request within the project"),
verbose: z.boolean().default(false).describe("By default a filtered version is returned, suitable for most cases. Only set true if more information is needed."),
},
async ({ project_id, merge_request_iid, verbose }) => {
try {
const mr = await api.MergeRequests.show(project_id, merge_request_iid);
const filteredMr = verbose ? mr : {
title: mr.title,
description: mr.description,
state: mr.state,
web_url: mr.web_url,
target_branch: mr.target_branch,
source_branch: mr.source_branch,
merge_status: mr.merge_status,
detailed_merge_status: mr.detailed_merge_status,
diff_refs: mr.diff_refs,
};
return {
content: [{ type: "text", text: JSON.stringify(filteredMr, null, 2) }],
};
} catch (error) {
return formatErrorResponse(error);
}
}
);
server.tool(
"get_merge_request_comments",
"Get general and file diff comments of a certain merge request",
{
project_id: z.number().describe("The project ID of the merge request"),
merge_request_iid: z.number().describe("The internal ID of the merge request within the project"),
verbose: z.boolean().default(false).describe("By default a filtered version is returned, suitable for most cases. Only set true if more information is needed."),
},
async ({ project_id, merge_request_iid, verbose }) => {
try {
const discussions = await api.MergeRequestDiscussions.all(project_id, merge_request_iid);
if (verbose) {
return {
content: [{ type: "text", text: JSON.stringify(discussions, null, 2) }],
};
}
const unresolvedNotes = discussions.flatMap(note => note.notes).filter(note => note.resolved === false);
const disscussionNotes = unresolvedNotes.filter(note => note.type === "DiscussionNote").map(note => ({
id: note.id,
noteable_id: note.noteable_id,
body: note.body,
author_name: note.author.name,
}));
const diffNotes = unresolvedNotes.filter(note => note.type === "DiffNote").map(note => ({
id: note.id,
noteable_id: note.noteable_id,
body: note.body,
author_name: note.author.name,
position: note.position,
}));
return {
content: [{ type: "text", text: JSON.stringify({
discussionNotes: Object.entries(_.groupBy(disscussionNotes, note => note.noteable_id)).map(([noteable_id, notes]) => ({ noteable_id, notes })),
diffNotes: Object.entries(_.groupBy(diffNotes, note => note.noteable_id)).map(([noteable_id, notes]) => ({ noteable_id, position: notes[0].position, notes: notes.map(note => _.omit(note, ['position', 'noteable_id'])) }))
}, null, 2) }],
};
} catch (error) {
return formatErrorResponse(error);
}
}
);
server.tool(
"add_merge_request_comment",
"Add a general comment to a merge request",
{
project_id: z.number().describe("The project ID of the merge request"),
merge_request_iid: z.number().describe("The internal ID of the merge request within the project"),
comment: z.string().describe("The comment text"),
},
async ({ project_id, merge_request_iid, comment }) => {
try {
const note = await api.MergeRequestDiscussions.create(project_id, merge_request_iid, comment);
return {
content: [{ type: "text", text: JSON.stringify(note, null, 2) }],
};
} catch (error) {
return formatErrorResponse(error);
}
}
);
server.tool(
"add_merge_request_diff_comment",
"Add a comment of a merge request at a specific line in a file diff",
{
project_id: z.number().describe("The project ID of the merge request"),
merge_request_iid: z.number().describe("The internal ID of the merge request within the project"),
comment: z.string().describe("The comment text"),
base_sha: z.string().describe("The SHA of the base commit"),
start_sha: z.string().describe("The SHA of the start commit"),
head_sha: z.string().describe("The SHA of the head commit"),
file_path: z.string().describe("The path to the file being commented on"),
line_number: z.number().describe("The line number in the new version of the file"),
},
async ({ project_id, merge_request_iid, comment, base_sha, start_sha, head_sha, file_path, line_number }) => {
try {
const discussion = await api.MergeRequestDiscussions.create(
project_id,
merge_request_iid,
comment,
{
position: {
base_sha: base_sha,
start_sha: start_sha,
head_sha: head_sha,
old_path: file_path,
new_path: file_path,
position_type: 'text',
new_line: line_number,
},
}
);
return {
content: [{ type: "text", text: JSON.stringify(discussion, null, 2) }],
};
} catch (error) {
return formatErrorResponse(error);
}
}
);
server.tool(
"get_merge_request_diff",
"Get the file diffs of a certain merge request",
{
project_id: z.number().describe("The project ID of the merge request"),
merge_request_iid: z.number().describe("The internal ID of the merge request within the project"),
},
async ({ project_id, merge_request_iid }) => {
try {
const diff = await api.MergeRequests.allDiffs(project_id, merge_request_iid);
const diffText = Array.isArray(diff) && diff.length > 0
? JSON.stringify(diff, null, 2)
: "No diff data available for this merge request.";
return {
content: [{ type: "text", text: diffText }],
};
} catch (error) {
return formatErrorResponse(error);
}
}
);
server.tool(
"get_issue_details",
"Get details of an issue within a certain project",
{
project_id: z.number().describe("The project ID of the issue"),
issue_iid: z.number().describe("The internal ID of the issue within the project"),
verbose: z.boolean().default(false).describe("By default a filtered version is returned, suitable for most cases. Only set true if more information is needed."),
},
async ({ project_id, issue_iid, verbose }) => {
try {
const issue = await api.Issues.show(issue_iid, { projectId: project_id });
const filteredIssue = verbose ? issue : {
title: issue.title,
description: issue.description,
};
return {
content: [{ type: "text", text: JSON.stringify(filteredIssue, null, 2) }],
};
} catch (error) {
return formatErrorResponse(error);
}
}
);
// Connect the server to a transport and start it
(async () => {
try {
const transport = new StdioServerTransport();
await server.connect(transport);
} catch (error) {
console.error("Failed to start server:", error.message);
process.exit(1);
}
})();