agentmux_mcp/tools/
drone_tool.rs1use super::*;
9
10const ACTIONS: &[&str] = &["list", "get", "fire", "runs"];
11
12pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
13 match name {
14 "Drone" => {
15 require_agent_env(cx.local_url, cx.auth_key, cx.block_id)?;
16 let action = arguments.get("action").and_then(Value::as_str).map(str::trim).unwrap_or("");
17 if !ACTIONS.contains(&action) {
18 anyhow::bail!("Drone: unknown action {action:?}; use {}", ACTIONS.join(", "));
19 }
20 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"))?;
21 let url = format!("{}/api/v1/drone/{action}", cx.local_url.trim_end_matches('/'));
22 let reply = crate::srv_http::srv_post_json(cx.client, &url, cx.auth_key, &body_of(&self_id, arguments), &format!("Drone {action}")).await?;
23 Ok(serde_json::to_string_pretty(&reply).unwrap_or_default())
24 }
25 _ => Err(not_in_family()),
26 }
27}
28
29fn body_of(self_id: &str, arguments: &Value) -> Value {
31 let mut body = json!({ "agent_id": self_id });
32 for key in ["drone_id", "input", "limit"] {
33 if let Some(v) = arguments.get(key).filter(|v| !v.is_null()) {
34 body[key] = v.clone();
35 }
36 }
37 body
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn every_action_is_in_the_schema() {
46 let schema: Value = serde_json::from_str(crate::tool_schemas::DRONE_TOOL).unwrap();
47 let mut listed: Vec<&str> = schema["inputSchema"]["properties"]["action"]["enum"].as_array().unwrap().iter().filter_map(|v| v.as_str()).collect();
48 let mut want = ACTIONS.to_vec();
49 listed.sort();
50 want.sort();
51 assert_eq!(listed, want);
52 }
53
54 #[test]
55 fn the_caller_is_the_agent_and_only_the_known_arguments_go_to_srv() {
56 let args = json!({ "action": "fire", "drone_id": "d1", "input": { "n": 1 }, "limit": 5, "agent_id": "someone-else", "extra": 1 });
57 let body = body_of("loap", &args);
58 assert_eq!(body["agent_id"], "loap", "an argument can't name another caller");
59 assert_eq!((body["drone_id"].as_str(), body["input"]["n"].as_i64(), body["limit"].as_i64()), (Some("d1"), Some(1), Some(5)));
60 assert!(body.get("extra").is_none() && body.get("action").is_none());
61 }
62
63 #[tokio::test]
64 async fn an_unknown_action_is_refused_before_anything_is_sent() {
65 let client = reqwest::Client::new();
66 let loops = LoopRegistry::default();
67 let counter = std::sync::atomic::AtomicU64::new(0);
68 let cx = ToolCtx { local_url: "http://127.0.0.1:1", auth_key: "k", block_id: "b", client: &client, loops: &loops, loop_counter: &counter };
69 let e = call("Drone", &json!({ "action": "delete" }), &cx).await.unwrap_err().to_string();
70 assert!(e.starts_with("Drone: unknown action"), "{e}");
71 }
72}