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            let session = tokio::task::block_in_place(|| session_lock.blocking_read());
38            session.project_root.clone().unwrap_or_default()
39        } else {
40            ctx.project_root.clone()
41        };
42
43        let policies = crate::core::context_policies::PolicySet::load_project(
44            &std::path::PathBuf::from(&root),
45        );
46        let result = crate::tools::ctx_compile::handle(Some(args), &ledger, &policies);
47
48        Ok(ToolOutput::simple(result))
49    }
50}