Skip to main content

lean_ctx/tools/registered/
ctx_smart_read.rs

1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, require_resolved_path};
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            "DEPRECATED → use ctx_read (it auto-selects the mode; omit `mode`). Folded\n\
19             into ctx_read (#509); hidden from tools/list, still callable for one release.",
20            json!({
21                "type": "object",
22                "properties": {
23                    "path": { "type": "string", "description": "File path" }
24                },
25                "required": ["path"]
26            }),
27        )
28    }
29
30    fn handle(
31        &self,
32        args: &Map<String, Value>,
33        ctx: &ToolContext,
34    ) -> Result<ToolOutput, ErrorData> {
35        let path = require_resolved_path(ctx, args, "path")?;
36
37        if crate::core::binary_detect::is_binary_file(&path) {
38            let msg = crate::core::binary_detect::binary_file_message(&path);
39            return Err(ErrorData::invalid_params(msg, None));
40        }
41        {
42            let cap = crate::core::limits::max_read_bytes() as u64;
43            if let Ok(meta) = std::fs::metadata(&path)
44                && meta.len() > cap
45            {
46                let msg = format!(
47                    "File too large ({} bytes, limit {} bytes via LCTX_MAX_READ_BYTES). \
48                         Use mode=\"lines:1-100\" for partial reads or increase the limit.",
49                    meta.len(),
50                    cap
51                );
52                return Err(ErrorData::invalid_params(msg, None));
53            }
54        }
55
56        {
57            let cache_lock = ctx
58                .cache
59                .as_ref()
60                .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
61            let Some(mut cache) =
62                crate::server::bounded_lock::write(cache_lock, "ctx_smart_read cache write")
63            else {
64                crate::core::io_health::record_freeze();
65                return Err(ErrorData::internal_error(
66                    "cache busy (ctx_smart_read) — retry in a moment",
67                    None,
68                ));
69            };
70            let output = crate::tools::ctx_smart_read::handle(&mut cache, &path, ctx.crp_mode);
71            let original = cache.get(&path).map_or(0, |e| e.original_tokens);
72            let tokens = crate::core::tokens::count_tokens(&output);
73            drop(cache);
74
75            let saved = original.saturating_sub(tokens);
76            Ok(ToolOutput {
77                text: output,
78                original_tokens: original,
79                saved_tokens: saved,
80                mode: Some("auto".to_string()),
81                path: Some(path),
82                changed: false,
83                shell_outcome: None,
84                content_blocks: None,
85            })
86        }
87    }
88}