lean-ctx 3.6.2

Context Runtime for AI Agents with CCP. 51 MCP tools, 10 read modes, 60+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing, LITM-aware positioning, AAAK compact format, adaptive compression with Thompson Sampling bandits. Supports 24+ AI tools. Reduces LLM token consumption by up to 99%.
Documentation
use rmcp::model::Tool;
use rmcp::ErrorData;
use serde_json::{json, Map, Value};

use crate::server::tool_trait::{get_str, McpTool, ToolContext, ToolOutput};
use crate::tool_defs::tool_def;

pub struct CtxRetrieveTool;

impl McpTool for CtxRetrieveTool {
    fn name(&self) -> &'static str {
        "ctx_retrieve"
    }

    fn tool_def(&self) -> Tool {
        tool_def(
            "ctx_retrieve",
            "Retrieve original uncompressed content from the session cache (CCR). \
             Use when a compressed ctx_read output is insufficient.",
            json!({
                "type": "object",
                "properties": {
                    "path": {
                        "type": "string",
                        "description": "File path whose original content to retrieve"
                    },
                    "query": {
                        "type": "string",
                        "description": "Optional: search within cached content"
                    }
                },
                "required": ["path"]
            }),
        )
    }

    fn handle(
        &self,
        args: &Map<String, Value>,
        ctx: &ToolContext,
    ) -> Result<ToolOutput, ErrorData> {
        let path_raw = get_str(args, "path")
            .ok_or_else(|| ErrorData::invalid_params("path is required", None))?;
        let resolved = ctx.resolved_path("path").unwrap_or(&path_raw).to_string();
        let query = get_str(args, "query");

        let cache = ctx.cache.as_ref().unwrap();
        let guard = tokio::task::block_in_place(|| cache.blocking_read());
        let result = match guard.get_full_content(&resolved) {
            Some(full) => {
                if let Some(ref q) = query {
                    ccr_search_within(&full, q)
                } else {
                    full
                }
            }
            None => {
                format!("No cached content for \"{path_raw}\". Use ctx_read(\"{path_raw}\") first.")
            }
        };

        Ok(ToolOutput::simple(result))
    }
}

fn ccr_search_within(content: &str, query: &str) -> String {
    let query_lower = query.to_lowercase();
    let terms: Vec<&str> = query_lower.split_whitespace().collect();
    if terms.is_empty() {
        return content.to_string();
    }

    let mut matches: Vec<(usize, &str)> = Vec::new();
    for (i, line) in content.lines().enumerate() {
        let lower = line.to_lowercase();
        if terms.iter().any(|t| lower.contains(t)) {
            matches.push((i + 1, line));
        }
    }

    if matches.is_empty() {
        return format!("No lines matching \"{query}\" in cached content.");
    }

    let total = content.lines().count();
    let mut out = format!("# {}/{total} lines match \"{query}\"\n", matches.len());
    for (lineno, line) in matches.iter().take(200) {
        out.push_str(&format!("{lineno:>6}| {line}\n"));
    }
    if matches.len() > 200 {
        out.push_str(&format!("... and {} more matches\n", matches.len() - 200));
    }
    out
}