Skip to main content

lean_ctx/tools/registered/
ctx_compose.rs

1use rmcp::model::Tool;
2use rmcp::ErrorData;
3use serde_json::{json, Map, Value};
4
5use crate::server::tool_trait::{get_str, McpTool, ToolContext, ToolOutput};
6use crate::tool_defs::tool_def;
7
8pub struct CtxComposeTool;
9
10impl McpTool for CtxComposeTool {
11    fn name(&self) -> &'static str {
12        "ctx_compose"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_compose",
18            "Task composer: one call returns keywords + semantically ranked files + exact match locations + the top symbol's body inline. Replaces the search→read→outline→read chain.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "task": { "type": "string", "description": "Natural-language task or question" },
23                    "path": { "type": "string", "description": "Project root (default: .)" }
24                },
25                "required": ["task"]
26            }),
27        )
28    }
29
30    fn handle(
31        &self,
32        args: &Map<String, Value>,
33        ctx: &ToolContext,
34    ) -> Result<ToolOutput, ErrorData> {
35        let task = get_str(args, "task")
36            .ok_or_else(|| ErrorData::invalid_params("task is required", None))?;
37        let path = if let Some(p) = ctx.resolved_path("path") {
38            p.to_string()
39        } else if let Some(err) = ctx.path_error("path") {
40            return Err(ErrorData::invalid_params(format!("path: {err}"), None));
41        } else {
42            ctx.project_root.clone()
43        };
44
45        // Share the resident BM25 cache with the composed semantic search.
46        if let Some(ref cache) = ctx.bm25_cache {
47            crate::tools::ctx_semantic_search::set_thread_cache(cache.clone());
48        }
49
50        let (text, sent) = tokio::task::block_in_place(|| {
51            crate::tools::ctx_compose::handle(&task, &path, ctx.crp_mode)
52        });
53
54        if text.starts_with("ERROR") {
55            return Err(ErrorData::invalid_params(text, None));
56        }
57
58        Ok(ToolOutput {
59            text,
60            original_tokens: sent,
61            saved_tokens: 0,
62            mode: Some("compose".to_string()),
63            path: Some(path),
64            changed: false,
65        })
66    }
67}