Skip to main content

lean_ctx/tools/registered/
ctx_refactor.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 CtxRefactorTool;
9
10impl McpTool for CtxRefactorTool {
11    fn name(&self) -> &'static str {
12        "ctx_refactor"
13    }
14
15    fn tool_def(&self) -> Tool {
16        tool_def(
17            "ctx_refactor",
18            "LSP-powered refactoring. Actions: rename, references, definition, implementations. \
19             Requires a running language server (rust-analyzer, typescript-language-server, pylsp, gopls).",
20            json!({
21                "type": "object",
22                "properties": {
23                    "action": {
24                        "type": "string",
25                        "enum": ["rename", "references", "definition", "implementations"],
26                        "description": "Refactoring action"
27                    },
28                    "path": { "type": "string", "description": "File path" },
29                    "line": { "type": "integer", "description": "1-indexed line number" },
30                    "column": { "type": "integer", "description": "0-indexed character offset" },
31                    "new_name": { "type": "string", "description": "New name (only for rename action)" }
32                },
33                "required": ["action", "path", "line"]
34            }),
35        )
36    }
37
38    fn handle(
39        &self,
40        args: &Map<String, Value>,
41        ctx: &ToolContext,
42    ) -> Result<ToolOutput, ErrorData> {
43        let args_value = Value::Object(args.clone());
44        let result = crate::tools::ctx_refactor::handle(&args_value, &ctx.project_root);
45
46        let action = get_str(args, "action").unwrap_or_default();
47        Ok(ToolOutput {
48            text: result,
49            original_tokens: 0,
50            saved_tokens: 0,
51            mode: Some(action),
52            path: get_str(args, "path"),
53            changed: false,
54        })
55    }
56}