lean-ctx 3.6.13

Context Runtime for AI Agents with CCP. 62 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 CtxDedupTool;

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

    fn tool_def(&self) -> Tool {
        tool_def(
            "ctx_dedup",
            "Cross-file dedup: analyze or apply shared block references.",
            json!({
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "description": "analyze (default) or apply (register shared blocks for auto-dedup in ctx_read)",
                        "default": "analyze"
                    }
                }
            }),
        )
    }

    fn handle(
        &self,
        args: &Map<String, Value>,
        ctx: &ToolContext,
    ) -> Result<ToolOutput, ErrorData> {
        let action = get_str(args, "action").unwrap_or_default();
        let cache = ctx.cache.as_ref().unwrap();
        let result = if action == "apply" {
            let Some(mut guard) = crate::server::bounded_lock::write(cache, "ctx_dedup:apply")
            else {
                return Ok(ToolOutput::simple(
                    "[dedup unavailable — cache busy, retry]".to_string(),
                ));
            };
            crate::tools::ctx_dedup::handle_action(&mut guard, &action)
        } else {
            let Some(guard) = crate::server::bounded_lock::read(cache, "ctx_dedup:status") else {
                return Ok(ToolOutput::simple(
                    "[dedup status unavailable — cache busy, retry]".to_string(),
                ));
            };
            crate::tools::ctx_dedup::handle(&guard)
        };
        Ok(ToolOutput::simple(result))
    }
}