diff --git a/crates/mcp/src/agents.rs b/crates/mcp/src/agents.rs index 8f7a8bc8b4..8fdc5ca5d5 100644 --- a/crates/mcp/src/agents.rs +++ b/crates/mcp/src/agents.rs @@ -18,6 +18,7 @@ use std::io::{BufRead, BufReader, ErrorKind, Read, Write}; use std::net::{SocketAddr, TcpStream}; use std::path::{Path, PathBuf}; use std::rc::Rc; +use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex, PoisonError}; use std::time::{Duration, Instant}; @@ -284,7 +285,15 @@ impl LiveBackend { pub fn new(cfg: LiveConfig) -> Self { let LiveConfig { config_dir, read_settings, when_closed, read_roots, write_root, launch, launch_timeout, allowed, default_folder, headless } = cfg; - let consent = move |dir: &Path| Consent::new(allowed(), read_settings(dir)); + // The router's own decision on the last lookup (it asks for consent, then makes the + // headless session, under one lock), so the session's file access follows that decision. + let agents_in = Arc::new(AtomicBool::new(false)); + let decided = Arc::clone(&agents_in); + let consent = move |dir: &Path| { + let consent = Consent::new(allowed(), read_settings(dir)); + decided.store(consent.allowed && consent.settings.allow_agent_control, Ordering::SeqCst); + consent + }; let connect = |w: &shared::Window| -> Result { let mut window = Window::new(w.port, w.token.clone()); window.connect()?; @@ -292,7 +301,9 @@ impl LiveBackend { }; let make_headless = move |wanted: &HeadlessFolders| -> Result { let roots = if wanted.read.is_empty() && wanted.write.is_none() { - None + // Agents the person let in are confined to the agent folders: when none can be + // used (the default one can't be made), the session gets no file access. + if agents_in.load(Ordering::SeqCst) { Some(AutomationRoots::none()) } else { None } } else { let read: Vec<&Path> = wanted.read.iter().map(PathBuf::as_path).collect(); let write: Vec<&Path> = wanted.write.as_deref().into_iter().collect(); diff --git a/crates/mcp/src/tests/agents.rs b/crates/mcp/src/tests/agents.rs index 4be7aa0e05..0453cd552f 100644 --- a/crates/mcp/src/tests/agents.rs +++ b/crates/mcp/src/tests/agents.rs @@ -151,6 +151,35 @@ fn a_connection_file_with_nothing_listening_follows_the_closed_app_choice() { assert!(e.contains("outside the read root"), "{e}"); } +// The agent folder can't be made (its parent is a file): the headless session the person chose for +// a closed app gets no file access, rather than access to everything. +#[test] +fn a_headless_agent_session_without_usable_folders_reads_and_writes_nothing() { + let dir = TempFolder::new("agents-no-folder"); + std::fs::write(dir.file("blocker"), b"").unwrap(); + let mut b = live(&dir, || true, settings_headless, dir.file("blocker").join("Agents")); + assert!(!b.has_ui()); + let folders = b.call(FOLDERS_METHOD, json!({})).unwrap(); + assert_ne!(folders["unrestricted"], true, "{folders}"); + let outside = dir.file("outside.svg"); + std::fs::write(&outside, r#""#).unwrap(); + let e = b.call("app.open", json!({"path": outside.to_string_lossy()})).unwrap_err(); + assert!(e.contains("read authority is absent"), "{e}"); + let saved = dir.file("saved.svg"); + let e = b.call("app.save", json!({"path": saved.to_string_lossy()})).unwrap_err(); + assert!(e.contains("write authority is absent"), "{e}"); + assert!(!saved.exists(), "the refused save must not write the file"); + + // With Allow Agent Control off, the session is the ordinary headless fallback, as before. + fn control_off(dir: &Path) -> AgentSettings { + let mut s = settings_headless(dir); + s.allow_agent_control = false; + s + } + let mut b = live(&dir, || true, control_off, dir.file("blocker").join("Agents")); + assert_eq!(b.call(FOLDERS_METHOD, json!({})).unwrap()["unrestricted"], true); +} + /// A canonical path as the automation roots show it: without Windows' `\\?\` prefix. fn shown(p: PathBuf) -> String { let s = p.to_string_lossy().into_owned(); diff --git a/docs/mcp.md b/docs/mcp.md index 52bb158dd9..583ed1074c 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -42,7 +42,8 @@ does: open VectorCraft and wait for it (the default), run the call in a session the agent folders, or answer with an error that asks to open VectorCraft. `--when-closed launch|headless|error` overrides the choice for one server. A session that went headless stays headless, so documents don't move under the agent. While agents may not use VectorCraft, plain `mcp` runs headless with -only the folders given on its command line, as before. +only the folders given on its command line, as before. If no agent folder is set and the default folder cannot +be created, a headless agent session has no file access. Its `agent_folders` response lists no folders. Preferences › AI Integrations shows the switch's state, the port agents connect on, Control Port (0: any free port), the two folders and the closed-app choice. Without the `AI-MCP` flag it only says that agents use