Skip to main content

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: 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:
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). 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:
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, in the repo:
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

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 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 has the full list of how the CLI is isolated.

Generate → audit → preview → publish

1. Generate the files

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:
Nothing is written until you add write: true. Existing files are never replaced unless you also pass overwrite: true.

2. Audit

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)

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):
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:
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:
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:
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

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

Limits

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