-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathdiscover.test.ts
More file actions
406 lines (360 loc) · 11.3 KB
/
discover.test.ts
File metadata and controls
406 lines (360 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
import { mkdtemp, rm, mkdir, writeFile } from "fs/promises"
import { tmpdir } from "os"
import path from "path"
import { discoverExternalMcp } from "../../src/mcp/discover"
let tempDir: string
beforeEach(async () => {
tempDir = await mkdtemp(path.join(tmpdir(), "mcp-discover-"))
})
afterEach(async () => {
await rm(tempDir, { recursive: true, force: true })
})
describe("discoverExternalMcp", () => {
test("parses .vscode/mcp.json with servers key", async () => {
await mkdir(path.join(tempDir, ".vscode"), { recursive: true })
await writeFile(
path.join(tempDir, ".vscode/mcp.json"),
JSON.stringify({
servers: {
"my-server": {
command: "node",
args: ["server.js"],
env: { API_KEY: "test" },
},
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["my-server"]).toMatchObject({
type: "local",
command: ["node", "server.js"],
environment: { API_KEY: "test" },
})
})
test("parses .github/copilot/mcp.json with mcpServers key", async () => {
await mkdir(path.join(tempDir, ".github/copilot"), { recursive: true })
await writeFile(
path.join(tempDir, ".github/copilot/mcp.json"),
JSON.stringify({
mcpServers: {
copilot: {
command: "python",
args: ["-m", "mcp_server"],
},
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["copilot"]).toMatchObject({
type: "local",
command: ["python", "-m", "mcp_server"],
})
})
test("parses .mcp.json (Claude Code) with mcpServers key", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
claude: {
command: "npx",
args: ["-y", "@anthropic/mcp-server"],
},
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["claude"]).toMatchObject({
type: "local",
command: ["npx", "-y", "@anthropic/mcp-server"],
})
})
test("parses .gemini/settings.json with mcpServers key", async () => {
await mkdir(path.join(tempDir, ".gemini"), { recursive: true })
await writeFile(
path.join(tempDir, ".gemini/settings.json"),
JSON.stringify({
mcpServers: {
gemini: {
command: "deno",
args: ["run", "server.ts"],
env: { PORT: "3000" },
},
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["gemini"]).toMatchObject({
type: "local",
command: ["deno", "run", "server.ts"],
environment: { PORT: "3000" },
})
})
test("command + args → command array", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
test: { command: "node", args: ["a", "b", "c"] },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["test"]).toMatchObject({
type: "local",
command: ["node", "a", "b", "c"],
})
})
test("command only → single-element array", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
simple: { command: "my-mcp-server" },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["simple"]).toMatchObject({
type: "local",
command: ["my-mcp-server"],
})
})
test("command as array is handled", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
arrayed: { command: ["node", "server.js"] },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["arrayed"]).toMatchObject({
type: "local",
command: ["node", "server.js"],
})
})
test("remote: url → Config.McpRemote", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
remote: { url: "https://example.com/mcp" },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["remote"]).toMatchObject({
type: "remote",
url: "https://example.com/mcp",
})
})
test("remote: url with headers", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
remote: {
url: "https://example.com/mcp",
headers: { Authorization: "Bearer token" },
},
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["remote"]).toMatchObject({
type: "remote",
url: "https://example.com/mcp",
headers: { Authorization: "Bearer token" },
})
})
test("env → environment rename", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
test: {
command: "node",
args: ["server.js"],
env: { FOO: "bar", BAZ: "qux" },
},
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["test"]!.type).toBe("local")
const local = result["test"] as { type: "local"; command: string[]; environment?: Record<string, string> }
expect(local.environment).toEqual({ FOO: "bar", BAZ: "qux" })
})
test("missing files → returns empty object", async () => {
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result).toEqual({})
})
test("malformed JSON → returns empty object", async () => {
await writeFile(path.join(tempDir, ".mcp.json"), "{ invalid json !!!")
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result).toEqual({})
})
test("duplicate names: first source wins (.vscode > .github > .mcp.json > .gemini)", async () => {
// Set up the same server name in multiple sources
await mkdir(path.join(tempDir, ".vscode"), { recursive: true })
await writeFile(
path.join(tempDir, ".vscode/mcp.json"),
JSON.stringify({
servers: {
shared: { command: "vscode-version" },
},
}),
)
await mkdir(path.join(tempDir, ".github/copilot"), { recursive: true })
await writeFile(
path.join(tempDir, ".github/copilot/mcp.json"),
JSON.stringify({
mcpServers: {
shared: { command: "copilot-version" },
},
}),
)
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
shared: { command: "claude-version" },
},
}),
)
await mkdir(path.join(tempDir, ".gemini"), { recursive: true })
await writeFile(
path.join(tempDir, ".gemini/settings.json"),
JSON.stringify({
mcpServers: {
shared: { command: "gemini-version" },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
// .vscode is first in priority order
expect(result["shared"]).toMatchObject({
type: "local",
command: ["vscode-version"],
})
})
test("entries without command or url are skipped", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
invalid: { description: "no command or url" },
valid: { command: "works" },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["invalid"]).toBeUndefined()
expect(result["valid"]).toBeDefined()
})
test("handles JSONC (comments in JSON)", async () => {
await mkdir(path.join(tempDir, ".vscode"), { recursive: true })
await writeFile(
path.join(tempDir, ".vscode/mcp.json"),
`{
// This is a comment
"servers": {
"commented": {
"command": "node",
"args": ["server.js"] // trailing comment
}
}
}`,
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["commented"]).toMatchObject({
type: "local",
command: ["node", "server.js"],
})
})
test("multiple sources contribute different servers", async () => {
await mkdir(path.join(tempDir, ".vscode"), { recursive: true })
await writeFile(
path.join(tempDir, ".vscode/mcp.json"),
JSON.stringify({
servers: {
alpha: { command: "alpha-cmd" },
},
}),
)
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
beta: { command: "beta-cmd" },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["alpha"]).toMatchObject({ type: "local", command: ["alpha-cmd"] })
expect(result["beta"]).toMatchObject({ type: "local", command: ["beta-cmd"] })
})
test("wrong key in file is ignored", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
servers: {
wrong: { command: "should-not-appear" },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result).toEqual({})
})
test("project-scoped servers are disabled by default for security", async () => {
await writeFile(
path.join(tempDir, ".mcp.json"),
JSON.stringify({
mcpServers: {
"project-server": { command: "test-cmd" },
},
}),
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["project-server"]).toBeDefined()
expect((result["project-server"] as any).enabled).toBe(false)
})
// altimate_change start — env-var interpolation in external MCP configs (commit f030bf8)
test("resolves ${VAR} env-var references in external config values", async () => {
const envKey = "__TEST_MCP_DISCOVER_CMD_" + Date.now()
process.env[envKey] = "my-custom-node"
try {
await mkdir(path.join(tempDir, ".vscode"), { recursive: true })
await writeFile(
path.join(tempDir, ".vscode/mcp.json"),
`{"servers": {"env-test": {"command": "\${${envKey}}", "args": ["serve"]}}}`,
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["env-test"]).toBeDefined()
expect(result["env-test"]).toMatchObject({
type: "local",
command: ["my-custom-node", "serve"],
})
} finally {
delete process.env[envKey]
}
})
test("resolves ${VAR:-default} with fallback when env var is unset", async () => {
const envKey = "__TEST_MCP_DISCOVER_UNSET_" + Date.now()
delete process.env[envKey]
await mkdir(path.join(tempDir, ".vscode"), { recursive: true })
await writeFile(
path.join(tempDir, ".vscode/mcp.json"),
`{"servers": {"default-test": {"command": "\${${envKey}:-fallback-cmd}", "args": ["run"]}}}`,
)
const { servers: result } = await discoverExternalMcp(tempDir)
expect(result["default-test"]).toBeDefined()
expect(result["default-test"]).toMatchObject({
type: "local",
command: ["fallback-cmd", "run"],
})
})
// altimate_change end
})