Skip to main content

lean_ctx/tools/registered/
ctx_compile.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxCompileTool;
9
10impl McpTool for CtxCompileTool {
11    fn name(&self) -> &'static str {
12        "ctx_compile"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_compile",
18            "Context compilation (CFT). Builds minimal context package via greedy knapsack + Boltzmann view selection. Modes: handles|compressed|full.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "mode": { "type": "string", "description": "handles|compressed|full (default: handles)" },
23                    "budget": { "type": "integer", "description": "Token budget (default: 12000)" }
24                }
25            }),
26        )
27    }
28
29    fn handle(
30        &self,
31        args: &Map<String, Value>,
32        ctx: &ToolContext,
33    ) -> Result<ToolOutput, ErrorData> {
34        let ledger = crate::core::context_ledger::ContextLedger::load();
35
36        let root = if let Some(ref session_lock) = ctx.session {
37            crate::server::bounded_lock::read(session_lock, "ctx_compile:session")
38                .as_ref()
39                .and_then(|s| s.project_root.clone())
40                .unwrap_or_else(|| ctx.project_root.clone())
41        } else {
42            ctx.project_root.clone()
43        };
44
45        let policies = crate::core::context_policies::PolicySet::load_project(
46            &std::path::PathBuf::from(&root),
47        );
48        let result = crate::tools::ctx_compile::handle(Some(args), &ledger, &policies);
49
50        Ok(ToolOutput::simple(result))
51    }
52}