Skip to main content

agentmux_mcp/tools/
accounts.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Identity accounts.
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        "IdentityAccounts" => {
12            require_agent_env(local_url, auth_key, block_id)?;
13            let agent_id = agent_slug()?;
14            let url = format!("{}/api/v1/agent/identity/accounts", local_url.trim_end_matches('/'));
15            let resp = client
16                .get(&url)
17                .header(AUTH_KEY_HEADER, auth_key)
18                .query(&[("agent_id", agent_id.as_str())])
19                .send()
20                .await
21                .map_err(|e| anyhow::anyhow!("request failed: {e}"))?;
22            if !resp.status().is_success() {
23                let status = resp.status();
24                let text = resp.text().await.unwrap_or_default();
25                anyhow::bail!("identity/accounts failed: HTTP {status} — {text}");
26            }
27            let result: Value = resp
28                .json()
29                .await
30                .map_err(|e| anyhow::anyhow!("response parse failed: {e}"))?;
31            Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
32        }
33        "IdentityValidate" => {
34            let account_id = arguments
35                .get("account_id")
36                .and_then(|v| v.as_str())
37                .filter(|s| !s.is_empty())
38                .ok_or_else(|| anyhow::anyhow!("missing required parameter: account_id"))?;
39            require_agent_env(local_url, auth_key, block_id)?;
40            let agent_id = agent_slug()?;
41            let url = format!("{}/api/v1/agent/identity/validate", local_url.trim_end_matches('/'));
42            let body = json!({
43                "agent_id": agent_id,
44                "account_id": account_id,
45            });
46            let resp = client
47                .post(&url)
48                .header(AUTH_KEY_HEADER, auth_key)
49                .json(&body)
50                .send()
51                .await
52                .map_err(|e| anyhow::anyhow!("request failed: {e}"))?;
53            if !resp.status().is_success() {
54                let status = resp.status();
55                let text = resp.text().await.unwrap_or_default();
56                anyhow::bail!("identity/validate failed: HTTP {status} — {text}");
57            }
58            let result: Value = resp
59                .json()
60                .await
61                .map_err(|e| anyhow::anyhow!("response parse failed: {e}"))?;
62            Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
63        }
64        _ => Err(not_in_family()),
65    }
66}