Skip to main content

agentmux_mcp/tools/
presets.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Presets.
5
6use super::*;
7
8pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
9    let ToolCtx { local_url, auth_key, block_id, client, .. } = *cx;
10    match name {
11        "PresetList" => {
12            require_agent_env(local_url, auth_key, block_id)?;
13            let url = format!("{}/api/v1/agent/preset/list", local_url.trim_end_matches('/'));
14            let resp = client
15                .get(&url)
16                .header(AUTH_KEY_HEADER, auth_key)
17                .send()
18                .await
19                .map_err(|e| anyhow::anyhow!("request failed: {e}"))?;
20            if !resp.status().is_success() {
21                let status = resp.status();
22                let text = resp.text().await.unwrap_or_default();
23                anyhow::bail!("preset/list failed: HTTP {status} — {text}");
24            }
25            let result: Value = resp
26                .json()
27                .await
28                .map_err(|e| anyhow::anyhow!("response parse failed: {e}"))?;
29            Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
30        }
31        "PresetGet" => {
32            require_agent_env(local_url, auth_key, block_id)?;
33            let agent_id = agent_slug()?;
34            let url = format!("{}/api/v1/agent/preset/get", local_url.trim_end_matches('/'));
35            // id/name are both optional; with neither set the server returns the
36            // agent's own bound preset ("self"), resolved from agent_id.
37            let mut query: Vec<(&str, &str)> = vec![("agent_id", agent_id.as_str())];
38            if let Some(pid) = arguments.get("id").and_then(|v| v.as_str()).filter(|s| !s.is_empty()) {
39                query.push(("id", pid));
40            }
41            if let Some(pname) = arguments.get("name").and_then(|v| v.as_str()).filter(|s| !s.is_empty()) {
42                query.push(("name", pname));
43            }
44            let resp = client
45                .get(&url)
46                .header(AUTH_KEY_HEADER, auth_key)
47                .query(&query)
48                .send()
49                .await
50                .map_err(|e| anyhow::anyhow!("request failed: {e}"))?;
51            if !resp.status().is_success() {
52                let status = resp.status();
53                let text = resp.text().await.unwrap_or_default();
54                anyhow::bail!("preset/get failed: HTTP {status} — {text}");
55            }
56            let result: Value = resp
57                .json()
58                .await
59                .map_err(|e| anyhow::anyhow!("response parse failed: {e}"))?;
60            Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
61        }
62        _ => Err(not_in_family()),
63    }
64}