Skip to main content

lean_ctx/tools/registered/
ctx_tree.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{get_bool, get_int, McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxTreeTool;
9
10impl McpTool for CtxTreeTool {
11    fn name(&self) -> &'static str {
12        "ctx_tree"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_tree",
18            "Directory listing with file counts. Supports multi-root via `paths` array.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "path": { "type": "string", "description": "Directory path (default: .)" },
23                    "paths": {
24                        "type": "array",
25                        "items": { "type": "string" },
26                        "description": "Multiple directories to list (alternative to path)"
27                    },
28                    "depth": { "type": "integer", "description": "Max depth (default: 3)" },
29                    "show_hidden": { "type": "boolean", "description": "Show hidden files" },
30                    "respect_gitignore": { "type": "boolean", "description": "Filter out .gitignore'd files (default: true). Set false to show all files." }
31                }
32            }),
33        )
34    }
35
36    fn handle(
37        &self,
38        args: &Map<String, Value>,
39        ctx: &ToolContext,
40    ) -> Result<ToolOutput, ErrorData> {
41        let resolved = crate::server::multi_path::resolve_tool_paths(args, ctx);
42        let depth = (get_int(args, "depth").unwrap_or(3) as usize).min(10);
43        let show_hidden = get_bool(args, "show_hidden").unwrap_or(false);
44        let respect_gitignore = get_bool(args, "respect_gitignore").unwrap_or(true);
45
46        if !resolved.is_multi {
47            return handle_single(&resolved.roots[0], depth, show_hidden, respect_gitignore);
48        }
49
50        let mut combined = String::new();
51        let mut total_original: usize = 0;
52        let mut total_sent: usize = 0;
53
54        for root in &resolved.roots {
55            let root_clone = root.clone();
56            let Ok((result, original)) =
57                std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
58                    crate::tools::ctx_tree::handle(
59                        &root_clone,
60                        depth,
61                        show_hidden,
62                        respect_gitignore,
63                    )
64                }))
65            else {
66                combined.push_str(&format!("── {root} ──\nERROR: internal panic\n\n"));
67                continue;
68            };
69
70            if result.starts_with("ERROR:") {
71                combined.push_str(&format!("── {root} ──\n{result}\n\n"));
72                continue;
73            }
74
75            combined.push_str(&format!("── {root} ──\n{result}\n\n"));
76            total_original += original;
77            total_sent += crate::core::tokens::count_tokens(&result);
78        }
79
80        let final_out =
81            crate::core::protocol::append_savings(&combined, total_original, total_sent);
82        let saved = total_original.saturating_sub(total_sent);
83
84        Ok(ToolOutput {
85            text: final_out,
86            original_tokens: total_original,
87            saved_tokens: saved,
88            mode: None,
89            path: None,
90            changed: false,
91        })
92    }
93}
94
95fn handle_single(
96    path: &str,
97    depth: usize,
98    show_hidden: bool,
99    respect_gitignore: bool,
100) -> Result<ToolOutput, ErrorData> {
101    let path_clone = path.to_string();
102    let Ok((result, original)) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
103        crate::tools::ctx_tree::handle(&path_clone, depth, show_hidden, respect_gitignore)
104    })) else {
105        return Err(ErrorData::internal_error(
106            format!(
107                "ctx_tree panicked while processing '{path}'. This is a bug — please report it."
108            ),
109            None,
110        ));
111    };
112
113    if result.starts_with("ERROR:") {
114        return Err(ErrorData::invalid_params(result, None));
115    }
116
117    let sent = crate::core::tokens::count_tokens(&result);
118    let saved = original.saturating_sub(sent);
119    let final_out = crate::core::protocol::append_savings(&result, original, sent);
120
121    Ok(ToolOutput {
122        text: final_out,
123        original_tokens: original,
124        saved_tokens: saved,
125        mode: None,
126        path: Some(path.to_string()),
127        changed: false,
128    })
129}