Skip to main content

agentmux_mcp/tools/
sensitive_tool.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Sensitive: ask for an operation too sweeping for one agent's say-so, or, as
5//! the super agent, decide such a request (docs/specs/SPEC_SUPER_AGENT_2026_10_25.md
6//! ยง4). srv holds the requests, the checks and the countdown the person can veto;
7//! this only carries the call, as the agent its token names.
8
9use super::*;
10
11const ACTIONS: &[&str] = &["request", "approve", "deny", "cancel", "status", "list"];
12
13pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
14    match name {
15        "Sensitive" => {
16            require_agent_env(cx.local_url, cx.auth_key, cx.block_id)?;
17            let action = arguments.get("action").and_then(Value::as_str).map(str::trim).unwrap_or("");
18            if !ACTIONS.contains(&action) {
19                anyhow::bail!("Sensitive: unknown action {action:?}; use {}", ACTIONS.join(", "));
20            }
21            let self_id = std::env::var("AGENTMUX_AGENT_ID").ok().filter(|s| !s.is_empty()).ok_or_else(|| anyhow::anyhow!("AGENTMUX_AGENT_ID is not set"))?;
22            let url = format!("{}/api/v1/sensitive/{action}", cx.local_url.trim_end_matches('/'));
23            let reply = crate::srv_http::srv_post_json(cx.client, &url, cx.auth_key, &body_of(&self_id, arguments), &format!("Sensitive {action}")).await?;
24            Ok(serde_json::to_string_pretty(&reply).unwrap_or_default())
25        }
26        _ => Err(not_in_family()),
27    }
28}
29
30/// What srv is sent: the caller, and the arguments the actions take.
31fn body_of(self_id: &str, arguments: &Value) -> Value {
32    let mut body = json!({ "agent_id": self_id });
33    for key in ["op", "id", "reason", "facts"] {
34        if let Some(v) = arguments.get(key).filter(|v| !v.is_null()) {
35            body[key] = v.clone();
36        }
37    }
38    body
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn every_action_is_in_the_schema() {
47        let schema: Value = serde_json::from_str(crate::tool_schemas::SENSITIVE_TOOL).unwrap();
48        let mut listed: Vec<&str> = schema["inputSchema"]["properties"]["action"]["enum"].as_array().unwrap().iter().filter_map(|v| v.as_str()).collect();
49        let mut want = ACTIONS.to_vec();
50        listed.sort();
51        want.sort();
52        assert_eq!(listed, want);
53    }
54
55    #[test]
56    fn the_caller_is_the_agent_and_only_the_known_arguments_go_to_srv() {
57        let args = json!({ "action": "request", "op": "upgrade", "facts": { "version": "1.0" }, "reason": "why", "agent_id": "someone-else", "extra": 1 });
58        let body = body_of("parlo", &args);
59        assert_eq!(body["agent_id"], "parlo", "an argument can't name another caller");
60        assert_eq!((body["op"].as_str(), body["facts"]["version"].as_str(), body["reason"].as_str()), (Some("upgrade"), Some("1.0"), Some("why")));
61        assert!(body.get("extra").is_none() && body.get("action").is_none());
62    }
63
64    #[tokio::test]
65    async fn an_unknown_action_is_refused_before_anything_is_sent() {
66        let client = reqwest::Client::new();
67        let loops = LoopRegistry::default();
68        let counter = std::sync::atomic::AtomicU64::new(0);
69        let cx = ToolCtx { local_url: "http://127.0.0.1:1", auth_key: "k", block_id: "b", client: &client, loops: &loops, loop_counter: &counter };
70        let e = call("Sensitive", &json!({ "action": "shred" }), &cx).await.unwrap_err().to_string();
71        assert!(e.starts_with("Sensitive: unknown action"), "{e}");
72    }
73}