-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup-mcp-servers.mjs
More file actions
342 lines (296 loc) · 11 KB
/
setup-mcp-servers.mjs
File metadata and controls
342 lines (296 loc) · 11 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
#!/usr/bin/env node
// =============================================================================
// MCP Server Setup Script
// =============================================================================
// Installs and configures MCP (Model Context Protocol) servers globally
// for VS Code and VS Code Insiders on Windows and macOS.
//
// Usage: node setup-mcp-servers.mjs [--dry-run] [--skip-cache-warm]
//
// Options:
// --dry-run Show what would be done without making changes
// --skip-cache-warm Skip pre-warming the npx cache
//
// Cross-platform: Windows 10/11 and macOS
// =============================================================================
import { execSync } from "node:child_process";
import {
existsSync,
mkdirSync,
readFileSync,
writeFileSync,
} from "node:fs";
import { arch, homedir, platform } from "node:os";
import { join } from "node:path";
// ---------------------------------------------------------------------------
// Configuration
// ---------------------------------------------------------------------------
const DRY_RUN = process.argv.includes("--dry-run");
const SKIP_CACHE_WARM = process.argv.includes("--skip-cache-warm");
/** npm packages that are launched via `npx -y` (no global install needed). */
const NPX_PACKAGES = [
"@azure-devops/mcp@latest",
"@modelcontextprotocol/server-sequential-thinking",
"@modelcontextprotocol/server-memory@latest",
"mongodb-mcp-server@latest",
"mssql-mcp-server@latest",
];
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
const isWindows = platform() === "win32";
const isMac = platform() === "darwin";
const colors = {
reset: "\x1b[0m",
green: "\x1b[32m",
yellow: "\x1b[33m",
red: "\x1b[31m",
cyan: "\x1b[36m",
dim: "\x1b[2m",
bold: "\x1b[1m",
};
function log(message) {
console.log(`${colors.green} ✔${colors.reset} ${message}`);
}
function logStep(message) {
console.log(`\n${colors.cyan}${colors.bold}▸ ${message}${colors.reset}`);
}
function logWarn(message) {
console.log(`${colors.yellow} ⚠${colors.reset} ${message}`);
}
function logError(message) {
console.error(`${colors.red} ✖${colors.reset} ${message}`);
}
function logDry(message) {
console.log(`${colors.dim} [dry-run] ${message}${colors.reset}`);
}
// ---------------------------------------------------------------------------
// Path resolution (cross-platform)
// ---------------------------------------------------------------------------
function getVSCodeUserConfigDir(variant = "Code") {
const home = homedir();
if (isWindows) {
const appData = process.env.APPDATA || join(home, "AppData", "Roaming");
return join(appData, variant, "User");
}
if (isMac) {
return join(home, "Library", "Application Support", variant, "User");
}
// Linux fallback
return join(home, ".config", variant, "User");
}
// ---------------------------------------------------------------------------
// npx cache warming
// ---------------------------------------------------------------------------
function warmNpxCache() {
logStep("Pre-warming npx cache (downloading packages for faster first use)");
for (const packageName of NPX_PACKAGES) {
if (DRY_RUN) {
logDry(`Would pre-cache: ${packageName}`);
continue;
}
try {
log(`Caching ${packageName}...`);
// Use npx with --yes to download and cache the package.
// We call it with --help or a version flag that exits quickly.
execSync(`npx -y ${packageName} --help`, {
stdio: "pipe",
timeout: 120_000,
env: { ...process.env, NODE_NO_WARNINGS: "1" },
});
log(`Cached ${packageName}`);
} catch {
// Some packages don't support --help, that's OK – the download still happened
log(`Cached ${packageName} (downloaded)`);
}
}
}
// ---------------------------------------------------------------------------
// mcp.json generation & merge
// ---------------------------------------------------------------------------
function buildMcpConfig() {
return {
inputs: [
{
id: "ado_org",
type: "promptString",
description: "Azure DevOps organization name (e.g. 'myorg')",
},
],
servers: {
// ── Azure DevOps ──────────────────────────────────────────────
"azure-devops": {
type: "stdio",
command: "npx",
args: ["-y", "@azure-devops/mcp@latest", "${input:ado_org}"],
},
// ── Sequential Thinking ───────────────────────────────────────
"sequential-thinking": {
type: "stdio",
command: "npx",
args: ["-y", "@modelcontextprotocol/server-sequential-thinking"],
},
// ── Microsoft Learn Docs (remote HTTP – no install needed!) ──
"microsoft-learn": {
type: "http",
url: "https://learn.microsoft.com/api/mcp",
},
// ── Knowledge Graph Memory ────────────────────────────────────
memory: {
type: "stdio",
command: "npx",
args: ["-y", "@modelcontextprotocol/server-memory@latest"],
},
// ── MongoDB ───────────────────────────────────────────────────
mongodb: {
type: "stdio",
command: "npx",
args: [
"-y",
"mongodb-mcp-server@latest",
"--connectionString",
"${env:MDB_MCP_CONNECTION_STRING}",
],
},
// ── MSSQL ─────────────────────────────────────────────────────
mssql: {
type: "stdio",
command: "npx",
args: [
"-y",
"mssql-mcp-server@latest",
"${env:MSSQL_MCP_CONNECTION_STRING}",
],
env: {
MSSQL_CONNECTION_STRING: "${env:MSSQL_MCP_CONNECTION_STRING}",
},
},
},
};
}
function deepMerge(target, source) {
const result = { ...target };
for (const key of Object.keys(source)) {
if (
result[key] &&
typeof result[key] === "object" &&
!Array.isArray(result[key]) &&
typeof source[key] === "object" &&
!Array.isArray(source[key])
) {
result[key] = deepMerge(result[key], source[key]);
} else if (Array.isArray(result[key]) && Array.isArray(source[key])) {
// Merge arrays by id (for inputs) – deduplicate by 'id' field
const mergedArray = [...result[key]];
for (const item of source[key]) {
const existingIndex = mergedArray.findIndex(
(existingItem) => existingItem.id && existingItem.id === item.id,
);
if (existingIndex >= 0) {
mergedArray[existingIndex] = item;
} else {
mergedArray.push(item);
}
}
result[key] = mergedArray;
} else {
result[key] = source[key];
}
}
return result;
}
function writeMcpJson(configDir, newConfig) {
const mcpJsonPath = join(configDir, "mcp.json");
if (DRY_RUN) {
logDry(`Would write/merge ${mcpJsonPath}`);
return;
}
mkdirSync(configDir, { recursive: true });
let finalConfig = newConfig;
if (existsSync(mcpJsonPath)) {
try {
const existingContent = readFileSync(mcpJsonPath, "utf-8");
const existingConfig = JSON.parse(existingContent);
finalConfig = deepMerge(existingConfig, newConfig);
log(`Merged with existing config at ${mcpJsonPath}`);
} catch (parseError) {
logWarn(
`Could not parse existing ${mcpJsonPath} – creating backup and overwriting`,
);
const backupPath = `${mcpJsonPath}.backup.${Date.now()}`;
writeFileSync(backupPath, readFileSync(mcpJsonPath));
log(`Backup saved to ${backupPath}`);
}
}
writeFileSync(mcpJsonPath, JSON.stringify(finalConfig, null, 2), "utf-8");
log(`Written ${mcpJsonPath}`);
}
// ---------------------------------------------------------------------------
// Main
// ---------------------------------------------------------------------------
async function main() {
console.log(
`\n${colors.bold}╔══════════════════════════════════════════════╗${colors.reset}`,
);
console.log(
`${colors.bold}║ MCP Server Setup for VS Code ║${colors.reset}`,
);
console.log(
`${colors.bold}╚══════════════════════════════════════════════╝${colors.reset}\n`,
);
console.log(
` Platform: ${platform()} (${arch()}) | Dry-run: ${DRY_RUN}\n`,
);
// ── Step 1: Pre-warm npx cache ──────────────────────────────────────────
if (!SKIP_CACHE_WARM) {
warmNpxCache();
} else {
logStep("Skipping npx cache warming (--skip-cache-warm)");
}
// ── Step 2: Build mcp.json config ───────────────────────────────────────
logStep("Building MCP configuration");
const mcpConfig = buildMcpConfig();
log(`Configured ${Object.keys(mcpConfig.servers).length} MCP servers`);
// ── Step 3: Write mcp.json for VS Code and VS Code Insiders ─────────────
const variants = ["Code", "Code - Insiders"];
for (const variant of variants) {
logStep(`Configuring ${variant}`);
const configDir = getVSCodeUserConfigDir(variant);
writeMcpJson(configDir, mcpConfig);
}
// ── Summary ─────────────────────────────────────────────────────────────
console.log(
`\n${colors.bold}${colors.green}══════════════════════════════════════════════${colors.reset}`,
);
console.log(
`${colors.bold}${colors.green} Setup complete!${colors.reset}\n`,
);
console.log(" Configured MCP servers:");
console.log(" 1. azure-devops (npx, stdio)");
console.log(" 2. sequential-thinking (npx, stdio)");
console.log(" 3. microsoft-learn (remote HTTP – no install needed)");
console.log(" 4. memory (npx, stdio)");
console.log(
" 5. mongodb (npx, stdio, --connectionString via env)",
);
console.log(
" 6. mssql (npx, stdio, connection via env)",
);
console.log("\n Locations:");
for (const variant of variants) {
const configDir = getVSCodeUserConfigDir(variant);
console.log(` ${variant}: ${join(configDir, "mcp.json")}`);
}
console.log(`\n${colors.yellow} Next steps:${colors.reset}`);
console.log(" 1. Restart VS Code / VS Code Insiders");
console.log(" 2. Open Copilot Chat in Agent Mode");
console.log(' 3. Click "Select Tools" and enable MCP servers');
console.log(
" 4. On first use, ADO server will prompt for organization name",
);
console.log("");
}
main().catch((error) => {
logError(`Fatal error: ${error.message}`);
process.exit(1);
});