Skip to main content

agentmux_mcp/tools/
history.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Conversation history search and transcripts. Not advertised: `Agents`
5//! (agents_tool.rs) calls these by their old names.
6
7use super::*;
8
9pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
10    match name {
11        "SearchHistory" => run_search_history(arguments, cx).await,
12        "GetAgentTranscript" => run_get_agent_transcript(arguments, cx).await,
13        "ListConversations" => run_list_conversations(arguments, cx).await,
14        _ => Err(not_in_family()),
15    }
16}
17
18async fn run_search_history(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
19    let ToolCtx { local_url, auth_key, client, .. } = *cx;
20    // Whose history this is, srv takes from this process's
21    // `X-Agent-Token` (sent on every request, see `main`), never from a
22    // name; Agents' search_history refuses an `agent` on purpose — see
23    // AGENTS_TOOL's note. The name is sent only for srv's
24    // actor counters, so a process without one still searches.
25    let slug = std::env::var("AGENTMUX_AGENT_ID").unwrap_or_default();
26    let query = arguments
27        .get("query")
28        .and_then(|v| v.as_str())
29        .unwrap_or_default()
30        .to_string();
31    let tool = arguments.get("tool").and_then(|v| v.as_str());
32    if query.trim().is_empty() && tool.is_none() {
33        anyhow::bail!(
34            "query must be non-empty unless `tool` is given (an empty query with no \
35             tool filter would match everything and return a truncated firehose)"
36        );
37    }
38
39    if local_url.is_empty() || auth_key.is_empty() {
40        anyhow::bail!(
41            "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
42             Is this agent pane opened via AgentMux?"
43        );
44    }
45
46    let url = format!(
47        "{}/agentmux/reactive/history/search",
48        local_url.trim_end_matches('/')
49    );
50    let mut query_params: Vec<(&str, String)> = vec![("query", query)];
51    if !slug.trim().is_empty() {
52        query_params.push(("agent", slug));
53    }
54    if let Some(t) = tool {
55        query_params.push(("tool", t.to_string()));
56    }
57    if let Some(r) = arguments.get("role").and_then(|v| v.as_str()) {
58        query_params.push(("role", r.to_string()));
59    }
60    for (key, arg) in [
61        ("since", "since"),
62        ("until", "until"),
63        ("max_sessions", "max_sessions"),
64        ("limit", "limit"),
65    ] {
66        if let Some(n) = arguments.get(arg).and_then(|v| v.as_i64()) {
67            query_params.push((key, n.to_string()));
68        }
69    }
70    if arguments.get("include_inferred").and_then(|v| v.as_bool()) == Some(true) {
71        query_params.push(("include_inferred", "true".to_string()));
72    }
73
74    let body = srv_get_json(client, &url, auth_key, &query_params, "history search").await?;
75    Ok(serde_json::to_string_pretty(&body).unwrap_or_else(|_| body.to_string()))
76}
77
78async fn run_get_agent_transcript(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
79    let ToolCtx { local_url, auth_key, .. } = *cx;
80    let agent = arguments
81        .get("agent")
82        .and_then(|v| v.as_str())
83        .filter(|s| !s.is_empty())
84        .ok_or_else(|| anyhow::anyhow!("missing required parameter: agent"))?;
85    let max_lines = arguments.get("max_lines").and_then(|v| v.as_u64());
86
87    if local_url.is_empty() || auth_key.is_empty() {
88        anyhow::bail!(
89            "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
90             Is this agent pane opened via AgentMux?"
91        );
92    }
93
94    let max_lines = max_lines.map(|n| n.to_string());
95    let mut query = vec![("agent", agent)];
96    if let Some(n) = max_lines.as_deref() {
97        query.push(("max_lines", n));
98    }
99    let result = cx.get_json("/agentmux/reactive/transcript", &query, "transcript fetch").await?;
100
101    Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
102}
103
104async fn run_list_conversations(_arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
105    let ToolCtx { local_url, auth_key, .. } = *cx;
106    if local_url.is_empty() || auth_key.is_empty() {
107        anyhow::bail!(
108            "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
109             Is this agent pane opened via AgentMux?"
110        );
111    }
112
113    let result = cx.get_json("/api/v1/muxspect/conversations", &[], "conversations fetch").await?;
114
115    Ok(serde_json::to_string_pretty(&result).unwrap_or_else(|_| result.to_string()))
116}
117
118#[cfg(test)]
119mod call_tests;