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
50            .cache
51            .as_ref()
52            .ok_or_else(|| ErrorData::internal_error("cache not available", None))?;
53        let Some(mut cache_guard) = crate::server::bounded_lock::write(cache, "ctx_intent:cache")
54        else {
55            return Ok(ToolOutput::simple(
56                "[intent unavailable — cache busy, retry]".to_string(),
57            ));
58        };
59        let output = crate::tools::ctx_intent::handle(
60            &mut cache_guard,
61            &query,
62            &root,
63            ctx.crp_mode,
64            format.as_deref(),
65        );
66        drop(cache_guard);
67
68        if let Some(ref session) = ctx.session {
69            if let Some(mut session_guard) =
70                crate::server::bounded_lock::write(session, "ctx_intent:session")
71            {
72                session_guard.set_task(&query, Some("intent"));
73            }
74        }
75
76        Ok(ToolOutput {
77            text: output,
78            original_tokens: 0,
79            saved_tokens: 0,
80            mode: Some("semantic".to_string()),
81            path: None,
82            changed: false,
83        })
84    }
85}