lean-ctx 3.6.0

Context Runtime for AI Agents with CCP. 63 MCP tools, 10 read modes, 95+ compression patterns, cross-session memory (CCP), persistent AI knowledge with temporal facts + contradiction detection, multi-agent context sharing + diaries, 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::{McpTool, ToolContext, ToolOutput};
use crate::tool_defs::tool_def;

pub struct CtxControlTool;

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

    fn tool_def(&self) -> Tool {
        tool_def(
            "ctx_control",
            "Universal context manipulation (Context Field Theory). Actions: exclude|include|pin|unpin|set_view|set_priority|mark_outdated|reset|list|history. Overlay-based, reversible, scoped.",
            json!({
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "description": "exclude|include|pin|unpin|set_view|set_priority|mark_outdated|reset|list|history"
                    },
                    "target": { "type": "string", "description": "@F1 or path or item ID" },
                    "value": { "type": "string", "description": "New content, view name, or priority" },
                    "scope": { "type": "string", "description": "call|session|project (default: session)" },
                    "reason": { "type": "string", "description": "Reason for the action" }
                },
                "required": ["action"]
            }),
        )
    }

    fn handle(
        &self,
        args: &Map<String, Value>,
        ctx: &ToolContext,
    ) -> Result<ToolOutput, ErrorData> {
        let root = if let Some(ref session_lock) = ctx.session {
            let session = tokio::task::block_in_place(|| session_lock.blocking_read());
            session.project_root.clone().unwrap_or_default()
        } else {
            ctx.project_root.clone()
        };

        let mut overlays = crate::core::context_overlay::OverlayStore::load_project(
            &std::path::PathBuf::from(&root),
        );

        let result = if let Some(ref ledger_lock) = ctx.ledger {
            let mut ledger = tokio::task::block_in_place(|| ledger_lock.blocking_write());
            let r = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
            ledger.save();
            r
        } else {
            let mut ledger = crate::core::context_ledger::ContextLedger::load();
            let r = crate::tools::ctx_control::handle(Some(args), &mut ledger, &mut overlays);
            ledger.save();
            r
        };
        let _ = overlays.save_project(&std::path::PathBuf::from(&root));

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