Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Report terminated MCP single-op calls (#6187)
Oleg Isonend4a876bc3583
diff --git a/packages/cli/src/commands/mcp.test.ts b/packages/cli/src/commands/mcp.test.tsindex ff096ba9fb..6caabfd525 100644--- a/packages/cli/src/commands/mcp.test.ts+++ b/packages/cli/src/commands/mcp.test.ts@@ -14,7 +14,12 @@ import {} from "@webstudio-is/project-build/mcp";import { publicApiOperations } from "@webstudio-is/protocol";import { updatePersistedMcpCheckpoint } from "./mcp-checkpoint";-import { __testing__, mcpOptions, prepareMcpProjectSession } from "./mcp";+import {+ __testing__,+ mcpOptions,+ mcpSingleOpCall,+ prepareMcpProjectSession,+} from "./mcp";const {assertSingleOpCallToolSupported,@@ -26,6 +31,7 @@ const {createMcpStatusReporter,getLoadedProjectSessionSnapshot,getMcpOperationInput,+ reportMcpSingleOpCallTermination,reportMcpRunTermination,createMcpRunTerminationController,parseMcpRunCalls,@@ -841,6 +847,106 @@ test("formats MCP run failures as structured JSON payloads", () => {});});+test("reports termination while inserting a styled nested SVG", async () => {+ const operation = publicApiOperations.find(+ ({ command }) => command === "insert-fragment"+ );+ if (operation === undefined) {+ throw new Error("Expected insert-fragment operation");+ }+ const executeOperation = vi.fn(async () => {+ throw new Error("Simulated interrupted operation");+ });+ const core = createProjectSessionMcpCore({+ operations: [operation],+ createProjectSession: () => {+ throw new Error("insert-fragment must use executeOperation");+ },+ executeOperation,+ });+ const fragment = `<ws.element ws:tag="a">+ <ws.element ws:tag="svg" ws:style={css\`pointer-events: none;\`}>+ <ws.element ws:tag="path" />+ </ws.element>+ </ws.element>`;++ await expect(+ core.callTool({+ name: "insert-fragment",+ input: { parentInstanceId: "body", fragment },+ dryRun: true,+ })+ ).rejects.toThrow("Simulated interrupted operation");+ expect(executeOperation).toHaveBeenCalledOnce();++ const writeResult = vi.fn();+ const setExitCode = vi.fn();+ reportMcpSingleOpCallTermination({+ termination: { type: "signal", signal: "SIGTERM" },+ tool: "insert-fragment",+ elapsedMs: 123,+ writeStatus: vi.fn(),+ writeResult,+ setExitCode,+ });++ expect(writeResult).toHaveBeenCalledWith({+ ok: false,+ error: {+ code: "MCP_CALL_TERMINATED",+ message:+ "MCP single-op-call insert-fragment terminated before returning a result.",+ },+ meta: {+ elapsedMs: 123,+ termination: { type: "signal", signal: "SIGTERM" },+ },+ });+ expect(setExitCode).not.toHaveBeenCalled();+});++test("installs termination reporting for an active MCP single-op call", async () => {+ const previousExitCode = process.exitCode;+ const consoleInfo = vi.spyOn(console, "info").mockImplementation(() => {});+ const stderrWrite = vi+ .spyOn(process.stderr, "write")+ .mockImplementation(() => true);++ try {+ const call = mcpSingleOpCall({+ tool: "insert-fragment",+ input: "{invalid-json",+ dryRun: true,+ });+ process.emit("beforeExit", 0);+ await expect(call).rejects.toBeDefined();++ const results = consoleInfo.mock.calls.map(([output]) =>+ JSON.parse(String(output))+ );+ expect(results).toEqual([+ {+ ok: false,+ error: {+ code: "MCP_CALL_TERMINATED",+ message:+ "MCP single-op-call insert-fragment terminated before returning a result.",+ },+ meta: {+ elapsedMs: expect.any(Number),+ termination: { type: "beforeExit", exitCode: 0 },+ },+ },+ ]);+ expect(stderrWrite).toHaveBeenCalledWith(+ "[webstudio mcp] single-op-call insert-fragment terminated: MCP single-op-call insert-fragment terminated before returning a result.\n"+ );+ expect(process.exitCode).toBe(1);+ } finally {+ process.exitCode = previousExitCode;+ }+});+test("preserves completed calls when a run terminates during an asset query preview", () => {const completedResults = ["status",diff --git a/packages/cli/src/commands/mcp.ts b/packages/cli/src/commands/mcp.tsindex 04f408c594..27e568f080 100644--- a/packages/cli/src/commands/mcp.ts+++ b/packages/cli/src/commands/mcp.ts@@ -701,6 +701,43 @@ type McpRunTermination =| { type: "beforeExit"; exitCode: number }| { type: "signal"; signal: NodeJS.Signals };+const reportMcpSingleOpCallTermination = ({+ termination,+ tool,+ elapsedMs,+ writeStatus = (message) => stderr.write(`${message}\n`),+ writeResult = printJson,+ setExitCode = (code) => {+ process.exitCode = code;+ },+}: {+ termination: McpRunTermination;+ tool: string;+ elapsedMs: number;+ writeStatus?: (message: string) => void;+ writeResult?: (result: unknown) => void;+ setExitCode?: (code: number) => void;+}) => {+ const error = {+ code: "MCP_CALL_TERMINATED",+ message: `MCP single-op-call ${tool} terminated before returning a result.`,+ };+ const payload = createMcpSingleOpCallErrorPayload({ error, elapsedMs });+ writeStatus(+ formatMcpStatusLine(`single-op-call ${tool} terminated: ${error.message}`)+ );+ writeResult({+ ...payload,+ meta: {+ ...payload.meta,+ termination,+ },+ });+ if (termination.type === "beforeExit") {+ setExitCode(1);+ }+};+const mcpRunTerminationCleanupTimeout = 5000;const reportMcpRunTermination = ({@@ -816,12 +853,14 @@ const installMcpRunTerminationHandlers = ({results,startedAt,disposeHost,+ reportTermination = reportMcpRunTermination,}: {getActiveCall: () => ActiveMcpRunCall | undefined;totalCalls: number;results: unknown[];startedAt: number;disposeHost: () => Promise<void>;+ reportTermination?: typeof reportMcpRunTermination;}) => {const controller = createMcpRunTerminationController({getActiveCall,@@ -829,6 +868,7 @@ const installMcpRunTerminationHandlers = ({results,startedAt,disposeHost,+ reportTermination,});const terminationSignals = ["SIGHUP", "SIGINT", "SIGTERM"] as const;const signalHandlers = new Map<NodeJS.Signals, () => void>();@@ -1284,12 +1324,40 @@ export const mcpSingleOpCall = async (options: McpSingleOpCallOptions) => {`single-op-call ${tool} started${options.dryRun === true ? " (dry run)" : ""}`)}\n`);+ let activeCall: ActiveMcpRunCall | undefined = { number: 1, tool };+ let didTerminate = false;+ let disposeHost: () => Promise<void> = async () => undefined;+ const disposeTerminationHandlers = installMcpRunTerminationHandlers({+ getActiveCall: () => activeCall,+ totalCalls: 1,+ results: [],+ startedAt,+ disposeHost: () => disposeHost(),+ reportTermination: ({ termination, activeCall, elapsedMs }) => {+ didTerminate = true;+ reportMcpSingleOpCallTermination({+ termination,+ tool: activeCall.tool,+ elapsedMs,+ });+ },+ });try {assertSingleOpCallToolSupported(tool);const input = await parseMcpSingleOpCallInput(options);validateSingleOpCallInput(tool, input);+ if (didTerminate) {+ throw new HandledCliError();+ }await withMcpHost(- () => createCliMcpHost({ projectId: options.project }),+ async () => {+ const mcpHost = await createCliMcpHost({ projectId: options.project });+ disposeHost = mcpHost.dispose;+ if (didTerminate) {+ throw new HandledCliError();+ }+ return mcpHost;+ },async ({ host, apiContract, scope }) => {assertMcpToolServerSupport(tool, apiContract);const core = createCliMcpCore(host);@@ -1297,20 +1365,33 @@ export const mcpSingleOpCall = async (options: McpSingleOpCallOptions) => {tool === "checkpoint.ack"? await readPersistedMcpCheckpoint(scope): undefined;+ if (didTerminate) {+ throw new HandledCliError();+ }await assertPersistedMcpCheckpointAcknowledged(tool,core.listTools(),scope);+ if (didTerminate) {+ throw new HandledCliError();+ }if (options.refresh === true && tool !== "refresh") {await core.callTool({ name: "refresh" });+ if (didTerminate) {+ throw new HandledCliError();+ }}const result = await core.callTool({name: tool,input,dryRun: options.dryRun,});+ if (didTerminate) {+ throw new HandledCliError();+ }if (isMcpToolCallFailure(result)) {+ activeCall = undefined;stderr.write(`${formatMcpStatusLine(`single-op-call ${tool} failed in ${Date.now() - startedAt}ms`@@ -1332,9 +1413,13 @@ export const mcpSingleOpCall = async (options: McpSingleOpCallOptions) => {structuredContent: result.structuredContent,scope,});+ if (didTerminate) {+ throw new HandledCliError();+ }const session = result.structuredContent.meta.session;const committed =session === undefined ? "" : `; committed=${session.committed}`;+ activeCall = undefined;stderr.write(`${formatMcpStatusLine(`single-op-call ${tool} succeeded in ${Date.now() - startedAt}ms${committed}`@@ -1348,6 +1433,10 @@ export const mcpSingleOpCall = async (options: McpSingleOpCallOptions) => {});} catch (error) {+ activeCall = undefined;+ if (didTerminate) {+ throw new HandledCliError();+ }if (isHandledCliError(error)) {throw error;}@@ -1362,6 +1451,9 @@ export const mcpSingleOpCall = async (options: McpSingleOpCallOptions) => {);printJson(payload);throw new HandledCliError();+ } finally {+ activeCall = undefined;+ disposeTerminationHandlers();}};@@ -1755,6 +1847,7 @@ export const __testing__ = {meta: { elapsedMs },}),createMcpRunErrorPayload,+ reportMcpSingleOpCallTermination,reportMcpRunTermination,createMcpRunTerminationController,createMcpRunCheckpointStopPayload,