@zereight/mcp-gitlab 2.0.23 → 2.0.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,187 @@
1
+ #!/usr/bin/env npx ts-node
2
+ /**
3
+ * Test script for merge request approval tools
4
+ *
5
+ * Usage:
6
+ * GITLAB_PERSONAL_ACCESS_TOKEN=<token> GITLAB_PROJECT_ID=<project> npx ts-node test/test-merge-request-approvals.ts
7
+ *
8
+ * Optional: Set MERGE_REQUEST_IID to test a specific merge request
9
+ */
10
+ import { spawn } from "child_process";
11
+ import path from "path";
12
+ import { fileURLToPath } from "url";
13
+ const __filename = fileURLToPath(import.meta.url);
14
+ const __dirname = path.dirname(__filename);
15
+ const GITLAB_TOKEN = process.env.GITLAB_PERSONAL_ACCESS_TOKEN || process.env.GITLAB_TOKEN;
16
+ const GITLAB_PROJECT_ID = process.env.GITLAB_PROJECT_ID;
17
+ const GITLAB_API_URL = process.env.GITLAB_API_URL || "https://gitlab.com/api/v4";
18
+ const MERGE_REQUEST_IID = process.env.MERGE_REQUEST_IID;
19
+ async function sendMcpRequest(serverProcess, method, params) {
20
+ return new Promise((resolve, reject) => {
21
+ const request = {
22
+ jsonrpc: "2.0",
23
+ id: Date.now(),
24
+ method,
25
+ params,
26
+ };
27
+ let responseData = "";
28
+ const onData = (data) => {
29
+ responseData += data.toString();
30
+ const lines = responseData.split("\n");
31
+ for (const line of lines) {
32
+ if (line.trim()) {
33
+ try {
34
+ const parsed = JSON.parse(line);
35
+ serverProcess.stdout?.off("data", onData);
36
+ resolve(parsed);
37
+ return;
38
+ }
39
+ catch {
40
+ // Continue accumulating data
41
+ }
42
+ }
43
+ }
44
+ };
45
+ serverProcess.stdout?.on("data", onData);
46
+ serverProcess.stdin?.write(JSON.stringify(request) + "\n");
47
+ setTimeout(() => {
48
+ serverProcess.stdout?.off("data", onData);
49
+ reject(new Error("Request timeout"));
50
+ }, 30000);
51
+ });
52
+ }
53
+ async function runTests() {
54
+ console.log("=== Merge Request Approval Tools Test ===\n");
55
+ if (!GITLAB_TOKEN) {
56
+ console.error("Error: GITLAB_PERSONAL_ACCESS_TOKEN or GITLAB_TOKEN environment variable is required");
57
+ process.exit(1);
58
+ }
59
+ if (!GITLAB_PROJECT_ID) {
60
+ console.error("Error: GITLAB_PROJECT_ID environment variable is required");
61
+ process.exit(1);
62
+ }
63
+ console.log(`GitLab API URL: ${GITLAB_API_URL}`);
64
+ console.log(`Project ID: ${GITLAB_PROJECT_ID}`);
65
+ console.log(`Merge Request IID: ${MERGE_REQUEST_IID || "(will find one)"}\n`);
66
+ // Start the MCP server
67
+ const serverPath = path.join(__dirname, "..", "build", "index.js");
68
+ const serverProcess = spawn("node", [serverPath], {
69
+ env: {
70
+ ...process.env,
71
+ GITLAB_PERSONAL_ACCESS_TOKEN: GITLAB_TOKEN,
72
+ GITLAB_API_URL,
73
+ },
74
+ stdio: ["pipe", "pipe", "pipe"],
75
+ });
76
+ serverProcess.stderr?.on("data", data => {
77
+ const msg = data.toString();
78
+ if (!msg.includes("GitLab MCP Server running")) {
79
+ console.error("Server stderr:", msg);
80
+ }
81
+ });
82
+ // Wait for server to start
83
+ await new Promise(resolve => setTimeout(resolve, 2000));
84
+ try {
85
+ // Initialize the MCP connection
86
+ console.log("1. Initializing MCP connection...");
87
+ await sendMcpRequest(serverProcess, "initialize", {
88
+ protocolVersion: "2024-11-05",
89
+ capabilities: {},
90
+ clientInfo: { name: "test-client", version: "1.0.0" },
91
+ });
92
+ console.log(" ✓ Connected\n");
93
+ // Find a merge request to test with
94
+ let mrIid = MERGE_REQUEST_IID;
95
+ if (!mrIid) {
96
+ console.log("2. Finding an open merge request...");
97
+ const listResponse = await sendMcpRequest(serverProcess, "tools/call", {
98
+ name: "list_merge_requests",
99
+ arguments: {
100
+ project_id: GITLAB_PROJECT_ID,
101
+ state: "opened",
102
+ per_page: 1,
103
+ },
104
+ });
105
+ if (listResponse.error) {
106
+ console.error(" ✗ Error:", listResponse.error.message);
107
+ process.exit(1);
108
+ }
109
+ const mrs = JSON.parse(listResponse.result?.content?.[0]?.text || "[]");
110
+ if (mrs.length === 0) {
111
+ console.log(" ⚠ No open merge requests found. Create one to test approval tools.");
112
+ process.exit(0);
113
+ }
114
+ mrIid = mrs[0].iid;
115
+ console.log(` ✓ Found MR !${mrIid}: ${mrs[0].title}\n`);
116
+ }
117
+ // Test get_merge_request_approval_state
118
+ console.log("3. Testing get_merge_request_approval_state...");
119
+ const approvalStateResponse = await sendMcpRequest(serverProcess, "tools/call", {
120
+ name: "get_merge_request_approval_state",
121
+ arguments: {
122
+ project_id: GITLAB_PROJECT_ID,
123
+ merge_request_iid: mrIid,
124
+ },
125
+ });
126
+ if (approvalStateResponse.error) {
127
+ console.error(" ✗ Error:", approvalStateResponse.error.message);
128
+ }
129
+ else {
130
+ const state = JSON.parse(approvalStateResponse.result?.content?.[0]?.text || "{}");
131
+ console.log(" ✓ Got approval state");
132
+ console.log(` Rules: ${state.rules?.length || 0}`);
133
+ // Show details for each rule
134
+ for (const rule of state.rules || []) {
135
+ const approvedBy = rule.approved_by || [];
136
+ const approvedNames = approvedBy.map((u) => u.name).join(", ") || "none";
137
+ const status = rule.approved ? "✓ APPROVED" : "○ pending";
138
+ console.log(`\n Rule: "${rule.name}"`);
139
+ console.log(` Status: ${status}`);
140
+ console.log(` Required: ${rule.approvals_required} approval(s)`);
141
+ console.log(` Approved by: ${approvedNames} (${approvedBy.length}/${rule.approvals_required})`);
142
+ }
143
+ console.log();
144
+ }
145
+ // Test approve_merge_request
146
+ console.log("4. Testing approve_merge_request...");
147
+ const approveResponse = await sendMcpRequest(serverProcess, "tools/call", {
148
+ name: "approve_merge_request",
149
+ arguments: {
150
+ project_id: GITLAB_PROJECT_ID,
151
+ merge_request_iid: mrIid,
152
+ },
153
+ });
154
+ if (approveResponse.error) {
155
+ console.log(" ✗ Approve error:", approveResponse.error);
156
+ }
157
+ else {
158
+ console.log(" ✓ Approved successfully");
159
+ }
160
+ // Wait 3 seconds before unapproving
161
+ console.log("\n Waiting 3 seconds...");
162
+ await new Promise(resolve => setTimeout(resolve, 3000));
163
+ // Test unapprove_merge_request
164
+ console.log("\n5. Testing unapprove_merge_request...");
165
+ const unapproveResponse = await sendMcpRequest(serverProcess, "tools/call", {
166
+ name: "unapprove_merge_request",
167
+ arguments: {
168
+ project_id: GITLAB_PROJECT_ID,
169
+ merge_request_iid: mrIid,
170
+ },
171
+ });
172
+ if (unapproveResponse.error) {
173
+ console.log(" ✗ Unapprove error:", unapproveResponse.error);
174
+ }
175
+ else {
176
+ console.log(" ✓ Unapproved successfully");
177
+ }
178
+ console.log("\n=== Tests Complete ===");
179
+ }
180
+ finally {
181
+ serverProcess.kill();
182
+ }
183
+ }
184
+ runTests().catch(error => {
185
+ console.error("Test failed:", error);
186
+ process.exit(1);
187
+ });
@@ -0,0 +1,132 @@
1
+ import { describe, test, before, after } from 'node:test';
2
+ import assert from 'node:assert';
3
+ import { spawn } from 'child_process';
4
+ import { MockGitLabServer, findMockServerPort } from './utils/mock-gitlab-server.js';
5
+ const MOCK_TOKEN = 'glpat-mock-token-12345';
6
+ const TEST_PROJECT_ID = '123';
7
+ const TEST_MR_IID = '1';
8
+ // Helper to call get_merge_request_diffs
9
+ async function callGetMergeRequestDiffs(args = {}, env) {
10
+ return new Promise((resolve, reject) => {
11
+ const proc = spawn('node', ['build/index.js'], {
12
+ stdio: ['pipe', 'pipe', 'pipe'],
13
+ env: {
14
+ ...process.env,
15
+ ...env,
16
+ GITLAB_READ_ONLY_MODE: 'true'
17
+ }
18
+ });
19
+ let output = '';
20
+ let errorOutput = '';
21
+ proc.stdout?.on('data', d => output += d);
22
+ proc.stderr?.on('data', d => errorOutput += d);
23
+ proc.on('close', (code) => {
24
+ if (code !== 0)
25
+ return reject(new Error(`Process exited with code ${code}: ${errorOutput}`));
26
+ // Find the JSON line in stdout
27
+ const line = output.split('\n').find(l => l.startsWith('{'));
28
+ if (!line)
29
+ return reject(new Error('No JSON output found'));
30
+ try {
31
+ const response = JSON.parse(line);
32
+ if (response.error) {
33
+ reject(response.error);
34
+ }
35
+ else {
36
+ // Parse the tool result content
37
+ const content = response.result?.content?.[0]?.text;
38
+ if (content) {
39
+ try {
40
+ resolve(JSON.parse(content));
41
+ }
42
+ catch (e) {
43
+ reject(new Error(`Failed to parse tool output JSON: ${content}`));
44
+ }
45
+ }
46
+ else {
47
+ resolve(response.result);
48
+ }
49
+ }
50
+ }
51
+ catch (e) {
52
+ reject(e);
53
+ }
54
+ });
55
+ proc.stdin?.end(JSON.stringify({
56
+ jsonrpc: "2.0", id: 1, method: "tools/call",
57
+ params: { name: "get_merge_request_diffs", arguments: args }
58
+ }) + '\n');
59
+ });
60
+ }
61
+ describe('get_merge_request_diffs with excluded_file_patterns', () => {
62
+ let mockGitLab;
63
+ let mockGitLabUrl;
64
+ before(async () => {
65
+ const mockPort = await findMockServerPort(9100);
66
+ mockGitLab = new MockGitLabServer({
67
+ port: mockPort,
68
+ validTokens: [MOCK_TOKEN]
69
+ });
70
+ await mockGitLab.start();
71
+ mockGitLabUrl = mockGitLab.getUrl();
72
+ });
73
+ after(async () => {
74
+ await mockGitLab.stop();
75
+ });
76
+ test('returns all diffs without filtering', async () => {
77
+ const diffs = await callGetMergeRequestDiffs({ project_id: TEST_PROJECT_ID, merge_request_iid: TEST_MR_IID }, {
78
+ GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
79
+ GITLAB_PERSONAL_ACCESS_TOKEN: MOCK_TOKEN
80
+ });
81
+ assert.ok(Array.isArray(diffs), 'Response should be an array');
82
+ assert.strictEqual(diffs.length, 4, 'Should return 4 diffs');
83
+ assert.strictEqual(diffs[0].new_path, 'src/index.ts');
84
+ assert.strictEqual(diffs[1].new_path, 'vendor/package/file.js');
85
+ assert.strictEqual(diffs[2].new_path, 'README.md');
86
+ assert.strictEqual(diffs[3].new_path, 'package-lock.json');
87
+ });
88
+ test('filters out vendor folder with ^vendor/ pattern', async () => {
89
+ const diffs = await callGetMergeRequestDiffs({
90
+ project_id: TEST_PROJECT_ID,
91
+ merge_request_iid: TEST_MR_IID,
92
+ excluded_file_patterns: ['^vendor/']
93
+ }, {
94
+ GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
95
+ GITLAB_PERSONAL_ACCESS_TOKEN: MOCK_TOKEN
96
+ });
97
+ assert.ok(Array.isArray(diffs), 'Response should be an array');
98
+ assert.strictEqual(diffs.length, 3, 'Should return 3 diffs (vendor filtered out)');
99
+ assert.strictEqual(diffs[0].new_path, 'src/index.ts');
100
+ assert.strictEqual(diffs[1].new_path, 'README.md');
101
+ assert.strictEqual(diffs[2].new_path, 'package-lock.json');
102
+ });
103
+ test('filters out package-lock.json with package-lock pattern', async () => {
104
+ const diffs = await callGetMergeRequestDiffs({
105
+ project_id: TEST_PROJECT_ID,
106
+ merge_request_iid: TEST_MR_IID,
107
+ excluded_file_patterns: ['package-lock\\.json']
108
+ }, {
109
+ GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
110
+ GITLAB_PERSONAL_ACCESS_TOKEN: MOCK_TOKEN
111
+ });
112
+ assert.ok(Array.isArray(diffs), 'Response should be an array');
113
+ assert.strictEqual(diffs.length, 3, 'Should return 3 diffs (package-lock.json filtered out)');
114
+ assert.strictEqual(diffs[0].new_path, 'src/index.ts');
115
+ assert.strictEqual(diffs[1].new_path, 'vendor/package/file.js');
116
+ assert.strictEqual(diffs[2].new_path, 'README.md');
117
+ });
118
+ test('filters multiple patterns at once', async () => {
119
+ const diffs = await callGetMergeRequestDiffs({
120
+ project_id: TEST_PROJECT_ID,
121
+ merge_request_iid: TEST_MR_IID,
122
+ excluded_file_patterns: ['^vendor/', 'package-lock\\.json']
123
+ }, {
124
+ GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
125
+ GITLAB_PERSONAL_ACCESS_TOKEN: MOCK_TOKEN
126
+ });
127
+ assert.ok(Array.isArray(diffs), 'Response should be an array');
128
+ assert.strictEqual(diffs.length, 2, 'Should return 2 diffs (vendor and package-lock filtered out)');
129
+ assert.strictEqual(diffs[0].new_path, 'src/index.ts');
130
+ assert.strictEqual(diffs[1].new_path, 'README.md');
131
+ });
132
+ });
@@ -327,6 +327,60 @@ export class MockGitLabServer {
327
327
  }
328
328
  ]);
329
329
  });
330
+ // GET /api/v4/projects/:projectId/merge_requests/:mr_iid/changes - Get MR diffs
331
+ this.app.get('/api/v4/projects/:projectId/merge_requests/:mr_iid/changes', (req, res) => {
332
+ const mrIid = parseInt(req.params.mr_iid);
333
+ res.json({
334
+ id: mrIid,
335
+ iid: mrIid,
336
+ project_id: parseInt(req.params.projectId),
337
+ title: `Test MR ${mrIid}`,
338
+ state: 'opened',
339
+ created_at: '2024-01-01T00:00:00Z',
340
+ changes: [
341
+ {
342
+ old_path: 'src/index.ts',
343
+ new_path: 'src/index.ts',
344
+ a_mode: '100644',
345
+ b_mode: '100644',
346
+ diff: '@@ -1,1 +1,2 @@\n-line 1\n+line 1 modified\n+new line 2\n',
347
+ new_file: false,
348
+ renamed_file: false,
349
+ deleted_file: false
350
+ },
351
+ {
352
+ old_path: 'vendor/package/file.js',
353
+ new_path: 'vendor/package/file.js',
354
+ a_mode: '100644',
355
+ b_mode: '100644',
356
+ diff: '@@ -1,1 +1,1 @@\n-vendor content old\n+vendor content new\n',
357
+ new_file: false,
358
+ renamed_file: false,
359
+ deleted_file: false
360
+ },
361
+ {
362
+ old_path: 'README.md',
363
+ new_path: 'README.md',
364
+ a_mode: '100644',
365
+ b_mode: '100644',
366
+ diff: '@@ -1,1 +1,1 @@\n-old readme\n+new readme\n',
367
+ new_file: false,
368
+ renamed_file: false,
369
+ deleted_file: false
370
+ },
371
+ {
372
+ old_path: 'package-lock.json',
373
+ new_path: 'package-lock.json',
374
+ a_mode: '100644',
375
+ b_mode: '100644',
376
+ diff: '{\n- "version": "1.0.0"\n+ "version": "1.0.1"\n}\n',
377
+ new_file: false,
378
+ renamed_file: false,
379
+ deleted_file: false
380
+ }
381
+ ]
382
+ });
383
+ });
330
384
  // Health check endpoint
331
385
  this.app.get('/health', (req, res) => {
332
386
  res.json({ status: 'ok', message: 'Mock GitLab API is running' });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zereight/mcp-gitlab",
3
- "version": "2.0.23",
3
+ "version": "2.0.25",
4
4
  "description": "MCP server for using the GitLab API",
5
5
  "license": "MIT",
6
6
  "author": "zereight",
@@ -29,18 +29,19 @@
29
29
  "changelog": "auto-changelog -p",
30
30
  "test": "npm run test:all",
31
31
  "test:all": "npm run build && npm run test:mock && npm run test:live",
32
- "test:mock": "npx tsx --test test/remote-auth-simple-test.ts && tsx test/oauth-tests.ts && tsx test/test-list-merge-requests.ts",
33
- "test:live": "node test/validate-api.js && tsx test/readonly-mcp-tests.ts",
32
+ "test:mock": "npx tsx --test test/remote-auth-simple-test.ts && tsx test/oauth-tests.ts && tsx test/test-list-merge-requests.ts && tsx test/test-list-project-members.ts",
33
+ "test:live": "node test/validate-api.js",
34
34
  "test:remote-auth": "npm run build && npx tsx --test test/remote-auth-simple-test.ts",
35
- "test:mcp:readonly": "tsx test/readonly-mcp-tests.ts",
36
35
  "test:oauth": "tsx test/oauth-tests.ts",
37
36
  "test:list-merge-requests": "npm run build && tsx test/test-list-merge-requests.ts",
37
+ "test:approvals": "npm run build && tsx test/test-merge-request-approvals.ts",
38
38
  "lint": "eslint . --ext .ts",
39
39
  "lint:fix": "eslint . --ext .ts --fix",
40
40
  "format": "prettier --write \"**/*.{js,ts,json,md}\"",
41
41
  "format:check": "prettier --check \"**/*.{js,ts,json,md}\""
42
42
  },
43
43
  "dependencies": {
44
+ "@modelcontextprotocol/sdk": "^1.24.2",
44
45
  "@types/node-fetch": "^2.6.12",
45
46
  "express": "^5.1.0",
46
47
  "fetch-cookie": "^3.1.0",
@@ -53,9 +54,9 @@
53
54
  "pino-pretty": "^13.0.0",
54
55
  "pkce-challenge": "^5.0.0",
55
56
  "socks-proxy-agent": "^8.0.5",
57
+ "tldts": "^6.1.86",
56
58
  "tough-cookie": "^5.1.2",
57
59
  "zod": "^3.24.2",
58
- "@modelcontextprotocol/sdk": "^1.24.2",
59
60
  "zod-to-json-schema": "3.24.5"
60
61
  },
61
62
  "devDependencies": {