MCP batch: refuse a command entry with an unknown key (#7854) (#7865)

* claim: #7854

* MCP batch: refuse a step with an unknown key (#7854)

A step's parameters under another key (`args`) were dropped and the
command ran without them. The batch is now refused before its first
step, naming the key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* MCP batch: a step carrying `expect` is refused, saying it is the batch's argument

#9904 gave batch an `expect` argument and tested that a step carrying one runs with
nothing checked. That is the silent drop this branch refuses: the step is now refused
before it runs, and the error says `expect` belongs to the batch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rvmzym5xv8-starandClaude Opus 5.5 authored and GitHub committed 2026-10-31 16:26:23 +00:00
1 parent b410c577e0
commit 0fb2e9b06b
2 files changed
+34 -5

No files matched your search

+25 -5
View File
@@ -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 mismatch
+9
View File
@@ -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);