lean_ctx/tools/registered/
ctx_intent.rs1use 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 mut cache_guard = tokio::task::block_in_place(|| cache.blocking_write());
51 let output = crate::tools::ctx_intent::handle(
52 &mut cache_guard,
53 &query,
54 &root,
55 ctx.crp_mode,
56 format.as_deref(),
57 );
58 drop(cache_guard);
59
60 if let Some(ref session) = ctx.session {
61 let mut session_guard = tokio::task::block_in_place(|| session.blocking_write());
62 session_guard.set_task(&query, Some("intent"));
63 }
64
65 Ok(ToolOutput {
66 text: output,
67 original_tokens: 0,
68 saved_tokens: 0,
69 mode: Some("semantic".to_string()),
70 path: None,
71 changed: false,
72 })
73 }
74}