DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Integration: MCP tool schemas list every key their tools forward
aionggc119c809e1ed
diff --git a/crates/mcp/src/tests.rs b/crates/mcp/src/tests.rsindex 2c10493f8..21270e5e8 100644--- a/crates/mcp/src/tests.rs+++ b/crates/mcp/src/tests.rs@@ -577,3 +577,103 @@ fn unknown_params_are_invalid_params() {// The right keys still run.ok(&mut s, "execute", json!({"command": "text.select", "params": {"story": f["story"], "anchor": 0, "focus": 5}}));}++/// A backend that answers every call with `{}` and keeps what each call forwarded.+struct Recorder(Vec<(String, Value)>);++impl Backend for Recorder {+ fn call(&mut self, method: &str, params: Value) -> Result<Value, String> {+ self.0.push((method.to_string(), params));+ Ok(json!({}))+ }+ fn has_ui(&self) -> bool {+ true+ }+ fn describe(&self) -> String {+ "recorder".into()+ }+}++/// The server refuses arguments a tool's schema doesn't list, so every key a tool forwards to a+/// command or UI method has to be in its schema. Tools that pass their arguments on whole+/// (`new_document`, `ui_set`) list every key their target reads.+#[test]+fn every_forwarded_key_is_in_the_tool_schema() {+ let defs = tool_definitions();+ let props = |t: &Value| t["inputSchema"]["properties"].as_object().cloned().unwrap_or_default();+ // Every key any tool or command names, typed as the first schema that lists it.+ let mut every: serde_json::Map<String, Value> = serde_json::Map::new();+ for t in &defs {+ for (k, p) in props(t) {+ let v = match p["type"].as_str() {+ Some("string") => p["enum"].get(0).cloned().unwrap_or(json!("a")),+ Some("boolean") => json!(true),+ Some("object") => json!({}),+ Some("array") => json!([]),+ _ => json!(1),+ };+ every.entry(k).or_insert(v);+ }+ }+ for spec in designcraft_engine::command_specs() {+ for k in designcraft_engine::cmd::params::doc_keys(spec.params).map(|d| d.keys).unwrap_or_default() {+ every.entry(k.to_string()).or_insert(json!(1));+ }+ }+ let whole = ["new_document", "ui_set"];+ // Tools that run commands named in their arguments forward whatever those take.+ let generic = ["execute", "batch"];+ let mut missing = Vec::new();+ for t in &defs {+ let name = t["name"].as_str().unwrap();+ if whole.contains(&name) || generic.contains(&name) {+ continue;+ }+ let own = props(t);+ let mut args = every.clone();+ args.extend(own.iter().filter_map(|(k, _)| every.get(k).map(|v| (k.clone(), v.clone()))));+ let mut rec = Recorder(Vec::new());+ let _ = crate::tools::call_tool(&mut rec, name, &Value::Object(args));+ for (method, params) in &rec.0 {+ let forwarded = if method == "engine.execute" { ¶ms["params"] } else { params };+ for k in forwarded.as_object().into_iter().flatten().map(|(k, _)| k) {+ if !own.contains_key(k) {+ missing.push(format!("{name} forwards `{k}` to {method}"));+ }+ }+ }+ }++ let new_document = defs.iter().find(|t| t["name"] == "new_document").map(props).unwrap();+ let spec = designcraft_engine::find_command("file.new").unwrap();+ for k in designcraft_engine::cmd::params::doc_keys(spec.params).unwrap().keys {+ if !new_document.contains_key(k) {+ missing.push(format!("new_document: file.new takes `{k}`"));+ }+ }++ // `ui.set` lives in the UI crate, which this one doesn't depend on: read its keys from the source.+ let control = std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/../ui-egui/src/control.rs")).unwrap();+ let start = control.find("\"ui.set\" => {").unwrap();+ let arm = &control[start..start + control[start..].find("\"ui.dialog.set\"").unwrap()];+ let mut read = Vec::new();+ for pat in ["s(\"", "p.get(\"", "(\""] {+ for (i, _) in arm.match_indices(pat) {+ let rest = &arm[i + pat.len()..];+ let key = &rest[..rest.find('"').unwrap()];+ if !key.contains('.') && !key.is_empty() {+ read.push(key);+ }+ }+ }+ read.sort_unstable();+ read.dedup();+ assert!(read.contains(&"zoom") && read.contains(&"rulers"), "{read:?}");+ let ui_set = defs.iter().find(|t| t["name"] == "ui_set").map(props).unwrap();+ for k in read {+ if !ui_set.contains_key(k) {+ missing.push(format!("ui_set: ui.set reads `{k}`"));+ }+ }+ assert!(missing.is_empty(), "keys missing from tool schemas: {missing:#?}");+}diff --git a/crates/mcp/src/tools.rs b/crates/mcp/src/tools.rsindex 77f194186..0d943b780 100644--- a/crates/mcp/src/tools.rs+++ b/crates/mcp/src/tools.rs@@ -176,8 +176,13 @@ pub fn tool_definitions() -> Vec<Value> {"columns": {"type": "integer", "minimum": 1, "description": "Column count"},"gutter": num("Column gutter (pt)"),"margins": {"description": "Uniform margin (pt) or {top, bottom, inside, outside}", "anyOf": [{"type": "number"}, {"type": "object"}]},- "bleed": num("Bleed on all sides (pt)"),+ "bleed": {"description": "Bleed on all sides (pt) or [top, bottom, inside, outside]", "anyOf": [{"type": "number"}, {"type": "array", "items": {"type": "number"}}]},"title": string("Document title"),+ "intent": {"type": "string", "enum": ["print", "web", "mobile"], "description": "Document intent"},+ "units": string("Ruler units, e.g. \"points\", \"picas\", \"millimeters\""),+ "startPage": {"type": "integer", "minimum": 1, "description": "Number of the first page"},+ "slug": {"description": "Slug on all sides (pt) or [top, bottom, inside, outside]", "anyOf": [{"type": "number"}, {"type": "array", "items": {"type": "number"}}]},+ "primaryTextFrame": boolean("Put a primary text frame on the parent page"),"sample": boolean("Open the sample magazine instead"),}),&[],@@ -357,7 +362,8 @@ pub fn tool_definitions() -> Vec<Value> {json!({"x": num("Start x"), "y": num("Start y"), "toX": num("End x"), "toY": num("End y"),"steps": {"type": "integer", "minimum": 1, "description": "Intermediate moves (default 8)"},- "shift": {"type": "boolean"}, "alt": {"type": "boolean"}, "cmd": {"type": "boolean"},+ "button": {"type": "string", "enum": ["left", "right"], "description": "Default left"},+ "shift": {"type": "boolean"}, "alt": {"type": "boolean"}, "cmd": {"type": "boolean"}, "ctrl": {"type": "boolean"},}),&["x", "y", "toX", "toY"],),@@ -382,14 +388,19 @@ pub fn tool_definitions() -> Vec<Value> {"ui_set","Set UI state",&app_only(- "Change UI state: panel to open, brightness, rulers/guides/frameEdges/baselineGrid/textThreads, screenMode, zoom, page (1-based), fit (page|spread).",+ "Change UI state: panel to open or close, brightness, status text, rulers/guides/frameEdges/baselineGrid/documentGrid/textThreads, \+ Control bar, dock expanded, screenMode, zoom, page (1-based), fit (page|spread).",),obj(json!({"panel": string("Panel: properties, pages, layers, swatches, paragraphStyles, characterStyles, stroke, character, paragraph, textWrap, links"),"brightness": {"type": "string", "enum": ["dark", "mediumDark", "mediumLight", "light"]},"rulers": {"type": "boolean"}, "guides": {"type": "boolean"}, "frameEdges": {"type": "boolean"},- "baselineGrid": {"type": "boolean"}, "textThreads": {"type": "boolean"},+ "baselineGrid": {"type": "boolean"}, "documentGrid": {"type": "boolean"}, "textThreads": {"type": "boolean"},+ "status": string("Status bar text"),+ "controlBar": boolean("Show the Control bar"),+ "closePanel": boolean("Close the panel that is showing"),+ "dockExpanded": boolean("Expand the panel dock (false: icons only)"),"screenMode": {"type": "string", "enum": ["normal", "preview", "bleed", "slug", "presentation"]},"zoom": num("1.0 = 100%"),"page": {"type": "integer", "minimum": 1, "description": "Go to page (1-based)"},@@ -595,8 +606,12 @@ fn dispatch(b: &mut dyn Backend, name: &str, a: &Args) -> Result<ToolResult, Str"ime" => j(b.call("ui.ime", pick(a, &["preedit", "active", "commit"]))?),"click" | "drag" => {need_ui(b, name)?;- let keys: &[&str] = &["x", "y", "toX", "toY", "steps", "button", "count", "shift", "alt", "cmd", "ctrl"];- j(b.call(if name == "click" { "ui.click" } else { "ui.drag" }, pick(a, keys))?)+ let (method, keys): (_, &[&str]) = if name == "click" {+ ("ui.click", &["x", "y", "button", "count", "shift", "alt", "cmd", "ctrl"])+ } else {+ ("ui.drag", &["x", "y", "toX", "toY", "steps", "button", "shift", "alt", "cmd", "ctrl"])+ };+ j(b.call(method, pick(a, keys))?)}"menu_list" => {need_ui(b, name)?;diff --git a/docs/mcp.md b/docs/mcp.mdindex 05894610f..bba394e33 100644--- a/docs/mcp.md+++ b/docs/mcp.md@@ -170,7 +170,7 @@ Use `execute` with `object.fit` to fit a selected graphic frame (or pass `ids`):| `inspect_document` | Pages, spreads, items (ids, bounds, fill, story), stories (overset), layers, styles, swatches, selection || `get_story` | Text, frames, paragraphs, lines, overset of a story (`story` or text `frame`) || `set_story_text` | Replace a story's text (`\n` = new paragraph) |-| `new_document` | `file.new` (preset, width, height, pages, facingPages, columns, gutter, margins, bleed, title) or `sample: true` |+| `new_document` | `file.new` (preset, intent, units, width, height, pages, startPage, facingPages, columns, gutter, margins, bleed, slug, title, primaryTextFrame) or `sample: true` || `open_document` / `save_document` | Native `.designcraft` files || `place_image` | Image from `path` or `base64` into `frame`, the selected frame, or a new frame at x, y, width || `render_page` | Render a page → MCP image content (PNG, base64); optional `path` to also save it |