diff --git a/crates/sandbox/src/main.rs b/crates/sandbox/src/main.rs index 36369bb..de78701 100644 --- a/crates/sandbox/src/main.rs +++ b/crates/sandbox/src/main.rs @@ -536,17 +536,31 @@ fn setup_windows(args: &[OsString]) -> Result { } } +/// `insbx mcp`: the sandbox server on stdio, its sandboxes under `--root` or, +/// by default, under `/insbx-mcp-`. That default root is this +/// server's own and is removed when the server stops, unless `--keep` was +/// given (tested by `tests/mcp_root.rs`; the server is +/// `incord_sandbox::server::Server`). fn mcp(args: &[OsString]) -> Result { - let (root, mut ceilings) = parse_mcp(args)?; - let root = root + let (given, mut ceilings) = parse_mcp(args)?; + let root = given + .clone() .unwrap_or_else(|| std::env::temp_dir().join(format!("insbx-mcp-{}", std::process::id()))); std::fs::create_dir_all(&root).map_err(|e| format!("{}: {e}", root.display()))?; ceilings.root = incord_sandbox::plain_path(std::fs::canonicalize(&root).map_err(|e| e.to_string())?); + let keep = ceilings.keep; let stdin = std::io::stdin(); - incord_sandbox::server::Server::new(ceilings) - .serve(stdin.lock(), std::io::stdout().lock()) - .map_err(|e| format!("mcp: {e}"))?; + let served = + incord_sandbox::server::Server::new(ceilings).serve(stdin.lock(), std::io::stdout().lock()); + // The default root is this server's own: once its sandboxes are gone, + // remove it too, so no empty `insbx-mcp-` is left per server. Only + // when empty (`remove_dir` refuses a folder with anything in it): a + // sandbox that could not be deleted stays where it is. + if given.is_none() && !keep { + let _ = std::fs::remove_dir(&root); + } + served.map_err(|e| format!("mcp: {e}"))?; Ok(0) } diff --git a/crates/sandbox/tests/mcp_root.rs b/crates/sandbox/tests/mcp_root.rs new file mode 100644 index 0000000..2188f62 --- /dev/null +++ b/crates/sandbox/tests/mcp_root.rs @@ -0,0 +1,79 @@ +//! `insbx mcp`'s default root (`/insbx-mcp-`) is the server's own: +//! it goes when the server stops, on every platform (`fn mcp` in +//! `src/main.rs`). A `--root` that was given is the caller's and stays; the +//! sandboxes in either are removed unless `--keep` (`tests/mcp.rs`). + +use std::io::{BufRead, BufReader, Write}; +use std::process::{ChildStdin, ChildStdout, Command, Stdio}; + +use serde_json::{json, Value}; + +/// Send request `id` and return its reply. +fn request( + stdin: &mut ChildStdin, + stdout: &mut BufReader, + id: u64, + method: &str, + params: Value, +) -> Value { + let msg = json!({"jsonrpc": "2.0", "id": id, "method": method, "params": params}); + writeln!(stdin, "{msg}").expect("write"); + stdin.flush().expect("flush"); + let mut line = String::new(); + stdout.read_line(&mut line).expect("read"); + let reply: Value = serde_json::from_str(&line).expect("json"); + assert_eq!(reply["id"], id, "{reply}"); + reply +} + +#[test] +fn the_default_root_goes_with_the_server() { + let temp = tempfile::tempdir().expect("tempdir"); + let mut child = Command::new(env!("CARGO_BIN_EXE_insbx")) + .args(["mcp", "--net", "allow"]) + .env("TMPDIR", temp.path()) + .env("TMP", temp.path()) + .env("TEMP", temp.path()) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .expect("insbx mcp starts"); + let mut stdin = child.stdin.take().expect("stdin"); + let mut stdout = BufReader::new(child.stdout.take().expect("stdout")); + request( + &mut stdin, + &mut stdout, + 1, + "initialize", + json!({"protocolVersion": "2025-06-18"}), + ); + writeln!( + stdin, + "{}", + json!({"jsonrpc": "2.0", "method": "notifications/initialized"}) + ) + .expect("write"); + let reply = request( + &mut stdin, + &mut stdout, + 2, + "tools/call", + json!({"name": "sandbox_create", "arguments": {"network": "allow"}}), + ); + let text = reply["result"]["content"][0]["text"] + .as_str() + .expect("text"); + let created: Value = serde_json::from_str(text).expect("tool result is JSON"); + let folder = std::path::PathBuf::from(created["folder"].as_str().expect("folder")); + let root = folder.parent().expect("root").to_path_buf(); + assert!(folder.is_dir()); + assert!(root + .file_name() + .expect("name") + .to_string_lossy() + .starts_with("insbx-mcp-")); + + drop(stdin); + child.wait().expect("insbx mcp exits"); + assert!(!root.exists(), "{} left behind", root.display()); +}