From bdfc3daabd4cb02827e6f113598b867c1e31e85f Mon Sep 17 00:00:00 2001 From: jayzed80 Date: Wed, 4 Nov 2026 21:58:48 +0000 Subject: [PATCH] cli: accept --automation-read-root / --automation-write-root (#55) materialcraft-cli mcp and serve now take the --automation-*-root spelling the other crafting apps use, as aliases of --read-root / --write-root. The parser stores them under the short names, so the last root given wins whichever spelling it used. Adds unit tests and a note in the protocol doc. Fixes #55 Co-Authored-By: Claude Opus --- apps/materialcraft-cli/src/main.rs | 47 +++++++++++++++++++++++++++--- docs/control-protocol.md | 3 +- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/apps/materialcraft-cli/src/main.rs b/apps/materialcraft-cli/src/main.rs index 57868c09..349746d8 100644 --- a/apps/materialcraft-cli/src/main.rs +++ b/apps/materialcraft-cli/src/main.rs @@ -40,7 +40,8 @@ USAGE: render: real displacement from the height output, ambient occlusion and a key light. materialcraft-cli mcp [--bridge ] [--token ] [--read-root ] [--write-root ] MCP server on stdio: headless (file access only below the roots), or bridged to - `materialcraft --control `. + `materialcraft --control `. --automation-read-root / --automation-write-root are + the same as --read-root / --write-root (mcp and serve). materialcraft-cli serve [--port ] [--read-root ] [--write-root ] JSON lines ({\"id\",\"method\",\"params\"}) on stdio, or on 127.0.0.1: with a token. @@ -60,10 +61,14 @@ const SUBCOMMANDS: &[(&str, &[&str], &[&str])] = &[ ("cook", &["--graph", "--set"], &["--full"]), ("export", &["--graph", "--set", "--format", "--depth", "--pattern"], &[]), ("render", &["--size", "--view", "--scene", "--node", "--yaw", "--pitch", "--graph", "--set", "--tiling", "--displacement"], &["--full", "--hq"]), - ("mcp", &["--bridge", "--token", "--read-root", "--write-root"], &[]), - ("serve", &["--port", "--read-root", "--write-root"], &[]), + ("mcp", &["--bridge", "--token", "--read-root", "--write-root", "--automation-read-root", "--automation-write-root"], &[]), + ("serve", &["--port", "--read-root", "--write-root", "--automation-read-root", "--automation-write-root"], &[]), ]; +/// (alias, flag): the `--automation-*-root` spelling the other crafting apps use, stored under the +/// short name so the last root given wins whichever spelling it used. +const ALIASES: &[(&str, &str)] = &[("--automation-read-root", "--read-root"), ("--automation-write-root", "--write-root")]; + struct Args { pos: Vec, flags: Vec<(String, Option)>, @@ -91,7 +96,8 @@ fn parse(cmd: &str, raw: &[String]) -> Result { while let Some(x) = it.next() { if valued.contains(&x.as_str()) { let v = it.next().ok_or_else(|| format!("{x} needs a value"))?; - a.flags.push((x.clone(), Some(v.clone()))); + let name = ALIASES.iter().find(|(alias, _)| alias == x).map_or(x.as_str(), |(_, flag)| flag); + a.flags.push((name.to_string(), Some(v.clone()))); } else if bare.contains(&x.as_str()) { a.flags.push((x.clone(), None)); } else if x.starts_with('-') && x.len() > 1 && x.parse::().is_err() { @@ -301,3 +307,36 @@ fn workspace(a: &Args) -> Result { let ws = AuthorizedWorkspace::new(r.map(std::path::Path::new), w.map(std::path::Path::new)).map_err(|e| e.to_string())?; Ok(Filesystem::Workspace(ws)) } + +#[cfg(test)] +mod tests { + use super::*; + + fn strings(v: &[&str]) -> Vec { + v.iter().map(|s| s.to_string()).collect() + } + + #[test] + fn automation_root_flags_are_aliases_of_the_short_ones() { + for cmd in ["mcp", "serve"] { + let a = parse(cmd, &strings(&["--automation-read-root", "/r", "--automation-write-root", "/w"])).unwrap(); + assert_eq!((a.get("--read-root"), a.get("--write-root")), (Some("/r"), Some("/w")), "{cmd}"); + let a = parse(cmd, &strings(&["--read-root", "/r", "--write-root", "/w"])).unwrap(); + assert_eq!((a.get("--read-root"), a.get("--write-root")), (Some("/r"), Some("/w")), "{cmd}"); + } + } + + #[test] + fn the_last_root_flag_wins_whatever_its_spelling() { + let a = parse("mcp", &strings(&["--read-root", "/a", "--automation-read-root", "/b"])).unwrap(); + assert_eq!(a.get("--read-root"), Some("/b")); + let a = parse("mcp", &strings(&["--automation-write-root", "/a", "--write-root", "/b"])).unwrap(); + assert_eq!(a.get("--write-root"), Some("/b")); + } + + #[test] + fn automation_root_flags_need_a_value_and_stay_unknown_elsewhere() { + assert!(parse("mcp", &strings(&["--automation-read-root"])).is_err()); + assert!(parse("cook", &strings(&["--automation-read-root", "/r"])).is_err()); + } +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 75488a5c..726cb5d6 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -5,7 +5,8 @@ Agents drive MaterialCraft three ways, all dispatching the same engine commands: 1. **MCP** (`materialcraft-cli mcp`): headless (an in-process session, files only below - `--read-root` / `--write-root`) or bridged to the running app (`--bridge `). + `--read-root` / `--write-root`, also spelled `--automation-read-root` / + `--automation-write-root` as in the other crafting apps; `serve` takes them too) or bridged to the running app (`--bridge `). 2. **JSON lines** (`materialcraft-cli serve`): the same methods on stdio, or on `127.0.0.1:` behind a token. 3. **The app's control channel** (`materialcraft --control `): one JSON request per line,