AI-native design editor. Open-source Figma alternative.
fix(mcp): support scoped save paths
Danila Poyarkove0d76ca72ac0
diff --git a/packages/mcp/src/tool/registration.ts b/packages/mcp/src/tool/registration.tsindex 1aac1f610..149b7f88d 100644--- a/packages/mcp/src/tool/registration.ts+++ b/packages/mcp/src/tool/registration.ts@@ -65,16 +65,20 @@ export function registerTools(mcpServer: McpServer, options: RegisterToolsOptionregister('save_file',{- description:- 'Save the current document to disk. Uses the existing file path if available, otherwise prompts for a location.',- inputSchema: z.object({})+ description: resolvedRoot+ ? `Save the current document to disk. If path is provided, it must be inside ${resolvedRoot}.`+ : 'Save the current document to disk. Uses the existing file path if available, otherwise prompts for a location.',+ inputSchema: resolvedRoot+ ? z.object({ path: z.string().describe('Optional absolute path for the .fig file').optional() })+ : z.object({})},- async () => {+ async (args: { path?: string }) => {try {- const result = await sendRpc({ command: 'save_file' })+ const safePath = args.path && resolvedRoot ? resolveSafePath(args.path, resolvedRoot) : undefined+ const result = await sendRpc({ command: 'save_file', args: { path: safePath } })const res = result as { ok?: boolean; error?: string }if (res.ok === false) return fail(new Error(res.error))- return ok({ saved: true })+ return ok({ saved: true, ...(safePath ? { path: safePath } : {}) })} catch (e) {return fail(e)}diff --git a/src/app/automation/bridge/file-handlers.ts b/src/app/automation/bridge/file-handlers.tsindex 1f5115755..f92b60701 100644--- a/src/app/automation/bridge/file-handlers.ts+++ b/src/app/automation/bridge/file-handlers.ts@@ -3,8 +3,14 @@ import { openFileFromPath } from '@/app/shell/menu/use'import { createTab, openFileInNewTab } from '@/app/tabs'import { isTauri } from '@/app/tauri/env'-export async function handleSaveFile(store: EditorStore): Promise<unknown> {+export async function handleSaveFile(store: EditorStore, args: unknown): Promise<unknown> {+ const path = (args as { path?: string }).path+ if (path) {+ store.setPlannedFilePath(path)+ await ensureTauriParentDirectory(path)+ }await store.saveFigFile()+ if (path) store.startWatchingCurrentFile()return { ok: true }}diff --git a/tests/engine/mcp/server.test.ts b/tests/engine/mcp/server.test.tsindex adbaf7f91..216f89a58 100644--- a/tests/engine/mcp/server.test.ts+++ b/tests/engine/mcp/server.test.ts@@ -16,15 +16,22 @@ import {import { startServer, paramToZod } from '#mcp/server'+interface MockBrowserRequest {+ command: string+ args?: unknown+}+interface MockBrowser {ws: WebSocketgraph: SceneGraph+ requests: MockBrowserRequest[]close: () => void}function connectMockBrowser(port: number, graph: SceneGraph): Promise<MockBrowser> {return new Promise((resolve, reject) => {const ws = new WebSocket(`ws://127.0.0.1:${port}`)+ const requests: MockBrowserRequest[] = []const token = 'test-token-' + Date.now()ws.on('open', () => {@@ -41,6 +48,7 @@ function connectMockBrowser(port: number, graph: SceneGraph): Promise<MockBrowsetry {const command = msg.command+ requests.push({ command, args: msg.args })const args = msg.args as { name?: string; args?: Record<string, unknown> } | undefinedlet result: unknown@@ -51,6 +59,8 @@ function connectMockBrowser(port: number, graph: SceneGraph): Promise<MockBrowseapi.currentPage = api.wrapNode(graph.getPages()[0].id)result = await def.execute(api, args.args ?? {})if (def.mutates) computeAllLayouts(graph)+ } else if (command === 'save_file' || command === 'new_document' || command === 'open_file') {+ result = {}} else {result = executeRpcCommand(graph, command, args ?? {})}@@ -68,7 +78,7 @@ function connectMockBrowser(port: number, graph: SceneGraph): Promise<MockBrowse}})- resolve({ ws, graph, close: () => ws.close() })+ resolve({ ws, graph, requests, close: () => ws.close() })})ws.on('error', reject)@@ -262,6 +272,71 @@ describe('MCP server with mcpRoot', () => {httpServer.close()})+ test('save_file accepts an explicit path inside mcpRoot', async () => {+ const {+ app,+ wss,+ close: closeServer+ } = startServer({ httpPort: 0, wsPort: 0, mcpRoot: '/tmp' })+ const httpServer = serve({ fetch: app.fetch, port: 0, hostname: '127.0.0.1' })+ const actualWsPort = await waitForWsListening(wss)++ const graph = new SceneGraph()+ const browser = await connectMockBrowser(actualWsPort, graph)++ const client = new Client({ name: 'test-root-save', version: '0.0.0' })+ const transport = new StreamableHTTPClientTransport(+ new URL(`http://127.0.0.1:${(httpServer.address() as AddressInfo).port}/mcp`)+ )+ await client.connect(transport)++ const result = await client.callTool({+ name: 'save_file',+ arguments: { path: '/tmp/unicode/пример.fig' }+ })++ expect(result.isError).not.toBe(true)+ const request = browser.requests.find((item) => item.command === 'save_file')+ expect(request?.args).toEqual({ path: '/tmp/unicode/пример.fig' })++ await client.close()+ browser.close()+ closeServer()+ httpServer.close()+ })++ test('save_file rejects paths outside mcpRoot', async () => {+ const {+ app,+ wss,+ close: closeServer+ } = startServer({ httpPort: 0, wsPort: 0, mcpRoot: '/tmp' })+ const httpServer = serve({ fetch: app.fetch, port: 0, hostname: '127.0.0.1' })+ const actualWsPort = await waitForWsListening(wss)++ const graph = new SceneGraph()+ const browser = await connectMockBrowser(actualWsPort, graph)++ const client = new Client({ name: 'test-root-save-outside', version: '0.0.0' })+ const transport = new StreamableHTTPClientTransport(+ new URL(`http://127.0.0.1:${(httpServer.address() as AddressInfo).port}/mcp`)+ )+ await client.connect(transport)++ const result = await client.callTool({+ name: 'save_file',+ arguments: { path: '/var/tmp/out.fig' }+ })++ expect(result.isError).toBe(true)+ expect(browser.requests.some((item) => item.command === 'save_file')).toBe(false)++ await client.close()+ browser.close()+ closeServer()+ httpServer.close()+ })+test('does not register open_file when mcpRoot is null', async () => {const { app, wss, close: closeServer } = startServer({ httpPort: 0, wsPort: 0 })const httpServer = serve({ fetch: app.fetch, port: 0, hostname: '127.0.0.1' })