Skip to main content
Glama
arinspunk

Claude Talk to Figma MCP

by arinspunk

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

TableJSON Schema
NameRequiredDescriptionDefault
nodeIdYesThe ID of the node to switch mode on
collectionIdYesThe ID of the variable collection
modeIdYesThe 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)}`,
              },
            ],
          };
        }
      }
    );
  • 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";

Schema Changelog

Changes observed during successful MCP inspections.

  1. Removed
  2. Addedv0.8.2

TDQS

B3.4/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the core action but does not elaborate on side effects, whether the operation is reversible, required permissions, or how bound variables are updated. This is minimal for a mutation tool.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single sentence of 20 words, front-loaded with the action. It is concise and avoids redundancy, though it could be slightly expanded for clarity without losing efficiency.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the simplicity of the tool with three well-documented parameters and no output schema, the description is adequate but leaves gaps about behavioral context, such as what happens to the previous mode or whether the change is persistent.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema already provides descriptions for all three parameters with 100% coverage. The description does not add extra meaning beyond what is in the schema, so a baseline score of 3 is appropriate.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the verb 'Switch' and the resource 'variable mode on a node for a specific collection'. It also explains the effect by noting it changes which mode's values are used for bound variables. This distinguishes it from sibling tools that set variables or apply them.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage when you need to change the variable mode for a node-collection pair, but it does not explicitly state when to use this tool versus alternatives like apply_variable_to_node or set_variable. No exclusions or context are provided.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Deploy Server

Other Tools