lean-ctx 3.9.15

Context Runtime for AI Agents with CCP. 71 MCP tools, 10 read modes, 95+ 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::ErrorData;
use rmcp::model::Tool;
use serde_json::{Map, Value, json};

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

/// `ctx_tools` — MCP Tool-Catalog Gateway (#210). Aggregates downstream MCP
/// servers and returns a per-query top-N shortlist instead of injecting every
/// downstream schema, then proxies the real call.
pub struct CtxToolsTool;

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

    fn tool_def(&self) -> Tool {
        tool_def(
            "ctx_tools",
            "Gateway to downstream MCP servers — unlimited external tools at ~constant context cost.\n\
             actions: find (query → top-N relevant tools) | call (proxy a server::tool) |\n\
             list (servers+counts) | refresh.\n\
             WORKFLOW: find to discover, then call the chosen server::tool.\n\
             ANTIPATTERN: not for built-in tools — use those directly.",
            json!({
                "type": "object",
                "properties": {
                    "action": {
                        "type": "string",
                        "enum": ["find", "call", "list", "refresh"],
                        "description": "find|call|list|refresh"
                    },
                    "query": { "type": "string", "description": "What you want to do (for find)" },
                    "tool": { "type": "string", "description": "`server::tool` handle (for call)" },
                    "arguments": { "type": "object", "description": "Arguments for downstream tool (call)" }
                },
                "allOf": [
                    {
                        "if": { "properties": { "action": { "const": "call" } }, "required": ["action"] },
                        "then": { "required": ["action", "tool"] }
                    }
                ]
            }),
        )
    }

    fn handle(
        &self,
        args: &Map<String, Value>,
        ctx: &ToolContext,
    ) -> Result<ToolOutput, ErrorData> {
        // `project_root` is threaded through so the gateway's L3 consolidation
        // (#1095) can write addon output into the project's BM25/graph/knowledge
        // stores. Empty (one-shot CLI ctx) disables project-scoped indexing.
        match crate::tools::ctx_tools::run(args, &ctx.project_root) {
            Ok(text) => Ok(ToolOutput::simple(text)),
            Err(e) => Err(ErrorData::invalid_params(e, None)),
        }
    }
}