> ## Documentation Index
> Fetch the complete documentation index at: https://docs.figma-console-mcp.southleft.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Code Connect

> Generate Code Connect files from your real code components, map Figma instances to code snippets, audit coverage and drift, and preview and publish through your repo's own Code Connect CLI.

# Code Connect

Code Connect links a Figma component to the code component that implements it. Dev Mode then shows a real snippet, such as `<Button size="lg" disabled />`, instead of generated markup, and AI assistants writing code from a design can use your components instead of inventing new ones.

The mappings live in your repository: `.figma.ts` template files plus a `figma.config.json`. Figma Console MCP writes, checks and publishes those files:

| Tool | What it does | Mode |
| - | - | - |
| `figma_generate_code_connect` | Reads a Figma component's properties and your code component's props, maps them, and returns (or writes) a `.figma.ts` template | Local / Cloud |
| `figma_get_code_connect_map` | For the instances in a frame or selection, returns the code component each maps to, with a snippet rendered from that instance's values | Local / Cloud |
| `figma_audit_code_connect` | Coverage and drift for a library against your repo, with a pass/fail status for CI | Local / Cloud |
| `figma_preview_code_connect` | Snippets rendered by Figma through your repo's Code Connect CLI | Local |
| `figma_publish_code_connect` | Publishes your Code Connect files with your repo's CLI: dry run first, then a confirmed publish | Local |
| `figma_unpublish_code_connect` | Removes published Code Connect, with the same dry run and confirmation | Local |

What this adds to the Code Connect CLI on its own:

* **Mapping from your real code.** Generate reads React props (TypeScript types, `cva` variants and defaults, `children`, native element props) or a Web Components `custom-elements.json` (React wrappers made with `@lit/react` take their props from it too; pass `manifestPath`), and maps values (`Size=Large` → `size="lg"`, `State=Disabled` → `disabled`). It never invents a prop the code doesn't declare.
* **Any Figma plan, published or not.** Component properties are read live through the Desktop Bridge plugin, so you can author and check mappings before a library is published.
* **Drift checks the CLI doesn't do.** Template files that use a property or variant value Figma no longer has, point at a deleted node, or that CLI 2.x would refuse.
* **A guarded publish.** The repo's CLI runs isolated, only after you confirm, and only what the dry run showed you gets published.

***

## Before you start

**Local Mode** starts with the core tools only. The Code Connect tools are in the `codeconnect` toolset. Your AI assistant switches it on with:

```javascript theme={null}
figma_enable_toolsets({ toolsets: ["codeconnect"] })
```

Or preload it by adding `codeconnect` to the `FIGMA_TOOLSETS` environment variable in your MCP config.

`figma_generate_code_connect` writes files, and the preview, publish and unpublish tools run code from your repo, so none of the four runs through `figma_call`. Call them by name. If your client doesn't refresh its tool list after `figma_enable_toolsets`, restart the client or set `FIGMA_TOOLSETS`. `figma_get_code_connect_map` and `figma_audit_code_connect` only read, so they also run through `figma_call`.

**Cloud Mode** lists every tool from the start but has no filesystem and can't run your CLI. It has generate, map and audit, working on file contents you pass in (see [Cloud Mode and Local Mode](#cloud-mode-and-local-mode)).

The server also ships a `code-connect` skill (Local Mode) that walks an AI assistant through the whole flow below.

***

## Setup

### The repository

Tell the tools where the repository is with `repoRoot` (an absolute path) on each call, or set `FIGMA_CODE_CONNECT_ROOT` in the server's environment. Every read and write stays inside that folder. In a monorepo, pass the workspace root as `repoRoot` and the package folder as `project`.

Add a `figma.config.json` to the project folder and **always set `include`**:

```json theme={null}
{
  "codeConnect": {
    "include": ["src/**/*.figma.ts"],
    "label": "React"
  }
}
```

Without `include`, the Code Connect CLI 2.x falls back to matching every `.tsx`/`.jsx` (or `.ts`/`.js`) file in a project with a `package.json`, and then refuses the whole project because those aren't template files. The tools report this, and generate returns a suggested config you can review before adding it. `label` is the Dev Mode tab (React, Web Components, SwiftUI…).

**For preview, publish and unpublish only**, install Figma's CLI, [`@figma/code-connect`](https://www.npmjs.com/package/@figma/code-connect), in the repo:

```bash theme={null}
npm install --save-dev @figma/code-connect
```

The tools use the copy installed between the project folder and `repoRoot`. They never download one. Preview needs CLI 1.5 or later.

**Editor types.** When the repo's `@figma/code-connect` ships `figma-types-no-require.d.ts` (1.4.9 and later), generated files start with a `/// <reference types="@figma/code-connect/figma-types-no-require" />` line so your editor types `figma.*`. Don't add Figma's types to `compilerOptions.types` in your main `tsconfig.json`: that list is exclusive and drops the implicit `@types/*`. If you want a tsconfig entry, use a separate `tsconfig.figma.json` that includes only `**/*.figma.ts`.

### Figma access

Generate, map and audit read Figma one of two ways:

* **The Desktop Bridge plugin** open in the library file. Works on any plan, published or not, and needs no token.
* **The REST API**, when the file isn't open or you pass a `figmaUrl` for another file. Needs `FIGMA_ACCESS_TOKEN`. Over REST, the audit sees published components only.

### Token scopes

| Scope | Needed for |
| - | - |
| **File content: Read** (`file_content:read`) | REST reads in generate, map and audit; also needed by publish, preview and unpublish |
| **Library content: Read** (`library_content:read`) | The audit's list of a library's components over REST |
| **Library assets: Read** (`library_assets:read`) | Finding where a component from another library lives, when that library isn't open in Figma |
| **Code Connect: Write** (`code_connect:write`) | Publish, preview and unpublish |

Publish, preview and unpublish need a **personal access token** (`figd_…`) in the server's `FIGMA_ACCESS_TOKEN`. OAuth tokens (`figu_…`) don't work with the Code Connect CLI. No tool takes a token as an argument, and the token is never put on a command line or returned in a result. See [Setup](https://docs.figma-console-mcp.southleft.com/setup.md) for creating the token.

**Plans.** Authoring, mapping, auditing and previewing need no particular plan. Publishing Code Connect works on Organization and Enterprise plans, and on Professional plans for one library and one framework: the first publish sets the plan's active library and framework (label), and changing them later needs a plan admin. The publish confirmation says so.

***

## The trust question

`node_modules/@figma/code-connect` is whatever your repository contains, so running "the Code Connect CLI" means running code from that folder. The first time preview, publish or unpublish runs a particular CLI in a server session, **you** are asked (not the AI) whether to trust it. The question names:

* the repository folder (your home folder shown as `~`),
* the CLI's folder inside it and its version,
* whether it will run with your Figma token.

Check that the folder is the project you meant. A repository path that came from a web page, an issue or a freshly downloaded starter kit is the case this question exists for. Trust is remembered for that exact CLI file for the rest of the server session. If the file changes (another version, edited, reinstalled), you're asked again.

A trust given for a dry run, which runs without the token, doesn't cover runs with the token (preview and the real publish or unpublish). Those ask again with the token wording.

Clients that can't show questions (and setups with `FIGMA_CONSOLE_CONFIRM_DESTRUCTIVE=off`) skip it. There, your client's own permission prompt for these tools is the gate, so don't auto-approve them. [Security](https://docs.figma-console-mcp.southleft.com/security.md#code-connect-cli) has the full list of how the CLI is isolated.

***

## Generate → audit → preview → publish

### 1. Generate the files

```javascript theme={null}
figma_generate_code_connect({
  repoRoot: "/Users/me/acme-ui",
  targets: [{ nodeId: "42:100" }]     // a component set, component, variant or instance
})
```

Pass up to 25 targets, or one target without `nodeId` for the current selection. The result has, per component:

* `file.content`: the `.figma.ts` template, with a mapping note per property.
* `mapping`: how each Figma property maps to a code prop, and how each value maps. A value is `exact` when the code declares the same word, ignoring case and separators (`Top Left` → `top-left`); `synonym` only when it doesn't (`Large` → `lg`, `Info` → `neutral` only if the code has no `info`).
* `unmatched`: Figma properties with no code prop, and code props with no Figma property.
* `nestedInstances` and `warnings`.

Fix mismatches with `propMap`, or point `codeComponent.path` at the right file, and generate again:

```javascript theme={null}
figma_generate_code_connect({
  repoRoot: "/Users/me/acme-ui",
  targets: [{
    nodeId: "42:100",
    codeComponent: { path: "src/components/Button.tsx", importFrom: "@acme/ui" },
    propMap: [{ figmaProperty: "Size", codeProp: "size", values: [{ figma: "Large", code: "lg" }] }]
  }]
})
```

Nothing is written until you add `write: true`. Existing files are never replaced unless you also pass `overwrite: true`.

### 2. Audit

```javascript theme={null}
figma_audit_code_connect({ repoRoot: "/Users/me/acme-ui" })
```

The audit reports coverage per label and per `figma.config.json` (mapped components, unmapped ones with a likely code counterpart) and drift: templates pointing at deleted nodes or variants, property names or variant values that no longer exist, missing layers, unpublished components, duplicate mappings, and files CLI 2.x would refuse. It is static and never runs your CLI. Fix errors first, then warnings, and audit again.

### 3. Preview (optional)

```javascript theme={null}
// The instances in a frame, with their own property values
figma_preview_code_connect({ repoRoot: "/Users/me/acme-ui", mode: "instances", nodeId: "120:4" })

// Property combinations of the components in some Code Connect files
figma_preview_code_connect({ repoRoot: "/Users/me/acme-ui", mode: "combinations", files: ["src/components/Button.figma.ts"] })
```

Preview runs your CLI with the token and sends the template source and property values to Figma (`api.figma.com`) to render, so it asks the trust question with the token wording. Values containing commas or line breaks, instance swaps, nested overrides and slot contents can't be passed to the CLI, so those snippets are listed under `approximations`.

### 4. Publish: dry run, then the real run

**Dry run first** (the default):

```javascript theme={null}
figma_publish_code_connect({ repoRoot: "/Users/me/acme-ui" })
```

The CLI parses exactly what would be published, without the token. The docs it found are linted against the live components, and the result has a `planId`. Review `docs`, `lint`, `unreadable`, `skipped` and `warnings`.

To publish, call again with **the same arguments** plus `dryRun: false` and that `planId`:

```javascript theme={null}
figma_publish_code_connect({ repoRoot: "/Users/me/acme-ui", dryRun: false, planId: "ccp_1a2b3c4d5e6f7a8b9c0d" })
```

You're asked to confirm. Publishing replaces the published Dev Mode snippets for those components and that label **for everyone who uses the library**. `force: true` also replaces mappings made in Figma's UI. The result's `publish.ok` is true only when every doc was published; anything else is listed in `failed`, `uiConflicts`, `unreported` or `validationFailures`, even where the CLI itself exits successfully.

### 5. Unpublish

`figma_unpublish_code_connect` uses the same dry run, `planId` and confirmation:

* `mode: "files"` removes what the project's Code Connect files published. You can publish them again from the files.
* `mode: "node"` with a `figmaUrl` and `label` removes whatever is published for that component and label, which may have come from another repo or from Figma's UI.

***

## Dry runs and planId

A `planId` stands for exactly what one dry run showed you: the parsed Code Connect docs, the sanitized config, the options and the CLI. The real run refuses with `CC_PLAN_MISMATCH` when any of these changed:

* **An argument differs** from the dry run's: the error names it. Call again with the dry run's arguments.
* **The files or the CLI changed.** The real run parses the files once more right before publishing, so a template, or a helper it imports, edited after the dry run is caught. Run a new dry run and review it.

Plans last 30 minutes and exist only in the server session that made them. After a restart, run the dry run again.

The real run also checks lint: docs with lint errors block it (`CC_LINT_BLOCKED`), and so do docs that couldn't be linted because Figma couldn't be read at that moment. Retry when the library is open with the Desktop Bridge or the token works. `ignoreLintErrors: true` and `allowUnreadableFiles: true` exist for when you've decided to publish anyway.

***

## Design to code: use the map first

Before writing code from a Figma frame, ask for the map:

```javascript theme={null}
figma_get_code_connect_map({ repoRoot: "/Users/me/acme-ui", nodeId: "120:4" })
// or, when the file isn't open in Figma:
figma_get_code_connect_map({ repoRoot: "/Users/me/acme-ui", figmaUrl: "https://www.figma.com/design/<key>/<name>?node-id=120-4" })
```

For each instance you get the code component, a snippet rendered from that instance's own values (`Size=Large`, `Label="Save"`, a swapped icon…) and the imports it needs. Instances with no Code Connect are listed under `unmapped`: build those from the design with your existing components, or ask, rather than inventing a component. Connected instances inside unmapped ones are listed under `foundInside`.

Snippets are rendered locally from your templates. Slot contents show as placeholders, and anything that isn't exact is listed under `approximations`. For Figma-rendered snippets, use `figma_preview_code_connect`.

The map reads your repository's files. Mappings published from another repository or made in Figma's UI can't be read through Figma's public APIs, so they don't appear.

***

## CI: fail a build on drift

`figma_audit_code_connect` returns `status: "pass"` or `"fail"`, so an agent step in CI (any MCP client that runs Figma Console MCP) can gate a pull request on it:

```javascript theme={null}
figma_audit_code_connect({
  repoRoot: "/path/to/checkout",
  figmaSource: "rest",          // CI has no Figma Desktop; needs FIGMA_ACCESS_TOKEN
  libraryFileKey: "<library file key>",
  failOn: "error",              // or "warning", or "never" to only report
  minCoverage: 80               // fail below 80% of non-private components mapped
})
```

Fail the job when `status` is `"fail"`; `failedBecause` says why. The audit covers every `figma.config.json` in a monorepo. Over REST it sees published components only, and it never runs the repo's CLI, so the CI job needs no Code Connect: Write scope.

***

## Cloud Mode and Local Mode

| | Local Mode | Cloud Mode |
| - | - | - |
| Generate | Reads the code component and existing Code Connect from `repoRoot`; writes with `write: true` | Pass `codeSource` (one component's source) and `files`; content is returned, nothing is written |
| Map and audit | `repoRoot` (or `FIGMA_CODE_CONNECT_ROOT`) | Pass `files`: your Code Connect files and `figma.config.json` |
| `// source=` links | From the git remote | Relative paths, unless you pass `repoUrl` |
| Preview, publish, unpublish | Yes, through your repo's CLI | No. Run `npx figma connect publish --dry-run`, then `npx figma connect publish`, in your repository |
| Figma access | Desktop Bridge or REST | The paired Desktop Bridge plugin, or REST with your sign-in |

In Cloud Mode, `files` takes up to 300 files, 256 KB each and 4 MB in total.

***

## Limits

| What | Limit |
| - | - |
| Components per generate call | 25 |
| Instances per map call | 500 (default 100) |
| Components per audit | 5,000 (default 2,000); lists in the result are capped at 500, issues at 1,000 |
| Files per publish or unpublish | 200 |
| Preview, instances mode | 20 CLI runs (default 10) within 10–120 seconds (default 60); the rest is rendered locally |
| Preview, combinations mode | 20 files, 500 combinations (default 100), 10 property names |
| Plans (`planId`) | 30 minutes, this server session only |
| CLI run time | parse 60 s, preview 120 s per run, unpublish 120 s, publish 300 s |

Only parserless template files (`.figma.ts`, `.figma.js`) are published. Legacy `figma.connect()` files from CLI 1.x can be generated for repos still on 1.x, and unpublished, but CLI 2.x won't publish them. Swift, Compose and custom-parser projects need a native build; run `npx figma connect` yourself for those.

***

## Troubleshooting

| Code | What to do |
| - | - |
| `CC_ROOT_REQUIRED` / `CC_ROOT_TOO_BROAD` | Pass `repoRoot` (or set `FIGMA_CODE_CONNECT_ROOT`). The filesystem root and your home folder are refused. |
| `CC_DOCS_REQUIRED` | Map and audit need `repoRoot` or `files` to read Code Connect from. In Cloud Mode, pass `files`; Cloud generate also returns this code when neither `codeSource` nor `files` is passed. |
| `CC_NOT_CONNECTED` / `CC_REST_FORBIDDEN` | Open the library file with the Desktop Bridge plugin, or set a token with File content: Read. In Cloud Mode, pair the plugin with `figma_pair_plugin`, or pass a `figmaUrl` to read the file over REST with your Figma sign-in. |
| `CC_CLI_NOT_FOUND` | `@figma/code-connect` isn't installed between the project folder and `repoRoot`. Install it in the repo, or pass the workspace root as `repoRoot`. |
| `CC_CLI_TOO_OLD` | The repo's CLI lacks a needed option. Upgrade it. |
| `CC_CONFIG_UNSAFE` | `codeConnect.apiUrl` points somewhere other than Figma's API and would send your token there. Remove it. |
| `CC_CONFIG_LEGACY` | Flatten the old `codeConnect.react` / `codeConnect.swift` blocks into `codeConnect`. |
| `CC_CLI_PARSER_UNSUPPORTED` | Swift, Compose or custom-parser files: run `npx figma connect` yourself, or move to templates. |
| `CC_TOKEN_MISSING` / `CC_TOKEN_OAUTH` / `CC_TOKEN_INVALID` | Set a personal access token (`figd_…`) with Code Connect: Write and File content: Read as `FIGMA_ACCESS_TOKEN` in the server's environment. |
| `CC_PLAN_REQUIRED` / `CC_PLAN_UNKNOWN` / `CC_PLAN_MISMATCH` | Run the dry run (again) and use its `planId` with the same arguments. |
| `CC_LINT_BLOCKED` / `CC_UNREADABLE_FILES` | Fix the listed files, or retry when Figma can be read. |
| `CC_BUSY` | Another CLI run on the same project is in progress. Wait for it. |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.