use rmcp::ErrorData;
use rmcp::model::Tool;
use serde_json::{Map, Value, json};
use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str};
use crate::tool_defs::tool_def;
pub struct ShellAliasTool;
impl McpTool for ShellAliasTool {
fn name(&self) -> &'static str {
"shell"
}
fn tool_def(&self) -> Tool {
tool_def(
"shell",
"Shell command with auto-compression (~95 patterns). Alias for ctx_shell.\n\
Output is compressed for token savings. For verbatim output pass raw=true.\n\
Use when your MCP client prefers shell/bash over ctx_shell — transparently\n\
delegates to ctx_shell internals.",
json!({
"type": "object",
"properties": {
"command": {
"type": "string",
"description": "Shell command"
},
"cwd": {
"type": "string",
"description": "Working dir"
},
"raw": {
"type": "boolean",
"description": "Return verbatim output (skip compression). Default false — pass true for the exact bytes."
}
},
"required": ["command"]
}),
)
}
fn handle(
&self,
args: &Map<String, Value>,
ctx: &ToolContext,
) -> Result<ToolOutput, ErrorData> {
let command = get_str(args, "command")
.ok_or_else(|| ErrorData::invalid_params("command is required", None))?;
let write_allow_paths =
crate::core::config::Config::load().shell_write_allow_paths_effective();
let project_root = crate::core::config::Config::find_project_root();
if let Some(rejection) = crate::tools::ctx_shell::validate_command_with_write_allow_paths(
&command,
&write_allow_paths,
project_root.as_deref(),
) {
return Ok(ToolOutput::simple(rejection));
}
if let Err(msg) = crate::core::shell_allowlist::check_shell_allowlist(&command) {
return Ok(ToolOutput::simple(msg.to_string()));
}
tokio::task::block_in_place(|| {
let cwd = get_str(args, "cwd");
let raw = args.get("raw").and_then(Value::as_bool).unwrap_or(false);
let mut shell_args = Map::new();
shell_args.insert("command".to_string(), Value::String(command));
if let Some(dir) = cwd {
shell_args.insert("cwd".to_string(), Value::String(dir));
}
shell_args.insert("raw".to_string(), Value::Bool(raw));
crate::tools::registered::ctx_shell::CtxShellTool.handle(&shell_args, ctx)
})
}
}