Skip to main content

lean_ctx_server/tools/registered/
shell_alias.rs

1// SPDX-License-Identifier: Apache-2.0
2use rmcp::ErrorData;
3use rmcp::model::Tool;
4use serde_json::{Map, Value, json};
5
6use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str};
7use lean_ctx::tool_defs::tool_def;
8
9/// A `shell` tool alias that transparently delegates to `ctx_shell`'s compression
10/// logic. Registered for all MCP clients (see `server::registry`); it exists for
11/// clients (like Codex Desktop) whose agent model prefers a tool named `shell` /
12/// `bash` over `ctx_shell` and would otherwise fall back to a native, uncompressed
13/// shell tool.
14///
15/// This solves the "Codex Desktop doesn't compress" issue (#337): the Desktop app
16/// loads the MCP server but the agent ignores `ctx_shell` and uses its native
17/// `Bash` tool instead. By providing a `shell` tool with a familiar interface,
18/// the model naturally routes commands through our compression pipeline.
19pub struct ShellAliasTool;
20
21impl McpTool for ShellAliasTool {
22    fn name(&self) -> &'static str {
23        "shell"
24    }
25
26    fn tool_def(&self) -> Tool {
27        tool_def(
28            "shell",
29            "Shell command with auto-compression (~95 patterns). Alias for ctx_shell.\n\
30             Output is compressed for token savings. Big logs: narrow the command (grep, sed -n, head/tail).\n\
31             Use when your MCP client prefers shell/bash over ctx_shell — transparently\n\
32             delegates to ctx_shell internals.",
33            json!({
34                "type": "object",
35                "properties": {
36                    "command": {
37                        "type": "string",
38                        "description": "Shell command"
39                    },
40                    "cwd": {
41                        "type": "string",
42                        "description": "Working dir"
43                    },
44                    "raw": {
45                        "type": "boolean",
46                        "description": "Exact bytes, uncompressed; only for diffs to apply, hashes, encoded output. Default false."
47                    }
48                },
49                "required": ["command"]
50            }),
51        )
52    }
53
54    fn handle(
55        &self,
56        args: &Map<String, Value>,
57        ctx: &ToolContext,
58    ) -> Result<ToolOutput, ErrorData> {
59        let command = get_str(args, "command")
60            .ok_or_else(|| ErrorData::invalid_params("command is required", None))?;
61
62        // #1811: the write-doctrine check is deliberately NOT repeated here.
63        // It judges where a relative redirect target lands, and only the
64        // delegate below knows that — it resolves the requested `cwd` through
65        // the project-root jail before deciding. Judging the raw argument here
66        // would answer for a directory the command may never run in, and a
67        // second copy of the rule is a second answer waiting to drift from the
68        // first. The delegate refuses, with its own message, before executing.
69        if let Err(msg) = lean_ctx::core::shell_allowlist::check_shell_allowlist(&command) {
70            return Ok(ToolOutput::simple(msg.to_string()));
71        }
72
73        tokio::task::block_in_place(|| {
74            let cwd = get_str(args, "cwd");
75            // Compressed by default (the point of this alias), but honor an explicit
76            // raw=true so clients restricted to "shell"/"bash" still have the verbatim
77            // escape the description advertises — no MCP-specific tool required.
78            let raw = args.get("raw").and_then(Value::as_bool).unwrap_or(false);
79            let mut shell_args = Map::new();
80            shell_args.insert("command".to_string(), Value::String(command));
81            if let Some(dir) = cwd {
82                shell_args.insert("cwd".to_string(), Value::String(dir));
83            }
84            shell_args.insert("raw".to_string(), Value::Bool(raw));
85
86            crate::tools::registered::ctx_shell::CtxShellTool.handle(&shell_args, ctx)
87        })
88    }
89}