lean_ctx/tools/registered/
ctx_retrieve.rs1use 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 guard = tokio::task::block_in_place(|| cache.blocking_read());
55 let result = match guard.get_full_content(&resolved) {
56 Some(full) => {
57 if let Some(ref q) = query {
58 ccr_search_within(&full, q)
59 } else {
60 full
61 }
62 }
63 None => {
64 format!("No cached content for \"{path_raw}\". Use ctx_read(\"{path_raw}\") first.")
65 }
66 };
67
68 Ok(ToolOutput::simple(result))
69 }
70}
71
72fn ccr_search_within(content: &str, query: &str) -> String {
73 let query_lower = query.to_lowercase();
74 let terms: Vec<&str> = query_lower.split_whitespace().collect();
75 if terms.is_empty() {
76 return content.to_string();
77 }
78
79 let mut matches: Vec<(usize, &str)> = Vec::new();
80 for (i, line) in content.lines().enumerate() {
81 let lower = line.to_lowercase();
82 if terms.iter().any(|t| lower.contains(t)) {
83 matches.push((i + 1, line));
84 }
85 }
86
87 if matches.is_empty() {
88 return format!("No lines matching \"{query}\" in cached content.");
89 }
90
91 let total = content.lines().count();
92 let mut out = format!("# {}/{total} lines match \"{query}\"\n", matches.len());
93 for (lineno, line) in matches.iter().take(200) {
94 out.push_str(&format!("{lineno:>6}| {line}\n"));
95 }
96 if matches.len() > 200 {
97 out.push_str(&format!("... and {} more matches\n", matches.len() - 200));
98 }
99 out
100}