@zereight/mcp-gitlab 2.1.69 → 2.2.0
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 +10 -4
- package/README.md +238 -233
- package/README.pt-BR.md +4 -4
- package/README.zh-CN.md +6 -4
- package/build/config.js +12 -4
- package/build/deprecated-env.js +47 -0
- package/build/index.js +456 -237
- package/build/oauth-device-flow.js +80 -0
- package/build/oauth.js +135 -18
- package/build/schemas.js +1 -1
- package/build/server/remote-http-stack.js +39 -0
- package/build/tools/discover-activation.js +38 -0
- package/build/tools/exposed-tools.js +35 -0
- package/build/tools/listed-tools.js +76 -0
- package/build/tools/registry.js +226 -57
- package/build/tools/tool-profile.js +66 -0
- package/build/utils/compact-tool-allowlist.js +14 -0
- package/build/utils/logger.js +34 -12
- package/build/utils/url.js +15 -0
- package/package.json +3 -3
- package/build/scripts/check-skill-sync.js +0 -143
- package/build/scripts/generate-server-card.js +0 -39
- package/build/scripts/generate-tool-coverage.js +0 -58
- package/build/scripts/generate-tool-docs.js +0 -430
- package/build/scripts/tool-coverage/coverage.js +0 -341
- package/build/scripts/tool-coverage/coverage.test.js +0 -142
- package/build/test/callback-proxy-tests.js +0 -321
- package/build/test/cli/args.test.js +0 -175
- package/build/test/cli/boolean-flags.test.js +0 -106
- package/build/test/cli/compact-mcp-result.test.js +0 -93
- package/build/test/cli/exposure.test.js +0 -104
- package/build/test/cli/load-config-snapshot.js +0 -5
- package/build/test/cli/output.test.js +0 -74
- package/build/test/cli/replay-command.test.js +0 -37
- package/build/test/cli/router.test.js +0 -183
- package/build/test/client-pool-test.js +0 -109
- package/build/test/clients/client.js +0 -34
- package/build/test/clients/custom-header-client.js +0 -122
- package/build/test/clients/sse-client.js +0 -95
- package/build/test/clients/stdio-client.js +0 -113
- package/build/test/clients/streamable-http-client.js +0 -95
- package/build/test/config-allowed-groups.test.js +0 -97
- package/build/test/create-commit-actions.test.js +0 -102
- package/build/test/draft-note-schema.test.js +0 -44
- package/build/test/dynamic-api-url-allowlist.test.js +0 -121
- package/build/test/dynamic-api-url-test.js +0 -304
- package/build/test/dynamic-routing-tests.js +0 -495
- package/build/test/get-merge-request-discussion-schema.test.js +0 -30
- package/build/test/gitlab-artifact-entry-schema.test.js +0 -44
- package/build/test/gitlab-diff-schema.test.js +0 -36
- package/build/test/gitlab-pipeline-schedule-schema.test.js +0 -32
- package/build/test/gitlab-tag-schema.test.js +0 -51
- package/build/test/group-milestone-schema.test.js +0 -45
- package/build/test/mcp-oauth-tests.js +0 -905
- package/build/test/mcp-server-name.test.js +0 -87
- package/build/test/merge-merge-request-schema.test.js +0 -75
- package/build/test/multi-server-test.js +0 -190
- package/build/test/no-proxy-integration-test.js +0 -183
- package/build/test/no-proxy-test.js +0 -149
- package/build/test/nullable-gitlab-response-fields.test.js +0 -37
- package/build/test/nullish-tool-arguments-schema.test.js +0 -118
- package/build/test/oauth-device-flow-tests.js +0 -450
- package/build/test/oauth-startup.test.js +0 -204
- package/build/test/oauth-tests.js +0 -428
- package/build/test/path-segment-encoding.test.js +0 -98
- package/build/test/remote-auth-simple-test.js +0 -277
- package/build/test/remote-auth-tests.js +0 -314
- package/build/test/response-masking.test.js +0 -445
- package/build/test/schema-tests.js +0 -1679
- package/build/test/server/metrics.test.js +0 -47
- package/build/test/server/request-helpers.test.js +0 -48
- package/build/test/server/version.test.js +0 -13
- package/build/test/sse-auth-guard.test.js +0 -96
- package/build/test/sse-session-limits.test.js +0 -273
- package/build/test/stateless/callback-proxy.test.js +0 -510
- package/build/test/stateless/client-id.test.js +0 -177
- package/build/test/stateless/codec.test.js +0 -328
- package/build/test/stateless/config-ttl.test.js +0 -149
- package/build/test/stateless/consumed-proxy-code-cache.test.js +0 -150
- package/build/test/stateless/session-id-integration.test.js +0 -675
- package/build/test/stateless/session-id.test.js +0 -157
- package/build/test/streamable-http-concurrent-session.test.js +0 -171
- package/build/test/streamable-http-dns-rebinding.test.js +0 -185
- package/build/test/streamable-http-sse-stream.test.js +0 -350
- package/build/test/streamable-http-static-token-auth.test.js +0 -157
- package/build/test/streamable-http-unauthenticated-discovery.test.js +0 -207
- package/build/test/test-all-transport-server.js +0 -255
- package/build/test/test-auth-retry.js +0 -195
- package/build/test/test-blank-filters.js +0 -156
- package/build/test/test-ci-catalog.js +0 -177
- package/build/test/test-ci-lint.js +0 -191
- package/build/test/test-ci-variables.js +0 -309
- package/build/test/test-create-repository.js +0 -120
- package/build/test/test-dependency-proxy.js +0 -195
- package/build/test/test-deployment-tools.js +0 -463
- package/build/test/test-download-attachment.js +0 -190
- package/build/test/test-dynamic-project-scope.js +0 -524
- package/build/test/test-empty-toolsets.js +0 -535
- package/build/test/test-get-file-blame.js +0 -145
- package/build/test/test-get-file-contents.js +0 -128
- package/build/test/test-geteffectiveprojectid.js +0 -812
- package/build/test/test-health-check.js +0 -115
- package/build/test/test-issue-description-patch.js +0 -256
- package/build/test/test-job-artifacts.js +0 -275
- package/build/test/test-json-schema.js +0 -166
- package/build/test/test-list-group-members.js +0 -150
- package/build/test/test-list-group-merge-requests.js +0 -165
- package/build/test/test-list-issues.js +0 -170
- package/build/test/test-list-merge-requests.js +0 -201
- package/build/test/test-list-milestones.js +0 -145
- package/build/test/test-list-project-members.js +0 -132
- package/build/test/test-merge-request-approval-state-tools.js +0 -213
- package/build/test/test-merge-request-approvals.js +0 -187
- package/build/test/test-merge-request-pipelines.js +0 -106
- package/build/test/test-mr-diffs-filter.js +0 -132
- package/build/test/test-mr-file-diffs.js +0 -251
- package/build/test/test-oauth-proxy-rate-limit.js +0 -133
- package/build/test/test-orbit.js +0 -155
- package/build/test/test-permission-mode.js +0 -424
- package/build/test/test-pipeline-schedules.js +0 -482
- package/build/test/test-protected-branches.js +0 -174
- package/build/test/test-remote-downloads.js +0 -510
- package/build/test/test-search-code.js +0 -275
- package/build/test/test-snippets.js +0 -735
- package/build/test/test-tags.js +0 -220
- package/build/test/test-todos.js +0 -195
- package/build/test/test-token-optimizations.js +0 -691
- package/build/test/test-toolset-filtering.js +0 -499
- package/build/test/test-update-issue-slim.js +0 -141
- package/build/test/test-update-project.js +0 -112
- package/build/test/test-upload-markdown.js +0 -206
- package/build/test/test-vulnerabilities.js +0 -399
- package/build/test/test-webhooks.js +0 -218
- package/build/test/tool-description-quality.test.js +0 -50
- package/build/test/utils/bulk-publish-options.test.js +0 -32
- package/build/test/utils/download-token.test.js +0 -35
- package/build/test/utils/forwarded-public-base-url.test.js +0 -46
- package/build/test/utils/gitlab-instance-version.test.js +0 -32
- package/build/test/utils/gitlab-version-gate.test.js +0 -74
- package/build/test/utils/graphql-query.test.js +0 -168
- package/build/test/utils/helpers-path.test.js +0 -104
- package/build/test/utils/jmespath-tool-result.test.js +0 -91
- package/build/test/utils/masking.test.js +0 -384
- package/build/test/utils/merge-request-position.test.js +0 -57
- package/build/test/utils/mock-gitlab-server.js +0 -614
- package/build/test/utils/proxy-client-ip.test.js +0 -28
- package/build/test/utils/redact-sensitive.test.js +0 -32
- package/build/test/utils/safe-redirect-fetch.test.js +0 -463
- package/build/test/utils/server-launcher.js +0 -231
- package/build/test/utils/tool-args.test.js +0 -210
- package/build/test/utils/url.test.js +0 -128
- package/build/test/utils/version-check.test.js +0 -239
- package/build/test/utils/wiki-title.test.js +0 -21
- package/build/test-note.js +0 -54
- package/build/test-resolve-issue-note.js +0 -127
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remote Authorization Test Suite
|
|
3
|
-
* Tests remote auth functionality with mock GitLab server
|
|
4
|
-
*/
|
|
5
|
-
import { describe, test, after, before } from 'node:test';
|
|
6
|
-
import assert from 'node:assert';
|
|
7
|
-
import { launchServer, findAvailablePort, cleanupServers, TransportMode, HOST } from './utils/server-launcher.js';
|
|
8
|
-
import { MockGitLabServer, findMockServerPort } from './utils/mock-gitlab-server.js';
|
|
9
|
-
import { CustomHeaderClient } from './clients/custom-header-client.js';
|
|
10
|
-
// Test constants
|
|
11
|
-
const MOCK_TOKEN = 'glpat-mock-token-12345';
|
|
12
|
-
const MOCK_JOB_TOKEN = 'glcbt-mock-job-token-9876';
|
|
13
|
-
// Port ranges to avoid collisions
|
|
14
|
-
const MOCK_GITLAB_PORT_BASE = 9000;
|
|
15
|
-
const MOCK_GITLAB_PORT_OFFSET = 500; // Offset for timeout test suite
|
|
16
|
-
const MCP_SERVER_PORT_BASE = 3000;
|
|
17
|
-
const MCP_SERVER_PORT_OFFSET = 500; // Offset for timeout test suite
|
|
18
|
-
// Timeout settings
|
|
19
|
-
const SESSION_TIMEOUT_SECONDS = 3;
|
|
20
|
-
const TIMEOUT_BUFFER_MS = 1000; // Extra time beyond timeout to ensure expiration
|
|
21
|
-
const TIMEOUT_TEST_WAIT_MS = SESSION_TIMEOUT_SECONDS * 1000 + TIMEOUT_BUFFER_MS;
|
|
22
|
-
const KEEPALIVE_INTERVAL_MS = 2000; // Must be less than SESSION_TIMEOUT_SECONDS
|
|
23
|
-
const KEEPALIVE_REQUEST_COUNT = 3; // Number of keepalive requests to test
|
|
24
|
-
async function getMetrics(mcpUrl) {
|
|
25
|
-
const metricsUrl = mcpUrl.replace(/\/mcp$/, '/metrics.json');
|
|
26
|
-
const response = await fetch(metricsUrl);
|
|
27
|
-
assert.strictEqual(response.status, 200, 'metrics JSON endpoint should be available');
|
|
28
|
-
return (await response.json());
|
|
29
|
-
}
|
|
30
|
-
async function waitForSessionDecrease(mcpUrl, beforeTimeout, timeoutMs = 3000) {
|
|
31
|
-
const startedAt = Date.now();
|
|
32
|
-
let last = await getMetrics(mcpUrl);
|
|
33
|
-
while (Date.now() - startedAt < timeoutMs) {
|
|
34
|
-
if (last.activeSessions < beforeTimeout.activeSessions &&
|
|
35
|
-
last.authenticatedSessions < beforeTimeout.authenticatedSessions) {
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
await new Promise(resolve => setTimeout(resolve, 100));
|
|
39
|
-
last = await getMetrics(mcpUrl);
|
|
40
|
-
}
|
|
41
|
-
assert.ok(last.activeSessions < beforeTimeout.activeSessions, `activeSessions should decrease after timeout (${last.activeSessions} !< ${beforeTimeout.activeSessions})`);
|
|
42
|
-
assert.ok(last.authenticatedSessions < beforeTimeout.authenticatedSessions, `authenticatedSessions should decrease after timeout (${last.authenticatedSessions} !< ${beforeTimeout.authenticatedSessions})`);
|
|
43
|
-
}
|
|
44
|
-
console.log('🔐 Remote Authorization Test Suite');
|
|
45
|
-
console.log('');
|
|
46
|
-
describe('Remote Authorization', { concurrency: 1 }, () => {
|
|
47
|
-
describe('Remote Authorization - Basic Functionality', () => {
|
|
48
|
-
let mcpUrl;
|
|
49
|
-
let mockGitLab;
|
|
50
|
-
let servers = [];
|
|
51
|
-
before(async () => {
|
|
52
|
-
// Start mock GitLab server
|
|
53
|
-
const mockPort = await findMockServerPort();
|
|
54
|
-
mockGitLab = new MockGitLabServer({
|
|
55
|
-
port: mockPort,
|
|
56
|
-
validTokens: [MOCK_TOKEN, MOCK_JOB_TOKEN],
|
|
57
|
-
});
|
|
58
|
-
await mockGitLab.start();
|
|
59
|
-
const mockGitLabUrl = mockGitLab.getUrl();
|
|
60
|
-
// Start MCP server with remote auth
|
|
61
|
-
const mcpPort = await findAvailablePort(MCP_SERVER_PORT_BASE);
|
|
62
|
-
const server = await launchServer({
|
|
63
|
-
mode: TransportMode.STREAMABLE_HTTP,
|
|
64
|
-
port: mcpPort,
|
|
65
|
-
timeout: 5000,
|
|
66
|
-
env: {
|
|
67
|
-
STREAMABLE_HTTP: 'true',
|
|
68
|
-
REMOTE_AUTHORIZATION: 'true',
|
|
69
|
-
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
|
|
70
|
-
GITLAB_READ_ONLY_MODE: 'true',
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
servers.push(server);
|
|
74
|
-
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
|
|
75
|
-
console.log(`Mock GitLab: ${mockGitLabUrl}`);
|
|
76
|
-
console.log(`MCP Server: ${mcpUrl}`);
|
|
77
|
-
});
|
|
78
|
-
after(async () => {
|
|
79
|
-
cleanupServers(servers);
|
|
80
|
-
if (mockGitLab) {
|
|
81
|
-
await mockGitLab.stop();
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
test('should reject invalid tokens before creating a session', async () => {
|
|
85
|
-
const before = await getMetrics(mcpUrl);
|
|
86
|
-
const response = await fetch(mcpUrl, {
|
|
87
|
-
method: 'POST',
|
|
88
|
-
headers: {
|
|
89
|
-
'Content-Type': 'application/json',
|
|
90
|
-
Accept: 'application/json, text/event-stream',
|
|
91
|
-
'Private-Token': 'aaaaaaaaaaaaaaaaaaaaaaaa',
|
|
92
|
-
},
|
|
93
|
-
body: JSON.stringify({
|
|
94
|
-
jsonrpc: '2.0',
|
|
95
|
-
id: 1,
|
|
96
|
-
method: 'initialize',
|
|
97
|
-
params: {
|
|
98
|
-
protocolVersion: '2025-03-26',
|
|
99
|
-
capabilities: {},
|
|
100
|
-
clientInfo: { name: 'invalid-token-test', version: '1.0.0' },
|
|
101
|
-
},
|
|
102
|
-
}),
|
|
103
|
-
});
|
|
104
|
-
assert.strictEqual(response.status, 401);
|
|
105
|
-
const after = await getMetrics(mcpUrl);
|
|
106
|
-
assert.strictEqual(after.activeSessions, before.activeSessions);
|
|
107
|
-
assert.strictEqual(after.authenticatedSessions, before.authenticatedSessions);
|
|
108
|
-
});
|
|
109
|
-
test('should connect with Authorization Bearer header', async () => {
|
|
110
|
-
const client = new CustomHeaderClient({
|
|
111
|
-
'authorization': `Bearer ${MOCK_TOKEN}`
|
|
112
|
-
});
|
|
113
|
-
await client.connect(mcpUrl);
|
|
114
|
-
const tools = await client.listTools();
|
|
115
|
-
assert.ok(tools.tools.length > 0, 'Should have tools');
|
|
116
|
-
console.log(` ✓ Connected successfully, got ${tools.tools.length} tools`);
|
|
117
|
-
await client.disconnect();
|
|
118
|
-
});
|
|
119
|
-
test('should connect with Private-Token header', async () => {
|
|
120
|
-
const client = new CustomHeaderClient({
|
|
121
|
-
'private-token': MOCK_TOKEN
|
|
122
|
-
});
|
|
123
|
-
await client.connect(mcpUrl);
|
|
124
|
-
const tools = await client.listTools();
|
|
125
|
-
assert.ok(tools.tools.length > 0, 'Should have tools');
|
|
126
|
-
console.log(` ✓ Connected with Private-Token, got ${tools.tools.length} tools`);
|
|
127
|
-
await client.disconnect();
|
|
128
|
-
});
|
|
129
|
-
test('should connect with JOB-TOKEN header', async () => {
|
|
130
|
-
const client = new CustomHeaderClient({
|
|
131
|
-
'job-token': MOCK_JOB_TOKEN,
|
|
132
|
-
});
|
|
133
|
-
await client.connect(mcpUrl);
|
|
134
|
-
const tools = await client.listTools();
|
|
135
|
-
assert.ok(tools.tools.length > 0, 'Should have tools');
|
|
136
|
-
console.log(` ✓ Connected with JOB-TOKEN, got ${tools.tools.length} tools`);
|
|
137
|
-
await client.disconnect();
|
|
138
|
-
});
|
|
139
|
-
test('should successfully call listTools with auth', async () => {
|
|
140
|
-
const client = new CustomHeaderClient({
|
|
141
|
-
'authorization': `Bearer ${MOCK_TOKEN}`
|
|
142
|
-
});
|
|
143
|
-
await client.connect(mcpUrl);
|
|
144
|
-
// List tools multiple times to verify auth persists
|
|
145
|
-
const tools1 = await client.listTools();
|
|
146
|
-
const tools2 = await client.listTools();
|
|
147
|
-
const tools3 = await client.listTools();
|
|
148
|
-
assert.ok(tools1.tools.length > 0, 'Should have tools');
|
|
149
|
-
assert.strictEqual(tools1.tools.length, tools2.tools.length, 'Tool count should be consistent');
|
|
150
|
-
assert.strictEqual(tools2.tools.length, tools3.tools.length, 'Tool count should be consistent');
|
|
151
|
-
console.log(' ✓ Multiple tool list calls successful with persistent auth');
|
|
152
|
-
await client.disconnect();
|
|
153
|
-
});
|
|
154
|
-
test('should expose Prometheus metrics at /metrics', async () => {
|
|
155
|
-
const metricsUrl = mcpUrl.replace(/\/mcp$/, '/metrics');
|
|
156
|
-
const response = await fetch(metricsUrl);
|
|
157
|
-
assert.strictEqual(response.status, 200, 'metrics endpoint should be available');
|
|
158
|
-
assert.match(response.headers.get('content-type') || '', /^text\/plain/);
|
|
159
|
-
const body = await response.text();
|
|
160
|
-
assert.match(body, /# HELP gitlab_mcp_requests_processed_total/);
|
|
161
|
-
assert.match(body, /# TYPE gitlab_mcp_active_sessions gauge/);
|
|
162
|
-
assert.match(body, /gitlab_mcp_config_info\{[^}]*remote_auth_enabled="true"/);
|
|
163
|
-
console.log(' ✓ Prometheus metrics available');
|
|
164
|
-
});
|
|
165
|
-
test('should reject connection without auth header', async () => {
|
|
166
|
-
const client = new CustomHeaderClient({});
|
|
167
|
-
try {
|
|
168
|
-
await client.connect(mcpUrl);
|
|
169
|
-
await client.listTools();
|
|
170
|
-
await client.disconnect();
|
|
171
|
-
assert.fail('Should have rejected connection without auth');
|
|
172
|
-
}
|
|
173
|
-
catch (error) {
|
|
174
|
-
assert.ok(error instanceof Error);
|
|
175
|
-
console.log(' ✓ Correctly rejected connection without auth');
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
});
|
|
179
|
-
describe('Remote Authorization - Session Timeout', () => {
|
|
180
|
-
let mcpUrl;
|
|
181
|
-
let mockGitLab;
|
|
182
|
-
let servers = [];
|
|
183
|
-
before(async () => {
|
|
184
|
-
// Use different port ranges to avoid collisions with basic tests
|
|
185
|
-
const mockPort = await findMockServerPort();
|
|
186
|
-
mockGitLab = new MockGitLabServer({
|
|
187
|
-
port: mockPort,
|
|
188
|
-
validTokens: [MOCK_TOKEN]
|
|
189
|
-
});
|
|
190
|
-
await mockGitLab.start();
|
|
191
|
-
const mockGitLabUrl = mockGitLab.getUrl();
|
|
192
|
-
const mcpPort = await findAvailablePort(MCP_SERVER_PORT_BASE + MCP_SERVER_PORT_OFFSET);
|
|
193
|
-
const server = await launchServer({
|
|
194
|
-
mode: TransportMode.STREAMABLE_HTTP,
|
|
195
|
-
port: mcpPort,
|
|
196
|
-
timeout: 5000,
|
|
197
|
-
env: {
|
|
198
|
-
STREAMABLE_HTTP: 'true',
|
|
199
|
-
REMOTE_AUTHORIZATION: 'true',
|
|
200
|
-
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
|
|
201
|
-
SESSION_TIMEOUT_SECONDS: String(SESSION_TIMEOUT_SECONDS),
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
servers.push(server);
|
|
205
|
-
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
|
|
206
|
-
console.log(`Session timeout: ${SESSION_TIMEOUT_SECONDS} seconds`);
|
|
207
|
-
});
|
|
208
|
-
after(async () => {
|
|
209
|
-
cleanupServers(servers);
|
|
210
|
-
if (mockGitLab) {
|
|
211
|
-
await mockGitLab.stop();
|
|
212
|
-
}
|
|
213
|
-
});
|
|
214
|
-
test('session timeout verification - activity keeps session alive', async () => {
|
|
215
|
-
const client = new CustomHeaderClient({
|
|
216
|
-
'authorization': `Bearer ${MOCK_TOKEN}`
|
|
217
|
-
});
|
|
218
|
-
// Connect and verify it works
|
|
219
|
-
await client.connect(mcpUrl);
|
|
220
|
-
const tools1 = await client.listTools();
|
|
221
|
-
assert.ok(tools1.tools.length > 0, 'Initial connection should work');
|
|
222
|
-
console.log(' ✓ Initial connection successful');
|
|
223
|
-
// Keep connection alive with periodic requests
|
|
224
|
-
console.log(' Keeping session alive with requests...');
|
|
225
|
-
for (let i = 0; i < KEEPALIVE_REQUEST_COUNT; i++) {
|
|
226
|
-
await new Promise(resolve => setTimeout(resolve, KEEPALIVE_INTERVAL_MS));
|
|
227
|
-
const tools = await client.listTools();
|
|
228
|
-
assert.ok(tools.tools.length > 0, `Request ${i + 1} should succeed`);
|
|
229
|
-
console.log(` ✓ Request ${i + 1} succeeded (session still alive)`);
|
|
230
|
-
}
|
|
231
|
-
await client.disconnect();
|
|
232
|
-
console.log(' ✓ Session remained active with periodic requests');
|
|
233
|
-
});
|
|
234
|
-
test('session timeout expiration - inactivity expires auth', async () => {
|
|
235
|
-
// Add a small delay to ensure server is ready/clean from previous test
|
|
236
|
-
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
237
|
-
const baseline = await getMetrics(mcpUrl);
|
|
238
|
-
// Step 1: Connect WITH auth header to establish session
|
|
239
|
-
const clientWithAuth = new CustomHeaderClient({
|
|
240
|
-
'authorization': `Bearer ${MOCK_TOKEN}`
|
|
241
|
-
});
|
|
242
|
-
await clientWithAuth.connect(mcpUrl);
|
|
243
|
-
const tools1 = await clientWithAuth.listTools();
|
|
244
|
-
assert.ok(tools1.tools.length > 0, 'Initial connection should work');
|
|
245
|
-
console.log(' ✓ Initial connection successful with auth');
|
|
246
|
-
// Extract session ID using proper API
|
|
247
|
-
const sessionId = clientWithAuth.getSessionId();
|
|
248
|
-
assert.ok(sessionId, 'Session ID should exist');
|
|
249
|
-
console.log(` ℹ️ Session ID: ${sessionId}`);
|
|
250
|
-
const beforeTimeout = await getMetrics(mcpUrl);
|
|
251
|
-
assert.strictEqual(beforeTimeout.activeSessions, baseline.activeSessions + 1, 'Session should occupy one additional active slot');
|
|
252
|
-
assert.strictEqual(beforeTimeout.authenticatedSessions, baseline.authenticatedSessions + 1, 'Session should add one authenticated session');
|
|
253
|
-
// Step 2: Wait for timeout WITHOUT making any requests
|
|
254
|
-
console.log(` ⏳ Waiting ${TIMEOUT_TEST_WAIT_MS / 1000}s for timeout without activity...`);
|
|
255
|
-
await new Promise(resolve => setTimeout(resolve, TIMEOUT_TEST_WAIT_MS));
|
|
256
|
-
await waitForSessionDecrease(mcpUrl, beforeTimeout);
|
|
257
|
-
console.log(' ✓ Timeout closed transport and released active session slot');
|
|
258
|
-
// Step 3: A stale stateful session must return the MCP re-initialization signal.
|
|
259
|
-
const response = await fetch(mcpUrl, {
|
|
260
|
-
method: 'POST',
|
|
261
|
-
headers: {
|
|
262
|
-
'Content-Type': 'application/json',
|
|
263
|
-
'mcp-session-id': sessionId,
|
|
264
|
-
},
|
|
265
|
-
body: JSON.stringify({
|
|
266
|
-
jsonrpc: '2.0',
|
|
267
|
-
id: 1,
|
|
268
|
-
method: 'tools/list'
|
|
269
|
-
})
|
|
270
|
-
});
|
|
271
|
-
assert.strictEqual(response.status, 404, 'Should return 404 Session Not Found');
|
|
272
|
-
assert.deepStrictEqual(await response.json(), { error: 'Session not found' });
|
|
273
|
-
console.log(' ✓ Expired session returned 404 so the client can re-initialize');
|
|
274
|
-
await clientWithAuth.disconnect();
|
|
275
|
-
});
|
|
276
|
-
});
|
|
277
|
-
});
|
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Remote Authorization Tests
|
|
3
|
-
* Tests for per-session HTTP header-based authorization
|
|
4
|
-
*/
|
|
5
|
-
import { describe, test, after, before } from 'node:test';
|
|
6
|
-
import assert from 'node:assert';
|
|
7
|
-
import { launchServer, findAvailablePort, cleanupServers, TransportMode, checkHealthEndpoint, HOST } from './utils/server-launcher.js';
|
|
8
|
-
console.log('🔐 Remote Authorization Tests');
|
|
9
|
-
console.log('');
|
|
10
|
-
// Configuration check
|
|
11
|
-
const GITLAB_API_URL = process.env.GITLAB_API_URL || "https://gitlab.com";
|
|
12
|
-
const GITLAB_TOKEN = process.env.GITLAB_TOKEN_TEST || process.env.GITLAB_TOKEN;
|
|
13
|
-
const TEST_PROJECT_ID = process.env.TEST_PROJECT_ID;
|
|
14
|
-
console.log('🔧 Test Configuration:');
|
|
15
|
-
console.log(` GitLab URL: ${GITLAB_API_URL}`);
|
|
16
|
-
console.log(` Token: ${GITLAB_TOKEN ? '✅ Provided' : '❌ Missing'}`);
|
|
17
|
-
console.log(` Project ID: ${TEST_PROJECT_ID || '❌ Missing'}`);
|
|
18
|
-
// Validate required configuration
|
|
19
|
-
if (!GITLAB_TOKEN) {
|
|
20
|
-
console.error('❌ Error: GITLAB_TOKEN_TEST or GITLAB_TOKEN environment variable is required for testing');
|
|
21
|
-
console.error(' Set one of these variables to your GitLab API token');
|
|
22
|
-
process.exit(1);
|
|
23
|
-
}
|
|
24
|
-
if (!TEST_PROJECT_ID) {
|
|
25
|
-
console.error('❌ Error: TEST_PROJECT_ID environment variable is required for testing');
|
|
26
|
-
console.error(' Set this variable to a valid GitLab project ID (e.g., "123" or "group/project")');
|
|
27
|
-
process.exit(1);
|
|
28
|
-
}
|
|
29
|
-
console.log('✅ Configuration validated');
|
|
30
|
-
console.log('');
|
|
31
|
-
let servers = [];
|
|
32
|
-
// Cleanup function for all tests
|
|
33
|
-
const cleanup = () => {
|
|
34
|
-
cleanupServers(servers);
|
|
35
|
-
servers = [];
|
|
36
|
-
};
|
|
37
|
-
// Handle process termination
|
|
38
|
-
process.on('SIGINT', cleanup);
|
|
39
|
-
process.on('SIGTERM', cleanup);
|
|
40
|
-
process.on('exit', cleanup);
|
|
41
|
-
/**
|
|
42
|
-
* Helper to send MCP request with custom headers
|
|
43
|
-
*/
|
|
44
|
-
async function sendMCPRequest(url, method, headers = {}) {
|
|
45
|
-
const response = await fetch(url, {
|
|
46
|
-
method: 'POST',
|
|
47
|
-
headers: {
|
|
48
|
-
'Content-Type': 'application/json',
|
|
49
|
-
...headers,
|
|
50
|
-
},
|
|
51
|
-
body: JSON.stringify(method),
|
|
52
|
-
});
|
|
53
|
-
if (!response.ok) {
|
|
54
|
-
const text = await response.text();
|
|
55
|
-
throw new Error(`HTTP ${response.status}: ${text}`);
|
|
56
|
-
}
|
|
57
|
-
return response.json();
|
|
58
|
-
}
|
|
59
|
-
describe('Remote Authorization - Streamable HTTP with Authorization header', () => {
|
|
60
|
-
let server;
|
|
61
|
-
let port;
|
|
62
|
-
let mcpUrl;
|
|
63
|
-
before(async () => {
|
|
64
|
-
port = await findAvailablePort();
|
|
65
|
-
server = await launchServer({
|
|
66
|
-
mode: TransportMode.STREAMABLE_HTTP,
|
|
67
|
-
port,
|
|
68
|
-
timeout: 3000,
|
|
69
|
-
env: {
|
|
70
|
-
STREAMABLE_HTTP: 'true',
|
|
71
|
-
REMOTE_AUTHORIZATION: 'true',
|
|
72
|
-
GITLAB_API_URL: `${GITLAB_API_URL}/api/v4`,
|
|
73
|
-
GITLAB_PROJECT_ID: TEST_PROJECT_ID,
|
|
74
|
-
GITLAB_READ_ONLY_MODE: 'true',
|
|
75
|
-
// Explicitly no GITLAB_PERSONAL_ACCESS_TOKEN
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
servers.push(server);
|
|
79
|
-
mcpUrl = `http://${HOST}:${port}/mcp`;
|
|
80
|
-
// Verify server started successfully
|
|
81
|
-
assert.ok(server.process.pid !== undefined, 'Server process should have PID');
|
|
82
|
-
// Verify health check
|
|
83
|
-
if (server.port) {
|
|
84
|
-
const health = await checkHealthEndpoint(server.port);
|
|
85
|
-
assert.strictEqual(health.status, 'healthy', 'Health status should be healthy');
|
|
86
|
-
}
|
|
87
|
-
console.log('Server started with remote authorization enabled');
|
|
88
|
-
});
|
|
89
|
-
after(async () => {
|
|
90
|
-
cleanup();
|
|
91
|
-
console.log('Server stopped');
|
|
92
|
-
});
|
|
93
|
-
test('should reject request without Authorization header', async () => {
|
|
94
|
-
const initRequest = {
|
|
95
|
-
jsonrpc: '2.0',
|
|
96
|
-
id: 1,
|
|
97
|
-
method: 'initialize',
|
|
98
|
-
params: {
|
|
99
|
-
protocolVersion: '2024-11-05',
|
|
100
|
-
capabilities: {},
|
|
101
|
-
clientInfo: { name: 'test-client', version: '1.0.0' }
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
try {
|
|
105
|
-
await sendMCPRequest(mcpUrl, initRequest, {
|
|
106
|
-
'mcp-session-id': 'test-session-no-auth'
|
|
107
|
-
});
|
|
108
|
-
assert.fail('Should have rejected request without auth header');
|
|
109
|
-
}
|
|
110
|
-
catch (error) {
|
|
111
|
-
assert.ok(error instanceof Error);
|
|
112
|
-
assert.ok(error.message.includes('401'), 'Should get 401 Unauthorized');
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
test('should accept request with Authorization Bearer header', async () => {
|
|
116
|
-
const sessionId = `test-session-bearer-${Date.now()}`;
|
|
117
|
-
const initRequest = {
|
|
118
|
-
jsonrpc: '2.0',
|
|
119
|
-
id: 1,
|
|
120
|
-
method: 'initialize',
|
|
121
|
-
params: {
|
|
122
|
-
protocolVersion: '2024-11-05',
|
|
123
|
-
capabilities: {},
|
|
124
|
-
clientInfo: { name: 'test-client', version: '1.0.0' }
|
|
125
|
-
}
|
|
126
|
-
};
|
|
127
|
-
const response = await sendMCPRequest(mcpUrl, initRequest, {
|
|
128
|
-
'mcp-session-id': sessionId,
|
|
129
|
-
'authorization': `Bearer ${GITLAB_TOKEN}`
|
|
130
|
-
});
|
|
131
|
-
assert.ok(response, 'Should get response');
|
|
132
|
-
assert.ok(response.result, 'Should have result');
|
|
133
|
-
assert.strictEqual(response.result.protocolVersion, '2024-11-05', 'Protocol version should match');
|
|
134
|
-
});
|
|
135
|
-
test('should reuse auth from first request in subsequent requests', async () => {
|
|
136
|
-
const sessionId = `test-session-reuse-${Date.now()}`;
|
|
137
|
-
// First request with auth
|
|
138
|
-
const initRequest = {
|
|
139
|
-
jsonrpc: '2.0',
|
|
140
|
-
id: 1,
|
|
141
|
-
method: 'initialize',
|
|
142
|
-
params: {
|
|
143
|
-
protocolVersion: '2024-11-05',
|
|
144
|
-
capabilities: {},
|
|
145
|
-
clientInfo: { name: 'test-client', version: '1.0.0' }
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
const initResponse = await sendMCPRequest(mcpUrl, initRequest, {
|
|
149
|
-
'mcp-session-id': sessionId,
|
|
150
|
-
'authorization': `Bearer ${GITLAB_TOKEN}`
|
|
151
|
-
});
|
|
152
|
-
assert.ok(initResponse.result, 'Init should succeed');
|
|
153
|
-
// Second request without auth header (should reuse)
|
|
154
|
-
const listToolsRequest = {
|
|
155
|
-
jsonrpc: '2.0',
|
|
156
|
-
id: 2,
|
|
157
|
-
method: 'tools/list',
|
|
158
|
-
params: {}
|
|
159
|
-
};
|
|
160
|
-
const listResponse = await sendMCPRequest(mcpUrl, listToolsRequest, {
|
|
161
|
-
'mcp-session-id': sessionId
|
|
162
|
-
// No authorization header
|
|
163
|
-
});
|
|
164
|
-
assert.ok(listResponse.result, 'List tools should succeed with reused auth');
|
|
165
|
-
assert.ok(Array.isArray(listResponse.result.tools), 'Should return tools array');
|
|
166
|
-
assert.ok(listResponse.result.tools.length > 0, 'Should have at least one tool');
|
|
167
|
-
});
|
|
168
|
-
test('should call tool with Bearer token', async () => {
|
|
169
|
-
const sessionId = `test-session-tool-${Date.now()}`;
|
|
170
|
-
// Initialize
|
|
171
|
-
const initRequest = {
|
|
172
|
-
jsonrpc: '2.0',
|
|
173
|
-
id: 1,
|
|
174
|
-
method: 'initialize',
|
|
175
|
-
params: {
|
|
176
|
-
protocolVersion: '2024-11-05',
|
|
177
|
-
capabilities: {},
|
|
178
|
-
clientInfo: { name: 'test-client', version: '1.0.0' }
|
|
179
|
-
}
|
|
180
|
-
};
|
|
181
|
-
await sendMCPRequest(mcpUrl, initRequest, {
|
|
182
|
-
'mcp-session-id': sessionId,
|
|
183
|
-
'authorization': `Bearer ${GITLAB_TOKEN}`
|
|
184
|
-
});
|
|
185
|
-
// Call tool
|
|
186
|
-
const callToolRequest = {
|
|
187
|
-
jsonrpc: '2.0',
|
|
188
|
-
id: 2,
|
|
189
|
-
method: 'tools/call',
|
|
190
|
-
params: {
|
|
191
|
-
name: 'get_project',
|
|
192
|
-
arguments: {
|
|
193
|
-
project_id: TEST_PROJECT_ID
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
};
|
|
197
|
-
const toolResponse = await sendMCPRequest(mcpUrl, callToolRequest, {
|
|
198
|
-
'mcp-session-id': sessionId
|
|
199
|
-
});
|
|
200
|
-
assert.ok(toolResponse.result, 'Tool call should succeed');
|
|
201
|
-
assert.ok(toolResponse.result.content, 'Should have content');
|
|
202
|
-
assert.ok(Array.isArray(toolResponse.result.content), 'Content should be array');
|
|
203
|
-
assert.ok(toolResponse.result.content.length > 0, 'Should have content items');
|
|
204
|
-
const projectData = JSON.parse(toolResponse.result.content[0].text);
|
|
205
|
-
assert.ok(projectData.id, 'Should have project id');
|
|
206
|
-
assert.ok(projectData.name, 'Should have project name');
|
|
207
|
-
});
|
|
208
|
-
});
|
|
209
|
-
describe('Remote Authorization - Streamable HTTP with Private-Token header', () => {
|
|
210
|
-
let server;
|
|
211
|
-
let port;
|
|
212
|
-
let mcpUrl;
|
|
213
|
-
before(async () => {
|
|
214
|
-
port = await findAvailablePort();
|
|
215
|
-
server = await launchServer({
|
|
216
|
-
mode: TransportMode.STREAMABLE_HTTP,
|
|
217
|
-
port,
|
|
218
|
-
timeout: 3000,
|
|
219
|
-
env: {
|
|
220
|
-
STREAMABLE_HTTP: 'true',
|
|
221
|
-
REMOTE_AUTHORIZATION: 'true',
|
|
222
|
-
GITLAB_API_URL: `${GITLAB_API_URL}/api/v4`,
|
|
223
|
-
GITLAB_PROJECT_ID: TEST_PROJECT_ID,
|
|
224
|
-
GITLAB_READ_ONLY_MODE: 'true',
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
servers.push(server);
|
|
228
|
-
mcpUrl = `http://${HOST}:${port}/mcp`;
|
|
229
|
-
console.log('Server started for Private-Token tests');
|
|
230
|
-
});
|
|
231
|
-
after(async () => {
|
|
232
|
-
cleanup();
|
|
233
|
-
console.log('Server stopped');
|
|
234
|
-
});
|
|
235
|
-
test('should accept request with Private-Token header', async () => {
|
|
236
|
-
const sessionId = `test-session-private-${Date.now()}`;
|
|
237
|
-
const initRequest = {
|
|
238
|
-
jsonrpc: '2.0',
|
|
239
|
-
id: 1,
|
|
240
|
-
method: 'initialize',
|
|
241
|
-
params: {
|
|
242
|
-
protocolVersion: '2024-11-05',
|
|
243
|
-
capabilities: {},
|
|
244
|
-
clientInfo: { name: 'test-client', version: '1.0.0' }
|
|
245
|
-
}
|
|
246
|
-
};
|
|
247
|
-
const response = await sendMCPRequest(mcpUrl, initRequest, {
|
|
248
|
-
'mcp-session-id': sessionId,
|
|
249
|
-
'private-token': GITLAB_TOKEN
|
|
250
|
-
});
|
|
251
|
-
assert.ok(response.result, 'Should succeed with Private-Token');
|
|
252
|
-
assert.strictEqual(response.result.protocolVersion, '2024-11-05', 'Protocol version should match');
|
|
253
|
-
});
|
|
254
|
-
test('should call tool with Private-Token', async () => {
|
|
255
|
-
const sessionId = `test-session-private-tool-${Date.now()}`;
|
|
256
|
-
// Initialize
|
|
257
|
-
const initRequest = {
|
|
258
|
-
jsonrpc: '2.0',
|
|
259
|
-
id: 1,
|
|
260
|
-
method: 'initialize',
|
|
261
|
-
params: {
|
|
262
|
-
protocolVersion: '2024-11-05',
|
|
263
|
-
capabilities: {},
|
|
264
|
-
clientInfo: { name: 'test-client', version: '1.0.0' }
|
|
265
|
-
}
|
|
266
|
-
};
|
|
267
|
-
await sendMCPRequest(mcpUrl, initRequest, {
|
|
268
|
-
'mcp-session-id': sessionId,
|
|
269
|
-
'private-token': GITLAB_TOKEN
|
|
270
|
-
});
|
|
271
|
-
// Call tool
|
|
272
|
-
const callToolRequest = {
|
|
273
|
-
jsonrpc: '2.0',
|
|
274
|
-
id: 2,
|
|
275
|
-
method: 'tools/call',
|
|
276
|
-
params: {
|
|
277
|
-
name: 'list_merge_requests',
|
|
278
|
-
arguments: {
|
|
279
|
-
project_id: TEST_PROJECT_ID
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
};
|
|
283
|
-
const toolResponse = await sendMCPRequest(mcpUrl, callToolRequest, {
|
|
284
|
-
'mcp-session-id': sessionId
|
|
285
|
-
});
|
|
286
|
-
assert.ok(toolResponse.result, 'Tool call should succeed with Private-Token');
|
|
287
|
-
assert.ok(toolResponse.result.content, 'Should have content');
|
|
288
|
-
});
|
|
289
|
-
});
|
|
290
|
-
describe('Remote Authorization - SSE mode should be disabled', () => {
|
|
291
|
-
test('should fail to start with SSE and REMOTE_AUTHORIZATION', async () => {
|
|
292
|
-
const port = await findAvailablePort();
|
|
293
|
-
try {
|
|
294
|
-
const server = await launchServer({
|
|
295
|
-
mode: TransportMode.SSE,
|
|
296
|
-
port,
|
|
297
|
-
timeout: 3000,
|
|
298
|
-
env: {
|
|
299
|
-
SSE: 'true',
|
|
300
|
-
REMOTE_AUTHORIZATION: 'true',
|
|
301
|
-
GITLAB_API_URL: `${GITLAB_API_URL}/api/v4`,
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
// If we get here, the server started when it shouldn't have
|
|
305
|
-
servers.push(server);
|
|
306
|
-
assert.fail('Server should not start with SSE and REMOTE_AUTHORIZATION=true');
|
|
307
|
-
}
|
|
308
|
-
catch (error) {
|
|
309
|
-
// Expected: server should fail to start
|
|
310
|
-
assert.ok(error instanceof Error, 'Should throw an error');
|
|
311
|
-
console.log('✅ Server correctly rejected SSE mode with remote authorization');
|
|
312
|
-
}
|
|
313
|
-
});
|
|
314
|
-
});
|