lean_ctx/tools/registered/
ctx_symbol.rs1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str};
6use crate::tool_defs::tool_def;
7
8pub struct CtxSymbolTool;
9
10impl McpTool for CtxSymbolTool {
11 fn name(&self) -> &'static str {
12 "ctx_symbol"
13 }
14
15 fn tool_def(&self) -> Tool {
16 tool_def(
17 "ctx_symbol",
18 "Get ONE symbol's body by name — exact, AST-precise (tree-sitter index).\n\
19 WORKFLOW: after ctx_compose gave overview, for one symbol's body.\n\
20 name='fnName' returns code block; file='path.rs' narrows;\n\
21 kind='fn'|'struct'|'class'|'trait'|'enum' disambiguates.\n\
22 ANTIPATTERN: NOT for finding all usages (grep) or exploring areas (ctx_compose).",
23 json!({
24 "type": "object",
25 "properties": {
26 "name": { "type": "string", "description": "fn|struct|class|method name" },
27 "file": { "type": "string", "description": "Narrow search to file" },
28 "kind": { "type": "string", "description": "fn|struct|class|trait|enum" }
29 },
30 "required": ["name"]
31 }),
32 )
33 }
34
35 fn handle(
36 &self,
37 args: &Map<String, Value>,
38 ctx: &ToolContext,
39 ) -> Result<ToolOutput, ErrorData> {
40 let sym_name = get_str(args, "name")
41 .ok_or_else(|| ErrorData::invalid_params("name is required", None))?;
42 let file = get_str(args, "file");
43 let kind = get_str(args, "kind");
44
45 let (result, original) = crate::tools::ctx_symbol::handle(
46 &sym_name,
47 file.as_deref(),
48 kind.as_deref(),
49 &ctx.project_root,
50 );
51 let sent = crate::core::tokens::count_tokens(&result);
52 let saved = original.saturating_sub(sent);
53
54 Ok(ToolOutput {
55 text: result,
56 original_tokens: original,
57 saved_tokens: saved,
58 mode: kind,
59 path: file,
60 changed: false,
61 shell_outcome: None,
62 })
63 }
64}