agentmux_mcp/tools/
pty_shell.rs1use super::*;
9
10pub(super) async fn call(name: &str, arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
11 match name {
12 "PtyShell" => run_pty_shell(arguments, cx).await,
13 "PtyShellInput" => run_pty_shell_input(arguments, cx).await,
14 "PtyShellResize" => run_pty_shell_resize(arguments, cx).await,
15 "PtyShellRead" => run_pty_shell_read(arguments, cx).await,
16 "PtyShellStatus" => run_pty_shell_status(arguments, cx).await,
17 "PtyShellStop" => run_pty_shell_stop(arguments, cx).await,
18 _ => Err(not_in_family()),
19 }
20}
21
22async fn run_pty_shell(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
23 let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
24 if local_url.is_empty() || auth_key.is_empty() {
25 anyhow::bail!(
26 "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
27 Is this agent pane opened via AgentMux?"
28 );
29 }
30 if block_id.is_empty() {
31 anyhow::bail!(
32 "neither AGENTMUX_AGENT_BUS_ID nor AGENTMUX_BLOCKID is set \
33 — cannot associate the shell with a conversation pane. \
34 Is this agent pane opened via AgentMux?"
35 );
36 }
37
38 let cwd = arguments.get("cwd").and_then(|v| v.as_str()).map(str::to_string);
39 let rows = arguments.get("rows").and_then(|v| v.as_u64()).map(|n| n as u16);
40 let cols = arguments.get("cols").and_then(|v| v.as_u64()).map(|n| n as u16);
41
42 let connection = arguments.get("connection").and_then(|v| v.as_str()).map(str::to_string);
43 let req = PtyShellCreateRequest {
44 agent_block_id: block_id.to_string(),
45 cwd,
46 rows,
47 cols,
48 connection,
49 auth: sign_ui_automation_auth().ok(),
50 };
51 let result: PtyShellCreateResponse =
52 cx.post_as_within("/api/v1/ptyshell/create", &req, "ptyshell/create", crate::srv_http::SHELL_CREATE_TIMEOUT).await?;
53 Ok(result.shell_id)
54}
55
56async fn run_pty_shell_input(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
57 let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
58 let shell_id = arguments
59 .get("shell_id")
60 .and_then(|v| v.as_str())
61 .ok_or_else(|| anyhow::anyhow!("missing required parameter: shell_id"))?;
62 let text = arguments
63 .get("text")
64 .and_then(|v| v.as_str())
65 .ok_or_else(|| anyhow::anyhow!("missing required parameter: text"))?;
66
67 if local_url.is_empty() || auth_key.is_empty() {
68 anyhow::bail!(
69 "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
70 Is this agent pane opened via AgentMux?"
71 );
72 }
73
74 let body = PtyShellInputRequest {
75 shell_id: shell_id.to_string(),
76 agent_block_id: block_id.to_string(),
77 text: text.to_string(),
78 };
79 let result: PtyShellInputResponse = cx.post_as("/api/v1/ptyshell/input", &body, "ptyshell/input").await?;
80 Ok(if result.written {
81 format!("wrote to shell {shell_id}")
82 } else {
83 format!(
84 "shell {shell_id}: write failed — {}",
85 result.error.unwrap_or_else(|| "unknown reason".to_string())
86 )
87 })
88}
89
90async fn run_pty_shell_resize(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
91 let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
92 let shell_id = arguments
93 .get("shell_id")
94 .and_then(|v| v.as_str())
95 .ok_or_else(|| anyhow::anyhow!("missing required parameter: shell_id"))?;
96 let rows = arguments
97 .get("rows")
98 .and_then(|v| v.as_u64())
99 .ok_or_else(|| anyhow::anyhow!("missing required parameter: rows"))? as u16;
100 let cols = arguments
101 .get("cols")
102 .and_then(|v| v.as_u64())
103 .ok_or_else(|| anyhow::anyhow!("missing required parameter: cols"))? as u16;
104
105 if local_url.is_empty() || auth_key.is_empty() {
106 anyhow::bail!(
107 "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
108 Is this agent pane opened via AgentMux?"
109 );
110 }
111
112 let body = PtyShellResizeRequest {
113 shell_id: shell_id.to_string(),
114 agent_block_id: block_id.to_string(),
115 rows,
116 cols,
117 };
118 let result: PtyShellResizeResponse = cx.post_as("/api/v1/ptyshell/resize", &body, "ptyshell/resize").await?;
119 Ok(if result.resized {
120 format!("resized shell {shell_id} to {rows}x{cols}")
121 } else {
122 format!(
123 "shell {shell_id}: resize failed — {}",
124 result.error.unwrap_or_else(|| "unknown reason".to_string())
125 )
126 })
127}
128
129async fn run_pty_shell_read(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
130 let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
131 let shell_id = arguments
132 .get("shell_id")
133 .and_then(|v| v.as_str())
134 .ok_or_else(|| anyhow::anyhow!("missing required parameter: shell_id"))?;
135 let tail_lines = arguments.get("tail_lines").and_then(|v| v.as_u64()).map(|n| n as u32);
136
137 if local_url.is_empty() || auth_key.is_empty() {
138 anyhow::bail!(
139 "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
140 Is this agent pane opened via AgentMux?"
141 );
142 }
143
144 let body = PtyShellReadRequest {
145 shell_id: shell_id.to_string(),
146 agent_block_id: block_id.to_string(),
147 tail_lines,
148 since: None,
149 };
150 let result: PtyShellReadResponse = cx.post_as("/api/v1/ptyshell/read", &body, "ptyshell/read").await?;
151 let prefix = if result.truncated { "[...output truncated...]\n" } else { "" };
152 Ok(format!("{prefix}{}", result.content))
153}
154
155async fn run_pty_shell_status(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
156 let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
157 let shell_id = arguments
158 .get("shell_id")
159 .and_then(|v| v.as_str())
160 .ok_or_else(|| anyhow::anyhow!("missing required parameter: shell_id"))?;
161
162 if local_url.is_empty() || auth_key.is_empty() {
163 anyhow::bail!(
164 "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
165 Is this agent pane opened via AgentMux?"
166 );
167 }
168
169 let body = PtyShellStatusRequest {
170 shell_id: shell_id.to_string(),
171 agent_block_id: block_id.to_string(),
172 };
173 let result: PtyShellStatusResponse = cx.post_as("/api/v1/ptyshell/status", &body, "ptyshell/status").await?;
174 Ok(if result.running {
175 format!("shell {shell_id} is running")
176 } else {
177 let code = result.exit_code.map(|c| c.to_string()).unwrap_or_else(|| "unknown".to_string());
178 format!("shell {shell_id} is not running — exit_code: {code}")
179 })
180}
181
182async fn run_pty_shell_stop(arguments: &Value, cx: &ToolCtx<'_>) -> Result<String> {
183 let ToolCtx { local_url, auth_key, block_id, .. } = *cx;
184 let shell_id = arguments
185 .get("shell_id")
186 .and_then(|v| v.as_str())
187 .ok_or_else(|| anyhow::anyhow!("missing required parameter: shell_id"))?;
188
189 if local_url.is_empty() || auth_key.is_empty() {
190 anyhow::bail!(
191 "AGENTMUX_LOCAL_URL and AGENTMUX_AUTH_KEY must be set. \
192 Is this agent pane opened via AgentMux?"
193 );
194 }
195
196 let body = PtyShellStopRequest {
197 shell_id: shell_id.to_string(),
198 agent_block_id: block_id.to_string(),
199 };
200 let result: PtyShellStopResponse = cx.post_as("/api/v1/ptyshell/stop", &body, "ptyshell/stop").await?;
201 Ok(if result.released {
202 format!("released the agent lock on shell {shell_id} (the shell itself keeps running)")
203 } else {
204 format!("shell {shell_id}: no active agent lock to release (unrecognized id, not yours, or already unlocked)")
205 })
206}
207
208#[cfg(test)]
209mod call_tests;