@zereight/mcp-gitlab 2.0.13 → 2.0.19
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.md +75 -9
- package/build/gitlab-client-pool.js +113 -0
- package/build/index.js +450 -356
- package/build/oauth.js +20 -11
- package/build/test/client-pool-test.js +109 -0
- package/build/test/dynamic-api-url-test.js +304 -0
- package/build/test/dynamic-routing-tests.js +442 -0
- package/build/test/multi-server-test.js +182 -0
- package/build/test/remote-auth-simple-test.js +2 -0
- package/build/test/utils/mock-gitlab-server.js +73 -4
- package/build/test/utils/server-launcher.js +25 -9
- package/package.json +8 -4
package/build/oauth.js
CHANGED
|
@@ -16,7 +16,7 @@ const pendingAuthRequests = new Map();
|
|
|
16
16
|
* Check if a port is already in use
|
|
17
17
|
*/
|
|
18
18
|
async function isPortInUse(port) {
|
|
19
|
-
return new Promise(
|
|
19
|
+
return new Promise(resolve => {
|
|
20
20
|
const server = net.createServer();
|
|
21
21
|
server.once("error", (err) => {
|
|
22
22
|
if (err.code === "EADDRINUSE") {
|
|
@@ -44,9 +44,9 @@ async function requestAuthFromExistingServer(port, requestId) {
|
|
|
44
44
|
path: `/auth-request?requestId=${requestId}`,
|
|
45
45
|
method: "GET",
|
|
46
46
|
};
|
|
47
|
-
const req = http.request(options,
|
|
47
|
+
const req = http.request(options, res => {
|
|
48
48
|
let data = "";
|
|
49
|
-
res.on("data",
|
|
49
|
+
res.on("data", chunk => {
|
|
50
50
|
data += chunk;
|
|
51
51
|
});
|
|
52
52
|
res.on("end", () => {
|
|
@@ -64,7 +64,7 @@ async function requestAuthFromExistingServer(port, requestId) {
|
|
|
64
64
|
}
|
|
65
65
|
});
|
|
66
66
|
});
|
|
67
|
-
req.on("error",
|
|
67
|
+
req.on("error", error => {
|
|
68
68
|
reject(new Error(`Failed to connect to existing OAuth server: ${error.message}`));
|
|
69
69
|
});
|
|
70
70
|
req.setTimeout(5 * 60 * 1000, () => {
|
|
@@ -82,8 +82,7 @@ export class GitLabOAuth {
|
|
|
82
82
|
constructor(config) {
|
|
83
83
|
this.config = config;
|
|
84
84
|
this.tokenStoragePath =
|
|
85
|
-
config.tokenStoragePath ||
|
|
86
|
-
path.join(process.env.HOME || "", ".gitlab-mcp-token.json");
|
|
85
|
+
config.tokenStoragePath || path.join(process.env.HOME || "", ".gitlab-mcp-token.json");
|
|
87
86
|
}
|
|
88
87
|
/**
|
|
89
88
|
* Get the authorization URL for OAuth flow
|
|
@@ -119,6 +118,10 @@ export class GitLabOAuth {
|
|
|
119
118
|
redirect_uri: this.config.redirectUri,
|
|
120
119
|
code_verifier: this.codeVerifier,
|
|
121
120
|
});
|
|
121
|
+
// Add client_secret for Confidential applications
|
|
122
|
+
if (this.config.clientSecret) {
|
|
123
|
+
params.append("client_secret", this.config.clientSecret);
|
|
124
|
+
}
|
|
122
125
|
const response = await fetch(tokenUrl, {
|
|
123
126
|
method: "POST",
|
|
124
127
|
headers: {
|
|
@@ -130,7 +133,7 @@ export class GitLabOAuth {
|
|
|
130
133
|
const errorText = await response.text();
|
|
131
134
|
throw new Error(`Token exchange failed: ${response.status} ${errorText}`);
|
|
132
135
|
}
|
|
133
|
-
const data = await response.json();
|
|
136
|
+
const data = (await response.json());
|
|
134
137
|
return {
|
|
135
138
|
access_token: data.access_token,
|
|
136
139
|
refresh_token: data.refresh_token,
|
|
@@ -150,6 +153,10 @@ export class GitLabOAuth {
|
|
|
150
153
|
grant_type: "refresh_token",
|
|
151
154
|
redirect_uri: this.config.redirectUri,
|
|
152
155
|
});
|
|
156
|
+
// Add client_secret for Confidential applications
|
|
157
|
+
if (this.config.clientSecret) {
|
|
158
|
+
params.append("client_secret", this.config.clientSecret);
|
|
159
|
+
}
|
|
153
160
|
const response = await fetch(tokenUrl, {
|
|
154
161
|
method: "POST",
|
|
155
162
|
headers: {
|
|
@@ -161,7 +168,7 @@ export class GitLabOAuth {
|
|
|
161
168
|
const errorText = await response.text();
|
|
162
169
|
throw new Error(`Token refresh failed: ${response.status} ${errorText}`);
|
|
163
170
|
}
|
|
164
|
-
const data = await response.json();
|
|
171
|
+
const data = (await response.json());
|
|
165
172
|
return {
|
|
166
173
|
access_token: data.access_token,
|
|
167
174
|
refresh_token: data.refresh_token || refreshToken,
|
|
@@ -271,7 +278,7 @@ export class GitLabOAuth {
|
|
|
271
278
|
const authUrl = await this.getAuthorizationUrl(newState);
|
|
272
279
|
logger.info("Opening browser for new authentication request...");
|
|
273
280
|
logger.info(`If browser doesn't open, visit: ${authUrl}`);
|
|
274
|
-
open(authUrl).catch(
|
|
281
|
+
open(authUrl).catch(err => {
|
|
275
282
|
logger.error("Failed to open browser:", err);
|
|
276
283
|
logger.info(`Please manually open: ${authUrl}`);
|
|
277
284
|
});
|
|
@@ -427,12 +434,12 @@ export class GitLabOAuth {
|
|
|
427
434
|
const authUrl = await this.getAuthorizationUrl(state);
|
|
428
435
|
logger.info("Opening browser for authentication...");
|
|
429
436
|
logger.info(`If browser doesn't open, visit: ${authUrl}`);
|
|
430
|
-
open(authUrl).catch(
|
|
437
|
+
open(authUrl).catch(err => {
|
|
431
438
|
logger.error("Failed to open browser:", err);
|
|
432
439
|
logger.info(`Please manually open: ${authUrl}`);
|
|
433
440
|
});
|
|
434
441
|
});
|
|
435
|
-
server.on("error",
|
|
442
|
+
server.on("error", error => {
|
|
436
443
|
logger.error("OAuth server error:", error);
|
|
437
444
|
const pending = pendingAuthRequests.get(initialRequestId);
|
|
438
445
|
if (pending) {
|
|
@@ -502,6 +509,7 @@ export class GitLabOAuth {
|
|
|
502
509
|
*/
|
|
503
510
|
export async function initializeOAuth(gitlabUrl = "https://gitlab.com") {
|
|
504
511
|
const clientId = process.env.GITLAB_OAUTH_CLIENT_ID;
|
|
512
|
+
const clientSecret = process.env.GITLAB_OAUTH_CLIENT_SECRET;
|
|
505
513
|
const redirectUri = process.env.GITLAB_OAUTH_REDIRECT_URI || "http://127.0.0.1:8888/callback";
|
|
506
514
|
const tokenStoragePath = process.env.GITLAB_OAUTH_TOKEN_PATH;
|
|
507
515
|
if (!clientId) {
|
|
@@ -509,6 +517,7 @@ export async function initializeOAuth(gitlabUrl = "https://gitlab.com") {
|
|
|
509
517
|
}
|
|
510
518
|
const oauth = new GitLabOAuth({
|
|
511
519
|
clientId,
|
|
520
|
+
clientSecret,
|
|
512
521
|
redirectUri,
|
|
513
522
|
gitlabUrl,
|
|
514
523
|
scopes: ["api"],
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client Pool Test Suite
|
|
3
|
+
* Tests connection pooling limits and functionality
|
|
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 POOL_MAX_SIZE = 2;
|
|
13
|
+
// Port ranges
|
|
14
|
+
const MOCK_GITLAB_PORT_BASE = 9500;
|
|
15
|
+
const MCP_SERVER_PORT_BASE = 3500;
|
|
16
|
+
console.log('🏊 Client Pool Test Suite');
|
|
17
|
+
console.log('');
|
|
18
|
+
describe('Client Pool Limits', () => {
|
|
19
|
+
let mcpUrl;
|
|
20
|
+
let mockGitLab;
|
|
21
|
+
let servers = [];
|
|
22
|
+
let mockGitLabUrl;
|
|
23
|
+
before(async () => {
|
|
24
|
+
// Start mock GitLab server
|
|
25
|
+
const mockPort = await findMockServerPort(MOCK_GITLAB_PORT_BASE);
|
|
26
|
+
mockGitLab = new MockGitLabServer({
|
|
27
|
+
port: mockPort,
|
|
28
|
+
validTokens: [MOCK_TOKEN]
|
|
29
|
+
});
|
|
30
|
+
await mockGitLab.start();
|
|
31
|
+
mockGitLabUrl = mockGitLab.getUrl();
|
|
32
|
+
// Start MCP server with pool limit
|
|
33
|
+
const mcpPort = await findAvailablePort(MCP_SERVER_PORT_BASE);
|
|
34
|
+
const server = await launchServer({
|
|
35
|
+
mode: TransportMode.STREAMABLE_HTTP,
|
|
36
|
+
port: mcpPort,
|
|
37
|
+
timeout: 5000,
|
|
38
|
+
env: {
|
|
39
|
+
STREAMABLE_HTTP: 'true',
|
|
40
|
+
REMOTE_AUTHORIZATION: 'true',
|
|
41
|
+
ENABLE_DYNAMIC_API_URL: 'true', // Enable dynamic URLs to test pooling
|
|
42
|
+
GITLAB_API_URL: `${mockGitLabUrl}/api/v4`,
|
|
43
|
+
GITLAB_POOL_MAX_SIZE: String(POOL_MAX_SIZE),
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
servers.push(server);
|
|
47
|
+
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
|
|
48
|
+
console.log(`Mock GitLab: ${mockGitLabUrl}`);
|
|
49
|
+
console.log(`MCP Server: ${mcpUrl}`);
|
|
50
|
+
console.log(`Pool Max Size: ${POOL_MAX_SIZE}`);
|
|
51
|
+
});
|
|
52
|
+
after(async () => {
|
|
53
|
+
cleanupServers(servers);
|
|
54
|
+
if (mockGitLab) {
|
|
55
|
+
await mockGitLab.stop();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
test('should enforce pool size limit', async () => {
|
|
59
|
+
// The pool size is configured to 2.
|
|
60
|
+
// We will connect with 3 distinct URLs.
|
|
61
|
+
// The first 2 should succeed (or fail with network error), adding to the pool.
|
|
62
|
+
// The 3rd should fail with "Pool is full".
|
|
63
|
+
// URL 1: 127.0.0.1 (Real mock server)
|
|
64
|
+
const url1 = `${mockGitLabUrl}/api/v4`;
|
|
65
|
+
const client1 = new CustomHeaderClient({
|
|
66
|
+
'authorization': `Bearer ${MOCK_TOKEN}`,
|
|
67
|
+
'x-gitlab-api-url': url1
|
|
68
|
+
});
|
|
69
|
+
await client1.connect(mcpUrl);
|
|
70
|
+
await client1.callTool('list_projects', { per_page: 1 });
|
|
71
|
+
console.log(' ✓ Request 1 (127.0.0.1) succeeded');
|
|
72
|
+
await client1.disconnect();
|
|
73
|
+
// URL 2: localhost (Real mock server, distinct string)
|
|
74
|
+
const url2 = url1.replace('127.0.0.1', 'localhost');
|
|
75
|
+
const client2 = new CustomHeaderClient({
|
|
76
|
+
'authorization': `Bearer ${MOCK_TOKEN}`,
|
|
77
|
+
'x-gitlab-api-url': url2
|
|
78
|
+
});
|
|
79
|
+
await client2.connect(mcpUrl);
|
|
80
|
+
try {
|
|
81
|
+
await client2.callTool('list_projects', { per_page: 1 });
|
|
82
|
+
console.log(' ✓ Request 2 (localhost) succeeded');
|
|
83
|
+
}
|
|
84
|
+
catch (e) {
|
|
85
|
+
// It might fail if localhost isn't listening or something, but it shouldn't be pool full
|
|
86
|
+
console.log(' ! Request 2 failed:', e);
|
|
87
|
+
if (String(e).includes('capacity reached')) {
|
|
88
|
+
assert.fail('Request 2 failed with pool limit error prematurely');
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
await client2.disconnect();
|
|
92
|
+
// URL 3: gitlab-3.example.com (Fake, distinct string)
|
|
93
|
+
// This should trigger the pool limit check
|
|
94
|
+
const client3 = new CustomHeaderClient({
|
|
95
|
+
'authorization': `Bearer ${MOCK_TOKEN}`,
|
|
96
|
+
'x-gitlab-api-url': 'https://gitlab-3.example.com/api/v4'
|
|
97
|
+
});
|
|
98
|
+
await client3.connect(mcpUrl);
|
|
99
|
+
try {
|
|
100
|
+
await client3.callTool('list_projects', { per_page: 1 });
|
|
101
|
+
assert.fail('Request 3 should have failed with pool limit error');
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
console.log(' ℹ️ Error received:', error.message);
|
|
105
|
+
assert.ok(error.message.includes('capacity reached') || error.message.includes('pool is full'), 'Error should be about server capacity');
|
|
106
|
+
}
|
|
107
|
+
await client3.disconnect();
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dynamic GitLab API URL Test Suite
|
|
3
|
+
* Tests the ability to connect to multiple GitLab instances via custom headers
|
|
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_1 = 'glpat-mock-token-instance-1';
|
|
12
|
+
const MOCK_TOKEN_2 = 'glpat-mock-token-instance-2';
|
|
13
|
+
// Port ranges
|
|
14
|
+
const MOCK_GITLAB_PORT_BASE_1 = 9100;
|
|
15
|
+
const MOCK_GITLAB_PORT_BASE_2 = 9200;
|
|
16
|
+
const MCP_SERVER_PORT_BASE = 3100;
|
|
17
|
+
console.log('🌐 Dynamic GitLab API URL Test Suite');
|
|
18
|
+
console.log('');
|
|
19
|
+
describe('Dynamic API URL - Multiple GitLab Instances', () => {
|
|
20
|
+
let mcpUrl;
|
|
21
|
+
let mockGitLab1;
|
|
22
|
+
let mockGitLab2;
|
|
23
|
+
let mockGitLabUrl1;
|
|
24
|
+
let mockGitLabUrl2;
|
|
25
|
+
let servers = [];
|
|
26
|
+
before(async () => {
|
|
27
|
+
// Start first mock GitLab server
|
|
28
|
+
const mockPort1 = await findMockServerPort(MOCK_GITLAB_PORT_BASE_1);
|
|
29
|
+
mockGitLab1 = new MockGitLabServer({
|
|
30
|
+
port: mockPort1,
|
|
31
|
+
validTokens: [MOCK_TOKEN_1]
|
|
32
|
+
});
|
|
33
|
+
await mockGitLab1.start();
|
|
34
|
+
mockGitLabUrl1 = mockGitLab1.getUrl();
|
|
35
|
+
// Start second mock GitLab server
|
|
36
|
+
const mockPort2 = await findMockServerPort(MOCK_GITLAB_PORT_BASE_2);
|
|
37
|
+
mockGitLab2 = new MockGitLabServer({
|
|
38
|
+
port: mockPort2,
|
|
39
|
+
validTokens: [MOCK_TOKEN_2]
|
|
40
|
+
});
|
|
41
|
+
await mockGitLab2.start();
|
|
42
|
+
mockGitLabUrl2 = mockGitLab2.getUrl();
|
|
43
|
+
// Start MCP server with dynamic API URL enabled
|
|
44
|
+
const mcpPort = await findAvailablePort(MCP_SERVER_PORT_BASE);
|
|
45
|
+
const server = await launchServer({
|
|
46
|
+
mode: TransportMode.STREAMABLE_HTTP,
|
|
47
|
+
port: mcpPort,
|
|
48
|
+
timeout: 5000,
|
|
49
|
+
env: {
|
|
50
|
+
STREAMABLE_HTTP: 'true',
|
|
51
|
+
REMOTE_AUTHORIZATION: 'true',
|
|
52
|
+
ENABLE_DYNAMIC_API_URL: 'true',
|
|
53
|
+
GITLAB_READ_ONLY_MODE: 'true',
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
servers.push(server);
|
|
57
|
+
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
|
|
58
|
+
console.log(`Mock GitLab Instance 1: ${mockGitLabUrl1}`);
|
|
59
|
+
console.log(`Mock GitLab Instance 2: ${mockGitLabUrl2}`);
|
|
60
|
+
console.log(`MCP Server: ${mcpUrl}`);
|
|
61
|
+
});
|
|
62
|
+
after(async () => {
|
|
63
|
+
cleanupServers(servers);
|
|
64
|
+
if (mockGitLab1) {
|
|
65
|
+
await mockGitLab1.stop();
|
|
66
|
+
}
|
|
67
|
+
if (mockGitLab2) {
|
|
68
|
+
await mockGitLab2.stop();
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
test('should connect to first GitLab instance with custom API URL', async () => {
|
|
72
|
+
const client = new CustomHeaderClient({
|
|
73
|
+
'authorization': `Bearer ${MOCK_TOKEN_1}`,
|
|
74
|
+
'x-gitlab-api-url': mockGitLabUrl1
|
|
75
|
+
});
|
|
76
|
+
await client.connect(mcpUrl);
|
|
77
|
+
const tools = await client.listTools();
|
|
78
|
+
assert.ok(tools.tools.length > 0, 'Should have tools');
|
|
79
|
+
console.log(` ✓ Connected to instance 1, got ${tools.tools.length} tools`);
|
|
80
|
+
await client.disconnect();
|
|
81
|
+
});
|
|
82
|
+
test('should connect to second GitLab instance with different API URL', async () => {
|
|
83
|
+
const client = new CustomHeaderClient({
|
|
84
|
+
'authorization': `Bearer ${MOCK_TOKEN_2}`,
|
|
85
|
+
'x-gitlab-api-url': mockGitLabUrl2
|
|
86
|
+
});
|
|
87
|
+
await client.connect(mcpUrl);
|
|
88
|
+
const tools = await client.listTools();
|
|
89
|
+
assert.ok(tools.tools.length > 0, 'Should have tools');
|
|
90
|
+
console.log(` ✓ Connected to instance 2, got ${tools.tools.length} tools`);
|
|
91
|
+
await client.disconnect();
|
|
92
|
+
});
|
|
93
|
+
test('should handle multiple concurrent connections to different instances', async () => {
|
|
94
|
+
const client1 = new CustomHeaderClient({
|
|
95
|
+
'authorization': `Bearer ${MOCK_TOKEN_1}`,
|
|
96
|
+
'x-gitlab-api-url': mockGitLabUrl1
|
|
97
|
+
});
|
|
98
|
+
const client2 = new CustomHeaderClient({
|
|
99
|
+
'authorization': `Bearer ${MOCK_TOKEN_2}`,
|
|
100
|
+
'x-gitlab-api-url': mockGitLabUrl2
|
|
101
|
+
});
|
|
102
|
+
// Connect both clients
|
|
103
|
+
await Promise.all([
|
|
104
|
+
client1.connect(mcpUrl),
|
|
105
|
+
client2.connect(mcpUrl)
|
|
106
|
+
]);
|
|
107
|
+
// List tools from both
|
|
108
|
+
const [tools1, tools2] = await Promise.all([
|
|
109
|
+
client1.listTools(),
|
|
110
|
+
client2.listTools()
|
|
111
|
+
]);
|
|
112
|
+
assert.ok(tools1.tools.length > 0, 'Instance 1 should have tools');
|
|
113
|
+
assert.ok(tools2.tools.length > 0, 'Instance 2 should have tools');
|
|
114
|
+
console.log(' ✓ Both instances accessible concurrently');
|
|
115
|
+
// Disconnect both
|
|
116
|
+
await Promise.all([
|
|
117
|
+
client1.disconnect(),
|
|
118
|
+
client2.disconnect()
|
|
119
|
+
]);
|
|
120
|
+
});
|
|
121
|
+
test('should reject invalid API URL format', async () => {
|
|
122
|
+
const client = new CustomHeaderClient({
|
|
123
|
+
'authorization': `Bearer ${MOCK_TOKEN_1}`,
|
|
124
|
+
'x-gitlab-api-url': 'not-a-valid-url'
|
|
125
|
+
});
|
|
126
|
+
try {
|
|
127
|
+
await client.connect(mcpUrl);
|
|
128
|
+
await client.listTools();
|
|
129
|
+
await client.disconnect();
|
|
130
|
+
assert.fail('Should have rejected invalid URL');
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
assert.ok(error instanceof Error);
|
|
134
|
+
console.log(' ✓ Correctly rejected invalid URL format');
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
test('should reject connection with wrong token for instance', async () => {
|
|
138
|
+
const client = new CustomHeaderClient({
|
|
139
|
+
'authorization': `Bearer ${MOCK_TOKEN_1}`, // Token for instance 1
|
|
140
|
+
'x-gitlab-api-url': mockGitLabUrl2 // But connecting to instance 2
|
|
141
|
+
});
|
|
142
|
+
try {
|
|
143
|
+
await client.connect(mcpUrl);
|
|
144
|
+
await client.listTools();
|
|
145
|
+
await client.disconnect();
|
|
146
|
+
assert.fail('Should have rejected wrong token');
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
assert.ok(error instanceof Error);
|
|
150
|
+
console.log(' ✓ Correctly rejected wrong token for instance');
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
test('should maintain separate sessions for different API URLs', async () => {
|
|
154
|
+
const client1 = new CustomHeaderClient({
|
|
155
|
+
'authorization': `Bearer ${MOCK_TOKEN_1}`,
|
|
156
|
+
'x-gitlab-api-url': mockGitLabUrl1
|
|
157
|
+
});
|
|
158
|
+
const client2 = new CustomHeaderClient({
|
|
159
|
+
'authorization': `Bearer ${MOCK_TOKEN_2}`,
|
|
160
|
+
'x-gitlab-api-url': mockGitLabUrl2
|
|
161
|
+
});
|
|
162
|
+
// Connect both
|
|
163
|
+
await client1.connect(mcpUrl);
|
|
164
|
+
await client2.connect(mcpUrl);
|
|
165
|
+
// Get session IDs
|
|
166
|
+
const sessionId1 = client1.getSessionId();
|
|
167
|
+
const sessionId2 = client2.getSessionId();
|
|
168
|
+
assert.ok(sessionId1, 'Session 1 should exist');
|
|
169
|
+
assert.ok(sessionId2, 'Session 2 should exist');
|
|
170
|
+
assert.notStrictEqual(sessionId1, sessionId2, 'Sessions should be different');
|
|
171
|
+
console.log(' ✓ Separate sessions maintained for different instances');
|
|
172
|
+
// Make requests from both sessions
|
|
173
|
+
const [tools1, tools2] = await Promise.all([
|
|
174
|
+
client1.listTools(),
|
|
175
|
+
client2.listTools()
|
|
176
|
+
]);
|
|
177
|
+
assert.ok(tools1.tools.length > 0, 'Instance 1 should work');
|
|
178
|
+
assert.ok(tools2.tools.length > 0, 'Instance 2 should work');
|
|
179
|
+
console.log(' ✓ Both sessions work independently');
|
|
180
|
+
await client1.disconnect();
|
|
181
|
+
await client2.disconnect();
|
|
182
|
+
});
|
|
183
|
+
test('should normalize API URLs correctly', async () => {
|
|
184
|
+
// Test with URL that needs normalization (no /api/v4)
|
|
185
|
+
const client = new CustomHeaderClient({
|
|
186
|
+
'authorization': `Bearer ${MOCK_TOKEN_1}`,
|
|
187
|
+
'x-gitlab-api-url': mockGitLabUrl1 // Without /api/v4
|
|
188
|
+
});
|
|
189
|
+
await client.connect(mcpUrl);
|
|
190
|
+
const tools = await client.listTools();
|
|
191
|
+
assert.ok(tools.tools.length > 0, 'Should work with normalized URL');
|
|
192
|
+
console.log(' ✓ URL normalization works correctly');
|
|
193
|
+
await client.disconnect();
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
describe('Dynamic API URL - Connection Pool', () => {
|
|
197
|
+
let mcpUrl;
|
|
198
|
+
let metricsUrl;
|
|
199
|
+
let mockGitLab1;
|
|
200
|
+
let mockGitLab2;
|
|
201
|
+
let mockGitLabUrl1;
|
|
202
|
+
let mockGitLabUrl2;
|
|
203
|
+
let servers = [];
|
|
204
|
+
before(async () => {
|
|
205
|
+
// Start mock GitLab servers
|
|
206
|
+
const mockPort1 = await findMockServerPort(MOCK_GITLAB_PORT_BASE_1 + 50);
|
|
207
|
+
mockGitLab1 = new MockGitLabServer({
|
|
208
|
+
port: mockPort1,
|
|
209
|
+
validTokens: [MOCK_TOKEN_1]
|
|
210
|
+
});
|
|
211
|
+
await mockGitLab1.start();
|
|
212
|
+
mockGitLabUrl1 = mockGitLab1.getUrl();
|
|
213
|
+
const mockPort2 = await findMockServerPort(MOCK_GITLAB_PORT_BASE_2 + 50);
|
|
214
|
+
mockGitLab2 = new MockGitLabServer({
|
|
215
|
+
port: mockPort2,
|
|
216
|
+
validTokens: [MOCK_TOKEN_2]
|
|
217
|
+
});
|
|
218
|
+
await mockGitLab2.start();
|
|
219
|
+
mockGitLabUrl2 = mockGitLab2.getUrl();
|
|
220
|
+
// Start MCP server
|
|
221
|
+
const mcpPort = await findAvailablePort(MCP_SERVER_PORT_BASE + 50);
|
|
222
|
+
const server = await launchServer({
|
|
223
|
+
mode: TransportMode.STREAMABLE_HTTP,
|
|
224
|
+
port: mcpPort,
|
|
225
|
+
timeout: 5000,
|
|
226
|
+
env: {
|
|
227
|
+
STREAMABLE_HTTP: 'true',
|
|
228
|
+
REMOTE_AUTHORIZATION: 'true',
|
|
229
|
+
ENABLE_DYNAMIC_API_URL: 'true',
|
|
230
|
+
GITLAB_CLIENT_POOL_SIZE: '5',
|
|
231
|
+
GITLAB_CLIENT_IDLE_TIMEOUT: '30000',
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
servers.push(server);
|
|
235
|
+
mcpUrl = `http://${HOST}:${mcpPort}/mcp`;
|
|
236
|
+
metricsUrl = `http://${HOST}:${mcpPort}/metrics`;
|
|
237
|
+
console.log(`MCP Server: ${mcpUrl}`);
|
|
238
|
+
console.log(`Metrics URL: ${metricsUrl}`);
|
|
239
|
+
});
|
|
240
|
+
after(async () => {
|
|
241
|
+
cleanupServers(servers);
|
|
242
|
+
if (mockGitLab1) {
|
|
243
|
+
await mockGitLab1.stop();
|
|
244
|
+
}
|
|
245
|
+
if (mockGitLab2) {
|
|
246
|
+
await mockGitLab2.stop();
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
test('should track pool statistics via metrics endpoint', async () => {
|
|
250
|
+
// Make some connections first
|
|
251
|
+
const client1 = new CustomHeaderClient({
|
|
252
|
+
'authorization': `Bearer ${MOCK_TOKEN_1}`,
|
|
253
|
+
'x-gitlab-api-url': mockGitLabUrl1
|
|
254
|
+
});
|
|
255
|
+
const client2 = new CustomHeaderClient({
|
|
256
|
+
'authorization': `Bearer ${MOCK_TOKEN_2}`,
|
|
257
|
+
'x-gitlab-api-url': mockGitLabUrl2
|
|
258
|
+
});
|
|
259
|
+
await client1.connect(mcpUrl);
|
|
260
|
+
await client2.connect(mcpUrl);
|
|
261
|
+
await client1.listTools();
|
|
262
|
+
await client2.listTools();
|
|
263
|
+
// Check metrics
|
|
264
|
+
const response = await fetch(metricsUrl);
|
|
265
|
+
assert.ok(response.ok, 'Metrics endpoint should be accessible');
|
|
266
|
+
const metrics = await response.json();
|
|
267
|
+
assert.ok(metrics.gitlabClientPool, 'Should have pool metrics');
|
|
268
|
+
assert.ok(typeof metrics.gitlabClientPool.size === 'number', 'Should have pool size');
|
|
269
|
+
assert.ok(typeof metrics.gitlabClientPool.maxSize === 'number', 'Should have max size');
|
|
270
|
+
console.log(' ✓ Pool metrics available');
|
|
271
|
+
console.log(` ℹ️ Pool size: ${metrics.gitlabClientPool.size}/${metrics.gitlabClientPool.maxSize}`);
|
|
272
|
+
await client1.disconnect();
|
|
273
|
+
await client2.disconnect();
|
|
274
|
+
});
|
|
275
|
+
test('should reuse connections for same API URL', async () => {
|
|
276
|
+
// Get initial metrics
|
|
277
|
+
const response1 = await fetch(metricsUrl);
|
|
278
|
+
const metrics1 = await response1.json();
|
|
279
|
+
const initialSize = metrics1.gitlabClientPool?.size || 0;
|
|
280
|
+
// Create multiple clients to same instance
|
|
281
|
+
const clients = [];
|
|
282
|
+
for (let i = 0; i < 3; i++) {
|
|
283
|
+
const client = new CustomHeaderClient({
|
|
284
|
+
'authorization': `Bearer ${MOCK_TOKEN_1}`,
|
|
285
|
+
'x-gitlab-api-url': mockGitLabUrl1
|
|
286
|
+
});
|
|
287
|
+
await client.connect(mcpUrl);
|
|
288
|
+
await client.listTools();
|
|
289
|
+
clients.push(client);
|
|
290
|
+
}
|
|
291
|
+
// Check metrics - should not have created 3 new pool entries
|
|
292
|
+
const response2 = await fetch(metricsUrl);
|
|
293
|
+
const metrics2 = await response2.json();
|
|
294
|
+
const finalSize = metrics2.gitlabClientPool?.size || 0;
|
|
295
|
+
// Pool size should increase by at most 1 (for the shared URL)
|
|
296
|
+
assert.ok(finalSize - initialSize <= 1, 'Should reuse connection for same URL');
|
|
297
|
+
console.log(' ✓ Connection reuse working');
|
|
298
|
+
console.log(` ℹ️ Pool size change: ${initialSize} → ${finalSize}`);
|
|
299
|
+
// Cleanup
|
|
300
|
+
for (const client of clients) {
|
|
301
|
+
await client.disconnect();
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
});
|