Skip to main content

lean_ctx/tools/registered/
ctx_outline.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 CtxOutlineTool;
9
10impl McpTool for CtxOutlineTool {
11    fn name(&self) -> &'static str {
12        "ctx_outline"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_outline",
18            "List all symbols in a file (functions, structs, classes, methods) with signatures. \
19Much fewer tokens than reading the full file.",
20            json!({
21                "type": "object",
22                "properties": {
23                    "path": { "type": "string", "description": "File path" },
24                    "kind": { "type": "string", "description": "Optional filter: fn|struct|class|all" }
25                },
26                "required": ["path"]
27            }),
28        )
29    }
30
31    fn handle(
32        &self,
33        args: &Map<String, Value>,
34        ctx: &ToolContext,
35    ) -> Result<ToolOutput, ErrorData> {
36        let path = ctx
37            .resolved_path("path")
38            .ok_or_else(|| ErrorData::invalid_params("path is required", None))?
39            .to_string();
40        let kind = get_str(args, "kind");
41
42        let (result, original) = crate::tools::ctx_outline::handle(&path, kind.as_deref());
43        let sent = crate::core::tokens::count_tokens(&result);
44        let saved = original.saturating_sub(sent);
45
46        Ok(ToolOutput {
47            text: result,
48            original_tokens: original,
49            saved_tokens: saved,
50            mode: kind,
51            path: Some(path),
52        })
53    }
54}