Skip to content

Commit d3c7adb

Browse files
kathiekiwiTrigger.dev RepoOps
authored andcommitted
feat(mcp): environment, branch and deployment tools for the MCP server
Mono-RevId: f55ef3a639dc752217763ade7a797687b40eab11
1 parent d942a40 commit d3c7adb

29 files changed

Lines changed: 4754 additions & 107 deletions
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: feature
4+
---
5+
6+
The remote MCP server can now list a project's environments, branches and regions, promote, roll back or cancel a deployment, create or archive a branch, set automatic archiving of preview branches, and change the project's default region.

‎apps/webapp/app/mcp/confirm/confirmation.server.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createHash, createHmac } from "node:crypto";
2+
import type { RbacAbility } from "@trigger.dev/rbac";
23
import { $replica } from "~/db.server";
34
import type { McpConfirmSpec, McpIdentity, McpPreviewRefusal, McpSubject } from "../gate/types";
45

@@ -47,7 +48,7 @@ export function confirmationDigest(tool: string, args: unknown, subject: McpSubj
4748
}
4849

4950
// Lazy so importing the gate doesn't parse env.
50-
async function defaultFingerprintKey() {
51+
export async function defaultFingerprintKey() {
5152
return (await import("~/env.server")).env.SESSION_SECRET;
5253
}
5354

@@ -63,17 +64,19 @@ export async function confirmationPreview(
6364
spec: true | McpConfirmSpec,
6465
args: unknown,
6566
subject: McpSubject | null,
66-
fingerprintKey?: string
67+
fingerprintKey?: string,
68+
ability?: RbacAbility
6769
): Promise<{ preview: string; truncated: boolean } | McpPreviewRefusal>;
6870
export async function confirmationPreview(
6971
tool: string,
7072
spec: true | McpConfirmSpec,
7173
args: unknown,
7274
subject: McpSubject | null,
73-
fingerprintKey?: string
75+
fingerprintKey?: string,
76+
ability?: RbacAbility
7477
): Promise<{ preview: string; truncated: boolean } | McpPreviewRefusal> {
7578
if (spec !== true) {
76-
const preview = await spec.preview(args, subject);
79+
const preview = await spec.preview(args, subject, ability);
7780
if (typeof preview !== "string") return preview;
7881
if (preview.length > PREVIEW_LIMIT) {
7982
return {

‎apps/webapp/app/mcp/gate/resolveSubject.server.ts‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,25 @@ const ENVIRONMENT_SLUGS: Record<string, string> = {
1010
preview: "preview",
1111
};
1212

13-
const environmentSelect = { id: true, type: true } as const;
13+
const environmentSelect = { id: true, type: true, archivedAt: true } as const;
14+
15+
/** The name as given first, so a listed name like `refs/heads/x` round-trips, then as a git ref. */
16+
export function branchLookupNames(raw: string): string[] {
17+
const normalized = sanitizeBranchName(raw);
18+
return normalized && normalized !== raw ? [raw, normalized] : [raw];
19+
}
20+
21+
/** The first non-null lookup, trying the names in order. */
22+
export async function firstBranchMatch<T>(
23+
names: string[],
24+
find: (name: string) => Promise<T | null>
25+
): Promise<T | null> {
26+
for (const name of names) {
27+
const found = await find(name);
28+
if (found) return found;
29+
}
30+
return null;
31+
}
1432

1533
async function findProject(where: Prisma.ProjectWhereInput) {
1634
const select = { id: true, organizationId: true } as const;
@@ -20,11 +38,19 @@ async function findProject(where: Prisma.ProjectWhereInput) {
2038
);
2139
}
2240

23-
async function findEnvironment(where: Prisma.RuntimeEnvironmentWhereInput) {
24-
return (
25-
(await $replica.runtimeEnvironment.findFirst({ where, select: environmentSelect })) ??
26-
(await prisma.runtimeEnvironment.findFirst({ where, select: environmentSelect }))
27-
);
41+
async function findEnvironment(
42+
where: Prisma.RuntimeEnvironmentWhereInput,
43+
orderBy?: Prisma.RuntimeEnvironmentOrderByWithRelationInput
44+
) {
45+
// A lagging replica can still show only an archived predecessor, so archived hits ask the primary.
46+
const replica = await $replica.runtimeEnvironment.findFirst({
47+
where,
48+
orderBy,
49+
select: environmentSelect,
50+
});
51+
return replica && !replica.archivedAt
52+
? replica
53+
: await prisma.runtimeEnvironment.findFirst({ where, orderBy, select: environmentSelect });
2854
}
2955

3056
export async function resolveMcpSubject(
@@ -61,13 +87,18 @@ export async function resolveMcpSubject(
6187
const branchName = typeof branch === "string" ? sanitizeBranchName(branch) : null;
6288
const isDefaultBranch = slug === "dev" && !!branchName && isDefaultDevBranch(branchName);
6389
if (runtimeEnvironment && branch !== undefined && !isDefaultBranch) {
64-
if ((slug !== "preview" && slug !== "dev") || !branchName) return null;
65-
runtimeEnvironment = await findEnvironment({
66-
projectId: project.id,
67-
parentEnvironmentId: runtimeEnvironment.id,
68-
branchName,
69-
archivedAt: null,
70-
});
90+
if ((slug !== "preview" && slug !== "dev") || typeof branch !== "string" || !branch) {
91+
return null;
92+
}
93+
const parentEnvironmentId = runtimeEnvironment.id;
94+
// The first name that exists decides, even when archived, so a listed name never falls through.
95+
const found = await firstBranchMatch(branchLookupNames(branch), (branchName) =>
96+
findEnvironment(
97+
{ projectId: project.id, parentEnvironmentId, branchName },
98+
{ archivedAt: { sort: "desc", nulls: "first" } }
99+
)
100+
);
101+
runtimeEnvironment = found?.archivedAt ? null : found;
71102
}
72103
if (!runtimeEnvironment) return null;
73104

‎apps/webapp/app/mcp/gate/types.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ export type McpConfirmSpec<A = unknown> = {
3131
) => McpPrecheckDenial | void | Promise<McpPrecheckDenial | void>;
3232
preview: (
3333
args: A,
34-
subject: McpSubject | null
34+
subject: McpSubject | null,
35+
ability?: RbacAbility
3536
) => string | McpPreviewRefusal | Promise<string | McpPreviewRefusal>;
3637
};
3738

‎apps/webapp/app/mcp/gate/withMcpToolAuth.server.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,8 @@ async function confirm(
229229
spec: true | McpConfirmSpec,
230230
args: unknown,
231231
identity: McpIdentity,
232-
subject: McpSubject | null
232+
subject: McpSubject | null,
233+
ability?: RbacAbility
233234
): Promise<
234235
| { approved: true; id: string; preview: string }
235236
| { approved: false; id: string; url: string; message: string }
@@ -246,7 +247,7 @@ async function confirm(
246247
const consumed = await store.consumeApproved(lookup);
247248
if (consumed) return { approved: true, id: consumed.id, preview: consumed.preview };
248249

249-
const preview = await confirmationPreview(tool, spec, args, subject);
250+
const preview = await confirmationPreview(tool, spec, args, subject, undefined, ability);
250251
if ("refuse" in preview) return { approved: false, refused: preview.refuse };
251252

252253
const created = await store.createPending({
@@ -318,7 +319,15 @@ export function withMcpToolAuth<TArgs>(
318319
return denialResult(outcome, named ?? auth);
319320
}
320321
const { store, mode } = await confirmationDefaults(options);
321-
const check = await confirm(store, tool, approval, args, identity, subject);
322+
const check = await confirm(
323+
store,
324+
tool,
325+
approval,
326+
args,
327+
identity,
328+
subject,
329+
authorization.ability
330+
);
322331
if ("refused" in check) {
323332
outcome = "refused";
324333
return errorText(check.refused);

‎apps/webapp/app/mcp/registry.server.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { dashboardsEditTools } from "./tools/dashboardsEdit.server";
3030
import { dashboardsReadTools } from "./tools/dashboardsRead.server";
3131
import { deploymentDetailsTools } from "./tools/deploymentDetails.server";
3232
import { deploysTools } from "./tools/deploys.server";
33+
import { environmentsTools } from "./tools/environments.server";
3334
import { docsTools } from "./tools/docs.server";
3435
import { envVarTools } from "./tools/envVars.server";
3536
import { errorsTools } from "./tools/errors.server";
@@ -68,6 +69,8 @@ import { webhooksWriteTools } from "./tools/webhooksWrite.server";
6869
import { webhooksEndpointWriteTools } from "./tools/webhooksEndpointWrite.server";
6970
import { waitpointsWriteTools } from "./tools/waitpointsWrite.server";
7071
import { webhooksWaiterWriteTools } from "./tools/webhooksWaiterWrite.server";
72+
import { deploymentsWriteTools } from "./tools/deploymentsWrite.server";
73+
import { environmentsWriteTools } from "./tools/environmentsWrite.server";
7174

7275
export type McpApprovalClass = "required" | "configurable" | "none";
7376

@@ -120,6 +123,7 @@ export const mcpTools: McpToolDefinition[] = assertUniqueToolNames([
120123
...deploysTools,
121124
...deploymentDetailsTools,
122125
...envVarTools,
126+
...environmentsTools,
123127
...queryTools,
124128
...dashboardsTools,
125129
...dashboardsReadTools,
@@ -153,6 +157,8 @@ export const mcpTools: McpToolDefinition[] = assertUniqueToolNames([
153157
...webhooksEndpointWriteTools,
154158
...webhooksWaiterWriteTools,
155159
...waitpointsWriteTools,
160+
...deploymentsWriteTools,
161+
...environmentsWriteTools,
156162
]);
157163

158164
export function registerMcpTools(

0 commit comments

Comments
 (0)