-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathinstall-mcp.ts
More file actions
698 lines (603 loc) · 17.5 KB
/
install-mcp.ts
File metadata and controls
698 lines (603 loc) · 17.5 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
import { confirm, intro, isCancel, log, multiselect, select } from "@clack/prompts";
import chalk from "chalk";
import { Command } from "commander";
import { extname } from "node:path";
import { z } from "zod";
import { OutroCommandError, wrapCommandAction } from "../cli/common.js";
import { cliLink } from "../utilities/cliOutput.js";
import { writeConfigHasSeenMCPInstallPrompt } from "../utilities/configFiles.js";
import {
expandTilde,
safeReadJSONCFile,
safeReadTomlFile,
writeJSONFile,
writeTomlFile,
} from "../utilities/fileSystem.js";
import { printStandloneInitialBanner } from "../utilities/initialBanner.js";
import { VERSION } from "../version.js";
const cliVersion = VERSION as string;
const cliTag = cliVersion.includes("v4-beta") ? "v4-beta" : "latest";
const clients = [
"claude-code",
"cursor",
"vscode",
"zed",
"windsurf",
"gemini-cli",
"crush",
"cline",
"openai-codex",
"opencode",
"amp",
"ruler",
] as const;
const scopes = ["user", "project", "local"] as const;
type ClientScopes = {
[key in (typeof clients)[number]]: {
[key in (typeof scopes)[number]]?: string;
};
};
type ClientLabels = {
[key in (typeof clients)[number]]: string;
};
const clientScopes: ClientScopes = {
"claude-code": {
user: "~/.claude.json",
project: "./.mcp.json",
local: "~/.claude.json",
},
cursor: {
user: "~/.cursor/mcp.json",
project: "./.cursor/mcp.json",
},
vscode: {
user: "~/Library/Application Support/Code/User/mcp.json",
project: "./.vscode/mcp.json",
},
zed: {
user: "~/.config/zed/settings.json",
},
windsurf: {
user: "~/.codeium/windsurf/mcp_config.json",
},
"gemini-cli": {
user: "~/.gemini/settings.json",
project: "./.gemini/settings.json",
},
crush: {
user: "~/.config/crush/crush.json",
project: "./crush.json",
local: "./.crush.json",
},
cline: {
user: "~/Library/Application Support/Code/User/globalStorage/saoudrizwan.claude-dev/settings/cline_mcp_settings.json",
},
amp: {
user: "~/.config/amp/settings.json",
},
"openai-codex": {
user: "~/.codex/config.toml",
},
opencode: {
user: "~/.config/opencode/opencode.json",
project: "./opencode.json",
},
ruler: {
project: "./.ruler/mcp.json",
},
};
const clientLabels: ClientLabels = {
"claude-code": "Claude Code",
cursor: "Cursor",
vscode: "VSCode",
zed: "Zed",
windsurf: "Windsurf",
"gemini-cli": "Gemini CLI",
crush: "Charm Crush",
cline: "Cline",
"openai-codex": "OpenAI Codex CLI",
amp: "Sourcegraph AMP",
opencode: "opencode",
ruler: "Ruler",
};
type SupportedClients = (typeof clients)[number];
type ResolvedClients = SupportedClients | "unsupported";
const InstallMcpCommandOptions = z.object({
projectRef: z.string().optional(),
tag: z.string().default(cliTag),
devOnly: z.boolean().optional(),
yolo: z.boolean().default(false),
scope: z.enum(scopes).optional(),
client: z.enum(clients).array().optional(),
logFile: z.string().optional(),
apiUrl: z.string().optional(),
logLevel: z.enum(["debug", "info", "log", "warn", "error", "none"]).default("log"),
});
type InstallMcpCommandOptions = z.infer<typeof InstallMcpCommandOptions>;
export function configureInstallMcpCommand(program: Command) {
return program
.command("install-mcp")
.description("Install the Trigger.dev MCP server")
.option(
"-p, --project-ref <project ref>",
"Scope the mcp server to a specific Trigger.dev project by providing its project ref"
)
.option(
"-t, --tag <package tag>",
"The version of the trigger.dev CLI package to use for the MCP server",
cliTag
)
.option("--dev-only", "Restrict the MCP server to the dev environment only")
.option("--yolo", "Install the MCP server into all supported clients")
.option("--scope <scope>", "Choose the scope of the MCP server, either user or project")
.option(
"--client <clients...>",
"Choose the client (or clients) to install the MCP server into. We currently support: " +
clients.join(", ")
)
.option("--log-file <log file>", "Configure the MCP server to write logs to a file")
.option(
"-a, --api-url <value>",
"Configure the MCP server to specify a custom Trigger.dev API URL"
)
.option(
"-l, --log-level <level>",
"The CLI log level to use (debug, info, log, warn, error, none). This does not effect the log level of your trigger.dev tasks.",
"log"
)
.action(async (options) => {
await printStandloneInitialBanner(true);
await installMcpCommand(options);
});
}
export async function installMcpCommand(options: unknown) {
return await wrapCommandAction(
"installMcpCommand",
InstallMcpCommandOptions,
options,
async (opts) => {
return await _installMcpCommand(opts);
}
);
}
async function _installMcpCommand(options: InstallMcpCommandOptions) {
intro("Welcome to the Trigger.dev MCP server install wizard 🧙");
await installMcpServer(options);
}
type InstallMcpServerResults = Array<InstallMcpServerResult>;
type InstallMcpServerResult = {
configPath: string;
clientName: (typeof clients)[number];
scope: McpServerScope;
};
export async function installMcpServer(
options: InstallMcpCommandOptions
): Promise<InstallMcpServerResults> {
const opts = InstallMcpCommandOptions.parse(options);
writeConfigHasSeenMCPInstallPrompt(true);
const devOnly = await resolveDevOnly(opts);
opts.devOnly = devOnly;
const clientNames = await resolveClients(opts);
if (clientNames.length === 1 && clientNames.includes("unsupported")) {
return handleUnsupportedClientOnly(opts);
}
const results = [];
for (const clientName of clientNames) {
const result = await installMcpServerForClient(clientName, opts);
if (result) {
results.push(result);
}
}
if (results.length > 0) {
log.step("Installed to:");
for (const r of results) {
const scopeLabel = `${r.scope.scope}`;
log.message(` • ${r.clientName} (${scopeLabel}) → ${chalk.gray(r.configPath)}`);
}
}
log.info("Next steps:");
log.message(" 1. Restart your MCP client(s) to load the new configuration.");
log.message(
' 2. In your client, look for a server named "trigger". It should connect automatically.'
);
log.message(" 3. Get started with Trigger.dev");
log.message(
` Try asking your vibe-coding friend to ${chalk.green("Add trigger.dev to my project")}`
);
log.info("More examples:");
log.message(` • ${chalk.green('"Trigger the hello-world task"')}`);
log.message(` • ${chalk.green('"Can you help me debug the prod run run_1234"')}`);
log.message(` • ${chalk.green('"Deploy my trigger project to staging"')}`);
log.message(` • ${chalk.green('"What trigger task handles uploading files to S3?"')}`);
log.message(` • ${chalk.green('"How do I create a scheduled task in Trigger.dev?"')}`);
log.message(` • ${chalk.green('"Search Trigger.dev docs for ffmpeg examples"')}`);
log.info("Helpful links:");
log.message(` • ${cliLink("Trigger.dev docs", "https://trigger.dev/docs")}`);
log.message(` • ${cliLink("MCP docs", "https://trigger.dev/docs/mcp")}`);
log.message(
` • Need help? ${cliLink(
"Join our Discord",
"https://trigger.dev/discord"
)} or email help@trigger.dev`
);
return results;
}
function handleUnsupportedClientOnly(options: InstallMcpCommandOptions): InstallMcpServerResults {
log.info("Manual MCP server configuration");
const args = [`trigger.dev@${options.tag}`, "mcp"];
if (options.logFile) {
args.push("--log-file", options.logFile);
}
if (options.apiUrl) {
args.push("--api-url", options.apiUrl);
}
if (options.devOnly) {
args.push("--dev-only");
}
if (options.projectRef) {
args.push("--project-ref", options.projectRef);
}
if (options.logLevel && options.logLevel !== "log") {
args.push("--log-level", options.logLevel);
}
log.message(
"Since your client isn't directly supported yet, you'll need to configure it manually:"
);
log.message("");
log.message(`${chalk.yellow("Command:")} ${chalk.green("npx")}`);
log.message(`${chalk.yellow("Arguments:")} ${chalk.green(args.join(" "))}`);
log.message("");
log.message("Add this MCP server configuration to your client's settings:");
log.message(` • ${chalk.cyan("Server name:")} trigger`);
log.message(` • ${chalk.cyan("Command:")} npx`);
log.message(` • ${chalk.cyan("Args:")} ${args.map((arg) => `"${arg}"`).join(", ")}`);
log.message("");
log.message("Most MCP clients use a JSON configuration format like:");
log.message(
chalk.dim(`{
"mcpServers": {
"trigger": {
"command": "npx",
"args": [${args.map((arg) => `"${arg}"`).join(", ")}]
}
}
}`)
);
return [];
}
async function installMcpServerForClient(
clientName: ResolvedClients,
options: InstallMcpCommandOptions
) {
if (clientName === "unsupported") {
// This should not happen as unsupported clients are handled separately
// but if it does, provide helpful output
log.message(
`${chalk.yellow("⚠")} Skipping unsupported client - see manual configuration above`
);
return;
}
const scope = await resolveScopeForClient(clientName, options);
// clientSpinner.message(`Installing in ${scope.scope} scope at ${scope.location}`);
const configPath = await performInstallForClient(clientName, scope, options);
// clientSpinner.stop(`Successfully installed in ${clientName} (${configPath})`);
return { configPath, clientName, scope };
}
type McpServerConfig = Record<string, string | Array<string> | boolean | number | undefined>;
type McpServerScope = {
scope: (typeof scopes)[number];
location: string;
};
async function performInstallForClient(
clientName: (typeof clients)[number],
scope: McpServerScope,
options: InstallMcpCommandOptions
) {
const config = resolveMcpServerConfig(clientName, options);
const pathComponents = resolveMcpServerConfigJsonPath(clientName, scope);
return await writeMcpServerConfig(scope.location, pathComponents, config);
}
async function writeMcpServerConfig(
location: string,
pathComponents: string[],
config: McpServerConfig
) {
const fullPath = expandTilde(location);
const extension = extname(fullPath);
switch (extension) {
case ".json": {
let existingConfig = await safeReadJSONCFile(fullPath);
if (!existingConfig) {
existingConfig = {};
}
const newConfig = applyConfigToExistingConfig(existingConfig, pathComponents, config);
await writeJSONFile(fullPath, newConfig, true);
break;
}
case ".toml": {
let existingConfig = await safeReadTomlFile(fullPath);
if (!existingConfig) {
existingConfig = {};
}
const newConfig = applyConfigToExistingConfig(existingConfig, pathComponents, config);
await writeTomlFile(fullPath, newConfig);
break;
}
}
return fullPath;
}
function applyConfigToExistingConfig(
existingConfig: any,
pathComponents: string[],
config: McpServerConfig
) {
const clonedConfig = structuredClone(existingConfig);
let currentValueAtPath = clonedConfig;
for (let i = 0; i < pathComponents.length; i++) {
const currentPathSegment = pathComponents[i];
if (!currentPathSegment) {
break;
}
if (i === pathComponents.length - 1) {
currentValueAtPath[currentPathSegment] = config;
break;
} else {
currentValueAtPath[currentPathSegment] = currentValueAtPath[currentPathSegment] || {};
currentValueAtPath = currentValueAtPath[currentPathSegment];
}
}
return clonedConfig;
}
function resolveMcpServerConfigJsonPath(
clientName: (typeof clients)[number],
scope: McpServerScope
) {
switch (clientName) {
case "cursor": {
return ["mcpServers", "trigger"];
}
case "vscode": {
return ["servers", "trigger"];
}
case "crush": {
return ["mcp", "trigger"];
}
case "windsurf": {
return ["mcpServers", "trigger"];
}
case "gemini-cli": {
return ["mcpServers", "trigger"];
}
case "cline": {
return ["mcpServers", "trigger"];
}
case "amp": {
return ["amp.mcpServers", "trigger"];
}
case "zed": {
return ["context_servers", "trigger"];
}
case "claude-code": {
if (scope.scope === "local") {
const projectPath = process.cwd();
return ["projects", projectPath, "mcpServers", "trigger"];
} else {
return ["mcpServers", "trigger"];
}
}
case "openai-codex": {
return ["mcp_servers", "trigger"];
}
case "opencode": {
return ["mcp", "trigger"];
}
case "ruler": {
return ["mcpServers", "trigger"];
}
}
}
function resolveMcpServerConfig(
clientName: (typeof clients)[number],
options: InstallMcpCommandOptions
): McpServerConfig {
const args = [`trigger.dev@${options.tag}`, "mcp"];
if (options.logFile) {
args.push("--log-file", options.logFile);
}
if (options.apiUrl) {
args.push("--api-url", options.apiUrl);
}
if (options.devOnly) {
args.push("--dev-only");
}
if (options.projectRef) {
args.push("--project-ref", options.projectRef);
}
switch (clientName) {
case "claude-code": {
return {
command: "npx",
args,
};
}
case "cursor": {
return {
command: "npx",
args,
};
}
case "vscode": {
return {
command: "npx",
args,
};
}
case "crush": {
return {
type: "stdio",
command: "npx",
args,
};
}
case "windsurf": {
return {
command: "npx",
args,
};
}
case "gemini-cli": {
return {
command: "npx",
args,
};
}
case "cline": {
return {
command: "npx",
args,
};
}
case "amp": {
return {
command: "npx",
args,
};
}
case "openai-codex": {
return {
command: "npx",
args,
startup_timeout_sec: 30,
};
}
case "zed": {
return {
source: "custom",
command: "npx",
args,
};
}
case "opencode": {
return {
type: "local",
command: ["npx", ...args],
enabled: true,
};
}
case "ruler": {
return {
type: "stdio",
command: "npx",
args,
};
}
}
}
async function resolveScopeForClient(
clientName: (typeof clients)[number],
options: InstallMcpCommandOptions
) {
if (options.scope) {
const location = clientScopes[clientName][options.scope];
if (!location) {
throw new OutroCommandError(
`The ${clientName} client does not support the ${
options.scope
} scope, it only supports ${Object.keys(clientScopes[clientName]).join(", ")} scopes`
);
}
return {
scope: options.scope,
location,
};
}
const scopeOptions = resolveScopeOptionsForClient(clientName);
if (scopeOptions.length === 1) {
return {
scope: scopeOptions[0]!.value.scope,
location: scopeOptions[0]!.value.location,
};
}
const selectedScope = await select({
message: `Where should the MCP server for ${clientName} be installed?`,
options: scopeOptions,
});
if (isCancel(selectedScope)) {
throw new OutroCommandError("No scope selected");
}
return selectedScope;
}
function resolveScopeOptionsForClient(clientName: (typeof clients)[number]): Array<{
value: { location: string; scope: (typeof scopes)[number] };
label: string;
hint: string;
}> {
const $clientScopes = clientScopes[clientName];
const options = Object.entries($clientScopes).map(([scope, location]) => ({
value: { location, scope: scope as (typeof scopes)[number] },
label: scope,
hint: scopeHint(scope as (typeof scopes)[number], location),
}));
return options;
}
function scopeHint(scope: (typeof scopes)[number], location: string) {
switch (scope) {
case "user": {
return `Install for your user account on your machine (${location})`;
}
case "project": {
return `Install in the current project shared with your team (${location})`;
}
case "local": {
return `Install in the current project, local to you only (${location})`;
}
}
}
async function resolveClients(options: InstallMcpCommandOptions): Promise<ResolvedClients[]> {
if (options.client) {
return options.client;
}
if (options.yolo) {
return [...clients];
}
const selectOptions: Array<{
value: string;
label: string;
hint?: string;
}> = clients.map((client) => ({
value: client,
label: clientLabels[client],
}));
selectOptions.push({
value: "unsupported",
label: "Unsupported client",
hint: "We don't support this client yet, but you can still install the MCP server manually.",
});
const $selectOptions = selectOptions as Array<{
value: ResolvedClients;
label: string;
hint?: string;
}>;
const selectedClients = await multiselect({
message: "Select one or more clients to install the MCP server into",
options: $selectOptions,
required: true,
});
if (isCancel(selectedClients)) {
throw new OutroCommandError("No clients selected");
}
return selectedClients;
}
async function resolveDevOnly(options: InstallMcpCommandOptions) {
if (typeof options.devOnly === "boolean") {
return options.devOnly;
}
const devOnly = await confirm({
message: "Restrict the MCP server to the dev environment only?",
initialValue: false,
});
if (isCancel(devOnly)) {
return false;
}
return devOnly;
}