switch_variable_mode
Switch the variable mode on a Figma node to use values from a different mode in a variable collection. Provide node ID, collection ID, and mode ID.
Instructions
Switch the variable mode on a node for a specific collection. This changes which mode's values are used for bound variables.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| nodeId | Yes | The ID of the node to switch mode on | |
| collectionId | Yes | The ID of the variable collection | |
| modeId | Yes | The ID of the mode to switch to |
Implementation Reference
- The handler function for the 'switch_variable_mode' tool. It is registered as an MCP server.tool, takes nodeId, collectionId, and modeId as input parameters, sends the 'switch_variable_mode' command to Figma via WebSocket, and returns the result with mode switch confirmation.
// Switch Variable Mode Tool server.tool( "switch_variable_mode", "Switch the variable mode on a node for a specific collection. This changes which mode's values are used for bound variables.", { nodeId: z.string().describe("The ID of the node to switch mode on"), collectionId: z.string().describe("The ID of the variable collection"), modeId: z.string().describe("The ID of the mode to switch to"), }, async ({ nodeId, collectionId, modeId }) => { try { const result = await sendCommandToFigma("switch_variable_mode", { nodeId, collectionId, modeId, }); const typedResult = result as { nodeName: string; collectionName: string; modeName: string }; return { content: [ { type: "text", text: `Switched to mode "${typedResult.modeName}" for collection "${typedResult.collectionName}" on node "${typedResult.nodeName}"`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error switching variable mode: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } ); - src/talk_to_figma_mcp/tools/variable-tools.ts:123-159 (registration)The tool is registered via server.tool('switch_variable_mode', ...) inside registerVariableTools() in variable-tools.ts, which is called from registerTools() in tools/index.ts.
// Switch Variable Mode Tool server.tool( "switch_variable_mode", "Switch the variable mode on a node for a specific collection. This changes which mode's values are used for bound variables.", { nodeId: z.string().describe("The ID of the node to switch mode on"), collectionId: z.string().describe("The ID of the variable collection"), modeId: z.string().describe("The ID of the mode to switch to"), }, async ({ nodeId, collectionId, modeId }) => { try { const result = await sendCommandToFigma("switch_variable_mode", { nodeId, collectionId, modeId, }); const typedResult = result as { nodeName: string; collectionName: string; modeName: string }; return { content: [ { type: "text", text: `Switched to mode "${typedResult.modeName}" for collection "${typedResult.collectionName}" on node "${typedResult.nodeName}"`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error switching variable mode: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } ); - Zod schema for the 'switch_variable_mode' tool: three required string parameters - nodeId (the target node), collectionId (the variable collection), and modeId (the mode to switch to).
{ nodeId: z.string().describe("The ID of the node to switch mode on"), collectionId: z.string().describe("The ID of the variable collection"), modeId: z.string().describe("The ID of the mode to switch to"), }, - The sendCommandToFigma function is the WebSocket helper that sends the 'switch_variable_mode' command string (typed as FigmaCommand) to the Figma plugin via a WebSocket connection, managing the request lifecycle with timeouts and promise resolution.
export function sendCommandToFigma( command: FigmaCommand, params: unknown = {}, timeoutMs: number = 300000 ): Promise<unknown> { return new Promise((resolve, reject) => { // If not connected, try to connect first if (!ws || ws.readyState !== WebSocket.OPEN) { connectToFigma(); reject(new Error("Not connected to Figma. Attempting to connect...")); return; } // Check if we need a channel for this command const requiresChannel = command !== "join"; if (requiresChannel && !currentChannel) { reject(new Error("Must join a channel before sending commands")); return; } const id = uuidv4(); const request = { id, type: command === "join" ? "join" : "message", ...(command === "join" ? { channel: (params as any).channel, sessionId: SESSION_ID } : { channel: currentChannel }), message: { id, command, params: { ...(params as any), commandId: id, // Include the command ID in params }, }, }; // Set timeout for request const timeout = setTimeout(() => { if (pendingRequests.has(id)) { pendingRequests.delete(id); logger.error(`Request ${id} to Figma timed out after ${timeoutMs / 1000} seconds`); reject(new Error('Request to Figma timed out')); } }, timeoutMs); // Store the promise callbacks to resolve/reject later pendingRequests.set(id, { resolve, reject, timeout, lastActivity: Date.now() }); // Send the request logger.info(`Sending command to Figma: ${command}`); logger.debug(`Request details: ${JSON.stringify(request)}`); ws.send(JSON.stringify(request)); }); } - The type definition for 'switch_variable_mode' as a valid FigmaCommand value in the FigmaCommand union type, ensuring type safety when sending this command via the WebSocket helper.
| "switch_variable_mode" | "get_figjam_elements" | "create_sticky" | "set_sticky_text" | "create_shape_with_text" | "create_connector" | "create_section";