lean_ctx/tools/registered/
ctx_smart_read.rs1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxSmartReadTool;
9
10impl McpTool for CtxSmartReadTool {
11 fn name(&self) -> &'static str {
12 "ctx_smart_read"
13 }
14
15 fn tool_def(&self) -> Tool {
16 tool_def(
17 "ctx_smart_read",
18 "Auto-select optimal read mode for a file.",
19 json!({
20 "type": "object",
21 "properties": {
22 "path": { "type": "string", "description": "Absolute file path to read" }
23 },
24 "required": ["path"]
25 }),
26 )
27 }
28
29 fn handle(
30 &self,
31 _args: &Map<String, Value>,
32 ctx: &ToolContext,
33 ) -> Result<ToolOutput, ErrorData> {
34 let path = ctx
35 .resolved_path("path")
36 .ok_or_else(|| ErrorData::invalid_params("path is required", None))?
37 .to_string();
38
39 if crate::core::binary_detect::is_binary_file(&path) {
40 let msg = crate::core::binary_detect::binary_file_message(&path);
41 return Err(ErrorData::invalid_params(msg, None));
42 }
43 {
44 let cap = crate::core::limits::max_read_bytes() as u64;
45 if let Ok(meta) = std::fs::metadata(&path) {
46 if meta.len() > cap {
47 let msg = format!(
48 "File too large ({} bytes, limit {} bytes via LCTX_MAX_READ_BYTES). \
49 Use mode=\"lines:1-100\" for partial reads or increase the limit.",
50 meta.len(),
51 cap
52 );
53 return Err(ErrorData::invalid_params(msg, None));
54 }
55 }
56 }
57
58 tokio::task::block_in_place(|| {
59 let cache_lock = ctx
60 .cache
61 .as_ref()
62 .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
63 let mut cache = cache_lock.blocking_write();
64 let output = crate::tools::ctx_smart_read::handle(&mut cache, &path, ctx.crp_mode);
65 let original = cache.get(&path).map_or(0, |e| e.original_tokens);
66 let tokens = crate::core::tokens::count_tokens(&output);
67 drop(cache);
68
69 let saved = original.saturating_sub(tokens);
70 Ok(ToolOutput {
71 text: output,
72 original_tokens: original,
73 saved_tokens: saved,
74 mode: Some("auto".to_string()),
75 path: Some(path),
76 changed: false,
77 })
78 })
79 }
80}