Skip to main content

lean_ctx/tools/registered/
ctx_retrieve.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{get_str, McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxRetrieveTool;
9
10impl McpTool for CtxRetrieveTool {
11    fn name(&self) -> &'static str {
12        "ctx_retrieve"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_retrieve",
18            "Retrieve original uncompressed content from the session cache (CCR). \
19             Use when a compressed ctx_read output is insufficient.",
20            json!({
21                "type": "object",
22                "properties": {
23                    "path": {
24                        "type": "string",
25                        "description": "File path whose original content to retrieve"
26                    },
27                    "query": {
28                        "type": "string",
29                        "description": "Optional: search within cached content"
30                    }
31                },
32                "required": ["path"]
33            }),
34        )
35    }
36
37    fn handle(
38        &self,
39        args: &Map<String, Value>,
40        ctx: &ToolContext,
41    ) -> Result<ToolOutput, ErrorData> {
42        let path_raw = get_str(args, "path")
43            .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
44        let resolved = if let Some(p) = ctx.resolved_path("path") {
45            p.to_string()
46        } else if let Some(err) = ctx.path_error("path") {
47            return Err(ErrorData::invalid_params(format!("path: {err}"), None));
48        } else {
49            path_raw.clone()
50        };
51        let query = get_str(args, "query");
52
53        let cache = ctx.cache.as_ref().unwrap();
54        let Some(guard) = crate::server::bounded_lock::read(cache, "ctx_retrieve") else {
55            return Ok(ToolOutput::simple(
56                "[retrieve unavailable — cache busy, retry]".to_string(),
57            ));
58        };
59        let result = match guard.get_full_content(&resolved) {
60            Some(full) => {
61                if let Some(ref q) = query {
62                    ccr_search_within(&full, q)
63                } else {
64                    full
65                }
66            }
67            None => {
68                format!("No cached content for \"{path_raw}\". Use ctx_read(\"{path_raw}\") first.")
69            }
70        };
71
72        Ok(ToolOutput::simple(result))
73    }
74}
75
76fn ccr_search_within(content: &str, query: &str) -> String {
77    let query_lower = query.to_lowercase();
78    let terms: Vec<&str> = query_lower.split_whitespace().collect();
79    if terms.is_empty() {
80        return content.to_string();
81    }
82
83    let mut matches: Vec<(usize, &str)> = Vec::new();
84    for (i, line) in content.lines().enumerate() {
85        let lower = line.to_lowercase();
86        if terms.iter().any(|t| lower.contains(t)) {
87            matches.push((i + 1, line));
88        }
89    }
90
91    if matches.is_empty() {
92        return format!("No lines matching \"{query}\" in cached content.");
93    }
94
95    let total = content.lines().count();
96    let mut out = format!("# {}/{total} lines match \"{query}\"\n", matches.len());
97    for (lineno, line) in matches.iter().take(200) {
98        out.push_str(&format!("{lineno:>6}| {line}\n"));
99    }
100    if matches.len() > 200 {
101        out.push_str(&format!("... and {} more matches\n", matches.len() - 200));
102    }
103    out
104}