Skip to main content

agentmux_mcp/tools/
global_memory.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Global Memory.
5
6use super::*;
7
8pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
9    match name {
10        "GlobalMemoryList" => run_global_memory_list(arguments, cx).await,
11        "GlobalMemoryRead" => run_global_memory_read(arguments, cx).await,
12        "GlobalMemoryWrite" => run_global_memory_write(arguments, cx).await,
13        "GlobalMemoryReach" => run_global_memory_reach(arguments, cx).await,
14        "GlobalMemoryRemove" => run_global_memory_remove(arguments, cx).await,
15        "GlobalMemoryHistory" => run_global_memory_history(arguments, cx).await,
16        "GlobalMemoryDiff" => run_global_memory_diff(arguments, cx).await,
17        "GlobalMemoryRevert" => run_global_memory_revert(arguments, cx).await,
18        _ => Err(not_in_family()),
19    }
20}
21
22async fn run_global_memory_list(_arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
23    let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
24    require_agent_env(local_url, auth_key, block_id)?;
25    let result = cx.get_json("/api/v1/agent/globalmemory/list", &[], "globalmemory/list").await?;
26    Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
27}
28
29async fn run_global_memory_read(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
30    let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
31    let id = arguments
32        .get("id")
33        .and_then(|v| v.as_str())
34        .filter(|s| !s.is_empty())
35        .ok_or_else(|| anyhow::anyhow!("missing required parameter: id"))?;
36    require_agent_env(local_url, auth_key, block_id)?;
37    let result = cx.get_json("/api/v1/agent/globalmemory/read", &[("id", id)], "globalmemory/read").await?;
38    Ok(result
39        .get("content")
40        .and_then(|v| v.as_str())
41        .map(str::to_string)
42        .unwrap_or_else(|| {
43            serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string())
44        }))
45}
46
47async fn run_global_memory_write(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
48    let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
49    let name = arguments
50        .get("name")
51        .and_then(|v| v.as_str())
52        .filter(|s| !s.is_empty())
53        .ok_or_else(|| anyhow::anyhow!("missing required parameter: name"))?;
54    let content = arguments
55        .get("content")
56        .and_then(|v| v.as_str())
57        .ok_or_else(|| anyhow::anyhow!("missing required parameter: content"))?;
58    require_agent_env(local_url, auth_key, block_id)?;
59    let agent_id = agent_slug()?;
60    let mut body = json!({
61        "agent_id": agent_id,
62        "name": name,
63        "content": content,
64    });
65    if let Some(id) = arguments.get("id").and_then(|v| v.as_str()).filter(|s| !s.is_empty()) {
66        body["id"] = json!(id);
67    }
68    if let Some(reach) = arguments.get("reach").and_then(|v| v.as_str()).filter(|s| !s.is_empty()) {
69        body["reach"] = json!(reach);
70    }
71    // Pass provenance through verbatim when the caller supplied it —
72    // same advisory-metadata pattern as MemoryWrite above.
73    if let Some(provenance) = arguments.get("provenance") {
74        body["provenance"] = provenance.clone();
75    }
76    let result = cx.post_json("/api/v1/agent/globalmemory/write", &body, "globalmemory/write").await?;
77    let id = result.get("id").and_then(|v| v.as_str()).unwrap_or("?");
78    Ok(format!("Wrote Global Memory entry \"{name}\" (id: {id})"))
79}
80
81async fn run_global_memory_reach(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
82    let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
83    let id = arguments
84        .get("id")
85        .and_then(|v| v.as_str())
86        .filter(|s| !s.is_empty())
87        .ok_or_else(|| anyhow::anyhow!("missing required parameter: id"))?;
88    let reach = arguments
89        .get("reach")
90        .and_then(|v| v.as_str())
91        .filter(|s| !s.is_empty())
92        .ok_or_else(|| anyhow::anyhow!("missing required parameter: reach (\"fleet\" or \"machine\")"))?;
93    require_agent_env(local_url, auth_key, block_id)?;
94    let agent_id = agent_slug()?;
95    let body = json!({ "id": id, "reach": reach, "agent_id": agent_id });
96    cx.post("/api/v1/agent/globalmemory/reach", &body, "globalmemory/reach").await?;
97    Ok(match reach {
98        "fleet" => format!("Global Memory entry {id} now goes to every proven machine on the LAN; the newest write wins."),
99        _ => format!("Global Memory entry {id} now stays on this machine; a copy another machine sends is skipped."),
100    })
101}
102
103async fn run_global_memory_remove(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
104    let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
105    let id = arguments
106        .get("id")
107        .and_then(|v| v.as_str())
108        .filter(|s| !s.is_empty())
109        .ok_or_else(|| anyhow::anyhow!("missing required parameter: id"))?;
110    require_agent_env(local_url, auth_key, block_id)?;
111    let agent_id = agent_slug()?;
112    let body = json!({ "id": id, "agent_id": agent_id });
113    cx.post("/api/v1/agent/globalmemory/remove", &body, "globalmemory/remove").await?;
114    Ok(format!("Removed Global Memory entry {id}. It is kept in history: list shows it under removed, and revert restores it."))
115}
116
117async fn run_global_memory_history(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
118    let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
119    let id = arguments
120        .get("id")
121        .and_then(|v| v.as_str())
122        .filter(|s| !s.is_empty())
123        .ok_or_else(|| anyhow::anyhow!("missing required parameter: id"))?;
124    require_agent_env(local_url, auth_key, block_id)?;
125    let result = cx.get_json("/api/v1/agent/globalmemory/history", &[("id", id)], "globalmemory/history").await?;
126    Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
127}
128
129async fn run_global_memory_diff(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
130    let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
131    let id = arguments
132        .get("id")
133        .and_then(|v| v.as_str())
134        .filter(|s| !s.is_empty())
135        .ok_or_else(|| anyhow::anyhow!("missing required parameter: id"))?;
136    let from_version_id = arguments
137        .get("from_version_id")
138        .and_then(|v| v.as_str())
139        .filter(|s| !s.is_empty())
140        .ok_or_else(|| anyhow::anyhow!("missing required parameter: from_version_id"))?;
141    let to_version_id = arguments
142        .get("to_version_id")
143        .and_then(|v| v.as_str())
144        .filter(|s| !s.is_empty())
145        .ok_or_else(|| anyhow::anyhow!("missing required parameter: to_version_id"))?;
146    require_agent_env(local_url, auth_key, block_id)?;
147    let result = cx.get_json("/api/v1/agent/globalmemory/diff", &[("id", id), ("from_version_id", from_version_id), ("to_version_id", to_version_id)], "globalmemory/diff").await?;
148    Ok(result
149        .get("diff")
150        .and_then(|v| v.as_str())
151        .map(str::to_string)
152        .unwrap_or_else(|| {
153            serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string())
154        }))
155}
156
157async fn run_global_memory_revert(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
158    let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
159    let id = arguments
160        .get("id")
161        .and_then(|v| v.as_str())
162        .filter(|s| !s.is_empty())
163        .ok_or_else(|| anyhow::anyhow!("missing required parameter: id"))?;
164    let version_id = arguments
165        .get("version_id")
166        .and_then(|v| v.as_str())
167        .filter(|s| !s.is_empty())
168        .ok_or_else(|| anyhow::anyhow!("missing required parameter: version_id"))?;
169    require_agent_env(local_url, auth_key, block_id)?;
170    let agent_id = agent_slug()?;
171    let body = json!({
172        "agent_id": agent_id,
173        "id": id,
174        "version_id": version_id,
175    });
176    cx.post("/api/v1/agent/globalmemory/revert", &body, "globalmemory/revert").await?;
177    Ok(format!("Reverted Global Memory entry {id} to version {version_id}"))
178}
179
180#[cfg(test)]
181mod call_tests;