lean_ctx/tools/registered/
ctx_tree.rs1use 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 "List a directory. Prefer over native ls/find (counts, compact tree).",
19 json!({
20 "type": "object",
21 "properties": {
22 "path": { "type": "string", "description": "Directory (default: .)" },
23 "paths": {
24 "type": "array",
25 "items": { "type": "string" },
26 "description": "Multiple roots (alternative to path)"
27 },
28 "depth": { "type": "integer", "description": "Max depth (default 3)" },
29 "show_hidden": { "type": "boolean" },
30 "respect_gitignore": { "type": "boolean", "description": "default true" }
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 shell_outcome: None,
92 })
93 }
94}
95
96fn handle_single(
97 path: &str,
98 depth: usize,
99 show_hidden: bool,
100 respect_gitignore: bool,
101) -> Result<ToolOutput, ErrorData> {
102 let path_clone = path.to_string();
103 let Ok((result, original)) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
104 crate::tools::ctx_tree::handle(&path_clone, depth, show_hidden, respect_gitignore)
105 })) else {
106 return Err(ErrorData::internal_error(
107 format!(
108 "ctx_tree panicked while processing '{path}'. This is a bug — please report it."
109 ),
110 None,
111 ));
112 };
113
114 if result.starts_with("ERROR:") {
115 return Err(ErrorData::invalid_params(result, None));
116 }
117
118 let sent = crate::core::tokens::count_tokens(&result);
119 let saved = original.saturating_sub(sent);
120 let final_out = crate::core::protocol::append_savings(&result, original, sent);
121
122 Ok(ToolOutput {
123 text: final_out,
124 original_tokens: original,
125 saved_tokens: saved,
126 mode: None,
127 path: Some(path.to_string()),
128 changed: false,
129 shell_outcome: None,
130 })
131}