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 "Directory listing with file counts.",
19 json!({
20 "type": "object",
21 "properties": {
22 "path": { "type": "string", "description": "Directory path (default: .)" },
23 "depth": { "type": "integer", "description": "Max depth (default: 3)" },
24 "show_hidden": { "type": "boolean", "description": "Show hidden files" }
25 }
26 }),
27 )
28 }
29
30 fn handle(
31 &self,
32 args: &Map<String, Value>,
33 ctx: &ToolContext,
34 ) -> Result<ToolOutput, ErrorData> {
35 let path = if let Some(p) = ctx.resolved_path("path") {
36 p.to_string()
37 } else if let Some(err) = ctx.path_error("path") {
38 return Err(ErrorData::invalid_params(format!("path: {err}"), None));
39 } else {
40 ".".to_string()
41 };
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
45 let path_clone = path.clone();
46 let Ok((result, original)) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
47 crate::tools::ctx_tree::handle(&path_clone, depth, show_hidden)
48 })) else {
49 return Err(ErrorData::internal_error(
50 format!(
51 "ctx_tree panicked while processing '{path}'. This is a bug — please report it."
52 ),
53 None,
54 ));
55 };
56
57 if result.starts_with("ERROR:") {
58 return Err(ErrorData::invalid_params(result, None));
59 }
60
61 let sent = crate::core::tokens::count_tokens(&result);
62 let saved = original.saturating_sub(sent);
63
64 let final_out = crate::core::protocol::append_savings(&result, original, sent);
65
66 Ok(ToolOutput {
67 text: final_out,
68 original_tokens: original,
69 saved_tokens: saved,
70 mode: None,
71 path: Some(path),
72 changed: false,
73 })
74 }
75}