Skip to content

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

fix(mcp): say why export_image returned no image (#1222)

Danila Poyarkov108543a9872a
diff --git a/changes/1222/fixed.md b/changes/1222/fixed.md
new file mode 100644
index 000000000..c61a2032a
--- /dev/null
+++ b/changes/1222/fixed.md
@@ -0,0 +1 @@
+- `export_image` says why it returned no image: an ID that is not in the document, layers that are all hidden, a render that came back empty, or, in the desktop app, a document whose canvas has no renderer, instead of always answering "No visible nodes to export".
diff --git a/packages/core/src/tools/vector/export.ts b/packages/core/src/tools/vector/export.ts
index dd3b780e7..90b16f58e 100644
--- a/packages/core/src/tools/vector/export.ts
+++ b/packages/core/src/tools/vector/export.ts
@@ -123,8 +123,10 @@ export const exportImage = defineTool({
const format = args.format.toUpperCase() as RasterExportFormat
const requestedScale = args.scale
const maxEdge = args.maxEdge
+ const missing = ids.find((id) => !figma.getNodeById(id))
+ if (missing) return { error: `Node "${missing}" not found` }
const nodes = ids.map((id) => figma.getNodeById(id)).filter((node) => node !== null)
- if (nodes.length === 0) return { error: 'No visible nodes to export' }
+ if (!nodes.some((node) => node.visible)) return { error: 'No visible nodes to export' }
const bounds = nodes.reduce(
(result, node) => {
const box = node.absoluteBoundingBox
@@ -149,7 +151,9 @@ export const exportImage = defineTool({
scale: boundedScale,
format
})
- if (!data || data.length === 0) return { error: 'No visible nodes to export' }
+ if (!data || data.length === 0) {
+ return { error: 'The renderer returned no image for these nodes' }
+ }
const base64 = fromUint8Array(data)
const mimeMap = { PNG: 'image/png', JPG: 'image/jpeg', WEBP: 'image/webp' } as const
return {
diff --git a/src/app/automation/bridge/export-handlers.ts b/src/app/automation/bridge/export-handlers.ts
index 0e45f6e87..7674d858e 100644
--- a/src/app/automation/bridge/export-handlers.ts
+++ b/src/app/automation/bridge/export-handlers.ts
@@ -5,6 +5,26 @@ import { selectionToJSX } from '@open-pencil/design-jsx'
import { sceneNodesToTailwindJSX } from '@open-pencil/dom-css/export'
import type { AutomationTarget } from '@/app/automation/bridge/target'
+import type { EditorStore } from '@/app/editor/active-store'
+
+/**
+ * Raster exports draw with the document's canvas renderer, which exists only once its canvas
+ * has created a WebGL surface; without one, say so instead of returning an empty image.
+ */
+export function renderAutomationImage(
+ store: EditorStore,
+ nodeIds: string[],
+ scale: number,
+ format: RasterExportFormat,
+ pageId?: string
+): Promise<Uint8Array | null> {
+ if (!store.renderer) {
+ throw new Error(
+ 'This document has no canvas renderer, so it cannot be exported as an image: its canvas has not started or could not create a WebGL surface'
+ )
+ }
+ return store.renderExportImage(nodeIds, scale, format, pageId)
+}
type ExportArgs = { nodeIds?: string[]; scope?: 'page'; scale?: number; format?: string }
@@ -21,12 +41,13 @@ export async function handleExport(target: AutomationTarget, args: unknown): Pro
const exportArgs = args as ExportArgs | undefined
const nodeIds = exportNodeIds(target, exportArgs)
if (nodeIds.length === 0) throw new Error('No nodes to export')
- const data = await store.renderExportImage(
+ const data = await renderAutomationImage(
+ store,
nodeIds,
exportArgs?.scale ?? 1,
(exportArgs?.format ?? 'PNG') as RasterExportFormat
)
- if (!data) throw new Error('Export failed')
+ if (!data) throw new Error('The renderer returned no image for these layers')
const base64 = fromUint8Array(data)
return {
ok: true,
diff --git a/src/app/automation/bridge/figma-factory.ts b/src/app/automation/bridge/figma-factory.ts
index bc6219496..ae1173f75 100644
--- a/src/app/automation/bridge/figma-factory.ts
+++ b/src/app/automation/bridge/figma-factory.ts
@@ -1,6 +1,7 @@
import { FigmaAPI } from '@open-pencil/core/figma-api'
import { createCanvasKitRasterCodec } from '@open-pencil/core/io/formats/raster'
+import { renderAutomationImage } from '@/app/automation/bridge/export-handlers'
import type { EditorStore } from '@/app/editor/active-store'
import { listFamilies, listFonts, loadFont } from '@/app/editor/fonts'
@@ -27,8 +28,14 @@ export function makeFigmaFromStore(
},
zoom: store.state.zoom
}
- api.exportImage = (nodeIds, opts) =>
- store.renderExportImage(nodeIds, opts.scale ?? 1, opts.format ?? 'PNG', opts.pageId ?? pageId)
+ api.exportImage = async (nodeIds, opts) =>
+ renderAutomationImage(
+ store,
+ nodeIds,
+ opts.scale ?? 1,
+ opts.format ?? 'PNG',
+ opts.pageId ?? pageId
+ )
if (store.renderer) api.rasterCodec = createCanvasKitRasterCodec(store.renderer.ck)
api.loadFontAsync = async ({ family, style }) => {
await loadFont(family, style).catch(() => null)
diff --git a/tests/app/automation/bridge/export.test.ts b/tests/app/automation/bridge/export.test.ts
index 0c9298ba4..5cd9a4da7 100644
--- a/tests/app/automation/bridge/export.test.ts
+++ b/tests/app/automation/bridge/export.test.ts
@@ -167,3 +167,19 @@ describe('automation export of a page that is not on screen', () => {
expect(store.state.currentPageId).toBe(shown)
})
})
+
+describe('automation export without a canvas renderer', () => {
+ test('export_image says the document has no renderer', async () => {
+ const store = createEditorStore()
+ stores.push(store)
+ const pageId = store.state.currentPageId
+ const frame = store.graph.createNode('FRAME', pageId, { width: 40, height: 30, fills: [RED] })
+
+ const response = handleTargetCommand(target(store, pageId), 'tool', {
+ name: 'export_image',
+ args: { ids: [frame.id] }
+ })
+
+ await expect(response).rejects.toThrow('This document has no canvas renderer')
+ })
+})
diff --git a/tests/engine/tools/export-image.test.ts b/tests/engine/tools/export-image.test.ts
index d07be795e..5f4364a43 100644
--- a/tests/engine/tools/export-image.test.ts
+++ b/tests/engine/tools/export-image.test.ts
@@ -56,4 +56,41 @@ describe('export_image tool', () => {
expect(calls[0]?.scale).toBe(1)
})
+
+ test('names an ID that is not in the document instead of reporting nothing visible', async () => {
+ const { figma } = setupToolTest()
+ const frame = figma.createFrame()
+ figma.exportImage = async () => new Uint8Array([1])
+
+ const result = await getTool('export_image').execute(figma, { ids: [frame.id, '0:404'] })
+
+ expect(result).toEqual({ error: 'Node "0:404" not found' })
+ })
+
+ test('reports hidden layers as nothing visible without rendering them', async () => {
+ const { figma } = setupToolTest()
+ const frame = figma.createFrame()
+ frame.visible = false
+ let rendered = false
+ figma.exportImage = async () => {
+ rendered = true
+ return new Uint8Array([1])
+ }
+
+ const result = await getTool('export_image').execute(figma, { ids: [frame.id] })
+
+ expect(result).toEqual({ error: 'No visible nodes to export' })
+ expect(rendered).toBe(false)
+ })
+
+ test('tells an empty render apart from nothing visible', async () => {
+ const { figma } = setupToolTest()
+ const frame = figma.createFrame()
+ frame.resize(40, 30)
+ figma.exportImage = async () => null
+
+ const result = await getTool('export_image').execute(figma, { ids: [frame.id] })
+
+ expect(result).toEqual({ error: 'The renderer returned no image for these nodes' })
+ })
})