|
| 1 | +# MCP get_code - Design |
| 2 | + |
| 3 | +This document describes the implementation design for MCP `get_code` in `packages/extension/mcp/tools/code`. It aligns with the requirements and reflects the current pipeline. |
| 4 | + |
| 5 | +## High-level pipeline |
| 6 | + |
| 7 | +1. **Validate selection** |
| 8 | + - Exactly one visible root node required. |
| 9 | + - Build a visible tree with depth capping. |
| 10 | + |
| 11 | +2. **Collect data** |
| 12 | + - `collectNodeData()` |
| 13 | + - `getCSSAsync()` once per node. |
| 14 | + - `getStyledTextSegments()` for text nodes. |
| 15 | + - Preprocess styles (clean background, expand shorthands, merge inferred layout, infer resizing, apply overflow). |
| 16 | + - Apply positioning (auto layout absolute, constraints). |
| 17 | + - Replace image fills with uploaded assets. |
| 18 | + |
| 19 | +3. **Normalize variables** |
| 20 | + - Normalize variable names and codeSyntax to a canonical form. |
| 21 | + - Capture variable usage candidates for token detection. |
| 22 | + |
| 23 | +4. **Asset planning and export** |
| 24 | + - Plan vector/image export at the tree level. |
| 25 | + - Export SVGs/images only once. |
| 26 | + |
| 27 | +5. **Sanitize styles** |
| 28 | + - Patch known layout issues (negative gap). |
| 29 | + - Ensure layout parents are `position: relative` when children are absolute. |
| 30 | + |
| 31 | +6. **Build layout-only styles** |
| 32 | + - Extract layout-related CSS into a secondary map for SVG external layout. |
| 33 | + |
| 34 | +7. **Render markup** |
| 35 | + - Render nodes into JSX/HTML component tree. |
| 36 | + - Inject SVG content or `<img>` when applicable. |
| 37 | + - Apply Tailwind class conversion. |
| 38 | + |
| 39 | +8. **Token pipeline** |
| 40 | + - Detect token references in output code. |
| 41 | + - Apply plugin transforms to token names. |
| 42 | + - Rewrite code with transformed token names. |
| 43 | + - Resolve tokens to values when requested. |
| 44 | + |
| 45 | +9. **Truncate and finalize output** |
| 46 | + - Enforce payload size limits. |
| 47 | + - Emit warnings only for truncation or inferred auto layout. |
| 48 | + |
| 49 | +## Tree and layout semantics |
| 50 | + |
| 51 | +### Visible tree |
| 52 | + |
| 53 | +- Built via `buildVisibleTree`. |
| 54 | +- Nodes include bounds, data-hints, and inferred layout hints. |
| 55 | +- GROUP/BOOLEAN nodes are preserved for structure but ignored for layout-parent calculations. |
| 56 | + |
| 57 | +### Layout parent resolution |
| 58 | + |
| 59 | +- `getLayoutParent` walks up to the nearest non-GROUP/BOOLEAN ancestor. |
| 60 | +- All relative transforms and constraints are interpreted against this layout parent. |
| 61 | + |
| 62 | +### Positioning rules |
| 63 | + |
| 64 | +- **Explicit auto layout (layoutMode)** |
| 65 | + - Children with `layoutPositioning === 'ABSOLUTE'` use absolute positioning. |
| 66 | + - Other children stay in flow. |
| 67 | +- **Inferred auto layout** |
| 68 | + - Can emit flex/padding/gap (hinted), but is non-authoritative. |
| 69 | + - Constraint-based positioning is skipped under inferred layout. |
| 70 | +- **No layout** |
| 71 | + - Constraint-based absolute positioning is applied to children. |
| 72 | + |
| 73 | +### Relative container injection |
| 74 | + |
| 75 | +- A layout parent is made `position: relative` if any descendant is absolute. |
| 76 | +- GROUP/BOOLEAN nodes are not made relative/absolute. |
| 77 | + |
| 78 | +## Styles pipeline |
| 79 | + |
| 80 | +### Preprocess (per node) |
| 81 | + |
| 82 | +- `cleanFigmaSpecificStyles`: |
| 83 | + - Fix background quirks and inject fills when absent. |
| 84 | +- `expandShorthands`: |
| 85 | + - Break down padding/margin/inset shorthands. |
| 86 | +- `mergeInferredAutoLayout`: |
| 87 | + - Inject inferred flex/padding/gap where applicable. |
| 88 | +- `inferResizingStyles`: |
| 89 | + - Translate Figma layout sizing into `align-self` and size deletions. |
| 90 | +- `applyOverflowStyles`: |
| 91 | + - Add overflow rules based on clips/scroll. |
| 92 | + |
| 93 | +### Sanitize (tree-level) |
| 94 | + |
| 95 | +- `patchNegativeGapStyles` |
| 96 | +- `ensureRelativeForAbsoluteChildren` |
| 97 | + |
| 98 | +### Layout-only extraction |
| 99 | + |
| 100 | +- Builds layout-only style map for SVG containers. |
| 101 | +- SVG roots remove width/height from layout map (SVG has own size). |
| 102 | + |
| 103 | +## SVG and asset strategy |
| 104 | + |
| 105 | +- Vector-like nodes may be exported to SVG. |
| 106 | +- Vector containers can be converted to a single SVG if all leaves are vector-like. |
| 107 | +- SVG nodes only receive external layout styles, not visual paint styles. |
| 108 | +- Placeholder SVG is emitted when export fails or is unavailable, using node width/height. |
| 109 | + |
| 110 | +## Token pipeline (detailed) |
| 111 | + |
| 112 | +1. Build source index from variable IDs and codeSyntax. |
| 113 | +2. Extract raw token names from code (boundary-aware). |
| 114 | +3. Apply plugin transforms to names. |
| 115 | +4. Rewrite code with transformed names. |
| 116 | +5. Extract final used names from code. |
| 117 | +6. Build used token metadata and (optionally) resolved values. |
| 118 | + |
| 119 | +## Error handling |
| 120 | + |
| 121 | +- Fatal: invalid selection, no renderable root, failure to build markup. |
| 122 | +- Non-fatal: CSS collection failure, text segment failures, export failure. |
| 123 | +- Warnings (output field): only truncation and inferred auto layout presence. |
| 124 | + |
| 125 | +## Performance notes |
| 126 | + |
| 127 | +- Single-pass CSS collection per node. |
| 128 | +- Asset export is planned and executed once. |
| 129 | +- Token detection is string-based and bounded by truncation. |
0 commit comments