@zereight/mcp-gitlab 2.1.68 → 2.1.70
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.
- package/README.ko.md +11 -3
- package/README.md +237 -232
- package/README.pt-BR.md +8 -4
- package/README.zh-CN.md +7 -3
- package/build/cli/args.js +298 -0
- package/build/cli/compact-mcp-result.js +87 -0
- package/build/cli/curated.js +243 -0
- package/build/cli/exposure.js +72 -0
- package/build/cli/help.js +108 -0
- package/build/cli/output.js +65 -0
- package/build/cli/replay-command.js +53 -0
- package/build/cli/router.js +158 -0
- package/build/cli/tables.js +117 -0
- package/build/cli-boolean-flags.js +38 -0
- package/build/cli-command.js +14 -4
- package/build/config.js +30 -7
- package/build/index.js +176 -90
- package/build/oauth.js +93 -10
- package/build/scripts/generate-tool-docs.js +2 -1
- package/build/test/cli/args.test.js +175 -0
- package/build/test/cli/boolean-flags.test.js +106 -0
- package/build/test/cli/compact-mcp-result.test.js +93 -0
- package/build/test/cli/credentials.test.js +153 -0
- package/build/test/cli/exposure.test.js +104 -0
- package/build/test/cli/load-config-snapshot.js +5 -0
- package/build/test/cli/output.test.js +74 -0
- package/build/test/cli/replay-command.test.js +37 -0
- package/build/test/cli/router.test.js +183 -0
- package/build/test/gitlab-api-tool-errors.test.js +112 -0
- package/build/test/oauth-device-flow-tests.js +10 -0
- package/build/test/oauth-download-url.test.js +157 -0
- package/build/test/oauth-startup.test.js +204 -0
- package/build/test/test-dynamic-project-scope.js +2 -0
- package/build/test/test-geteffectiveprojectid.js +20 -26
- package/build/test/test-permission-mode.js +86 -0
- package/build/test/test-pipeline-schedules.js +3 -1
- package/build/test/utils/url.test.js +14 -1
- package/build/tools/denied-regex.js +38 -0
- package/build/utils/url.js +15 -0
- package/package.json +1 -1
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import * as fs from "node:fs";
|
|
4
|
+
import * as http from "node:http";
|
|
5
|
+
import * as os from "node:os";
|
|
6
|
+
import * as path from "node:path";
|
|
7
|
+
import * as url from "node:url";
|
|
8
|
+
import { describe, it } from "node:test";
|
|
9
|
+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
|
10
|
+
const CLI_ENTRY = path.resolve(__dirname, "../../build/index.js");
|
|
11
|
+
const CLI_TIMEOUT_MS = 30_000;
|
|
12
|
+
const MR_FIXTURE = {
|
|
13
|
+
id: 1,
|
|
14
|
+
iid: 1,
|
|
15
|
+
project_id: 7,
|
|
16
|
+
title: "Hello MR",
|
|
17
|
+
description: "d",
|
|
18
|
+
state: "opened",
|
|
19
|
+
web_url: "http://example.test/mr/1",
|
|
20
|
+
source_branch: "feature",
|
|
21
|
+
target_branch: "main",
|
|
22
|
+
author: { id: 1, username: "u", name: "U", state: "active", avatar_url: null, web_url: "http://example.test/u" },
|
|
23
|
+
created_at: "2026-01-01T00:00:00Z",
|
|
24
|
+
updated_at: "2026-01-01T00:00:00Z",
|
|
25
|
+
merged_at: null,
|
|
26
|
+
closed_at: null,
|
|
27
|
+
merge_commit_sha: null,
|
|
28
|
+
};
|
|
29
|
+
function buildCleanEnv(extra) {
|
|
30
|
+
const env = {};
|
|
31
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
32
|
+
if (value !== undefined && !key.startsWith("GITLAB_")) {
|
|
33
|
+
env[key] = value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return { ...env, ...extra };
|
|
37
|
+
}
|
|
38
|
+
function startFakeGitLabAsync(onRequest) {
|
|
39
|
+
const server = http.createServer((req, res) => {
|
|
40
|
+
onRequest(req.headers.authorization);
|
|
41
|
+
res.setHeader("content-type", "application/json");
|
|
42
|
+
res.end(JSON.stringify(MR_FIXTURE));
|
|
43
|
+
});
|
|
44
|
+
return new Promise(resolve => server.listen(0, "127.0.0.1", () => resolve(server)));
|
|
45
|
+
}
|
|
46
|
+
function getPort(server) {
|
|
47
|
+
const address = server.address();
|
|
48
|
+
if (address === null || typeof address === "string") {
|
|
49
|
+
throw new Error("fake GitLab server is not listening on a TCP port");
|
|
50
|
+
}
|
|
51
|
+
return address.port;
|
|
52
|
+
}
|
|
53
|
+
function runCliAsync(env) {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
const child = spawn(process.execPath, [CLI_ENTRY, "mr", "view", "--project-id", "g/p", "--mr-iid", "1"], {
|
|
56
|
+
env,
|
|
57
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
58
|
+
timeout: CLI_TIMEOUT_MS,
|
|
59
|
+
killSignal: "SIGKILL",
|
|
60
|
+
});
|
|
61
|
+
let stdout = "";
|
|
62
|
+
let stderr = "";
|
|
63
|
+
child.stdout.on("data", chunk => {
|
|
64
|
+
stdout += String(chunk);
|
|
65
|
+
});
|
|
66
|
+
child.stderr.on("data", chunk => {
|
|
67
|
+
stderr += String(chunk);
|
|
68
|
+
});
|
|
69
|
+
child.on("error", reject);
|
|
70
|
+
child.on("close", exitCode => resolve({ exitCode, stdout, stderr }));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function buildOAuthEnv(apiUrl, tmpDir) {
|
|
74
|
+
const tokenPath = path.join(tmpDir, "token.json");
|
|
75
|
+
fs.writeFileSync(tokenPath, JSON.stringify({
|
|
76
|
+
access_token: "oauth-cli-test",
|
|
77
|
+
refresh_token: "refresh",
|
|
78
|
+
expires_in: 7200,
|
|
79
|
+
created_at: Date.now(),
|
|
80
|
+
token_type: "Bearer",
|
|
81
|
+
}));
|
|
82
|
+
return {
|
|
83
|
+
GITLAB_API_URL: apiUrl,
|
|
84
|
+
GITLAB_USE_OAUTH: "true",
|
|
85
|
+
GITLAB_OAUTH_CLIENT_ID: "client-id",
|
|
86
|
+
GITLAB_OAUTH_TOKEN_PATH: tokenPath,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async function runMrViewAgainstFakeGitLabAsync(buildEnv) {
|
|
90
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "gitlab-cli-credentials-"));
|
|
91
|
+
let authorization;
|
|
92
|
+
const server = await startFakeGitLabAsync(value => {
|
|
93
|
+
authorization = value;
|
|
94
|
+
});
|
|
95
|
+
try {
|
|
96
|
+
const apiUrl = `http://127.0.0.1:${getPort(server)}/api/v4`;
|
|
97
|
+
const result = await runCliAsync(buildCleanEnv(buildEnv(apiUrl, tmpDir)));
|
|
98
|
+
return { ...result, authorization };
|
|
99
|
+
}
|
|
100
|
+
finally {
|
|
101
|
+
server.close();
|
|
102
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
describe("When the human CLI runs without an MCP client", () => {
|
|
106
|
+
describe("with a personal access token in the environment", () => {
|
|
107
|
+
it("should send the token as a bearer credential", async () => {
|
|
108
|
+
const run = await runMrViewAgainstFakeGitLabAsync(apiUrl => ({
|
|
109
|
+
GITLAB_API_URL: apiUrl,
|
|
110
|
+
GITLAB_PERSONAL_ACCESS_TOKEN: "glpat-cli-test",
|
|
111
|
+
}));
|
|
112
|
+
assert.equal(run.exitCode, 0);
|
|
113
|
+
assert.equal(run.authorization, "Bearer glpat-cli-test");
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe("with an OAuth token stored by the auth command", () => {
|
|
117
|
+
it("should send the stored token as a bearer credential", async () => {
|
|
118
|
+
const run = await runMrViewAgainstFakeGitLabAsync(buildOAuthEnv);
|
|
119
|
+
assert.equal(run.exitCode, 0);
|
|
120
|
+
assert.equal(run.authorization, "Bearer oauth-cli-test");
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
describe("with an OAuth token and GITLAB_IS_OLD enabled", () => {
|
|
124
|
+
it("should still send the token as a bearer credential", async () => {
|
|
125
|
+
const run = await runMrViewAgainstFakeGitLabAsync((apiUrl, tmpDir) => ({
|
|
126
|
+
...buildOAuthEnv(apiUrl, tmpDir),
|
|
127
|
+
GITLAB_IS_OLD: "true",
|
|
128
|
+
}));
|
|
129
|
+
assert.equal(run.exitCode, 0);
|
|
130
|
+
assert.equal(run.authorization, "Bearer oauth-cli-test");
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
describe("with OAuth enabled against a cleartext remote URL", () => {
|
|
134
|
+
it("should refuse to run before any network call", async () => {
|
|
135
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "gitlab-cli-credentials-"));
|
|
136
|
+
try {
|
|
137
|
+
const result = await runCliAsync(buildCleanEnv(buildOAuthEnv("http://gitlab.example.invalid/api/v4", tmpDir)));
|
|
138
|
+
assert.equal(result.exitCode, 1);
|
|
139
|
+
assert.match(result.stderr, /cleartext HTTP/);
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
describe("with no credentials at all", () => {
|
|
147
|
+
it("should exit with a usage error before any network call", async () => {
|
|
148
|
+
const run = await runMrViewAgainstFakeGitLabAsync(apiUrl => ({ GITLAB_API_URL: apiUrl }));
|
|
149
|
+
assert.equal(run.exitCode, 2);
|
|
150
|
+
assert.equal(run.authorization, undefined);
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { exposureRefusalMessage, isToolExposed, parseToolNameSet } from "../../cli/exposure.js";
|
|
4
|
+
import { compileDeniedToolsRegex } from "../../tools/denied-regex.js";
|
|
5
|
+
import { parseEnabledToolsets } from "../../tools/registry.js";
|
|
6
|
+
const ISSUES = parseEnabledToolsets("issues");
|
|
7
|
+
const EMPTY = new Set();
|
|
8
|
+
function exposure(overrides) {
|
|
9
|
+
return {
|
|
10
|
+
toolName: overrides.toolName,
|
|
11
|
+
enabledToolsets: overrides.enabledToolsets ?? ISSUES,
|
|
12
|
+
individuallyEnabledTools: overrides.individuallyEnabledTools ?? EMPTY,
|
|
13
|
+
featureFlagOverrides: overrides.featureFlagOverrides ?? EMPTY,
|
|
14
|
+
deniedRegex: overrides.deniedRegex,
|
|
15
|
+
hiddenTools: overrides.hiddenTools ?? EMPTY,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
describe("When isToolExposed applies MCP filters", () => {
|
|
19
|
+
describe("with a tool in the enabled toolset", () => {
|
|
20
|
+
it("should expose list_issues", () => {
|
|
21
|
+
assert.equal(isToolExposed(exposure({ toolName: "list_issues" })), true);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
describe("with a tool outside the enabled toolsets", () => {
|
|
25
|
+
it("should hide list_pipelines", () => {
|
|
26
|
+
assert.equal(isToolExposed(exposure({ toolName: "list_pipelines" })), false);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
describe("with GITLAB_TOOLS", () => {
|
|
30
|
+
it("should expose an individually enabled tool", () => {
|
|
31
|
+
assert.equal(isToolExposed(exposure({
|
|
32
|
+
toolName: "list_pipelines",
|
|
33
|
+
individuallyEnabledTools: new Set(["list_pipelines"]),
|
|
34
|
+
})), true);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
describe("with a legacy feature-flag override", () => {
|
|
38
|
+
it("should expose the overridden wiki tool", () => {
|
|
39
|
+
assert.equal(isToolExposed(exposure({
|
|
40
|
+
toolName: "list_wiki_pages",
|
|
41
|
+
featureFlagOverrides: new Set(["list_wiki_pages"]),
|
|
42
|
+
})), true);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe("with GITLAB_DENIED_TOOLS_REGEX", () => {
|
|
46
|
+
it("should hide a matching tool even when the toolset is enabled", () => {
|
|
47
|
+
assert.equal(isToolExposed(exposure({
|
|
48
|
+
toolName: "list_issues",
|
|
49
|
+
deniedRegex: /^list_/,
|
|
50
|
+
})), false);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
describe("with GITLAB_TOOL_POLICY_HIDDEN", () => {
|
|
54
|
+
it("should hide the named tool", () => {
|
|
55
|
+
assert.equal(isToolExposed(exposure({
|
|
56
|
+
toolName: "list_issues",
|
|
57
|
+
hiddenTools: new Set(["list_issues"]),
|
|
58
|
+
})), false);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe("When exposureRefusalMessage explains a block", () => {
|
|
63
|
+
describe("with a hidden tool", () => {
|
|
64
|
+
it("should mention GITLAB_TOOL_POLICY_HIDDEN", () => {
|
|
65
|
+
const message = exposureRefusalMessage(exposure({ toolName: "list_issues", hiddenTools: new Set(["list_issues"]) }));
|
|
66
|
+
assert.match(String(message), /GITLAB_TOOL_POLICY_HIDDEN/);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
describe("with a denied regex", () => {
|
|
70
|
+
it("should mention GITLAB_DENIED_TOOLS_REGEX", () => {
|
|
71
|
+
const message = exposureRefusalMessage(exposure({ toolName: "list_issues", deniedRegex: /^list_/ }));
|
|
72
|
+
assert.match(String(message), /GITLAB_DENIED_TOOLS_REGEX/);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
describe("with a toolset miss", () => {
|
|
76
|
+
it("should mention GITLAB_TOOLSETS", () => {
|
|
77
|
+
const message = exposureRefusalMessage(exposure({ toolName: "list_pipelines" }));
|
|
78
|
+
assert.match(String(message), /GITLAB_TOOLSETS/);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
describe("When parseToolNameSet reads a policy list", () => {
|
|
83
|
+
describe("with comma-separated names", () => {
|
|
84
|
+
it("should trim and drop empties", () => {
|
|
85
|
+
assert.deepEqual([...parseToolNameSet(" push_files, delete_issue , ")], ["push_files", "delete_issue"]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe("When compileDeniedToolsRegex validates a pattern", () => {
|
|
90
|
+
describe("with nested quantifiers", () => {
|
|
91
|
+
it("should ignore the pattern", () => {
|
|
92
|
+
const compiled = compileDeniedToolsRegex("(a+)+");
|
|
93
|
+
assert.equal(compiled.regex, undefined);
|
|
94
|
+
assert.match(String(compiled.error), /nested quantifiers/);
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
describe("with invalid characters", () => {
|
|
98
|
+
it("should reject unsafe characters", () => {
|
|
99
|
+
const compiled = compileDeniedToolsRegex("create_; rm -rf /");
|
|
100
|
+
assert.equal(compiled.regex, undefined);
|
|
101
|
+
assert.match(String(compiled.error), /invalid characters/);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { formatCliError, formatToolOutput } from "../../cli/output.js";
|
|
4
|
+
describe("When formatToolOutput prints a tool result", () => {
|
|
5
|
+
describe("with json mode", () => {
|
|
6
|
+
it("should write the unwrapped payload as JSON", () => {
|
|
7
|
+
const formatted = formatToolOutput({
|
|
8
|
+
result: { content: [{ type: "text", text: JSON.stringify({ id: 1, username: "tao" }) }] },
|
|
9
|
+
mode: "json",
|
|
10
|
+
tableSpec: undefined,
|
|
11
|
+
});
|
|
12
|
+
assert.equal(formatted.stdout, "{\"id\":1,\"username\":\"tao\"}\n");
|
|
13
|
+
assert.equal(formatted.stderr, "");
|
|
14
|
+
assert.equal(formatted.isError, false);
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
describe("with table mode for a list", () => {
|
|
18
|
+
it("should print iid state title columns", () => {
|
|
19
|
+
const formatted = formatToolOutput({
|
|
20
|
+
result: {
|
|
21
|
+
content: [
|
|
22
|
+
{
|
|
23
|
+
type: "text",
|
|
24
|
+
text: JSON.stringify([
|
|
25
|
+
{ iid: 45, state: "opened", title: "fix: pipeline retry race" },
|
|
26
|
+
{ iid: 44, state: "opened", title: "chore: bump deps" },
|
|
27
|
+
]),
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
mode: "table",
|
|
32
|
+
tableSpec: { kind: "list", columns: ["iid", "state", "title"] },
|
|
33
|
+
});
|
|
34
|
+
assert.match(formatted.stdout, /IID\s+STATE\s+TITLE/);
|
|
35
|
+
assert.match(formatted.stdout, /45\s+opened\s+fix: pipeline retry race/);
|
|
36
|
+
assert.equal(formatted.stderr, "");
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
describe("with a non-text content block first", () => {
|
|
40
|
+
it("should unwrap the first text payload", () => {
|
|
41
|
+
const formatted = formatToolOutput({
|
|
42
|
+
result: {
|
|
43
|
+
content: [
|
|
44
|
+
{ type: "image", data: "xxx" },
|
|
45
|
+
{ type: "text", text: JSON.stringify({ id: 2 }) },
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
mode: "json",
|
|
49
|
+
tableSpec: undefined,
|
|
50
|
+
});
|
|
51
|
+
assert.equal(formatted.stdout, "{\"id\":2}\n");
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
describe("with an API-style error object", () => {
|
|
55
|
+
it("should mark the result as an error", () => {
|
|
56
|
+
const formatted = formatToolOutput({
|
|
57
|
+
result: { content: [{ type: "text", text: "GitLab API error: 404 Not Found" }], isError: true },
|
|
58
|
+
mode: "json",
|
|
59
|
+
tableSpec: undefined,
|
|
60
|
+
});
|
|
61
|
+
assert.equal(formatted.isError, true);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
describe("When formatCliError prints a thrown error", () => {
|
|
66
|
+
describe("with a token in the message", () => {
|
|
67
|
+
it("should redact the token", () => {
|
|
68
|
+
const leaked = `glpat-${"notarealtokenvalue"}`;
|
|
69
|
+
const text = formatCliError(new Error(`auth failed ${leaked}`));
|
|
70
|
+
assert.match(text, /\[REDACTED\]/);
|
|
71
|
+
assert.equal(text.includes(leaked), false);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { buildToolReplayCommand } from "../../cli/replay-command.js";
|
|
4
|
+
describe("When buildToolReplayCommand prints a CLI invocation", () => {
|
|
5
|
+
describe("with scalar tool arguments", () => {
|
|
6
|
+
it("should emit kebab-case flags without the token", () => {
|
|
7
|
+
const command = buildToolReplayCommand("list_issues", {
|
|
8
|
+
project_id: "123",
|
|
9
|
+
state: "opened",
|
|
10
|
+
token: "glpat-secret",
|
|
11
|
+
});
|
|
12
|
+
assert.equal(command, "zereight-mcp-gitlab tool list_issues --project-id 123 --state opened");
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
describe("with a nested object", () => {
|
|
16
|
+
it("should pass it through --args-json", () => {
|
|
17
|
+
const command = buildToolReplayCommand("execute_graphql", {
|
|
18
|
+
query: "{ currentUser { id } }",
|
|
19
|
+
variables: { n: 1 },
|
|
20
|
+
});
|
|
21
|
+
assert.match(command, /tool execute_graphql/);
|
|
22
|
+
assert.match(command, /--query/);
|
|
23
|
+
assert.match(command, /--args-json/);
|
|
24
|
+
assert.equal(command.includes("glpat"), false);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
describe("with confirmation metadata", () => {
|
|
28
|
+
it("should omit _confirmed", () => {
|
|
29
|
+
const command = buildToolReplayCommand("push_files", {
|
|
30
|
+
project_id: "1",
|
|
31
|
+
_confirmed: true,
|
|
32
|
+
});
|
|
33
|
+
assert.equal(command.includes("_confirmed"), false);
|
|
34
|
+
assert.match(command, /--project-id 1/);
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { isCliInvocation, resolveCli } from "../../cli/router.js";
|
|
4
|
+
function argv(...args) {
|
|
5
|
+
return ["node", "index.js", ...args];
|
|
6
|
+
}
|
|
7
|
+
const OPEN_EXPOSURE = {
|
|
8
|
+
isExposed() {
|
|
9
|
+
return true;
|
|
10
|
+
},
|
|
11
|
+
needsConfirmation() {
|
|
12
|
+
return false;
|
|
13
|
+
},
|
|
14
|
+
exposureRefusal() {
|
|
15
|
+
return undefined;
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
function resolve(args, extra = {}) {
|
|
19
|
+
return resolveCli(args, { exposure: extra.exposure ?? OPEN_EXPOSURE, permissionMode: extra.permissionMode });
|
|
20
|
+
}
|
|
21
|
+
describe("When resolveCli classifies argv", () => {
|
|
22
|
+
describe("with no positional command", () => {
|
|
23
|
+
it("should stay on the MCP server path", () => {
|
|
24
|
+
const resolved = resolve(argv("--token", "glpat-xxxxxxxxxxxxxxxxxxxx"));
|
|
25
|
+
assert.equal(resolved.kind, "server");
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
describe("with --help", () => {
|
|
29
|
+
it("should print global help without starting the server", () => {
|
|
30
|
+
const resolved = resolve(argv("--help"));
|
|
31
|
+
assert.equal(resolved.kind, "help");
|
|
32
|
+
if (resolved.kind === "help") {
|
|
33
|
+
assert.match(resolved.text, /tool <tool-name>/);
|
|
34
|
+
assert.match(resolved.text, /auth/);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
describe("with --help after an unknown positional", () => {
|
|
39
|
+
it("should print global help instead of starting the server", () => {
|
|
40
|
+
const resolved = resolve(argv("not-a-command", "--help"));
|
|
41
|
+
assert.equal(resolved.kind, "help");
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe("with tool whoami", () => {
|
|
45
|
+
it("should run whoami with json output by default", () => {
|
|
46
|
+
const resolved = resolve(argv("tool", "whoami"));
|
|
47
|
+
assert.equal(resolved.kind, "run");
|
|
48
|
+
if (resolved.kind === "run") {
|
|
49
|
+
assert.equal(resolved.toolName, "whoami");
|
|
50
|
+
assert.equal(resolved.output, "json");
|
|
51
|
+
assert.deepEqual(resolved.args, {});
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
describe("When a curated command is resolved", () => {
|
|
57
|
+
describe("with mr list --group-id", () => {
|
|
58
|
+
it("should call list_group_merge_requests", () => {
|
|
59
|
+
const resolved = resolve(argv("mr", "list", "--group-id", "42", "--state", "opened"));
|
|
60
|
+
assert.equal(resolved.kind, "run");
|
|
61
|
+
if (resolved.kind === "run") {
|
|
62
|
+
assert.equal(resolved.toolName, "list_group_merge_requests");
|
|
63
|
+
assert.equal(resolved.args.group_id, "42");
|
|
64
|
+
assert.equal(resolved.output, "table");
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
describe("with issue close", () => {
|
|
69
|
+
it("should call update_issue with state_event close", () => {
|
|
70
|
+
const resolved = resolve(argv("issue", "close", "--project-id", "123", "--issue-iid", "9"));
|
|
71
|
+
assert.equal(resolved.kind, "run");
|
|
72
|
+
if (resolved.kind === "run") {
|
|
73
|
+
assert.equal(resolved.toolName, "update_issue");
|
|
74
|
+
assert.equal(resolved.args.issue_iid, "9");
|
|
75
|
+
assert.equal(resolved.args.state_event, "close");
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
describe("with mr merge and --yes=false", () => {
|
|
80
|
+
it("should reject the inline value instead of merging", () => {
|
|
81
|
+
const resolved = resolve(argv("mr", "merge", "--project-id", "123", "--mr-iid", "45", "--yes=false"));
|
|
82
|
+
assert.equal(resolved.kind, "usage");
|
|
83
|
+
if (resolved.kind === "usage") {
|
|
84
|
+
assert.match(resolved.message, /--yes does not accept a value/);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
describe("with mr merge and no --yes", () => {
|
|
89
|
+
it("should refuse until --yes is passed without dumping argument values", () => {
|
|
90
|
+
const resolved = resolve(argv("mr", "merge", "--project-id", "123", "--mr-iid", "45"));
|
|
91
|
+
assert.equal(resolved.kind, "refused");
|
|
92
|
+
if (resolved.kind === "refused") {
|
|
93
|
+
assert.match(resolved.message, /--yes/);
|
|
94
|
+
assert.match(resolved.message, /merge_merge_request/);
|
|
95
|
+
assert.match(resolved.message, /project_id/);
|
|
96
|
+
assert.equal(resolved.message.includes("123"), false);
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
describe("with variable list scoped via --args-json", () => {
|
|
101
|
+
it("should call list_group_variables when group_id is only in JSON", () => {
|
|
102
|
+
const resolved = resolve(argv("variable", "list", "--args-json", '{"group_id":"42"}'));
|
|
103
|
+
assert.equal(resolved.kind, "run");
|
|
104
|
+
if (resolved.kind === "run") {
|
|
105
|
+
assert.equal(resolved.toolName, "list_group_variables");
|
|
106
|
+
assert.equal(resolved.args.group_id, "42");
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
describe("When permission mode blocks a write", () => {
|
|
112
|
+
describe("with readonly mode and issue create", () => {
|
|
113
|
+
it("should refuse before dispatch", () => {
|
|
114
|
+
const resolved = resolve(argv("issue", "create", "--project-id", "123", "--title", "bug"), { permissionMode: "readonly" });
|
|
115
|
+
assert.equal(resolved.kind, "refused");
|
|
116
|
+
if (resolved.kind === "refused") {
|
|
117
|
+
assert.match(resolved.message, /read-only/);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
describe("When isCliInvocation inspects argv", () => {
|
|
123
|
+
describe("with a curated group", () => {
|
|
124
|
+
it("should treat it as a human CLI invocation", () => {
|
|
125
|
+
assert.equal(isCliInvocation(argv("mr", "list")), true);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
describe("with --help", () => {
|
|
129
|
+
it("should treat it as a human CLI invocation", () => {
|
|
130
|
+
assert.equal(isCliInvocation(argv("--help")), true);
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
describe("with only a server token flag", () => {
|
|
134
|
+
it("should stay on the MCP server path", () => {
|
|
135
|
+
assert.equal(isCliInvocation(argv("--token", "x")), false);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
describe("When MCP exposure filters block a CLI tool", () => {
|
|
140
|
+
describe("with a denied-tools regex", () => {
|
|
141
|
+
it("should refuse before dispatch", () => {
|
|
142
|
+
const resolved = resolve(argv("tool", "whoami"), {
|
|
143
|
+
exposure: {
|
|
144
|
+
isExposed() {
|
|
145
|
+
return false;
|
|
146
|
+
},
|
|
147
|
+
needsConfirmation() {
|
|
148
|
+
return false;
|
|
149
|
+
},
|
|
150
|
+
exposureRefusal(toolName) {
|
|
151
|
+
return `${toolName} is blocked by GITLAB_DENIED_TOOLS_REGEX`;
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
assert.equal(resolved.kind, "refused");
|
|
156
|
+
if (resolved.kind === "refused") {
|
|
157
|
+
assert.match(resolved.message, /GITLAB_DENIED_TOOLS_REGEX/);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
describe("with GITLAB_TOOL_POLICY_APPROVE", () => {
|
|
162
|
+
it("should require --yes", () => {
|
|
163
|
+
const resolved = resolve(argv("tool", "whoami"), {
|
|
164
|
+
exposure: {
|
|
165
|
+
isExposed() {
|
|
166
|
+
return true;
|
|
167
|
+
},
|
|
168
|
+
needsConfirmation() {
|
|
169
|
+
return true;
|
|
170
|
+
},
|
|
171
|
+
exposureRefusal() {
|
|
172
|
+
return undefined;
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
assert.equal(resolved.kind, "refused");
|
|
177
|
+
if (resolved.kind === "refused") {
|
|
178
|
+
assert.match(resolved.message, /GITLAB_TOOL_POLICY_APPROVE/);
|
|
179
|
+
assert.match(resolved.message, /--yes/);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import http from "node:http";
|
|
3
|
+
import { describe, test } from "node:test";
|
|
4
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
5
|
+
import { getDefaultEnvironment, StdioClientTransport, } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
6
|
+
const MASKED_IP = "10.20.30.40";
|
|
7
|
+
async function startServer(t) {
|
|
8
|
+
const api = http.createServer((req, res) => {
|
|
9
|
+
res.setHeader("Content-Type", "application/json");
|
|
10
|
+
if (req.url === "/api/v4/namespaces/missing") {
|
|
11
|
+
res.statusCode = 404;
|
|
12
|
+
res.end(JSON.stringify({ message: "Namespace not found", host: MASKED_IP }));
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (req.url === "/api/v4/groups/10/members") {
|
|
16
|
+
res.statusCode = 403;
|
|
17
|
+
res.end(JSON.stringify({ message: "Forbidden" }));
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
if (req.method === "POST" && req.url === "/api/v4/projects/123/merge_requests") {
|
|
21
|
+
res.statusCode = 400;
|
|
22
|
+
res.end(JSON.stringify({ message: "Invalid merge request" }));
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (req.url === "/api/v4/namespaces/core/exists") {
|
|
26
|
+
res.end(JSON.stringify({ exists: true, suggests: [] }));
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
res.statusCode = 404;
|
|
30
|
+
res.end(JSON.stringify({ message: "Not found" }));
|
|
31
|
+
});
|
|
32
|
+
await new Promise((resolve, reject) => {
|
|
33
|
+
api.once("error", reject);
|
|
34
|
+
api.listen(0, "127.0.0.1", resolve);
|
|
35
|
+
});
|
|
36
|
+
const apiUrl = `http://127.0.0.1:${api.address().port}/api/v4`;
|
|
37
|
+
const client = new Client({ name: "gitlab-api-tool-error-test", version: "1.0.0" });
|
|
38
|
+
const transport = new StdioClientTransport({
|
|
39
|
+
command: process.execPath,
|
|
40
|
+
args: ["build/index.js"],
|
|
41
|
+
stderr: "pipe",
|
|
42
|
+
env: {
|
|
43
|
+
...getDefaultEnvironment(),
|
|
44
|
+
GITLAB_PERSONAL_ACCESS_TOKEN: "test-token",
|
|
45
|
+
GITLAB_API_URL: apiUrl,
|
|
46
|
+
GITLAB_PERMISSION_MODE: "modify",
|
|
47
|
+
GITLAB_TOOLSETS: "projects,merge_requests",
|
|
48
|
+
GITLAB_MASKING_ENABLED: "true",
|
|
49
|
+
GITLAB_DISABLE_VERSION_CHECK: "true",
|
|
50
|
+
LOG_LEVEL: "error",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
transport.stderr?.on("data", () => undefined);
|
|
54
|
+
t.after(async () => {
|
|
55
|
+
try {
|
|
56
|
+
await client.close();
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
api.closeAllConnections();
|
|
60
|
+
await new Promise(resolve => api.close(() => resolve()));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
await client.connect(transport);
|
|
64
|
+
return client;
|
|
65
|
+
}
|
|
66
|
+
function resultText(result) {
|
|
67
|
+
assert.ok("content" in result && Array.isArray(result.content), "expected tool content");
|
|
68
|
+
const block = result.content.find((item) => typeof item === "object" &&
|
|
69
|
+
item !== null &&
|
|
70
|
+
"type" in item &&
|
|
71
|
+
item.type === "text" &&
|
|
72
|
+
"text" in item &&
|
|
73
|
+
typeof item.text === "string");
|
|
74
|
+
assert.ok(block, "expected a text tool result");
|
|
75
|
+
return block.text;
|
|
76
|
+
}
|
|
77
|
+
describe("GitLab API tool errors", () => {
|
|
78
|
+
test("returns HTTP failures as tool errors and keeps the MCP connection usable", async (t) => {
|
|
79
|
+
const client = await startServer(t);
|
|
80
|
+
const notFound = await client.callTool({
|
|
81
|
+
name: "get_namespace",
|
|
82
|
+
arguments: { namespace_id: "missing" },
|
|
83
|
+
});
|
|
84
|
+
assert.equal(notFound.isError, true);
|
|
85
|
+
assert.match(resultText(notFound), /GitLab API error: 404 Not Found/);
|
|
86
|
+
assert.doesNotMatch(resultText(notFound), new RegExp(MASKED_IP.replaceAll(".", "\\.")));
|
|
87
|
+
assert.match(resultText(notFound), /\[IP address\]/);
|
|
88
|
+
const forbidden = await client.callTool({
|
|
89
|
+
name: "list_group_members",
|
|
90
|
+
arguments: { group_id: "10" },
|
|
91
|
+
});
|
|
92
|
+
assert.equal(forbidden.isError, true);
|
|
93
|
+
assert.match(resultText(forbidden), /GitLab API error: 403 Forbidden/);
|
|
94
|
+
const invalidMergeRequest = await client.callTool({
|
|
95
|
+
name: "create_merge_request",
|
|
96
|
+
arguments: {
|
|
97
|
+
project_id: "123",
|
|
98
|
+
title: "Test MR",
|
|
99
|
+
source_branch: "feature",
|
|
100
|
+
target_branch: "main",
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
assert.equal(invalidMergeRequest.isError, true);
|
|
104
|
+
assert.match(resultText(invalidMergeRequest), /Invalid request/);
|
|
105
|
+
const recovery = await client.callTool({
|
|
106
|
+
name: "verify_namespace",
|
|
107
|
+
arguments: { path: "core" },
|
|
108
|
+
});
|
|
109
|
+
assert.notEqual(recovery.isError, true);
|
|
110
|
+
assert.deepEqual(JSON.parse(resultText(recovery)), { exists: true, suggests: [] });
|
|
111
|
+
});
|
|
112
|
+
});
|