Skip to content

Commit aaf7f0d

Browse files
kathiekiwiTrigger.dev RepoOps
authored andcommitted
feat(mcp): enable remote MCP per organization with a feature flag
Mono-RevId: 94d91c7329a978742c7cb87dd82b9f809dc9625b
1 parent abde78b commit aaf7f0d

33 files changed

Lines changed: 361 additions & 91 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+
Remote MCP can now be turned on for selected organizations, and self-hosted installs turn it on with the `hasRemoteMcp` feature flag. Organizations without it get a clear message from MCP tools and don't see the Access settings page. If you already run remote MCP with `MCP_SERVER_ENABLED`, turn on `hasRemoteMcp` globally or per organization after upgrading, or MCP tools will refuse organization calls.

‎apps/webapp/app/components/navigation/OrganizationSettingsSideMenu.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export function OrganizationSettingsSideMenu({
161161
to={organizationSecurityPath(organization)}
162162
data-action="security"
163163
/>
164-
{isMcpServerEnabled && (
164+
{isMcpServerEnabled && featureFlags.hasRemoteMcp && (
165165
<SideMenuItem
166166
name="Access"
167167
icon={KeyIcon}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { fenceUntrusted } from "./fence.server";
22

33
type McpAccessRefusalReason =
4+
| "remote_mcp_not_enabled"
45
| "org_mcp_disabled"
56
| "org_connections_disabled"
67
| "org_pat_disabled"
@@ -12,6 +13,8 @@ export type McpAccessDecision =
1213
| { allowed: false; reason: McpAccessRefusalReason; organizationSlug: string };
1314

1415
const REFUSALS: Record<McpAccessRefusalReason, (org: string) => string> = {
16+
remote_mcp_not_enabled: (org) =>
17+
`Remote MCP is not enabled for organization ${org} yet. Ask whoever runs this Trigger.dev instance to enable it.`,
1518
org_mcp_disabled: (org) => `Organization ${org} does not allow MCP access. Nothing changed.`,
1619
org_connections_disabled: (org) =>
1720
`Organization ${org} does not allow MCP connections. Nothing changed.`,

‎apps/webapp/app/mcp/policy/accessPage.server.ts‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import { dashboardAction, dashboardLoader } from "~/services/routeBuilders/dashb
2626
import { CONNECTIONS_CURSOR_PARAM } from "~/utils/mcpClient";
2727
import { organizationPath } from "~/utils/pathBuilder";
2828

29-
export type AccessPageOptions = { mcpServerEnabled: boolean };
29+
export type AccessPageOptions = {
30+
mcpServerEnabled: boolean;
31+
canUseRemoteMcp: (organizationId: string) => Promise<boolean>;
32+
};
3033

3134
const Params = z.object({ organizationSlug: z.string() });
3235

@@ -77,12 +80,14 @@ export type ToolRowData = Omit<McpWriteToolEntry, "approval" | "orgConfigurable"
7780
revision: string;
7881
};
7982

80-
export const accessLoader = ({ mcpServerEnabled }: AccessPageOptions) =>
83+
export const accessLoader = ({ mcpServerEnabled, canUseRemoteMcp }: AccessPageOptions) =>
8184
dashboardLoader(
8285
{ params: Params, context, authorization },
8386
async ({ request, params, context, user }) => {
84-
if (!mcpServerEnabled) throw redirect(organizationPath({ slug: params.organizationSlug }));
87+
const home = organizationPath({ slug: params.organizationSlug });
88+
if (!mcpServerEnabled) throw redirect(home);
8589
if (!context.organizationId) throw new Response("Not Found", { status: 404 });
90+
if (!(await canUseRemoteMcp(context.organizationId))) throw redirect(home);
8691
await requireRecentMfa(request, {
8792
userId: user.id,
8893
action: "organizationSecurity",
@@ -143,13 +148,15 @@ export const accessLoader = ({ mcpServerEnabled }: AccessPageOptions) =>
143148
}
144149
);
145150

146-
export const accessAction = ({ mcpServerEnabled }: AccessPageOptions) =>
151+
export const accessAction = ({ mcpServerEnabled, canUseRemoteMcp }: AccessPageOptions) =>
147152
dashboardAction(
148153
{ params: Params, context, authorization },
149154
async ({ request, params, context, user }) => {
150-
if (!mcpServerEnabled) throw redirect(organizationPath({ slug: params.organizationSlug }));
155+
const home = organizationPath({ slug: params.organizationSlug });
156+
if (!mcpServerEnabled) throw redirect(home);
151157
const organizationId = context.organizationId;
152158
if (!organizationId) throw new Response("Not Found", { status: 404 });
159+
if (!(await canUseRemoteMcp(organizationId))) throw redirect(home);
153160

154161
if ((await getImpersonationState(request, user.id)).isImpersonating) {
155162
return jsonWithErrorMessage(

‎apps/webapp/app/mcp/policy/mcpAccess.server.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Prisma } from "@trigger.dev/database";
22
import cuid from "cuid";
33
import { prisma, sqlDatabaseSchema, type PrismaClientOrTransaction } from "~/db.server";
4+
import { hasRemoteMcp } from "~/v3/canUseRemoteMcp.server";
5+
import { FEATURE_FLAG } from "~/v3/featureFlags";
46
import type { McpAccessDecision } from "../gate/accessRefusal.server";
57
import type { McpIdentity } from "../types";
68

@@ -9,6 +11,8 @@ type McpAccessRefusalReason = Extract<McpAccessDecision, { allowed: false }>["re
911
type AccessRow = {
1012
organizationId: string;
1113
slug: string;
14+
featureFlags: unknown;
15+
remoteMcpGlobal: unknown;
1216
allowMcp: boolean;
1317
allowConnections: boolean;
1418
allowPersonalTokens: boolean;
@@ -25,7 +29,9 @@ function accessRows(
2529
): Promise<AccessRow[]> {
2630
const sourceTokenId = identity.kind === "uat" ? identity.sourceTokenId : null;
2731
return db.$queryRaw<AccessRow[]>`
28-
SELECT o."id" AS "organizationId", o."slug",
32+
SELECT o."id" AS "organizationId", o."slug", o."featureFlags",
33+
(SELECT f."value" FROM ${sqlDatabaseSchema}."FeatureFlag" f
34+
WHERE f."key" = ${FEATURE_FLAG.hasRemoteMcp}) AS "remoteMcpGlobal",
2935
COALESCE(p."allowMcp", true) AS "allowMcp",
3036
COALESCE(p."allowConnections", true) AS "allowConnections",
3137
COALESCE(p."allowPersonalTokens", true) AS "allowPersonalTokens",
@@ -54,6 +60,7 @@ function decide(row: AccessRow, identity: McpIdentity): McpAccessDecision {
5460
reason,
5561
organizationSlug: row.slug,
5662
});
63+
if (!hasRemoteMcp(row.featureFlags, row.remoteMcpGlobal)) return refuse("remote_mcp_not_enabled");
5764
if (!row.allowMcp) return refuse("org_mcp_disabled");
5865
if (identity.kind === "uat" && !row.allowConnections) return refuse("org_connections_disabled");
5966
if (identity.kind === "pat" && !row.allowPersonalTokens) return refuse("org_pat_disabled");

‎apps/webapp/app/mcp/tools/orgs.server.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ async function withoutRefused<T>(
2222

2323
const notAvailableText = (count: number) =>
2424
count === 1
25-
? "1 organization is not available to this connection because of its MCP access settings."
26-
: `${count} organizations are not available to this connection because of their MCP access settings.`;
25+
? "1 organization is not available to this connection: remote MCP is not enabled for it yet, or its MCP access settings refuse this connection."
26+
: `${count} organizations are not available to this connection: remote MCP is not enabled for them yet, or their MCP access settings refuse this connection.`;
2727

2828
// A partial list says how many organizations were left out, never which.
2929
const withHidden = (text: string, hidden: number) =>

‎apps/webapp/app/presenters/OrganizationsPresenter.server.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { SelectBestEnvironmentPresenter } from "./SelectBestEnvironmentPresenter
1111
import { sortEnvironments } from "~/utils/environmentSort";
1212
import { defaultAvatar, parseAvatar } from "~/components/primitives/Avatar";
1313
import { bufferedTasksEnabled } from "~/v3/buffer/bufferedTasksFlag.server";
14+
import { hasRemoteMcp } from "~/v3/canUseRemoteMcp.server";
1415
import { globalFeatureFlags, mergeOrgFeatureFlags } from "~/v3/featureFlags.server";
1516
import { hydrateEnvsWithActivity } from "./v3/BranchesPresenter.server";
1617

@@ -183,6 +184,7 @@ export class OrganizationsPresenter {
183184
oidcEnabled: await isExternalOidcEnabled(org.featureFlags),
184185
trustedDeploysEnabled: await isTrustedDeploysEnabled(org.featureFlags),
185186
bufferedTasksEnabled: bufferedTasksEnabled(org.featureFlags),
187+
hasRemoteMcp: hasRemoteMcp(org.featureFlags, globalFlags.hasRemoteMcp),
186188
},
187189
supportAccessMode: org.supportAccessMode,
188190
projects: org.projects.map((project) => ({

‎apps/webapp/app/routes/_app.orgs.$organizationSlug.settings.access/route.tsx‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,16 @@ import { accessAction, accessLoader, type ToolRowData } from "~/mcp/policy/acces
2828
import { areaConfirmation } from "~/utils/mcpConfirmationArea";
2929
import { CONNECTIONS_CURSOR_PARAM } from "~/utils/mcpClient";
3030
import { pageMeta } from "~/utils/pageTitle";
31+
import { canUseRemoteMcp } from "~/v3/canUseRemoteMcp.server";
3132
import { McpAccessSection, Subsection } from "./McpAccessSection";
3233

3334
export const meta = pageMeta("Access");
3435

35-
export const loader = accessLoader({ mcpServerEnabled: env.MCP_SERVER_ENABLED });
36+
const accessOptions = { mcpServerEnabled: env.MCP_SERVER_ENABLED, canUseRemoteMcp };
3637

37-
export const action = accessAction({ mcpServerEnabled: env.MCP_SERVER_ENABLED });
38+
export const loader = accessLoader(accessOptions);
39+
40+
export const action = accessAction(accessOptions);
3841

3942
const SEGMENTS = [
4043
{ value: "ask" as const, label: "Ask every time", icon: <HandRaisedIcon className="size-3" /> },
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { prisma, type PrismaClientOrTransaction } from "~/db.server";
2+
import { FEATURE_FLAG, FeatureFlagCatalog, isPlainFlagObject } from "~/v3/featureFlags";
3+
4+
/** The organization's own value wins, then the global flag; off when neither is set. */
5+
export function hasRemoteMcp(organizationFlags: unknown, globalValue: unknown): boolean {
6+
const schema = FeatureFlagCatalog[FEATURE_FLAG.hasRemoteMcp];
7+
const override = isPlainFlagObject(organizationFlags)
8+
? organizationFlags[FEATURE_FLAG.hasRemoteMcp]
9+
: undefined;
10+
for (const value of [override, globalValue]) {
11+
const parsed = schema.safeParse(value);
12+
if (parsed.success) return parsed.data;
13+
}
14+
return false;
15+
}
16+
17+
export async function canUseRemoteMcp(
18+
organizationId: string,
19+
db: PrismaClientOrTransaction = prisma
20+
): Promise<boolean> {
21+
const [organization, global] = await Promise.all([
22+
db.organization.findFirst({ where: { id: organizationId }, select: { featureFlags: true } }),
23+
db.featureFlag.findFirst({
24+
where: { key: FEATURE_FLAG.hasRemoteMcp },
25+
select: { value: true },
26+
}),
27+
]);
28+
return hasRemoteMcp(organization?.featureFlags, global?.value);
29+
}

‎apps/webapp/app/v3/featureFlags.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const FEATURE_FLAG = {
2828
promotedDashboardAgentPrompt: "promotedDashboardAgentPrompt",
2929
hasComputeAccess: "hasComputeAccess",
3030
hasPrivateConnections: "hasPrivateConnections",
31+
hasRemoteMcp: "hasRemoteMcp",
3132
hasSso: "hasSso",
3233
hasThemeSwitcher: "hasThemeSwitcher",
3334
hasDocsPanel: "hasDocsPanel",
@@ -163,6 +164,8 @@ export const FeatureFlagCatalog = {
163164
// (microVM) lanes, with no container fallback. Org-scoped only. Strict z.boolean().
164165
[FEATURE_FLAG.computeOnly]: z.boolean(),
165166
[FEATURE_FLAG.hasPrivateConnections]: z.coerce.boolean(),
167+
// Lets an organization use the remote MCP server. Strict: a stray string must not turn it on.
168+
[FEATURE_FLAG.hasRemoteMcp]: z.boolean(),
166169
[FEATURE_FLAG.hasSso]: z.coerce.boolean(),
167170
// Gates the Interface theme setting in /account. Off by default.
168171
[FEATURE_FLAG.hasThemeSwitcher]: z.coerce.boolean(),

0 commit comments

Comments
 (0)