# Figma MCP URL: /ai-integration/figma-mcp Source: https://github.com/daangn/seed-design/blob/dev/docs/content/ai-integration/(mcp)/figma-mcp.mdx Provides Model Context Protocol (MCP) to convert Figma designs to React code using AI Agents. ## Quick Setup You can issue a Personal Access Token (PAT) from [Figma Settings](https://www.figma.com/settings). - **If you have PAT**: You can use REST API mode by copying the Figma URL and passing it to an AI Agent. - **If you don't have PAT**: You need to use WebSocket mode to fetch selected layers in real-time through the Figma plugin. For detailed differences between each method, see the [Installation](#installation) section below. ```bash claude mcp add seed-design-figma \ --env FIGMA_PERSONAL_ACCESS_TOKEN=${FIGMA_PERSONAL_ACCESS_TOKEN} \ -- bunx -y @seed-design/mcp@latest ``` ```bash codex mcp add seed-design-figma \ --env FIGMA_PERSONAL_ACCESS_TOKEN=${FIGMA_PERSONAL_ACCESS_TOKEN} \ -- bunx -y @seed-design/mcp@latest ``` Add the following to your `~/.gemini/settings.json` file: ```json { "mcpServers": { "seed-design-figma": { "command": "bunx", "args": ["-y", "@seed-design/mcp@latest"], "env": { "FIGMA_PERSONAL_ACCESS_TOKEN": "" } } } } ``` Add the following to your `~/Library/Application Support/Claude/claude_desktop_config.json` file: ```json { "mcpServers": { "seed-design-figma": { "command": "bunx", "args": ["-y", "@seed-design/mcp@latest"], "env": { "FIGMA_PERSONAL_ACCESS_TOKEN": "" } } } } ``` First, run the WebSocket server in the background and install the [MCP plugin](https://www.figma.com/community/plugin/1496384010980477154) from Figma. The Bun runtime is required to run the WebSocket server. Please use the `--bun` flag. ```bash bunx --bun @seed-design/mcp@latest socket ``` Then register the MCP server. ```bash claude mcp add seed-design-figma \ -- bunx -y @seed-design/mcp@latest ``` ```bash codex mcp add seed-design-figma \ -- bunx -y @seed-design/mcp@latest ``` Add the following to your `~/.gemini/settings.json` file: ```json { "mcpServers": { "seed-design-figma": { "command": "bunx", "args": ["-y", "@seed-design/mcp@latest"] } } } ``` Add the following to your `~/Library/Application Support/Claude/claude_desktop_config.json` file: ```json { "mcpServers": { "seed-design-figma": { "command": "bunx", "args": ["-y", "@seed-design/mcp@latest"] } } } ``` ## Installation ### Choose Figma Access Method **Fetch layer information using Figma URL and Personal Access Token.** 1. Issue a Personal Access Token from [Figma Settings](https://www.figma.com/settings). Grant the following permissions to the token: - `file_content:read` - `file_metadata:read` - `file_variables:read` - `library_assets:read` - `library_content:read` - `team_library_content:read` - `file_dev_resources:read` 2. Allow the MCP to use the token through an environment variable. Set the `FIGMA_PERSONAL_ACCESS_TOKEN` environment variable in the [Register MCP Server](#register-mcp-server) step. **Fetch selected layer information in real-time from the Figma app through WebSocket server and Figma plugin.** Real-time selection-based tools like `get_selection` only work with this method. 1. Run the WebSocket server in the background. The Bun runtime is required to run the WebSocket server. Please use the `--bun` flag. ```bash bunx --bun @seed-design/mcp@latest socket ``` 2. Install and run the [MCP plugin](https://www.figma.com/community/plugin/1496384010980477154) from Figma. ### Register MCP Server #### Limiting Tool Scope with `--mode` Option When running the MCP server, specify the `--mode` option to register only tools available in that mode, reducing context size. If the `--mode` option is not provided, the default `all` mode is used. | Mode | Tools | Note | | --------------- | -------------------- | -------------------------------------------------------- | | `all` (default) | Register all tools | If PAT exists, REST is prioritized; WS-only tools use WS | | `rest` | REST API tools only | WS-only tools like `get_selection` are not registered | | `websocket` | WebSocket tools only | All tools currently work with WebSocket; PAT is ignored | You must set the `FIGMA_PERSONAL_ACCESS_TOKEN` environment variable. If you always use only REST API, you can add the `--mode=rest` flag. ```json title=".mcp.json" { "mcpServers": { "seed-design-figma": { "command": "bunx", "args": ["-y", "@seed-design/mcp@latest"], "env": { "FIGMA_PERSONAL_ACCESS_TOKEN": "${FIGMA_PERSONAL_ACCESS_TOKEN}" } } } } ``` You don't need to set the `FIGMA_PERSONAL_ACCESS_TOKEN` environment variable. If you always use only WebSocket, you can add the `--mode=websocket` flag. ```json title=".mcp.json" { "mcpServers": { "seed-design-figma": { "command": "bunx", "args": ["-y", "@seed-design/mcp@latest"] } } } ``` ### Using the Tool 1. Copy the layer URL from Figma (⌘ + L). 2. Request from the LLM tool. ``` Please get the React code for this layer: https://www.figma.com/design/abc123/MyDesign?node-id=123-456 ``` 1. Select a layer in Figma. 2. Request from the LLM tool. ``` Please get the React code for the currently selected layer. ``` ## Main Tools List ### REST API + WebSocket Common When passing a Figma URL or `fileKey` + `nodeId`, it works via REST API; when passing only `nodeId`, it works via WebSocket. Registered in all modes. | Tool | Description | | ------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | `get_node_info` | Returns structural information of a layer. | | `get_nodes_info` | Returns structural information of multiple layers at once. | | `get_node_react_code` | Generates React code for a layer. | | `get_component_info` | Returns component key and property definitions. | | `find_nodes` | Search child layers by name (regex) and return ID list. | | `export_node_as_image` | Renders layer as image (PNG, JPG) and returns it. | | `export_node_as_svg` | Returns layer as SVG markup (text). | | `get_annotations` | Retrieves [Annotations](https://help.figma.com/hc/en-us/articles/20774752502935-Add-measurements-and-annotate-designs) from layer and child layers. | | `retrieve_color_variable_names` | Returns list of Crispy color variable names. | ### WebSocket Only Requires communication with Figma plugin. Registered in `websocket` or `all` mode. | Tool | Description | | ------------------- | ----------------------------------------------------------------------------------------------------------------------------- | | `get_selection` | Returns ID of currently selected layer. | | `get_document_info` | Returns information about the currently open Figma document. | | `add_annotations` | Adds [Annotations](https://help.figma.com/hc/en-us/articles/20774752502935-Add-measurements-and-annotate-designs) to a layer. | | `join_channel` | Join a specific channel. | ## Using Custom Settings By default, `get_node_react_code` only recognizes Crispy Figma components. If you use an internal design system or custom components, you can write a ComponentHandler to map Figma components to your desired React components. Add the `--config` flag to the MCP server configuration to load a custom configuration file. The `--config` flag requires Bun runtime. Please use the `--bun` flag. ```bash bunx --bun @seed-design/mcp@latest --config /path/to/config.ts ``` ### Supported Configuration File Formats `@seed-design/mcp` supports configuration files with the following extensions: - `.js` - `.mjs` - `.ts` - `.mts` ### Configuration File Example ```typescript import { type react, defineComponentHandler, createElement, } from "@seed-design/figma"; export default { extend: { componentHandlers: [ (_deps: react.ComponentHandlerDeps) => defineComponentHandler( "figma_component_key", ({ componentProperties: props }) => { const tone = props.Tone.value.toLowerCase(); return createElement("CustomButton", { tone, }); }, ), ], }, } satisfies react.CreatePipelineConfig; ``` ### Writing ComponentHandler with Crispy Figma MCP You can check a [ComponentHandler example](https://github.com/daangn/seed-design/blob/dev/packages/figma/src/codegen/targets/react/component/handlers/slider.ts) that maps Crispy Figma Slider component to Crispy React ``. #### Copy the Prompt Copy the prompt below and save it as a file. ````markdown # ComponentHandler Implementation Guide ## Initial Context Gathering 1. **Analyze Figma Selection**: - Use `get_selection()` to examine the currently selected node(s) - If no selection exists, prompt the user to select a specific component node - Verify the selected node is a component that can be transformed 2. **Retrieve Component Information**: - Use `get_component_info(nodeId)` to extract detailed information about the component - Collect the component key, all component properties, and variant definitions - Identify any nested components or instances that might need special handling ## Component Analysis and Planning 3. **Determine Component Structure**: - Analyze the component's visual structure, layout, and behavior patterns - If the target React component implementation is unknown, request an example or props interface - Identify which Figma properties should map to which React props 4. **Define Transformation Strategy**: - Plan how to handle variants, properties, nested components and children - Determine if additional utilities or helper functions are needed - Consider edge cases and special requirements for this component ## Implementation 5. **Create Component Handler**: - Implement the handler using the following pattern: ```typescript import { createElement, defineComponentHandler, type InferComponentDefinition, type react, } from "@seed-design/figma"; // Define component properties based on Figma definition export type ComponentNameProperties = InferComponentDefinition<{ // Map all component properties from Figma // Example: PropertyName: { type: "VARIANT" | "TEXT" | "BOOLEAN" | "INSTANCE_SWAP"; variantOptions?: string[]; // For VARIANT type defaultValue?: string | boolean; }; }>; // Create and export the handler export const createComponentNameHandler = (_deps: react.ComponentHandlerDeps) => defineComponentHandler( "component-key-from-figma", // Replace with actual component key ({ componentProperties: props }) => { // Transform Figma properties to React props // Use conditionals for variants and property handling return createElement( "ComponentName", // The React component name { // Map properties appropriately propName: props.PropertyName.value, // Add additional props as needed }, [], // Nested elements if any (undefined or omitted if not needed) "Optional comment for accessibility or development notes, use sparingly", ); }, ); ``` ## Validation and Testing 6. **Verify Handler Functionality**: - Test the handler with different component variants - Ensure all properties are correctly mapped and transformed - Check that nested components and children are handled properly ## Best Practices - Create reusable helper functions for common transformation patterns - Handle edge cases and optional properties gracefully - Add descriptive comments for complex transformations - Consider performance implications for deeply nested components - Ensure type safety throughout the transformation process ```` #### Run the Prompt Execute the prompt by passing the saved prompt file and the component file to be implemented as context. ```bash @CustomButton.tsx @ComponentHandlerRule.md Please implement the ComponentHandler for the CustomButton component. ``` #### Check Results Review the ComponentHandler generated from the prompt execution. ```typescript import { createElement, defineComponentHandler, type InferComponentDefinition, type react, } from "@seed-design/figma"; export type CustomButtonProperties = InferComponentDefinition<{ tone: { type: "VARIANT"; variantOptions: ["neutral", "brand"]; }; }>; export const createCustomButtonHandler = (_deps: react.ComponentHandlerDeps) => defineComponentHandler( "figma_component_key", ({ componentProperties: props }) => { const tone = props.tone.value.toLowerCase(); return createElement("CustomButton", { tone, }); }, ); ``` #### Apply Configuration File Add the generated ComponentHandler to the configuration file. ```typescript import { type react } from "@seed-design/figma"; import { createCustomButtonHandler } from "./CustomButton"; export default { extend: { componentHandlers: [createCustomButtonHandler], }, } satisfies react.CreatePipelineConfig; ```