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 <noreply@anthropic.com>
This commit is contained in:
jayzed80andClaude Opus committed 2026-11-04 21:58:48 +00:00
1 parent 57c06be62a
commit bdfc3daabd
2 files changed
+45 -5

No files matched your search

+43 -4
View File
@@ -40,7 +40,8 @@ USAGE:
render: real displacement from the height output, ambient occlusion and a key light.
materialcraft-cli mcp [--bridge <port>] [--token <token>] [--read-root <dir>] [--write-root <dir>]
MCP server on stdio: headless (file access only below the roots), or bridged to
`materialcraft --control <port>`.
`materialcraft --control <port>`. --automation-read-root / --automation-write-root are
the same as --read-root / --write-root (mcp and serve).
materialcraft-cli serve [--port <port>] [--read-root <dir>] [--write-root <dir>]
JSON lines ({\"id\",\"method\",\"params\"}) on stdio, or on 127.0.0.1:<port> 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<String>,
flags: Vec<(String, Option<String>)>,
@@ -91,7 +96,8 @@ fn parse(cmd: &str, raw: &[String]) -> Result<Args, String> {
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::<f64>().is_err() {
@@ -301,3 +307,36 @@ fn workspace(a: &Args) -> Result<Filesystem, String> {
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<String> {
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());
}
}
+2 -1
View File
@@ -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 <port>`).
`--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 <port>`).
2. **JSON lines** (`materialcraft-cli serve`): the same methods on stdio, or on `127.0.0.1:<port>`
behind a token.
3. **The app's control channel** (`materialcraft --control <port>`): one JSON request per line,