SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
MCP batch: refuse a command entry with an unknown key (#7854) (#7865)
rvmzym5xv8-star0fb2e9b06bfa
diff --git a/crates/mcp/src/tests.rs b/crates/mcp/src/tests.rsindex ef16301ff3..aee23fe1cb 100644--- a/crates/mcp/src/tests.rs+++ b/crates/mcp/src/tests.rs@@ -828,6 +828,23 @@ fn batch_refuses_an_unknown_command_before_the_first_step() {assert_eq!(tool(&mut s, "inspect_design", json!({"measure": false}))["undo"], 0);}+/// A step with its parameters under another key (`args`) refuses the batch before its first+/// step, naming the key, instead of running the command without them.+#[test]+fn batch_refuses_a_step_with_an_unknown_key() {+ let mut s = server();+ let steps = json!([+ {"command": "solid.box", "params": {"length": 10, "width": 10, "height": 10}},+ {"command": "sketch.create", "args": {"plane": "XY"}},+ ]);+ let r = raw_tool(&mut s, "batch", json!({"commands": steps}));+ assert_eq!(r["result"]["isError"], true, "{r}");+ let body: Value = serde_json::from_str(r["result"]["content"][0]["text"].as_str().unwrap()).unwrap();+ assert_eq!((body["completed"].clone(), body["failed"]["index"].clone()), (json!(0), json!(1)), "{body}");+ assert!(body["failed"]["error"].as_str().is_some_and(|e| e.contains("unknown key `args`") && e.contains("`params`")), "{body}");+ assert_eq!(tool(&mut s, "measure", json!({}))["body_count"], 0, "no step ran");+}+/// #5822: with `coalesce_parameters` a batch of parameter edits, one per step, rebuilds the/// design once for the run, and every step still has its own result. A run with an edit/// `changes` can't make (a new parameter) goes step by step, and a refused edit is still named@@ -988,12 +1005,15 @@ fn a_batch_with_a_matching_expect_reports_the_expectation_met() {let box40 = json!({"command": "solid.box", "params": {"length": 40, "width": 30, "height": 20, "corner": [0, 0, 0]}});let r = tool(&mut s, "batch", json!({"commands": [box40]}));assert!(r.get("expectation").is_none(), "{r}");- // `expect` is the tool's argument, not a step key: a step carrying one runs as any step with- // a stray key does, and nothing is checked.+ // `expect` is the tool's argument, not a step key: a step carrying one is refused, as any step+ // with a stray key is, saying where `expect` goes, instead of running with nothing checked.let sphere = json!({"command": "solid.sphere", "params": {"radius": 5, "center": [100, 0, 0]}, "expect": {"bodies": 7}});- let r = tool(&mut s, "batch", json!({"commands": [sphere]}));- assert_eq!(r["completed"], 1, "{r}");- assert!(r.get("expectation").is_none() && r["results"][0].get("expectation").is_none(), "{r}");+ let r = raw_tool(&mut s, "batch", json!({"commands": [sphere]}));+ assert_eq!(r["result"]["isError"], true, "{r}");+ let body: Value = serde_json::from_str(r["result"]["content"][0]["text"].as_str().unwrap()).unwrap();+ assert_eq!((body["completed"].clone(), body["failed"]["index"].clone()), (json!(0), json!(0)), "{body}");+ assert!(body["failed"]["error"].as_str().is_some_and(|e| e.contains("`expect` is the batch's argument")), "{body}");+ assert_eq!(tool(&mut s, "measure", json!({}))["body_count"], 1, "the sphere did not run");}/// A volume the design does not match flags the reply as an error that names the mismatchdiff --git a/crates/mcp/src/tools.rs b/crates/mcp/src/tools.rsindex a472056546..cc266567f9 100644--- a/crates/mcp/src/tools.rs+++ b/crates/mcp/src/tools.rs@@ -918,6 +918,15 @@ impl Narrow {/// `command` means the step is an engine command already.fn batch_step(step: &Value) -> Result<(String, Value, Option<Narrow>), String> {let command = step.get("command").or_else(|| step.get("id")).and_then(Value::as_str).ok_or("missing `command`")?;+ // Any other key would be dropped and the command run without its parameters, failing (if at+ // all) with an error about a parameter that seems to be there: `args` for `params`.+ if let Some(k) = step.as_object().into_iter().flat_map(Map::keys).find(|k| !matches!(k.as_str(), "command" | "id" | "params")) {+ if k == "expect" {+ return Err("unknown key `expect` in a step: `expect` is the batch's argument, checked after the last step".into());+ }+ let k: String = k.chars().take(64).collect();+ return Err(format!("unknown key `{k}` in a step: a step is {{command, params}}, its parameters go in `params`"));+ }let empty = Map::new();let params = step.get("params").cloned().filter(|p| !p.is_null()).unwrap_or(json!({}));let a = params.as_object().unwrap_or(&empty);