@zereight/mcp-gitlab 2.1.70 → 2.2.1

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.
Files changed (158) hide show
  1. package/README.ko.md +9 -5
  2. package/README.md +15 -11
  3. package/README.pt-BR.md +9 -5
  4. package/README.zh-CN.md +9 -5
  5. package/build/config.js +12 -4
  6. package/build/deprecated-env.js +63 -0
  7. package/build/index.js +430 -200
  8. package/build/oauth-device-flow.js +80 -0
  9. package/build/oauth.js +135 -18
  10. package/build/schemas.js +1 -1
  11. package/build/server/remote-http-stack.js +39 -0
  12. package/build/tools/discover-activation.js +38 -0
  13. package/build/tools/exposed-tools.js +35 -0
  14. package/build/tools/listed-tools.js +76 -0
  15. package/build/tools/registry.js +232 -63
  16. package/build/tools/tool-descriptions.js +3 -3
  17. package/build/tools/tool-profile.js +66 -0
  18. package/build/utils/compact-tool-allowlist.js +14 -0
  19. package/build/utils/logger.js +34 -12
  20. package/package.json +3 -3
  21. package/build/scripts/check-skill-sync.js +0 -143
  22. package/build/scripts/generate-server-card.js +0 -39
  23. package/build/scripts/generate-tool-coverage.js +0 -58
  24. package/build/scripts/generate-tool-docs.js +0 -430
  25. package/build/scripts/tool-coverage/coverage.js +0 -341
  26. package/build/scripts/tool-coverage/coverage.test.js +0 -142
  27. package/build/test/callback-proxy-tests.js +0 -321
  28. package/build/test/cli/args.test.js +0 -175
  29. package/build/test/cli/boolean-flags.test.js +0 -106
  30. package/build/test/cli/compact-mcp-result.test.js +0 -93
  31. package/build/test/cli/credentials.test.js +0 -153
  32. package/build/test/cli/exposure.test.js +0 -104
  33. package/build/test/cli/load-config-snapshot.js +0 -5
  34. package/build/test/cli/output.test.js +0 -74
  35. package/build/test/cli/replay-command.test.js +0 -37
  36. package/build/test/cli/router.test.js +0 -183
  37. package/build/test/client-pool-test.js +0 -109
  38. package/build/test/clients/client.js +0 -34
  39. package/build/test/clients/custom-header-client.js +0 -122
  40. package/build/test/clients/sse-client.js +0 -95
  41. package/build/test/clients/stdio-client.js +0 -113
  42. package/build/test/clients/streamable-http-client.js +0 -95
  43. package/build/test/config-allowed-groups.test.js +0 -97
  44. package/build/test/create-commit-actions.test.js +0 -102
  45. package/build/test/draft-note-schema.test.js +0 -44
  46. package/build/test/dynamic-api-url-allowlist.test.js +0 -121
  47. package/build/test/dynamic-api-url-test.js +0 -304
  48. package/build/test/dynamic-routing-tests.js +0 -495
  49. package/build/test/get-merge-request-discussion-schema.test.js +0 -30
  50. package/build/test/gitlab-api-tool-errors.test.js +0 -112
  51. package/build/test/gitlab-artifact-entry-schema.test.js +0 -44
  52. package/build/test/gitlab-diff-schema.test.js +0 -36
  53. package/build/test/gitlab-pipeline-schedule-schema.test.js +0 -32
  54. package/build/test/gitlab-tag-schema.test.js +0 -51
  55. package/build/test/group-milestone-schema.test.js +0 -45
  56. package/build/test/mcp-oauth-tests.js +0 -905
  57. package/build/test/mcp-server-name.test.js +0 -87
  58. package/build/test/merge-merge-request-schema.test.js +0 -75
  59. package/build/test/multi-server-test.js +0 -190
  60. package/build/test/no-proxy-integration-test.js +0 -183
  61. package/build/test/no-proxy-test.js +0 -149
  62. package/build/test/nullable-gitlab-response-fields.test.js +0 -37
  63. package/build/test/nullish-tool-arguments-schema.test.js +0 -118
  64. package/build/test/oauth-device-flow-tests.js +0 -450
  65. package/build/test/oauth-download-url.test.js +0 -157
  66. package/build/test/oauth-startup.test.js +0 -204
  67. package/build/test/oauth-tests.js +0 -428
  68. package/build/test/path-segment-encoding.test.js +0 -98
  69. package/build/test/remote-auth-simple-test.js +0 -277
  70. package/build/test/remote-auth-tests.js +0 -314
  71. package/build/test/response-masking.test.js +0 -445
  72. package/build/test/schema-tests.js +0 -1679
  73. package/build/test/server/metrics.test.js +0 -47
  74. package/build/test/server/request-helpers.test.js +0 -48
  75. package/build/test/server/version.test.js +0 -13
  76. package/build/test/sse-auth-guard.test.js +0 -96
  77. package/build/test/sse-session-limits.test.js +0 -273
  78. package/build/test/stateless/callback-proxy.test.js +0 -510
  79. package/build/test/stateless/client-id.test.js +0 -177
  80. package/build/test/stateless/codec.test.js +0 -328
  81. package/build/test/stateless/config-ttl.test.js +0 -149
  82. package/build/test/stateless/consumed-proxy-code-cache.test.js +0 -150
  83. package/build/test/stateless/session-id-integration.test.js +0 -675
  84. package/build/test/stateless/session-id.test.js +0 -157
  85. package/build/test/streamable-http-concurrent-session.test.js +0 -171
  86. package/build/test/streamable-http-dns-rebinding.test.js +0 -185
  87. package/build/test/streamable-http-sse-stream.test.js +0 -350
  88. package/build/test/streamable-http-static-token-auth.test.js +0 -157
  89. package/build/test/streamable-http-unauthenticated-discovery.test.js +0 -207
  90. package/build/test/test-all-transport-server.js +0 -255
  91. package/build/test/test-auth-retry.js +0 -195
  92. package/build/test/test-blank-filters.js +0 -156
  93. package/build/test/test-ci-catalog.js +0 -177
  94. package/build/test/test-ci-lint.js +0 -191
  95. package/build/test/test-ci-variables.js +0 -309
  96. package/build/test/test-create-repository.js +0 -120
  97. package/build/test/test-dependency-proxy.js +0 -195
  98. package/build/test/test-deployment-tools.js +0 -463
  99. package/build/test/test-download-attachment.js +0 -190
  100. package/build/test/test-dynamic-project-scope.js +0 -524
  101. package/build/test/test-empty-toolsets.js +0 -535
  102. package/build/test/test-get-file-blame.js +0 -145
  103. package/build/test/test-get-file-contents.js +0 -128
  104. package/build/test/test-geteffectiveprojectid.js +0 -804
  105. package/build/test/test-health-check.js +0 -115
  106. package/build/test/test-issue-description-patch.js +0 -256
  107. package/build/test/test-job-artifacts.js +0 -275
  108. package/build/test/test-json-schema.js +0 -166
  109. package/build/test/test-list-group-members.js +0 -150
  110. package/build/test/test-list-group-merge-requests.js +0 -165
  111. package/build/test/test-list-issues.js +0 -170
  112. package/build/test/test-list-merge-requests.js +0 -201
  113. package/build/test/test-list-milestones.js +0 -145
  114. package/build/test/test-list-project-members.js +0 -132
  115. package/build/test/test-merge-request-approval-state-tools.js +0 -213
  116. package/build/test/test-merge-request-approvals.js +0 -187
  117. package/build/test/test-merge-request-pipelines.js +0 -106
  118. package/build/test/test-mr-diffs-filter.js +0 -132
  119. package/build/test/test-mr-file-diffs.js +0 -251
  120. package/build/test/test-oauth-proxy-rate-limit.js +0 -133
  121. package/build/test/test-orbit.js +0 -155
  122. package/build/test/test-permission-mode.js +0 -424
  123. package/build/test/test-pipeline-schedules.js +0 -484
  124. package/build/test/test-protected-branches.js +0 -174
  125. package/build/test/test-remote-downloads.js +0 -510
  126. package/build/test/test-search-code.js +0 -275
  127. package/build/test/test-snippets.js +0 -735
  128. package/build/test/test-tags.js +0 -220
  129. package/build/test/test-todos.js +0 -195
  130. package/build/test/test-token-optimizations.js +0 -691
  131. package/build/test/test-toolset-filtering.js +0 -499
  132. package/build/test/test-update-issue-slim.js +0 -141
  133. package/build/test/test-update-project.js +0 -112
  134. package/build/test/test-upload-markdown.js +0 -206
  135. package/build/test/test-vulnerabilities.js +0 -399
  136. package/build/test/test-webhooks.js +0 -218
  137. package/build/test/tool-description-quality.test.js +0 -50
  138. package/build/test/utils/bulk-publish-options.test.js +0 -32
  139. package/build/test/utils/download-token.test.js +0 -35
  140. package/build/test/utils/forwarded-public-base-url.test.js +0 -46
  141. package/build/test/utils/gitlab-instance-version.test.js +0 -32
  142. package/build/test/utils/gitlab-version-gate.test.js +0 -74
  143. package/build/test/utils/graphql-query.test.js +0 -168
  144. package/build/test/utils/helpers-path.test.js +0 -104
  145. package/build/test/utils/jmespath-tool-result.test.js +0 -91
  146. package/build/test/utils/masking.test.js +0 -384
  147. package/build/test/utils/merge-request-position.test.js +0 -57
  148. package/build/test/utils/mock-gitlab-server.js +0 -614
  149. package/build/test/utils/proxy-client-ip.test.js +0 -28
  150. package/build/test/utils/redact-sensitive.test.js +0 -32
  151. package/build/test/utils/safe-redirect-fetch.test.js +0 -463
  152. package/build/test/utils/server-launcher.js +0 -231
  153. package/build/test/utils/tool-args.test.js +0 -210
  154. package/build/test/utils/url.test.js +0 -141
  155. package/build/test/utils/version-check.test.js +0 -239
  156. package/build/test/utils/wiki-title.test.js +0 -21
  157. package/build/test-note.js +0 -54
  158. package/build/test-resolve-issue-note.js +0 -127
@@ -1,524 +0,0 @@
1
- import { describe, test, before, after } from "node:test";
2
- import assert from "node:assert";
3
- import { cleanupServers, findAvailablePort, HOST, launchServer, TransportMode, } from "./utils/server-launcher.js";
4
- import { MockGitLabServer, findMockServerPort } from "./utils/mock-gitlab-server.js";
5
- import { CustomHeaderClient } from "./clients/custom-header-client.js";
6
- const MOCK_TOKEN = "glpat-dynamic-scope-token";
7
- const SCOPE_HEADER = "x-gitlab-allowed-project-ids";
8
- function parseToolJson(result) {
9
- assert.ok(result.isError !== true, "tool should succeed");
10
- const first = result.content[0];
11
- assert.ok(first !== undefined && first.type === "text");
12
- return JSON.parse(first.text);
13
- }
14
- function snippetTitleFrom(value) {
15
- if (value === null || typeof value !== "object" || !("title" in value)) {
16
- assert.fail("expected a snippet object with title");
17
- }
18
- const title = value.title;
19
- if (typeof title !== "string") {
20
- assert.fail("expected snippet title to be a string");
21
- }
22
- return title;
23
- }
24
- async function connectClient(mcpUrl, scope) {
25
- const headers = { authorization: `Bearer ${MOCK_TOKEN}` };
26
- if (scope !== undefined) {
27
- headers[SCOPE_HEADER] = scope;
28
- }
29
- const client = new CustomHeaderClient(headers);
30
- await client.connect(mcpUrl);
31
- return client;
32
- }
33
- async function getProjectId(client, projectId) {
34
- const result = await client.callTool("get_project", { project_id: projectId });
35
- assert.ok(result.content, "Should have content");
36
- const content = result.content[0];
37
- assert.ok("text" in content, "Content should have text");
38
- const project = JSON.parse(content.text);
39
- return project.id.toString();
40
- }
41
- describe("Dynamic project scope", { concurrency: 1 }, () => {
42
- describe("with GITLAB_ALLOWED_PROJECT_IDS set", () => {
43
- let mockGitLab;
44
- const servers = [];
45
- let mcpUrl;
46
- before(async () => {
47
- const mockPort = await findMockServerPort();
48
- mockGitLab = new MockGitLabServer({ port: mockPort, validTokens: [MOCK_TOKEN] });
49
- mockGitLab.addMockHandler("get", "/projects", (req, res) => {
50
- if (req.query.search === "no-totals") {
51
- const requestedPage = Number(req.query.page ?? "1");
52
- if (requestedPage < 3) {
53
- res.set("x-next-page", String(requestedPage + 1));
54
- }
55
- }
56
- else {
57
- res.set("x-total", "3");
58
- res.set("x-total-pages", "2");
59
- }
60
- res.json(["1", "2", "3"].map(id => ({
61
- id: Number(id),
62
- name: `Project ${id}`,
63
- path: `project-${id}`,
64
- path_with_namespace: `group/project-${id}`,
65
- description: null,
66
- visibility: "private",
67
- namespace: { id: 1, name: "Group", path: "group", kind: "group", full_path: "group" },
68
- })));
69
- });
70
- await mockGitLab.start();
71
- const mcpPort = await findAvailablePort(3200);
72
- const server = await launchServer({
73
- mode: TransportMode.STREAMABLE_HTTP,
74
- port: mcpPort,
75
- timeout: 5000,
76
- env: {
77
- STREAMABLE_HTTP: "true",
78
- REMOTE_AUTHORIZATION: "true",
79
- ENABLE_DYNAMIC_PROJECT_SCOPE: "true",
80
- ENABLE_STRICT_PROJECT_SCOPE: "true",
81
- GITLAB_API_URL: `${mockGitLab.getUrl()}/api/v4`,
82
- GITLAB_TOOLS: "execute_graphql",
83
- GITLAB_ALLOWED_PROJECT_IDS: "1,2",
84
- },
85
- });
86
- servers.push(server);
87
- mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
88
- });
89
- after(async () => {
90
- cleanupServers(servers);
91
- await mockGitLab?.stop();
92
- });
93
- test("header narrows the allowlist to the requested projects", async () => {
94
- const client = await connectClient(mcpUrl, "1");
95
- try {
96
- assert.strictEqual(await getProjectId(client, ""), "1");
97
- await assert.rejects(() => client.callTool("get_project", { project_id: "2" }), /Access denied/);
98
- }
99
- finally {
100
- await client.disconnect();
101
- }
102
- });
103
- test("project outside GITLAB_ALLOWED_PROJECT_IDS rejects the session", async () => {
104
- const client = new CustomHeaderClient({
105
- authorization: `Bearer ${MOCK_TOKEN}`,
106
- [SCOPE_HEADER]: "1,3",
107
- });
108
- let connected = false;
109
- try {
110
- await client.connect(mcpUrl);
111
- connected = true;
112
- await client.callTool("get_project", { project_id: "1" });
113
- }
114
- catch {
115
- // Expected: the session is rejected before any tool call is possible.
116
- }
117
- finally {
118
- await client.disconnect();
119
- }
120
- assert.strictEqual(connected, false, "out-of-scope header must not initialize a session");
121
- });
122
- test("empty header rejects the session", async () => {
123
- const client = new CustomHeaderClient({
124
- authorization: `Bearer ${MOCK_TOKEN}`,
125
- [SCOPE_HEADER]: " ",
126
- });
127
- let connected = false;
128
- try {
129
- await client.connect(mcpUrl);
130
- connected = true;
131
- await client.callTool("get_project", { project_id: "1" });
132
- }
133
- catch {
134
- // Expected: the session is rejected before any tool call is possible.
135
- }
136
- finally {
137
- await client.disconnect();
138
- }
139
- assert.strictEqual(connected, false, "an empty header must not initialize a session");
140
- });
141
- test("scoped session refuses non-scopable tools", async () => {
142
- const client = await connectClient(mcpUrl, "1");
143
- try {
144
- await assert.rejects(() => client.callTool("execute_graphql", { query: "query { currentUser { id } }" }), /not allowed while a project allowlist is in effect/);
145
- }
146
- finally {
147
- await client.disconnect();
148
- }
149
- });
150
- test("session without the header keeps the full env allowlist", async () => {
151
- const client = await connectClient(mcpUrl);
152
- try {
153
- assert.strictEqual(await getProjectId(client, "2"), "2");
154
- }
155
- finally {
156
- await client.disconnect();
157
- }
158
- });
159
- test("list_issues without project_id stays within the session scope", async () => {
160
- const client = await connectClient(mcpUrl, "1");
161
- try {
162
- const result = await client.callTool("list_issues", {});
163
- assert.ok(result.content, "Should have content");
164
- const content = result.content[0];
165
- assert.ok("text" in content, "Content should have text");
166
- const issues = JSON.parse(content.text);
167
- assert.ok(issues.every(issue => issue.web_url.includes("/project/1/")), "Should only return issues of the scoped project");
168
- }
169
- finally {
170
- await client.disconnect();
171
- }
172
- });
173
- test("list_todos is refused under strict scope", async () => {
174
- const client = await connectClient(mcpUrl, "1");
175
- try {
176
- await assert.rejects(() => client.callTool("list_todos", {}), /not allowed while a project allowlist is in effect/);
177
- }
178
- finally {
179
- await client.disconnect();
180
- }
181
- });
182
- test("search_repositories keeps the unfiltered pagination totals", async () => {
183
- const client = await connectClient(mcpUrl, "1");
184
- try {
185
- const result = await client.callTool("search_repositories", { search: "project" });
186
- assert.ok(result.content, "Should have content");
187
- const content = result.content[0];
188
- assert.ok("text" in content, "Content should have text");
189
- const search = JSON.parse(content.text);
190
- assert.deepStrictEqual(search.items.map(item => item.id), ["1"]);
191
- assert.strictEqual(search.count, 3, "count must reflect the unfiltered GitLab total");
192
- assert.strictEqual(search.total_pages, 2, "total_pages must cover the unfiltered pages");
193
- }
194
- finally {
195
- await client.disconnect();
196
- }
197
- });
198
- test("search_repositories follows next_page across all pages when totals are missing", async () => {
199
- const client = await connectClient(mcpUrl, "1");
200
- try {
201
- let page = 1;
202
- const visited = [];
203
- while (page !== undefined) {
204
- const result = await client.callTool("search_repositories", {
205
- search: "no-totals",
206
- page,
207
- });
208
- assert.ok(result.content, "Should have content");
209
- const content = result.content[0];
210
- assert.ok("text" in content, "Content should have text");
211
- const search = JSON.parse(content.text);
212
- assert.strictEqual(search.total_pages, undefined, "total_pages must stay unset");
213
- assert.deepStrictEqual(search.items.map(item => item.id), ["1"]);
214
- visited.push(search.current_page);
215
- page = search.next_page;
216
- }
217
- assert.deepStrictEqual(visited, [1, 2, 3], "next_page must make every page reachable");
218
- }
219
- finally {
220
- await client.disconnect();
221
- }
222
- });
223
- test("list_projects is filtered to the session scope", async () => {
224
- const client = await connectClient(mcpUrl, "1");
225
- try {
226
- const result = await client.callTool("list_projects", {});
227
- assert.ok(result.content, "Should have content");
228
- const content = result.content[0];
229
- assert.ok("text" in content, "Content should have text");
230
- const projects = JSON.parse(content.text);
231
- assert.deepStrictEqual(projects.map(project => project.id), ["1"]);
232
- }
233
- finally {
234
- await client.disconnect();
235
- }
236
- });
237
- test("list_projects is filtered to the env allowlist without the header", async () => {
238
- const client = await connectClient(mcpUrl);
239
- try {
240
- const result = await client.callTool("list_projects", {});
241
- assert.ok(result.content, "Should have content");
242
- const content = result.content[0];
243
- assert.ok("text" in content, "Content should have text");
244
- const projects = JSON.parse(content.text);
245
- assert.deepStrictEqual(projects.map(project => project.id), ["1", "2"]);
246
- }
247
- finally {
248
- await client.disconnect();
249
- }
250
- });
251
- });
252
- describe("without GITLAB_ALLOWED_PROJECT_IDS", () => {
253
- let mockGitLab;
254
- const servers = [];
255
- let mcpUrl;
256
- before(async () => {
257
- const mockPort = await findMockServerPort();
258
- mockGitLab = new MockGitLabServer({ port: mockPort, validTokens: [MOCK_TOKEN] });
259
- mockGitLab.addRootHandler("post", "/api/graphql", (req, res) => {
260
- const { query } = req.body;
261
- if (query.includes("vulnerability(")) {
262
- res.json({
263
- data: {
264
- vulnerability: {
265
- id: "gid://gitlab/Vulnerability/42",
266
- title: "Mock vulnerability",
267
- project: {
268
- id: "gid://gitlab/Project/8",
269
- name: "Other project",
270
- fullPath: "other/project",
271
- },
272
- },
273
- },
274
- });
275
- return;
276
- }
277
- res.status(500).json({ message: `Unexpected GraphQL query: ${query}` });
278
- });
279
- await mockGitLab.start();
280
- const mcpPort = await findAvailablePort(3200);
281
- const server = await launchServer({
282
- mode: TransportMode.STREAMABLE_HTTP,
283
- port: mcpPort,
284
- timeout: 5000,
285
- env: {
286
- STREAMABLE_HTTP: "true",
287
- REMOTE_AUTHORIZATION: "true",
288
- ENABLE_DYNAMIC_PROJECT_SCOPE: "true",
289
- GITLAB_API_URL: `${mockGitLab.getUrl()}/api/v4`,
290
- GITLAB_TOOLS: "execute_graphql,get_vulnerability",
291
- },
292
- });
293
- servers.push(server);
294
- mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
295
- });
296
- after(async () => {
297
- cleanupServers(servers);
298
- await mockGitLab?.stop();
299
- });
300
- test("header bounds an otherwise unrestricted server", async () => {
301
- const client = await connectClient(mcpUrl, "7");
302
- try {
303
- assert.strictEqual(await getProjectId(client, ""), "7");
304
- await assert.rejects(() => client.callTool("get_project", { project_id: "8" }), /Access denied/);
305
- await assert.rejects(() => client.callTool("execute_graphql", { query: "query { currentUser { id } }" }), /not allowed while a project allowlist is in effect/);
306
- }
307
- finally {
308
- await client.disconnect();
309
- }
310
- });
311
- test("session without the header stays unrestricted", async () => {
312
- const client = await connectClient(mcpUrl);
313
- try {
314
- assert.strictEqual(await getProjectId(client, "8"), "8");
315
- }
316
- finally {
317
- await client.disconnect();
318
- }
319
- });
320
- test("scoped session cannot reach vulnerabilities of other projects", async () => {
321
- const client = await connectClient(mcpUrl, "7");
322
- try {
323
- await assert.rejects(() => client.callTool("get_vulnerability", { vulnerability_id: "42" }), /Access denied/);
324
- }
325
- finally {
326
- await client.disconnect();
327
- }
328
- });
329
- test("unscoped session can reach vulnerabilities", async () => {
330
- const client = await connectClient(mcpUrl);
331
- try {
332
- const result = await client.callTool("get_vulnerability", { vulnerability_id: "42" });
333
- assert.ok(result.content, "Should have content");
334
- const content = result.content[0];
335
- assert.ok("text" in content, "Content should have text");
336
- const vulnerability = JSON.parse(content.text);
337
- assert.strictEqual(vulnerability.project.fullPath, "other/project");
338
- }
339
- finally {
340
- await client.disconnect();
341
- }
342
- });
343
- });
344
- describe("with session-scoped snippet tools", () => {
345
- let mockGitLab;
346
- const servers = [];
347
- let mcpUrl;
348
- const snippetHits = [];
349
- before(async () => {
350
- const mockPort = await findMockServerPort();
351
- mockGitLab = new MockGitLabServer({ port: mockPort, validTokens: [MOCK_TOKEN] });
352
- mockGitLab.addMockHandler("get", "/snippets", (_req, res) => {
353
- snippetHits.push("personal:list");
354
- res.json([{ id: 99, title: "personal snippet of the token owner" }]);
355
- });
356
- mockGitLab.addMockHandler("get", "/projects/5/snippets", (_req, res) => {
357
- snippetHits.push("project:list");
358
- res.json([{ id: 42, title: "project snippet" }]);
359
- });
360
- mockGitLab.addMockHandler("get", "/projects/5/snippets/42", (_req, res) => {
361
- snippetHits.push("project:get");
362
- res.json({ id: 42, title: "project snippet" });
363
- });
364
- mockGitLab.addMockHandler("get", "/snippets/42", (_req, res) => {
365
- snippetHits.push("personal:get");
366
- res.json({ id: 42, title: "personal snippet of the token owner" });
367
- });
368
- mockGitLab.addMockHandler("delete", "/snippets/99", (_req, res) => {
369
- snippetHits.push("personal:delete");
370
- res.status(204).end();
371
- });
372
- mockGitLab.addMockHandler("delete", "/projects/5/snippets/42", (_req, res) => {
373
- snippetHits.push("project:delete");
374
- res.status(204).end();
375
- });
376
- mockGitLab.addMockHandler("post", "/snippets", (_req, res) => {
377
- snippetHits.push("personal:create");
378
- res.json({ id: 99, title: "personal snippet of the token owner" });
379
- });
380
- mockGitLab.addMockHandler("post", "/projects/5/snippets", (_req, res) => {
381
- snippetHits.push("project:create");
382
- res.json({ id: 42, title: "project snippet" });
383
- });
384
- mockGitLab.addMockHandler("put", "/snippets/42", (_req, res) => {
385
- snippetHits.push("personal:update");
386
- res.json({ id: 42, title: "updated personal snippet" });
387
- });
388
- mockGitLab.addMockHandler("put", "/projects/5/snippets/42", (_req, res) => {
389
- snippetHits.push("project:update");
390
- res.json({ id: 42, title: "updated project snippet" });
391
- });
392
- await mockGitLab.start();
393
- const mcpPort = await findAvailablePort(3200);
394
- const server = await launchServer({
395
- mode: TransportMode.STREAMABLE_HTTP,
396
- port: mcpPort,
397
- timeout: 5000,
398
- env: {
399
- STREAMABLE_HTTP: "true",
400
- REMOTE_AUTHORIZATION: "true",
401
- ENABLE_DYNAMIC_PROJECT_SCOPE: "true",
402
- GITLAB_TOOLSETS: "snippets",
403
- GITLAB_API_URL: `${mockGitLab.getUrl()}/api/v4`,
404
- },
405
- });
406
- servers.push(server);
407
- mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
408
- });
409
- after(async () => {
410
- cleanupServers(servers);
411
- await mockGitLab?.stop();
412
- });
413
- test("list_snippets without project_id stays within the session scope", async () => {
414
- snippetHits.length = 0;
415
- const client = await connectClient(mcpUrl, "5");
416
- try {
417
- const result = await client.callTool("list_snippets", {});
418
- assert.ok(result.content, "Should have content");
419
- const content = result.content[0];
420
- assert.ok("text" in content, "Content should have text");
421
- const snippets = JSON.parse(content.text);
422
- assert.strictEqual(snippets[0].title, "project snippet");
423
- }
424
- finally {
425
- await client.disconnect();
426
- }
427
- assert.deepStrictEqual(snippetHits, ["project:list"], "must route to the scoped project, not the personal /snippets endpoint");
428
- });
429
- test("delete_snippet without project_id stays within the session scope", async () => {
430
- snippetHits.length = 0;
431
- const client = await connectClient(mcpUrl, "5");
432
- try {
433
- await client.callTool("delete_snippet", { snippet_id: 42 });
434
- }
435
- finally {
436
- await client.disconnect();
437
- }
438
- assert.deepStrictEqual(snippetHits, ["project:delete"], "must route to the scoped project, not the personal /snippets endpoint");
439
- });
440
- test("get_snippet without project_id stays within the session scope", async () => {
441
- snippetHits.length = 0;
442
- const client = await connectClient(mcpUrl, "5");
443
- try {
444
- const result = await client.callTool("get_snippet", { snippet_id: 42 });
445
- assert.ok(result.content, "Should have content");
446
- const content = result.content[0];
447
- assert.ok("text" in content, "Content should have text");
448
- const snippet = JSON.parse(content.text);
449
- assert.strictEqual(snippet.title, "project snippet");
450
- }
451
- finally {
452
- await client.disconnect();
453
- }
454
- assert.deepStrictEqual(snippetHits, ["project:get"], "must route to the scoped project, not the personal /snippets endpoint");
455
- });
456
- test("create_snippet without project_id stays within the session scope", async () => {
457
- snippetHits.length = 0;
458
- const client = await connectClient(mcpUrl, "5");
459
- try {
460
- const result = await client.callTool("create_snippet", {
461
- title: "scoped create",
462
- file_name: "note.md",
463
- content: "hi",
464
- });
465
- assert.strictEqual(snippetTitleFrom(parseToolJson(result)), "project snippet");
466
- }
467
- finally {
468
- await client.disconnect();
469
- }
470
- assert.deepStrictEqual(snippetHits, ["project:create"], "must route to the scoped project, not the personal /snippets endpoint");
471
- });
472
- test("update_snippet without project_id stays within the session scope", async () => {
473
- snippetHits.length = 0;
474
- const client = await connectClient(mcpUrl, "5");
475
- try {
476
- const result = await client.callTool("update_snippet", {
477
- snippet_id: 42,
478
- title: "scoped update",
479
- });
480
- assert.strictEqual(snippetTitleFrom(parseToolJson(result)), "updated project snippet");
481
- }
482
- finally {
483
- await client.disconnect();
484
- }
485
- assert.deepStrictEqual(snippetHits, ["project:update"], "must route to the scoped project, not the personal /snippets endpoint");
486
- });
487
- });
488
- describe("with ENABLE_DYNAMIC_PROJECT_SCOPE disabled", () => {
489
- let mockGitLab;
490
- const servers = [];
491
- let mcpUrl;
492
- before(async () => {
493
- const mockPort = await findMockServerPort();
494
- mockGitLab = new MockGitLabServer({ port: mockPort, validTokens: [MOCK_TOKEN] });
495
- await mockGitLab.start();
496
- const mcpPort = await findAvailablePort(3200);
497
- const server = await launchServer({
498
- mode: TransportMode.STREAMABLE_HTTP,
499
- port: mcpPort,
500
- timeout: 5000,
501
- env: {
502
- STREAMABLE_HTTP: "true",
503
- REMOTE_AUTHORIZATION: "true",
504
- GITLAB_API_URL: `${mockGitLab.getUrl()}/api/v4`,
505
- },
506
- });
507
- servers.push(server);
508
- mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
509
- });
510
- after(async () => {
511
- cleanupServers(servers);
512
- await mockGitLab?.stop();
513
- });
514
- test("header is ignored when the feature flag is off", async () => {
515
- const client = await connectClient(mcpUrl, "1");
516
- try {
517
- assert.strictEqual(await getProjectId(client, "2"), "2");
518
- }
519
- finally {
520
- await client.disconnect();
521
- }
522
- });
523
- });
524
- });