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 = if let Some(p) = ctx.resolved_path("project_root") {
38            p.to_string()
39        } else if let Some(err) = ctx.path_error("project_root") {
40            return Err(ErrorData::invalid_params(
41                format!("project_root: {err}"),
42                None,
43            ));
44        } else {
45            ".".to_string()
46        };
47        let format = get_str(args, "format");
48
49        let cache = ctx.cache.as_ref().unwrap();
50        let Some(mut cache_guard) = crate::server::bounded_lock::write(cache, "ctx_intent:cache")
51        else {
52            return Ok(ToolOutput::simple(
53                "[intent unavailable — cache busy, retry]".to_string(),
54            ));
55        };
56        let output = crate::tools::ctx_intent::handle(
57            &mut cache_guard,
58            &query,
59            &root,
60            ctx.crp_mode,
61            format.as_deref(),
62        );
63        drop(cache_guard);
64
65        if let Some(ref session) = ctx.session {
66            if let Some(mut session_guard) =
67                crate::server::bounded_lock::write(session, "ctx_intent:session")
68            {
69                session_guard.set_task(&query, Some("intent"));
70            }
71        }
72
73        Ok(ToolOutput {
74            text: output,
75            original_tokens: 0,
76            saved_tokens: 0,
77            mode: Some("semantic".to_string()),
78            path: None,
79            changed: false,
80        })
81    }
82}