Skip to main content

agentmux_mcp/tools/
aws_grants.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Identity `aws_grants` and `aws_grant`: the AWS grants this machine shares with its container agents, and the answer
5//! to a grant that is new or wider than what was allowed. Not advertised by name: `Identity` (identity_tool.rs) calls
6//! these.
7
8use super::*;
9
10fn arg(args: &Value, key: &str) -> Option<String> {
11    args.get(key).and_then(Value::as_str).map(str::trim).filter(|s| !s.is_empty()).map(str::to_string)
12}
13
14fn pretty(v: &Value) -> String {
15    serde_json::to_string_pretty(v).unwrap_or_else(|_| v.to_string())
16}
17
18pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
19    let ToolCtx { local_url, auth_key, block_id, client, .. } = *cx;
20    require_agent_env(local_url, auth_key, block_id)?;
21    let base = local_url.trim_end_matches('/');
22    let request = match name {
23        "IdentityAwsGrants" => client.get(format!("{base}/api/v1/aws/grants")),
24        "IdentityAwsGrant" => {
25            let need = |key: &str| arg(arguments, key).ok_or_else(|| anyhow::anyhow!("missing required parameter: {key}"));
26            let body = json!({ "grant": need("grant")?, "change": need("change")?, "decision": need("decision")?, "reason": need("reason")? });
27            client.post(format!("{base}/api/v1/aws/grants/decide")).json(&body)
28        }
29        _ => return Err(not_in_family()),
30    };
31    let resp = request.header(AUTH_KEY_HEADER, auth_key).send().await.map_err(|e| anyhow::anyhow!("request failed: {e}"))?;
32    let status = resp.status();
33    let text = resp.text().await.unwrap_or_default();
34    if !status.is_success() {
35        let said = serde_json::from_str::<Value>(&text).ok().and_then(|v| v.get("error").and_then(Value::as_str).map(str::to_string)).unwrap_or(text);
36        anyhow::bail!("aws grants: HTTP {status}: {said}");
37    }
38    let result: Value = serde_json::from_str(&text).map_err(|e| anyhow::anyhow!("response parse failed: {e}"))?;
39    Ok(pretty(&result))
40}