set_text_style_id
Apply a text style to a text node in Figma by specifying the node ID and the desired text style ID, enabling precise typography control through AI commands.
Instructions
Apply a text style to a text node in Figma
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| nodeId | Yes | The ID of the text node to modify | |
| textStyleId | Yes | The ID of the text style to apply |
Implementation Reference
- The tool handler for 'set_text_style_id'. Registers the tool on the MCP server with Zod schema validation for nodeId and textStyleId. The handler sends the command to Figma via WebSocket, and returns a formatted result message or an error.
// Set Text Style ID Tool server.tool( "set_text_style_id", "Apply a text style to a text node in Figma", { nodeId: z.string().describe("The ID of the text node to modify"), textStyleId: z.string().describe("The ID of the text style to apply"), }, async ({ nodeId, textStyleId }) => { try { const result = await sendCommandToFigma("set_text_style_id", { nodeId, textStyleId }); const typedResult = result as { name: string, textStyleId: string, styleName: string }; return { content: [ { type: "text", text: `Applied text style "${typedResult.styleName}" to node "${typedResult.name}"` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error setting text style: ${error instanceof Error ? error.message : String(error)}` } ] }; } } ); - Input schema for the 'set_text_style_id' tool, defining required string parameters: nodeId (the text node) and textStyleId (the style to apply).
{ nodeId: z.string().describe("The ID of the text node to modify"), textStyleId: z.string().describe("The ID of the text style to apply"), }, - src/talk_to_figma_mcp/tools/text-tools.ts:497-531 (registration)Tool registration via server.tool('set_text_style_id', ...) inside the registerTextTools function, which is called from the central registerTools function in tools/index.ts.
// Set Text Style ID Tool server.tool( "set_text_style_id", "Apply a text style to a text node in Figma", { nodeId: z.string().describe("The ID of the text node to modify"), textStyleId: z.string().describe("The ID of the text style to apply"), }, async ({ nodeId, textStyleId }) => { try { const result = await sendCommandToFigma("set_text_style_id", { nodeId, textStyleId }); const typedResult = result as { name: string, textStyleId: string, styleName: string }; return { content: [ { type: "text", text: `Applied text style "${typedResult.styleName}" to node "${typedResult.name}"` } ] }; } catch (error) { return { content: [ { type: "text", text: `Error setting text style: ${error instanceof Error ? error.message : String(error)}` } ] }; } } ); - Type definition: 'set_text_style_id' is included in the FigmaCommand union type, which validates allowed command strings sent via WebSocket.
| "set_text_style_id" - The sendCommandToFigma helper function used by the handler to send the 'set_text_style_id' command over WebSocket to the Figma plugin.
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)); }); }