diff --git a/crates/native/src/agents.rs b/crates/native/src/agents.rs index 6a3c9333..81c04ee2 100644 --- a/crates/native/src/agents.rs +++ b/crates/native/src/agents.rs @@ -38,14 +38,49 @@ pub fn mcp_dir() -> Option { } } -/// The app list for `services`: each app with a CLI, started as ` mcp`. Apps without a CLI -/// beside them are left out. +/// Arguments after `mcp` for apps whose plain `mcp` is not what agents should get: +/// - PhotoCraft: `--live` reaches the open window through the connection the app publishes, and +/// starts it when it is closed (storytold/photocraft#13396). +/// - VectorCraft: plain `mcp` attaches to a control port that has no token yet whenever something +/// listens on 7979; `--headless` keeps agents in their own session until it authenticates. +/// +/// Each is used only when that install's CLI accepts it ([`cli_accepts`]), so an older release +/// still gets plain `mcp` instead of an error. +const MCP_EXTRA_ARGS: &[(&str, &[&str])] = &[("photocraft", &["--live"]), ("vectorcraft", &["--headless"])]; + +/// Whether `cli mcp --help` names every argument of `extra` ([`help_names`]). Asking the CLI to +/// parse them is no test: some CLIs print help and succeed whatever comes before `--help`. No +/// console window, standard input closed, 5 s at most. +fn cli_accepts(cli: &Path, extra: &[&str]) -> bool { + let Some(program) = cli.to_str() else { return false }; + crate::process::capture(program, &["mcp", "--help"], std::time::Duration::from_secs(5)).is_some_and(|help| help_names(&help, extra)) +} + +/// Whether `help` has each of `args` as a word of its own (`--live`, not `--lively`). +fn help_names(help: &str, args: &[&str]) -> bool { + let words: Vec<&str> = help.split(|c: char| c.is_whitespace() || "[]|,()=<>`'\"".contains(c)).collect(); + args.iter().all(|a| words.contains(a)) +} + +/// The app list for `services`: each app with a CLI, started as ` mcp` plus its +/// [`MCP_EXTRA_ARGS`]. Apps without a CLI beside them are left out. pub fn apps_json(services: &[InstalledApp]) -> Value { + apps_json_with(services, &cli_accepts) +} + +/// [`apps_json`], asking `accepts` whether a CLI takes an app's extra arguments. +fn apps_json_with(services: &[InstalledApp], accepts: &dyn Fn(&Path, &[&str]) -> bool) -> Value { let apps: Vec = services .iter() .filter_map(|s| { let cli = s.cli_path.as_ref().filter(|p| p.is_absolute())?; - Some(json!({"id": s.id, "name": s.name, "command": cli, "args": ["mcp"]})) + let mut args = vec!["mcp".to_string()]; + if let Some((_, extra)) = MCP_EXTRA_ARGS.iter().find(|(id, _)| *id == s.id) + && accepts(cli, extra) + { + args.extend(extra.iter().map(|a| (*a).to_string())); + } + Some(json!({"id": s.id, "name": s.name, "command": cli, "args": args})) }) .collect(); json!({"version": 1, "apps": apps}) diff --git a/crates/native/src/agents_tests.rs b/crates/native/src/agents_tests.rs index c6ec2238..3f43308f 100644 --- a/crates/native/src/agents_tests.rs +++ b/crates/native/src/agents_tests.rs @@ -30,6 +30,55 @@ fn the_app_list_has_every_app_with_a_cli_in_the_gateways_format() { assert_eq!(apps[0]["args"], json!(["mcp"])); } +#[test] +fn apps_get_their_extra_mcp_arguments_only_where_the_cli_takes_them() { + let dir = tempfile::tempdir().unwrap(); + let at = |name: &str| dir.path().join(name); + let services = [ + service("photocraft", "PhotoCraft", Some(at("photocraft-cli"))), + service("vectorcraft", "VectorCraft", Some(at("vectorcraft-cli"))), + service("filmcraft", "FilmCraft", Some(at("filmcraft-cli"))), + ]; + let asked = std::cell::RefCell::new(Vec::new()); + let new = apps_json_with(&services, &|cli: &Path, extra: &[&str]| { + asked.borrow_mut().push((cli.file_name().unwrap().to_string_lossy().into_owned(), extra.join(" "))); + true + }); + let args = |list: &Value, i: usize| list["apps"][i]["args"].clone(); + assert_eq!(args(&new, 0), json!(["mcp", "--live"]), "PhotoCraft: the open window"); + assert_eq!(args(&new, 1), json!(["mcp", "--headless"]), "VectorCraft: off its unauthenticated port"); + assert_eq!(args(&new, 2), json!(["mcp"]), "other apps as they are"); + assert_eq!( + *asked.borrow(), + [("photocraft-cli".into(), "--live".into()), ("vectorcraft-cli".into(), "--headless".into())], + "only apps with extras are asked" + ); + // An older release that doesn't know the argument gets plain `mcp`, not a usage error. + let old = apps_json_with(&services, &|_: &Path, _: &[&str]| false); + for i in 0..3 { + assert_eq!(args(&old, i), json!(["mcp"])); + } +} + +#[test] +fn the_help_text_must_name_the_argument_as_a_word() { + let help = "Usage: photocraft-cli mcp [OPTIONS] + --live + Reach the open window + vectorcraft-cli mcp [--connect ADDR | --headless]"; + assert!(help_names(help, &["--live"])); + assert!(help_names(help, &["--headless"]), "inside brackets"); + assert!(!help_names(" --lively", &["--live"])); + assert!(!help_names("bridge to a running app, or live", &["--live"])); + assert!(!help_names(help, &["--live", "--when-closed"]), "every argument"); +} + +#[test] +fn a_cli_that_cannot_run_takes_no_extra_arguments() { + let dir = tempfile::tempdir().unwrap(); + assert!(!cli_accepts(&dir.path().join("missing-cli"), &["--live"])); +} + #[test] fn switching_on_and_off() { let dir = tempfile::tempdir().unwrap();