# Using Forma with AI Agents Forma is a ~1.7 MB native (~0.8 MB WASM) pure-Rust headless 3D polyhedral modeling engine built so any LLM or coding agent can create, inspect, and export watertight 3D models in milliseconds with minimal token overhead. --- ## 1. Connect via MCP (`forma-mcp`) — Recommended `forma-mcp` is a stateful stdio JSON-RPC 2.0 server with **4 tools** totaling **789 `o200k` tokens** (including the full `Forma-Pipe` syntax spec). Build the binary once: ```bash cargo build --release ``` Add to your MCP client config (Claude Code, Cursor, Codex, Antigravity, etc.) — either zero-install via npm (`forma-3d`) or pointing directly at the compiled native binary: ```json { "mcpServers": { "forma": { "command": "npx", "args": ["-y", "-p", "forma-3d", "forma-3d-mcp"] } } } ``` > Also see [`SKILL.md`](SKILL.md) for a compact, drop-in Agent Skill file ready for LLM skill loaders. ### Exposed Tools | Tool | Purpose | |---|---| | `eval` | Execute transactional `Forma-Pipe` code (`strict: true` auto-rolls back any non-manifold result); returns a ~100-token spatial diff (`WT`, `NM`, `g` genus, `vol`, `bbox`, `size`, tags). | | `inspect` | Inspect current scene invariants and bounding boxes without mutating state. | | `tx` | O(1) copy-on-write state control: `checkpoint`, `rollback`, `undo`, `reset`. | | `export` | Export to `.glb` (glTF 2.0 Y-up, PBR + UVs), faceted `.step` (planar faces), `.3mf`, `.stl`, `.obj`, or render a zero-GPU 4-view studio `.png` preview (`~20–75 KB`). | --- ## 2. Use via Shell / Unix CLI (`forma`) If your agent already has a shell tool, you don't even need MCP—pipe scripts directly into `forma`: ```bash # 1. Read the compact zero-shot language spec (~355 tokens) forma spec # 2. Build, verify watertightness, export CAD/mesh, and render a 4-view preview in ~15 ms forma eval --strict --out part.glb --render preview.png --draw blueprint.svg <<'EOF' flange = box(4, 2, 0.4) | mat("steel", color=(0.55, 0.68, 0.85), metallic=0.9, roughness=0.25) | cut(cyl(0.5, 1)) | cut(cyl(0.25, 1), at=[(-1.4,-0.5,0), (-1.4,0.5,0), (1.4,-0.5,0), (1.4,0.5,0)]) section(flange, 0.0) EOF ``` Example compact diagnostic returned to the agent (~85 tokens): ```text OK [6.92ms] parts=1 - flange: V=265 F=546 WT=ok NM=0 B=0 g=5 vol=2.576 bbox=[-2.00,-1.00,-0.20]..[2.00,1.00,0.20] size=[4.00,2.00,0.40] tags: back(2) bottom(196) front(2) left(2) right(2) side(146) top(196) diff: +flange (vol=2.576) query: section(part, z=0.000): loops=6 area=6.4393 perim=21.4096 xy=[-2.000,-1.000]..[2.000,1.000] size=[4.000,2.000] ``` --- ## 3. Recommended Agent Loop 1. **Draft in `Forma-Pipe`**: Use semantic selectors (`vertical`, `top_rim`, `bottom_rim`, `outer_rim`, `top`, `sides`) and relational placement (`z0=0`, `| on(base)`, `| align(z, min, 0)`) instead of manual vertex/offset math. 2. **Verify Invariants Immediately**: Check `WT=ok NM=0 B=0`, bounding box `size=[x,y,z]`, volume `vol`, and Euler genus `g` (e.g., `g=1` confirms a mug handle or bore is open; `g=5` confirms 5 through-holes). 3. **Run Geometric Checks When Assembling Parts**: - `clearance(part_a, part_b)` verifies exact minimum surface distance and catches collisions. - `section(part, z)` verifies cross-section loop count and wall area at any height `z`. 4. **Optional Visual Check for VLMs**: Render a 4-view (`Front / Right / Top / Iso`) studio preview via `--render preview.png` or `export(path="preview.png")` in ~4 ms with zero GPU, then inspect the image. 5. **Safe Exploration**: Use `tx(action="checkpoint", tag="v1")` before risky boolean/bevel steps and `tx(action="rollback", tag="v1")` on error. --- ## 4. Zero-Syntax-Error Constrained Decoding (GBNF) For local or self-hosted inference engines (`llama.cpp`, `vLLM`, `XGrammar`), export the formal GBNF grammar directly from the CLI or repo: ```bash forma spec --gbnf > forma_pipe.gbnf # or use grammar/forma_pipe.gbnf directly ``` --- ## 5. Common Pitfalls & Best Practices - **Cutting through-holes cleanly:** Make cutting cylinders slightly taller than the target plate (e.g., `cyl(0.5, 1.0)` for a `0.4` thick plate) to avoid grazing coplanar caps. - **Multi-hole patterns:** Pass `at=[(x1,y1,z1), (x2,y2,z2), ...]` in a single `| cut(...)` or `| add(...)` call—Forma automatically batches pairwise-disjoint booleans in one pass. - **Sharp revolved/mechanical subsurf:** Use `subsurf(1, crease=true)` if you want smooth curvature without shrinking sharp rims. - **Exporting for downstream tools:** Use `.glb` for web/Blender/Three.js (glTF 2.0 Y-up, PBR materials + LSCM/triplanar UVs), faceted `.step` (watertight planar-face ISO 10303-21 solid; no analytic cylinders/NURBS) for reference/assembly import into SolidWorks/Fusion 360/FreeCAD, `.3mf`/`.stl` for 3D printing (model in mm? start the script with `units("mm")` so `.3mf`/`.step` declare millimetres; `.stl`/`.obj` have no unit field and keep model units), `.svg` for dimensioned blueprints, and `.blend` via Blender bridge.