From 31dbe9690c8bb42c8eea1115ebbeb133eff878a4 Mon Sep 17 00:00:00 2001 From: Anton A S Date: Tue, 10 Mar 2026 22:21:21 +0300 Subject: [PATCH] Add MCP codegen pipeline: tools, prompt, and plan --- docs/design-to-code-overview.md | 244 +++++++++++ docs/mcp-codegen-plan.md | 97 +++++ lol.fig | Bin 0 -> 24779 bytes packages/core/src/tools/codegen.ts | 446 +++++++++++++++++++++ packages/core/src/tools/prompts/codegen.md | 231 +++++++++++ packages/core/src/tools/registry.ts | 5 + packages/mcp/skills/open-pencil/SKILL.md | 236 +++++++++++ 7 files changed, 1259 insertions(+) create mode 100644 docs/design-to-code-overview.md create mode 100644 docs/mcp-codegen-plan.md create mode 100644 lol.fig create mode 100644 packages/core/src/tools/codegen.ts create mode 100644 packages/core/src/tools/prompts/codegen.md create mode 100644 packages/mcp/skills/open-pencil/SKILL.md diff --git a/docs/design-to-code-overview.md b/docs/design-to-code-overview.md new file mode 100644 index 000000000..19c99e254 --- /dev/null +++ b/docs/design-to-code-overview.md @@ -0,0 +1,244 @@ +# Design → Code Pipeline: Overview + +## Goal + +Take a .fig design and produce production-ready code on the user's target stack (React, Vue, Svelte, etc. + Tailwind/CSS Modules/etc.) via AI-assisted code generation through the MCP server. + +## Current State + +### What exists (extraction layer) + +| Capability | Tool/Module | Output | +|-----------|-------------|--------| +| Read node tree | `get_page_tree`, `get_node`, `find_nodes`, `query_nodes` | JSON node properties | +| Semantic analysis | `describe` | Role, layout, visual style, issues | +| JSX representation | `get_jsx` | OpenPencil JSX (``) | +| Tailwind JSX | `export-jsx.ts` (tailwind format) | `
` | +| SVG export | `export_svg` | SVG markup string | +| Image export | `export_image` | PNG/JPG/WEBP raster | +| Components list | `get_components` | Component IDs, names, pages | +| Design tokens | `list_variables`, `find_variables`, `list_collections` | Variable names, types, values, modes | +| Color analysis | `analyze_colors` | Palette, frequencies, variable bindings, similar clusters | +| Typography analysis | `analyze_typography` | Font families, sizes, weights, frequencies | +| Spacing analysis | `analyze_spacing` | Gaps, paddings, grid compliance | +| Pattern detection | `analyze_clusters` | Repeated structures → potential components | +| Structural diff | `diff_jsx`, `diff_create` | Unified diff between two nodes | +| XPath queries | `query_nodes` | `//FRAME[@width < 300]`, `//TEXT[contains(@text, 'Hello')]` | + +### What does NOT exist + +1. **System/instructions prompt for code generation** — no guidance for the AI on how to convert design → code +2. **Component decomposition** — `analyze_clusters` finds repeated patterns, but doesn't determine component boundaries, props, variants, slots +3. **Design token → CSS variable mapping** — `list_variables` returns raw Figma variables, but nothing maps them to `--color-primary`, `var(--spacing-4)`, etc. +4. **Target stack awareness** — no concept of "this project uses Vue 3 + Tailwind" vs "React + CSS Modules" +5. **Production JSX output** — `export-jsx.ts` tailwind format produces unstyled `
` soup without component structure, prop interfaces, or framework idioms +6. **Verification** — no way to compare generated code output against the design visually + +--- + +## Architecture + +``` +.fig design file + │ + ▼ +┌──────────────────────────────────────────────────────────────┐ +│ PHASE 1: EXTRACTION (tools exist) │ +│ │ +│ get_page_tree → full structure │ +│ get_components → component inventory │ +│ list_variables → design tokens │ +│ analyze_colors/typography/spacing → design system snapshot │ +│ analyze_clusters → repeated patterns │ +│ describe → semantic roles per node │ +│ get_jsx → structural JSX │ +│ export_svg → vector assets │ +│ export_image → screenshots for verification │ +└──────────────┬───────────────────────────────────────────────┘ + │ + ▼ +┌──────────────────────────────────────────────────────────────┐ +│ PHASE 2: DECOMPOSITION (needs: prompt + possibly tools) │ +│ │ +│ Which nodes are screens vs components vs primitives? │ +│ What props does each component accept? │ +│ Which components have variants (state, size, theme)? │ +│ Which design variables map to which CSS tokens? │ +│ What's the component hierarchy / dependency graph? │ +└──────────────┬───────────────────────────────────────────────┘ + │ + ▼ +┌──────────────────────────────────────────────────────────────┐ +│ PHASE 3: CODE GENERATION (needs: prompt) │ +│ │ +│ Generate component files on target stack │ +│ Map design tokens → CSS/theme variables │ +│ Extract SVG assets for icons/illustrations │ +│ Wire up component hierarchy and props │ +│ Match typography, spacing, colors exactly │ +└──────────────┬───────────────────────────────────────────────┘ + │ + ▼ +┌──────────────────────────────────────────────────────────────┐ +│ PHASE 4: VERIFICATION (needs: prompt guidance) │ +│ │ +│ Compare generated code visually against design │ +│ Check token coverage, missing styles │ +│ Verify responsive behavior │ +└──────────────────────────────────────────────────────────────┘ +``` + +--- + +## What to Build + +### 1. MCP System Prompt for Code Generation + +A system prompt served when AI is asked to generate code from a design. Not the same as the design-chat prompt in `use-chat.ts`. This one instructs the AI to use extraction tools, decompose the design, and output production code. + +**Content outline:** + +``` +You are a frontend engineer generating production code from Figma designs. + +# Workflow + +1. UNDERSTAND the design + - get_page_tree → scan structure + - get_components → inventory reusable parts + - list_variables + list_collections → design tokens + - analyze_colors, analyze_typography, analyze_spacing → design system snapshot + +2. PLAN the component tree + - analyze_clusters → find repeated patterns + - describe on key nodes → semantic roles + - Determine: which frames are pages/screens, which are components, which are primitives + - Map Figma components → code components + - Identify props: text content, colors, sizes, visibility, children (slots) + - Identify variants: if component has states (hover, active, disabled), map to props + +3. EXTRACT design tokens + - list_variables → get all variables with values per mode (light/dark) + - Map to CSS custom properties or theme object + - Color variables → --color-{name} + - Number variables → --spacing-{name}, --radius-{name}, etc. + - Fonts → font-family definitions + +4. GENERATE code + - One component per file + - Use get_jsx on each component to read structure + - Use export_svg for vector icons/illustrations + - Apply design tokens as CSS variables / theme references + - Match pixel values exactly: font sizes, spacing, radii, colors + - Framework-specific: + - React: functional components, TypeScript props interface, named exports + - Vue: