Skip to main content

lean_ctx/tools/registered/
ctx_intent.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 CtxIntentTool;
9
10impl McpTool for CtxIntentTool {
11    fn name(&self) -> &'static str {
12        "ctx_intent"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_intent",
18            "Structured intent input (optional) — submit compact JSON or short text; server also infers intents automatically from tool calls.",
19            json!({
20                "type": "object",
21                "properties": {
22                    "query": { "type": "string", "description": "Compact JSON intent or short text" },
23                    "project_root": { "type": "string", "description": "Project root directory (default: .)" }
24                },
25                "required": ["query"]
26            }),
27        )
28    }
29
30    fn handle(
31        &self,
32        args: &Map<String, Value>,
33        ctx: &ToolContext,
34    ) -> Result<ToolOutput, ErrorData> {
35        let query = get_str(args, "query")
36            .ok_or_else(|| ErrorData::invalid_params("query is required", None))?;
37        let root = ctx.resolved_path("project_root").unwrap_or(".").to_string();
38        let format = get_str(args, "format");
39
40        let cache = ctx.cache.as_ref().unwrap();
41        let mut cache_guard = tokio::task::block_in_place(|| cache.blocking_write());
42        let output = crate::tools::ctx_intent::handle(
43            &mut cache_guard,
44            &query,
45            &root,
46            ctx.crp_mode,
47            format.as_deref(),
48        );
49        drop(cache_guard);
50
51        if let Some(ref session) = ctx.session {
52            let mut session_guard = tokio::task::block_in_place(|| session.blocking_write());
53            session_guard.set_task(&query, Some("intent"));
54        }
55
56        Ok(ToolOutput {
57            text: output,
58            original_tokens: 0,
59            saved_tokens: 0,
60            mode: Some("semantic".to_string()),
61            path: None,
62            changed: false,
63        })
64    }
65}