SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
MCP: server instructions that say how to plan, build and verify (refs #3380) (#9903)
froglemonade29b31d2944390
diff --git a/crates/mcp/src/server.rs b/crates/mcp/src/server.rsindex 557b58c3b1..0b4662a778 100644--- a/crates/mcp/src/server.rs+++ b/crates/mcp/src/server.rs@@ -25,18 +25,37 @@ const RESOURCE_NOT_FOUND: i64 = -32002;/// Most messages in one JSON-RPC batch.const MAX_BATCH: usize = 1000;+/// What every MCP client is told at `initialize`: how to plan, build and verify a part. It stays+/// version-neutral (no workaround for a quirk of the current kernel or solver: those belong in+/// `skills/solidcraft-modeling/reference.md`, which `skill_reference` serves per release), and+/// `tests::instructions_name_only_real_tools` checks every tool and command id it names.const INSTRUCTIONS: &str = "SolidCraft is an independent parametric 3D CAD app, and these tools drive it: when its window is \-open the user watches the model appear, otherwise a headless session builds it. When asked to design or build a part \-(a bracket, an enclosure, a gear) do it with these tools straight away; nothing needs explaining first. Units are millimetres \-and degrees in expressions (internally radians), Z is up. Model like this: `sketch.create {plane: XY}`, draw \-(`sketch.rectangle.two_point {p0, p1}`, `sketch.circle.center {center, radius}`, `sketch.line {points, closed}`), constrain \-and dimension (`sketch.dimension {entities, value}`; values may name parameters), `sketch.finish`, then features \-(`solid.extrude {distance, operation?: new|join|cut|intersect}`, `solid.revolve`, `solid.fillet {edges: [[x,y,z] point on \-edge], radius}`, `solid.chamfer`, patterns, mirror, primitives). Use `batch` for several steps at once, \-`list_commands` for ids and parameter docs, `describe_command` for a command's schemas, `inspect_design` / `measure` to verify, \-`body_topology` to find edge points, `set_parameter` to drive the design, `check_parameters` to try values (volume, errors, \-cylinder axes) and have them put back, and `screenshot` to look at it. A call whose error \-starts `still running:` was not cancelled: don't send it again; `pending` waits for its result.";+open the user watches the model appear, otherwise a headless session builds it. When asked to design or build a part, do it \+with these tools straight away. Units are millimetres and degrees in expressions, Z is up.\n\n\+Start: for anything beyond one primitive, call `skill_reference {\"skill\":\"solidcraft-modeling\"}` first (command shapes, \+ids, plane axes, worked examples and the known traps of this version; `file: \"SKILL.md\"` has the workflow). Then \+`inspect_design {\"measure\":false,\"sketches\":false}` to see what is already there.\n\n\+Plan: work the numbers out first (the parameters, the expected volume and bounding box after each stage, the world point you \+will use for each edge or face pick). Then build in a few `batch` calls (all-or-nothing; the reply names the failing step; \+`save`, `measure` and `set_parameter` can be steps). Use `verbose: false`. Name every feature. `list_commands {\"filter\": word}` \+gives ids and one-line parameter docs (never unfiltered); `describe_command` gives one command's docs and schemas.\n\n\+Routes: a plate, block, boss, standoff or hole is cheapest as a primitive with expression fields (`solid.box`, `solid.cylinder`, \+`solid.hole`, `solid.fillet`, `solid.shell`, patterns and mirror), no sketch. Sketch when the profile is not a box or a circle. \+Fillet outside edges before shelling, build inside features after the shell, then the holes and cuts through walls. Give a blind \+hole or a hole through one wall of a hollow part its depth.\n\n\+Parametric: a size or position that must follow a parameter is an expression in a feature field (`\"L/2 - 10\"`), a sketch \+dimension to `\"origin\"` or an edge, or a pattern spacing; a plain number never follows a parameter. Every sketch must end at \+dof 0 (`sketch.solve` reports it). A pick (`edges: [[x,y,z]]`, `faces: [[x,y,z]]`) is a number that snaps to the nearest edge \+or face and follows it when parameters change.\n\n\+Verify: `measure {}` after each stage and compare volume and bbox with your numbers; `ok: true` alone proves nothing: a cut \+that leaves the volume unchanged has missed, a join that adds a body did not touch. Positions of holes and bosses come from \+`body_topology` (cylinder origin, axis and radius), not from the volume or the centre of mass of a symmetric part. To check \+that the design follows its parameters, `check_parameters {\"set\": {name: value}}` tries the values and puts the design \+back (volume, errors, cylinder axes), or change them in one `batch` with `coalesce_parameters: true` (one rebuild), `measure`, \+then set them back the same way before saving.\n\n\+Fix, don't rebuild: a wrong number in a feature is changed with `timeline.edit` or `parameters.change` on its input \+(`parameters.list {\"filter\":\"feature\"}` names them); `edit.checkpoint` and `edit.restore_checkpoint` go back exactly.\n\n\+A call whose error starts `still running:` was not cancelled: don't send it again; `pending` waits for its result.";pub struct Server {backend: Box<dyn Backend>,diff --git a/crates/mcp/src/tests.rs b/crates/mcp/src/tests.rsindex 12361f8711..859fc3c981 100644--- a/crates/mcp/src/tests.rs+++ b/crates/mcp/src/tests.rs@@ -1822,6 +1822,100 @@ fn skill_reference_example_builds_as_documented() {holes_at(&mut s, 52.0);}+/// The server instructions' recipe (primitives with expression fields, fillet before the shell,+/// inside features after it, a blind hole with its depth, picks by world point, parameters+/// checked in one coalesced batch and set back) builds the part it describes, with the volume+/// worked out by hand, and follows its parameters, so the instructions and the engine stay in+/// step whatever the kernel does.+#[test]+fn instructions_recipe_builds_a_parametric_shelled_box() {+ // A box L × W × H with its four vertical edges rounded r, shelled from the top with wall t+ // (the inner corners are r - t), a boss of radius R from the floor to the rim, and a blind+ // hole Ø d, depth hd, down the boss.+ let (fr, t, big_r, d, hd) = (5.0, 2.0, 6.0, 5.0, 10.0);+ let expected = |l: f64, w: f64, h: f64| {+ let corner = 4.0 * (1.0 - PI / 4.0);+ let outer = l * w * h - corner * fr * fr * h;+ let cavity = (l - 2.0 * t) * (w - 2.0 * t) * (h - t) - corner * (fr - t) * (fr - t) * (h - t);+ let boss = PI * big_r * big_r * (h - t);+ let hole = PI * (d / 2.0) * (d / 2.0) * hd;+ outer - cavity + boss - hole+ };+ let mut s = server();+ let r = tool(+ &mut s,+ "batch",+ json!({"verbose": false, "commands": [+ {"command": "parameters.add", "params": {"name": "L", "expression": "60 mm"}},+ {"command": "parameters.add", "params": {"name": "W", "expression": "40 mm"}},+ {"command": "parameters.add", "params": {"name": "H", "expression": "30 mm"}},+ {"command": "parameters.add", "params": {"name": "wall", "expression": "2 mm"}},+ {"command": "solid.box", "params": {"length": "L", "width": "W", "height": "H", "corner": [0, 0, 0], "name": "Block", "body_name": "Case"}},+ {"command": "solid.fillet", "params": {"edges": [[0, 0, 15], [60, 0, 15], [60, 40, 15], [0, 40, 15]], "radius": fr, "name": "Corners"}},+ {"command": "solid.shell", "params": {"faces": [[30, 20, 30]], "thickness": "wall", "name": "Hollow"}},+ {"command": "solid.cylinder", "params": {"radius": big_r, "height": "H - wall", "base": ["L/2", "W/2", "wall"], "operation": "join", "name": "Boss"}},+ {"command": "solid.hole", "params": {"position": ["L/2", "W/2", "H"], "diameter": d, "depth": hd, "name": "Pilot"}},+ {"command": "measure", "params": {}},+ ]}),+ );+ let results = r["results"].as_array().unwrap();+ assert_eq!(results.len(), 10, "{r}");+ let m = &results[9];+ assert_eq!(m["bodies"].as_array().map(Vec::len), Some(1), "{m}");+ assert!(rel(m["total"]["volume_mm3"].as_f64().unwrap(), expected(60.0, 40.0, 30.0)) < 1e-3, "{m}");+ let no_errors = |s: &mut Server| {+ let d = tool(s, "inspect_design", json!({"measure": false, "sketches": false}));+ let failed: Vec<&Value> = d["timeline"].as_array().unwrap().iter().filter(|f| !f["error"].is_null()).collect();+ assert!(failed.is_empty(), "{failed:?}");+ assert_eq!(d["timeline"].as_array().map(Vec::len), Some(5), "{d}");+ };+ no_errors(&mut s);+ // The boss and the pilot hole are where the expressions put them.+ let axes = |s: &mut Server, x: f64, y: f64| {+ let topo = tool(s, "body_topology", json!({"body": "Case"}));+ let cylinders: Vec<(f64, f64, f64)> = topo["faces"]+ .as_array()+ .unwrap()+ .iter()+ .filter_map(|f| f.get("cylinder"))+ .map(|c| (c["origin"][0].as_f64().unwrap(), c["origin"][1].as_f64().unwrap(), c["radius"].as_f64().unwrap()))+ .collect();+ for radius in [big_r, d / 2.0] {+ assert!(+ cylinders.iter().any(|&(cx, cy, cr)| (cx - x).abs() < 1e-6 && (cy - y).abs() < 1e-6 && (cr - radius).abs() < 1e-6),+ "no cylinder r {radius} on ({x}, {y}): {cylinders:?}"+ );+ }+ };+ axes(&mut s, 30.0, 20.0);+ // Two parameters changed in one coalesced batch (one rebuild per run), measured, set back.+ let r = tool(+ &mut s,+ "batch",+ json!({"verbose": false, "coalesce_parameters": true, "commands": [+ {"command": "parameters.change", "params": {"name": "L", "expression": "80 mm"}},+ {"command": "parameters.change", "params": {"name": "W", "expression": "50 mm"}},+ {"command": "measure", "params": {}},+ {"command": "parameters.change", "params": {"name": "L", "expression": "60 mm"}},+ {"command": "parameters.change", "params": {"name": "W", "expression": "40 mm"}},+ ]}),+ );+ let results = r["results"].as_array().unwrap();+ assert_eq!(results.len(), 5, "{r}");+ assert_eq!(results[0]["coalesced"], 2, "{r}");+ assert_eq!(results[0]["errors"].as_array().map(Vec::len), Some(0), "{r}");+ let m = &results[2];+ assert_eq!(m["bodies"].as_array().map(Vec::len), Some(1), "{m}");+ assert!(rel(m["total"]["volume_mm3"].as_f64().unwrap(), expected(80.0, 50.0, 30.0)) < 1e-3, "{m}");+ assert_eq!(results[3]["coalesced"], 2, "{r}");+ assert_eq!(results[4]["errors"].as_array().map(Vec::len), Some(0), "{r}");+ let m = tool(&mut s, "measure", json!({}));+ assert_eq!(m["bodies"].as_array().map(Vec::len), Some(1), "{m}");+ assert!(rel(m["total"]["volume_mm3"].as_f64().unwrap(), expected(60.0, 40.0, 30.0)) < 1e-3, "{m}");+ no_errors(&mut s);+ axes(&mut s, 30.0, 20.0);+}+/// The skill reference's sketch-plane table: where sketch (10, 20) lands and which way +distance goes.#[test]fn sketch_plane_axes_match_the_skill_reference() {@@ -1947,18 +2041,29 @@ fn skills_name_only_real_tools_and_commands() {assert!(checked > 30, "only {checked} tool and command names found in the skills");}-/// Every tool the server instructions name (a bare `snake_case` word in backticks) is a real tool.+/// Every tool and command the server instructions name is real: a backticked `snake_case` word+/// (bare or as a call, `measure {}`) is a tool, a dotted id (`solid.box`) a command in the+/// engine's registry. A rename or removal can't leave the instructions sending agents to names+/// the server no longer has.#[test]fn instructions_name_only_real_tools() {let mut s = server();let init = call(&mut s, 1, "initialize", json!({"protocolVersion": PROTOCOL_VERSION, "capabilities": {}}));let text = init["result"]["instructions"].as_str().unwrap().to_owned();let tools: Vec<String> = tool_definitions().iter().map(|t| t["name"].as_str().unwrap().to_owned()).collect();+ let commands: Vec<String> =+ tool(&mut s, "list_commands", json!({})).as_array().unwrap().iter().filter_map(|c| c["id"].as_str()).map(str::to_owned).collect();+ let ident = |w: &str| !w.is_empty() && w.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == '.');+ // A span is `name`, `name {}` or `name {"json":…}`; `edges: [[x,y,z]]`, `verbose: false` and+ // `"L/2 - 10"` are fields and values, not calls.let named: Vec<&str> =- text.split('`').skip(1).step_by(2).filter(|w| !w.is_empty() && w.chars().all(|c| c.is_ascii_lowercase() || c == '_')).collect();- assert!(named.contains(&"body_topology"), "{named:?}");+ text.split('`').skip(1).step_by(2).map(|span| span.split_once(" {").map_or(span, |(head, _)| head)).filter(|w| ident(w)).collect();+ for must in ["skill_reference", "batch", "measure", "body_topology", "pending", "solid.box", "parameters.change", "edit.restore_checkpoint"] {+ assert!(named.contains(&must), "{named:?}");+ }for name in named {- assert!(tools.iter().any(|t| t == name), "the instructions name `{name}`, which is not a tool");+ let known = if name.contains('.') { commands.iter().any(|c| c == name) } else { tools.iter().any(|t| t == name) };+ assert!(known, "the instructions name `{name}`, which is neither a tool nor a command");}}diff --git a/crates/ui-egui/src/agent_chat/acp.rs b/crates/ui-egui/src/agent_chat/acp.rsindex 897f91d40a..fe4d8e7ec1 100644--- a/crates/ui-egui/src/agent_chat/acp.rs+++ b/crates/ui-egui/src/agent_chat/acp.rs@@ -214,8 +214,10 @@ pub fn wsl_path(path: &std::path::Path) -> std::path::PathBuf {/// What every session is told about where it is. Agents that don't read `_meta` ignore it.pub const SYSTEM_NOTE: &str = "You are running inside SolidCraft, a parametric 3D CAD application, as the user's assistant. \The open design is reachable through the `solidcraft-app` MCP server: use its tools (list_commands, batch, execute, inspect_design, \-measure, screenshot, set_parameter, undo) to read and change the design the user is looking at, and verify with measure or \-inspect_design. Prefer one `batch` over many small calls. Saving, opening or exporting files waits for the user's approval in \+measure, screenshot, set_parameter, undo, skill_reference) to read and change the design the user is looking at, and verify with \+measure or inspect_design. Before building anything beyond one primitive, read `skill_reference` for `solidcraft-modeling` (its \+SKILL.md has the workflow, its reference.md the command shapes and this version's traps), work the numbers out first, and after \+each stage compare `measure` with them. Prefer one `batch` over many small calls. Saving, opening or exporting files waits for the user's approval in \the chat, and each message allows a limited number of changes. Don't use shell or file tools: work through the SolidCraft tools. \Keep answers short.";diff --git a/docs/mcp.md b/docs/mcp.mdindex 5d7e5cbc54..91f402f827 100644--- a/docs/mcp.md+++ b/docs/mcp.md@@ -279,7 +279,7 @@ completely (from the notification area) and start it again to load the servers.They cover the workflow, the command shapes, how to verify with `measure`, and the known traps. With them an agent builds a part in a few `batch` calls and reads far less output. To install them for Claude Code, copy the folders into `~/.claude/skills/`; [skills/README.md](https://github.com/bherbruck/solidcraft/blob/main/skills/README.md) has the details.-The `skill_reference` tool returns these documents from the copies built into SolidCraft, so an agent without file access can follow them: `{"skill": "solidcraft-modeling"}` returns its `reference.md`, the default file. Mechanisms also has `reference.md`, checking has `reference/porting.md`, and every skill has `SKILL.md` (editing uses modeling's reference). It never reads a path from disk, and an unknown skill or file is refused with the available choices. SolidCraft's own chat turns the agent's file tools off, so this is how its agent reads the skills there.+The `skill_reference` tool returns these documents from the copies built into SolidCraft, so an agent without file access can follow them: `{"skill": "solidcraft-modeling"}` returns its `reference.md`, the default file. Mechanisms also has `reference.md`, checking has `reference/porting.md`, and every skill has `SKILL.md` (editing uses modeling's reference). It never reads a path from disk, and an unknown skill or file is refused with the available choices. SolidCraft's own chat turns the agent's file tools off, so this is how its agent reads the skills there. The instructions the server sends at `initialize` point agents at `skill_reference` first, then say how to plan the numbers, build in a few `batch` calls and verify each stage with `measure`; the same pointer is in the system note SolidCraft's own chat gives its agent.## Tools