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,
cvavariants and defaults,children, native element props) or a Web Componentscustom-elements.json(React wrappers made with@lit/reacttake their props from it too; passmanifestPath), 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 thecodeconnect toolset. Your AI assistant switches it on with:
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 withrepoRoot (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:
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:
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
figmaUrlfor another file. NeedsFIGMA_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.
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
nodeId for the current selection. The result has, per component:
file.content: the.figma.tstemplate, with a mapping note per property.mapping: how each Figma property maps to a code prop, and how each value maps. A value isexactwhen the code declares the same word, ignoring case and separators (Top Left→top-left);synonymonly when it doesn’t (Large→lg,Info→neutralonly if the code has noinfo).unmatched: Figma properties with no code prop, and code props with no Figma property.nestedInstancesandwarnings.
propMap, or point codeComponent.path at the right file, and generate again:
write: true. Existing files are never replaced unless you also pass overwrite: true.
2. Audit
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)
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):planId. Review docs, lint, unreadable, skipped and warnings.
To publish, call again with the same arguments plus dryRun: false and that planId:
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 afigmaUrlandlabelremoves whatever is published for that component and label, which may have come from another repo or from Figma’s UI.
Dry runs and planId
AplanId 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.
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: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:
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.