1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use pyo3::IntoPyObjectExt;
use rust_mcp_sdk::{
macros::{mcp_tool, JsonSchema},
schema::{schema_utils::CallToolError, CallToolResult, TextContent},
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, error};
#[mcp_tool(
name = "angreal_command",
description = "Execute an angreal command with arguments",
idempotent_hint = false,
destructive_hint = false,
open_world_hint = true,
read_only_hint = false
)]
#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct AngrealCommandTool {
/// The command path (e.g., "docs.build", "test.rust")
pub command_path: String,
/// Command arguments as key-value pairs
pub args: Option<HashMap<String, serde_json::Value>>,
}
impl AngrealCommandTool {
pub async fn call_tool(&self) -> Result<CallToolResult, CallToolError> {
debug!("Executing angreal command: {}", self.command_path);
// Ensure Python tasks are initialized
angreal::initialize_python_tasks().map_err(|e| {
CallToolError::new(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to initialize angreal tasks: {}", e),
))
})?;
// Get the command from the registry
let tasks = angreal::task::ANGREAL_TASKS.lock().map_err(|e| {
CallToolError::new(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to lock ANGREAL_TASKS: {}", e),
))
})?;
let command = tasks.get(&self.command_path).ok_or_else(|| {
CallToolError::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Command '{}' not found", self.command_path),
))
})?;
debug!(
"Found command '{}', executing with args: {:?}",
command.name, self.args
);
// Get command arguments definition
let args_registry = angreal::task::ANGREAL_ARGS.lock().map_err(|e| {
CallToolError::new(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Failed to lock ANGREAL_ARGS: {}", e),
))
})?;
let command_args = args_registry
.get(&self.command_path)
.cloned()
.unwrap_or_default();
// Execute the command with Python, capturing stdout/stderr
let result = Python::attach(|py| -> PyResult<(String, String, String)> {
debug!("Starting Python execution for command: {}", command.name);
// Import necessary modules for capturing output
let sys = py.import("sys")?;
let io = py.import("io")?;
// Create StringIO objects to capture stdout and stderr
let stdout_capture = io.call_method0("StringIO")?;
let stderr_capture = io.call_method0("StringIO")?;
// Save original stdout/stderr
let original_stdout = sys.getattr("stdout")?;
let original_stderr = sys.getattr("stderr")?;
// Redirect stdout/stderr to our captures
sys.setattr("stdout", &stdout_capture)?;
sys.setattr("stderr", &stderr_capture)?;
let mut kwargs: Vec<(&str, Py<PyAny>)> = Vec::new();
// Process provided arguments
if let Some(provided_args) = &self.args {
for arg in command_args.iter() {
let arg_name = &arg.name;
if let Some(value) = provided_args.get(arg_name) {
// Convert based on the argument's python_type
let python_type = arg.python_type.as_deref().unwrap_or("str");
let py_value = match python_type {
"str" => {
if let Some(s) = value.as_str() {
s.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
} else {
value
.to_string()
.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
}
}
"int" => {
if let Some(i) = value.as_i64() {
i.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
} else if let Some(s) = value.as_str() {
s.parse::<i64>()
.unwrap_or(0)
.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
} else {
0.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
}
}
"float" => {
if let Some(f) = value.as_f64() {
f.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
} else if let Some(s) = value.as_str() {
s.parse::<f64>()
.unwrap_or(0.0)
.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
} else {
0.0.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
}
}
"bool" => {
if let Some(b) = value.as_bool() {
b.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
} else if let Some(s) = value.as_str() {
s.parse::<bool>()
.unwrap_or(false)
.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
} else {
false
.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind()
}
}
_ => value
.to_string()
.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind(),
};
kwargs.push((Box::leak(Box::new(arg_name.clone())).as_str(), py_value));
} else if arg.is_flag.unwrap_or(false) {
// Default false for missing flags
kwargs.push((
Box::leak(Box::new(arg_name.clone())).as_str(),
false
.into_bound_py_any(py)
.expect("Failed to convert to Python")
.unbind(),
));
}
}
}
debug!("Calling Python function with {} arguments", kwargs.len());
// Call the command function
let kwargs_dict = kwargs.into_py_dict(py)?;
let result = command.func.call(py, (), Some(&kwargs_dict));
// Restore original stdout/stderr
sys.setattr("stdout", original_stdout)?;
sys.setattr("stderr", original_stderr)?;
// Get captured output
let stdout_output = stdout_capture.call_method0("getvalue")?.to_string();
let stderr_output = stderr_capture.call_method0("getvalue")?.to_string();
// Handle the command result
let result_str = match result {
Ok(result_obj) => {
if result_obj.is_none(py) {
format!("Command '{}' executed successfully", command.name)
} else {
result_obj.to_string()
}
}
Err(e) => {
return Err(e);
}
};
Ok((result_str, stdout_output, stderr_output))
});
match result {
Ok((return_value, stdout, stderr)) => {
debug!("Successfully executed command: {}", self.command_path);
let response = serde_json::json!({
"command": self.command_path,
"result": "success",
"return_value": return_value,
"stdout": stdout,
"stderr": stderr,
"timestamp": chrono::Utc::now().to_rfc3339()
});
Ok(CallToolResult::text_content(vec![TextContent::from(
serde_json::to_string_pretty(&response).map_err(CallToolError::new)?,
)]))
}
Err(err) => {
error!("Failed to execute command '{}': {}", self.command_path, err);
let error_response = serde_json::json!({
"command": self.command_path,
"result": "error",
"error": err.to_string(),
"timestamp": chrono::Utc::now().to_rfc3339()
});
Ok(CallToolResult::text_content(vec![TextContent::from(
serde_json::to_string_pretty(&error_response).map_err(CallToolError::new)?,
)]))
}
}
}
}