Skip to content

AI-native design editor. Open-source Figma alternative.

Fix switch_page not persisting across MCP tool calls

Danila Poyarkov2956507783a8
diff --git a/src/automation/server.ts b/src/automation/server.ts
index 9eae53c8f..4bb6c92d0 100644
--- a/src/automation/server.ts
+++ b/src/automation/server.ts
@@ -76,6 +76,11 @@ export function connectAutomation(getStore: () => EditorStore) {
if (!def) throw new Error(`Unknown tool: ${toolName}`)
const figma = makeFigma()
const result = await def.execute(figma, toolArgs)
+
+ if (figma.currentPageId !== store.state.currentPageId) {
+ void store.switchPage(figma.currentPageId)
+ }
+
if (def.mutates) {
computeAllLayouts(store.graph, store.state.currentPageId)
store.requestRender()
diff --git a/tests/engine/tools.test.ts b/tests/engine/tools.test.ts
index d3a42efac..ad3de43cf 100644
--- a/tests/engine/tools.test.ts
+++ b/tests/engine/tools.test.ts
@@ -489,6 +489,31 @@ describe('page tools', () => {
expect(figma.currentPage.name).toBe('Page 2')
})
+
+ test('switch_page persists across separate FigmaAPI instances (RPC simulation)', () => {
+ const { graph } = setup()
+ const switchPage = ALL_TOOLS.find((t) => t.name === 'switch_page')!
+ const getCurrentPage = ALL_TOOLS.find((t) => t.name === 'get_current_page')!
+ const createPage = ALL_TOOLS.find((t) => t.name === 'create_page')!
+
+ let currentPageId = graph.getPages()[0].id
+
+ function rpcCall(tool: (typeof ALL_TOOLS)[number], args: Record<string, unknown>) {
+ const figma = new FigmaAPI(graph)
+ figma.currentPage = figma.wrapNode(currentPageId)
+ const result = tool.execute(figma, args)
+ if (figma.currentPageId !== currentPageId) {
+ currentPageId = figma.currentPageId
+ }
+ return result
+ }
+
+ rpcCall(createPage, { name: 'Second' })
+ rpcCall(switchPage, { page: 'Second' })
+ const result = rpcCall(getCurrentPage, {}) as { id: string; name: string }
+
+ expect(result.name).toBe('Second')
+ })
})
describe('eval', () => {