-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdeploys.ts
More file actions
226 lines (177 loc) · 6.2 KB
/
deploys.ts
File metadata and controls
226 lines (177 loc) · 6.2 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
import { DeployInput, ListDeploysInput } from "../schemas.js";
import { toolsMetadata } from "../config.js";
import { ToolMeta } from "../types.js";
import { respondWithError, toolHandler } from "../utils.js";
import { McpContext } from "../context.js";
import { x } from "tinyexec";
import { getPackageJson, tryResolveTriggerPackageVersion } from "../../commands/update.js";
import { VERSION } from "../../version.js";
import { resolveSync as esmResolve } from "mlly";
import { fileURLToPath } from "node:url";
import stripAnsi from "strip-ansi";
export const deployTool = {
name: toolsMetadata.deploy.name,
title: toolsMetadata.deploy.title,
description: toolsMetadata.deploy.description,
inputSchema: DeployInput.shape,
handler: toolHandler(DeployInput.shape, async (input, { ctx, createProgressTracker, _meta }) => {
ctx.logger?.log("calling deploy", { input });
// Check if the deployment target environment is allowed
if (!ctx.isEnvironmentAllowed(input.environment)) {
return respondWithError(
`Cannot deploy to ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
);
}
const cwd = await ctx.getProjectDir({ cwd: input.configPath });
if (!cwd.ok) {
return respondWithError(cwd.error);
}
const auth = await ctx.getAuth();
const args = ["deploy", "--env", input.environment, "--api-url", auth.auth.apiUrl];
if (input.environment === "preview" && input.branch) {
args.push("--branch", input.branch);
}
if (ctx.options.profile) {
args.push("--profile", ctx.options.profile);
}
if (input.skipPromotion) {
args.push("--skip-promotion");
}
if (input.skipSyncEnvVars) {
args.push("--skip-sync-env-vars");
}
if (input.skipUpdateCheck) {
args.push("--skip-update-check");
}
const [nodePath, cliPath] = await resolveCLIExec(ctx, cwd.cwd);
ctx.logger?.log("deploy process args", {
nodePath,
cliPath,
args,
meta: _meta,
});
const progressTracker = createProgressTracker(100);
await progressTracker.updateProgress(
5,
`Starting deploy to ${input.environment}${input.branch ? ` on branch ${input.branch}` : ""}`
);
const deployProcess = x(nodePath, [cliPath, ...args], {
nodeOptions: {
cwd: cwd.cwd,
env: {
TRIGGER_MCP_SERVER: "1",
},
},
});
const logs = [];
for await (const line of deployProcess) {
const lineWithoutAnsi = stripAnsi(line);
const buildingVersion = lineWithoutAnsi.match(/Building version (\d+\.\d+)/);
if (buildingVersion) {
await progressTracker.incrementProgress(1, `Building version ${buildingVersion[1]}`);
} else {
await progressTracker.incrementProgress(1);
}
logs.push(stripAnsi(line));
}
await progressTracker.complete("Deploy complete");
ctx.logger?.log("deploy deployProcess", {
logs,
});
if (deployProcess.exitCode !== 0) {
return respondWithError(logs.join("\n"));
}
return {
content: [{ type: "text", text: logs.join("\n") }],
};
}),
};
export const listDeploysTool = {
name: toolsMetadata.list_deploys.name,
title: toolsMetadata.list_deploys.title,
description: toolsMetadata.list_deploys.description,
inputSchema: ListDeploysInput.shape,
handler: toolHandler(ListDeploysInput.shape, async (input, { ctx }) => {
ctx.logger?.log("calling list_deploys", { input });
if (!ctx.isEnvironmentAllowed(input.environment)) {
return respondWithError(
`Cannot access ${input.environment} environment. This MCP server is restricted to: ${ctx.getAllowedEnvironments()}`
);
}
const projectRef = await ctx.getProjectRef({
projectRef: input.projectRef,
cwd: input.configPath,
});
const apiClient = await ctx.getApiClient({
projectRef,
environment: input.environment,
scopes: ["read:deployments"],
branch: input.branch,
});
const result = await apiClient.listDeployments(input);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}),
};
async function resolveCLIExec(context: McpContext, cwd?: string): Promise<[string, string]> {
// Lets first try to get the version of the CLI package
const installedCLI = await tryResolveTriggerCLIPath(context, cwd);
if (installedCLI) {
context.logger?.log("resolve_cli_exec installedCLI", { installedCLI });
return [process.argv[0] ?? "node", installedCLI.path];
}
const sdkVersion = await tryResolveTriggerPackageVersion("@trigger.dev/sdk", cwd);
if (!sdkVersion) {
context.logger?.log("resolve_cli_exec no sdk version found", { cwd });
return [process.argv[0] ?? "npx", process.argv[1] ?? "trigger.dev@latest"];
}
if (sdkVersion === VERSION) {
context.logger?.log("resolve_cli_exec sdk version is the same as the current version", {
sdkVersion,
});
if (typeof process.argv[0] === "string" && typeof process.argv[1] === "string") {
return [process.argv[0], process.argv[1]];
}
return ["npx", "trigger.dev@latest"];
}
return ["npx", `trigger.dev@${sdkVersion}`];
}
async function tryResolveTriggerCLIPath(
context: McpContext,
basedir?: string
): Promise<
| {
path: string;
version: string;
}
| undefined
> {
try {
const resolvedPathFileURI = esmResolve("trigger.dev", {
url: basedir,
});
const resolvedPath = fileURLToPath(resolvedPathFileURI);
context.logger?.log("resolve_cli_exec resolvedPathFileURI", { resolvedPathFileURI });
const { packageJson } = await getPackageJson(resolvedPath, {
test: (filePath) => {
// We need to skip any type-marker files
if (filePath.includes("dist/commonjs")) {
return false;
}
if (filePath.includes("dist/esm")) {
return false;
}
return true;
},
});
if (packageJson.version) {
context.logger?.log("resolve_cli_exec packageJson", { packageJson });
return { path: resolvedPath, version: packageJson.version };
}
return;
} catch (error) {
context.logger?.log("resolve_cli_exec error", { error });
return undefined;
}
}