From 25cdd23e574ba23b6334c44935be5901a4fa7ffe Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 2 Dec 2026 16:54:57 -0600 Subject: [PATCH 1/3] chore(mcp): verify publishing permissions over HTTP --- .github/scripts/http-e2e.sh | 5 + apps/sim/scripts/test-mcp-publishing-e2e.ts | 184 ++++++++++++++++++++ apps/sim/turbo.json | 2 + 3 files changed, 191 insertions(+) create mode 100644 apps/sim/scripts/test-mcp-publishing-e2e.ts diff --git a/.github/scripts/http-e2e.sh b/.github/scripts/http-e2e.sh index 13a7962c47d..62570d3158d 100755 --- a/.github/scripts/http-e2e.sh +++ b/.github/scripts/http-e2e.sh @@ -223,6 +223,11 @@ case "$group" in MCP_HOST_E2E_BASE_URL="$NEXT_PUBLIC_APP_URL" \ MCP_HOST_E2E_REPORT_PATH="$report_dir/mcp-host-e2e-report.json" \ bun --no-env-file scripts/test-mcp-host-e2e.ts + MCP_PUBLISHING_E2E_BASE_URL="$NEXT_PUBLIC_APP_URL" \ + MCP_PUBLISHING_E2E_DATABASE_URL="$DATABASE_URL" \ + MCP_PUBLISHING_E2E_AUTH_SECRET="$BETTER_AUTH_SECRET" \ + MCP_PUBLISHING_E2E_REPORT_PATH="$report_dir/mcp-publishing-e2e-report.json" \ + bun --no-env-file scripts/test-mcp-publishing-e2e.ts CLI_LATENCY_E2E_BASE_URL="$NEXT_PUBLIC_APP_URL" \ CLI_LATENCY_E2E_DATABASE_URL="$DATABASE_URL" \ CLI_LATENCY_E2E_RUNS=1 \ diff --git a/apps/sim/scripts/test-mcp-publishing-e2e.ts b/apps/sim/scripts/test-mcp-publishing-e2e.ts new file mode 100644 index 00000000000..db4fc25dd36 --- /dev/null +++ b/apps/sim/scripts/test-mcp-publishing-e2e.ts @@ -0,0 +1,184 @@ +import assert from 'node:assert/strict' +import { mkdir, writeFile } from 'node:fs/promises' +import { dirname } from 'node:path' +import { assertDisposableTestDatabaseUrl } from '@sim/db/testing/test-infrastructure' +import { createLogger } from '@sim/logger' +import { getErrorMessage } from '@sim/utils/errors' +import { generateId, generateShortId } from '@sim/utils/id' +import { makeSignature } from 'better-auth/crypto' +import postgres from 'postgres' +import { createWorkflowMcpToolContract } from '@/lib/api/contracts/workflow-mcp-servers' +import { readResponseTextWithLimit } from '@/lib/core/utils/stream-limits' + +const logger = createLogger('McpPublishingE2E') +const startedAt = new Date().toISOString() + +function requiredEnvironment(name: string): string { + const value = process.env[name] + assert(value, `${name} must be explicitly provided`) + return value +} + +const baseUrl = new URL(requiredEnvironment('MCP_PUBLISHING_E2E_BASE_URL')) +const databaseUrl = assertDisposableTestDatabaseUrl( + requiredEnvironment('MCP_PUBLISHING_E2E_DATABASE_URL') +) +const authSecret = requiredEnvironment('MCP_PUBLISHING_E2E_AUTH_SECRET') +const reportPath = requiredEnvironment('MCP_PUBLISHING_E2E_REPORT_PATH') +assert(['localhost', '127.0.0.1', '[::1]'].includes(baseUrl.hostname), 'Use a loopback app') +assert.equal(baseUrl.protocol, 'http:', 'Use a local HTTP app') +assert.equal(baseUrl.pathname, '/', 'App URL must be an origin') +assert(!baseUrl.username && !baseUrl.password && !baseUrl.search && !baseUrl.hash) +assert(authSecret.length >= 32, 'Use a fixture auth secret of at least 32 characters') + +const sql = postgres(databaseUrl.toString(), { max: 2 }) +const actors = ['admin', 'write', 'read', 'outsider'].map((role) => ({ + role, + id: generateId(), + token: generateShortId(), + cookie: '', +})) +const admin = actors[0] +const writer = actors[1] +const workspaceId = generateId() +const workflowId = generateId() +const servers = [true, false].map((isPublic) => ({ id: generateId(), isPublic })) +const checks: { name: string; status: 'passed' | 'failed'; durationMs: number; error?: string }[] = + [] +const requests: { role: string; status: number; durationMs: number }[] = [] + +async function check(name: string, run: () => Promise) { + const start = performance.now() + try { + await run() + checks.push({ name, status: 'passed', durationMs: Math.round(performance.now() - start) }) + } catch (error) { + checks.push({ + name, + status: 'failed', + durationMs: Math.round(performance.now() - start), + error: getErrorMessage(error), + }) + } +} + +async function seed() { + await sql.begin(async (tx) => { + for (const actor of actors) { + const email = `${actor.id}@mcp-publishing.test` + await tx`insert into "user" (id, name, email, normalized_email, email_verified, created_at, updated_at) + values (${actor.id}, 'Publishing fixture', ${email}, ${email}, true, now(), now())` + await tx`insert into session (id, token, user_id, expires_at, created_at, updated_at) + values (${generateId()}, ${actor.token}, ${actor.id}, now() + interval '1 hour', now(), now())` + } + await tx`insert into project (id, name, owner_id) + values (${workspaceId}, 'Publishing fixture', ${admin.id})` + await tx`insert into workspace (id, project_id, name, owner_id, billed_account_user_id) + values (${workspaceId}, ${workspaceId}, 'Publishing fixture', ${admin.id}, ${admin.id})` + for (const actor of actors.filter((entry) => entry.role !== 'outsider')) { + await tx`insert into permissions (id, user_id, entity_type, entity_id, permission_type) + values (${generateId()}, ${actor.id}, 'workspace', ${workspaceId}, ${actor.role})` + } + await tx`insert into workflow (id, user_id, workspace_id, name, last_synced, created_at, updated_at, is_deployed, deployed_at) + values (${workflowId}, ${writer.id}, ${workspaceId}, 'Publishing fixture', now(), now(), now(), true, now())` + const block = { + id: generateId(), + type: 'start', + name: 'Start', + position: { x: 0, y: 0 }, + enabled: true, + subBlocks: {}, + outputs: {}, + } + await tx`insert into workflow_blocks (id, workflow_id, type, name, position_x, position_y, sub_blocks, outputs) + values (${block.id}, ${workflowId}, 'start', 'Start', 0, 0, '{}'::jsonb, '{}'::jsonb)` + const state = { blocks: { [block.id]: block }, edges: [], loops: {}, parallels: {} } + await tx`insert into workflow_deployment_version (id, workflow_id, version, state, is_active, created_by) + values (${generateId()}, ${workflowId}, 1, ${JSON.stringify(state)}::json, true, ${admin.id})` + for (const server of servers) { + await tx`insert into workflow_mcp_server (id, workspace_id, created_by, name, is_public) + values (${server.id}, ${workspaceId}, ${admin.id}, 'Publishing fixture', ${server.isPublic})` + } + }) + for (const actor of actors) { + actor.cookie = encodeURIComponent( + `${actor.token}.${await makeSignature(actor.token, authSecret)}` + ) + } +} + +async function attach(serverId: string, actor?: (typeof actors)[number]) { + const start = performance.now() + // boundary-raw-fetch: exercises the running app's session authentication and publishing route + const response = await fetch( + new URL(`/api/mcp/workflow-servers/${serverId}/tools?workspaceId=${workspaceId}`, baseUrl), + { + method: 'POST', + headers: { + 'content-type': 'application/json', + origin: baseUrl.origin, + ...(actor ? { cookie: `better-auth.session_token=${actor.cookie}` } : {}), + }, + body: JSON.stringify({ workflowId, toolName: 'publishing_fixture' }), + redirect: 'error', + signal: AbortSignal.timeout(300_000), + } + ) + requests.push({ + role: actor?.role ?? 'anonymous', + status: response.status, + durationMs: Math.round(performance.now() - start), + }) + const body = await readResponseTextWithLimit(response, { + maxBytes: 1024 * 1024, + label: 'Publishing response', + }) + return { response, body } +} + +try { + await seed() + for (const server of servers) { + const visibility = server.isPublic ? 'public' : 'private' + await check( + `admin can publish an existing deployed workflow to a ${visibility} server`, + async () => { + const { response, body } = await attach(server.id, admin) + assert.equal(response.status, 201, body) + const result = createWorkflowMcpToolContract.response.schema.parse(JSON.parse(body)) + assert.equal(result.data.tool.workflowId, workflowId) + assert.equal(result.data.tool.serverId, server.id) + const rows = await sql`select id from workflow_mcp_tool where server_id = ${server.id}` + assert.equal(rows.length, 1) + } + ) + await sql`delete from workflow_mcp_tool where server_id = ${server.id}` + for (const actor of [...actors.slice(1), undefined]) { + await check( + `${actor?.role ?? 'anonymous'} cannot publish to a ${visibility} server`, + async () => { + const { response } = await attach(server.id, actor) + const rows = await sql`select id from workflow_mcp_tool where server_id = ${server.id}` + assert.equal(response.status, actor ? 403 : 401) + assert.equal(rows.length, 0, 'Denied publishing must leave the server unchanged') + } + ) + await sql`delete from workflow_mcp_tool where server_id = ${server.id}` + } + } +} finally { + try { + await sql`delete from project where id = ${workspaceId}` + await sql`delete from "user" where id in ${sql(actors.map((actor) => actor.id))}` + } finally { + await sql.end() + await mkdir(dirname(reportPath), { recursive: true }) + await writeFile( + reportPath, + JSON.stringify({ startedAt, finishedAt: new Date().toISOString(), checks, requests }, null, 2) + ) + } +} + +logger.info('Publishing verification finished', { checks }) +if (checks.some((result) => result.status === 'failed')) process.exitCode = 1 diff --git a/apps/sim/turbo.json b/apps/sim/turbo.json index 82a1eb1e3e5..225746d2f50 100644 --- a/apps/sim/turbo.json +++ b/apps/sim/turbo.json @@ -33,6 +33,7 @@ "!scripts/test-file-*-e2e.ts", "!scripts/test-landing-seo-e2e.ts", "!scripts/test-mcp-host-e2e.ts", + "!scripts/test-mcp-publishing-e2e.ts", "!scripts/test-mobile-e2e.ts", "!scripts/test-markdown-viewer-e2e.ts", "!scripts/test-workflow-code-ui-e2e.ts", @@ -118,6 +119,7 @@ "inputs": [ "scripts/test-cli-run-latency-e2e.ts", "scripts/test-mcp-host-e2e.ts", + "scripts/test-mcp-publishing-e2e.ts", "scripts/test-search-*-e2e.ts" ], "cache": false From fbc3f37040276fb8d785d16807f34cb19a793932 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 2 Dec 2026 17:05:34 -0600 Subject: [PATCH 2/3] fix(mcp): require admin access to publish workflow tools --- apps/sim/app/api/mcp/workflow-servers/[id]/tools/route.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/sim/app/api/mcp/workflow-servers/[id]/tools/route.ts b/apps/sim/app/api/mcp/workflow-servers/[id]/tools/route.ts index 9991ddc7fb4..88534fdc9fe 100644 --- a/apps/sim/app/api/mcp/workflow-servers/[id]/tools/route.ts +++ b/apps/sim/app/api/mcp/workflow-servers/[id]/tools/route.ts @@ -95,7 +95,7 @@ export const GET = withRouteHandler( */ export const POST = withRouteHandler( withMcpAuth( - 'write', + 'admin', 'deploy.mcp' )( async ( From 43ebd8efa326749d552621debb3dd0a3b83ed9ff Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Wed, 2 Dec 2026 17:17:20 -0600 Subject: [PATCH 3/3] chore(mcp): clean up publishing fixtures in dependency order --- apps/sim/scripts/test-mcp-publishing-e2e.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/sim/scripts/test-mcp-publishing-e2e.ts b/apps/sim/scripts/test-mcp-publishing-e2e.ts index db4fc25dd36..166e188c146 100644 --- a/apps/sim/scripts/test-mcp-publishing-e2e.ts +++ b/apps/sim/scripts/test-mcp-publishing-e2e.ts @@ -168,6 +168,7 @@ try { } } finally { try { + await sql`delete from workspace where id = ${workspaceId}` await sql`delete from project where id = ${workspaceId}` await sql`delete from "user" where id in ${sql(actors.map((actor) => actor.id))}` } finally {