lean_ctx/tools/registered/
ctx_refactor.rs1use rmcp::ErrorData;
2use rmcp::model::Tool;
3use serde_json::{Map, Value, json};
4
5use crate::server::tool_trait::{McpTool, ToolContext, ToolOutput, get_str, require_resolved_path};
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 "Rename, move, safe_delete, inline, read-only analyses via LSP/IDE.\n\
19 WORKFLOW: use action=references first to find usages before refactoring.\n\
20 ANTIPATTERN: not for symbol discovery — use ctx_symbol/ctx_compose.\n\
21 Single-phase edits (replace_symbol_body, reformat) work headless via name_path.\n\
22 Two-phase ops (_preview+_apply) need JetBrains IDE (else BACKEND_REQUIRED).\n\
23 Conflicts blocked unless force=true. See `action` parameter for full list.",
24 json!({
25 "type": "object",
26 "properties": {
27 "action": {
28 "type": "string",
29 "description": "rename|references|definition|implementations|declaration|type_hierarchy|symbols_overview|inspections|replace_symbol_body|insert_before_symbol|insert_after_symbol|rename_preview|rename_apply|move_preview|move_apply|safe_delete_preview|safe_delete_apply|inline_preview|inline_apply|reformat"
30 },
31 "path": { "type": "string", "description": "Path" },
32 "line": { "type": "integer", "description": "1-indexed line" },
33 "column": { "type": "integer", "description": "0-indexed column" },
34 "new_name": { "type": "string", "description": "New symbol name" },
35 "scope": {
36 "type": "string",
37 "enum": ["project", "all"],
38 "description": "project|all"
39 },
40 "direction": {
41 "type": "string",
42 "enum": ["supertypes", "subtypes"],
43 "description": "supertypes|subtypes"
44 },
45 "mode": {
46 "type": "string",
47 "enum": ["run", "list"],
48 "description": "run|list"
49 },
50 "name_path": { "type": "string", "description": "Symbol path for body edits (qualified or bare)" },
51 "file": { "type": "string", "description": "Scope name_path resolution to this file (avoids AMBIGUOUS_SYMBOL for common names)" },
52 "new_body": { "type": "string", "description": "Full replacement declaration text" },
53 "text": { "type": "string", "description": "Sibling text to insert (auto-indented)" },
54 "end_line": { "type": "integer", "description": "1-based last line (path+line fallback)" },
55 "expected_hash": { "type": "string", "description": "BLAKE3 hex of current range (TOCTOU guard)" },
56 "plan_hash": { "type": "string", "description": "BLAKE3 plan hash from rename_preview" },
57 "force": { "type": "boolean", "description": "Override refactoring conflicts" },
58 "search_comments": { "type": "boolean", "description": "Rename in comments/strings" },
59 "search_text_occurrences": { "type": "boolean", "description": "Rename in non-code text" },
60 "target_path": { "type": "string", "description": "Destination directory/file (project-relative)" },
61 "target_parent": { "type": "string", "description": "Destination parent symbol for member move" },
62 "propagate": { "type": "boolean", "description": "Delete unreferenced dependencies" },
63 "keep_definition": { "type": "boolean", "description": "Keep declaration after inline" },
64 "optimize_imports": { "type": "boolean", "description": "Remove unused imports" }
65 },
66 "required": ["action"]
67 }),
68 )
69 }
70
71 fn handle(
72 &self,
73 args: &Map<String, Value>,
74 ctx: &ToolContext,
75 ) -> Result<ToolOutput, ErrorData> {
76 let has_path = args.get("path").and_then(Value::as_str).is_some();
79 let abs_path = if has_path {
80 require_resolved_path(ctx, args, "path")?
81 } else {
82 String::new()
83 };
84
85 let args_value = Value::Object(args.clone());
86 let result = crate::tools::ctx_refactor::handle(&args_value, &ctx.project_root, &abs_path);
87
88 let action = get_str(args, "action").unwrap_or_default();
89 Ok(ToolOutput {
90 text: result,
91 original_tokens: 0,
92 saved_tokens: 0,
93 mode: Some(action.clone()),
94 path: get_str(args, "path"),
95 changed: matches!(
96 action.as_str(),
97 "replace_symbol_body"
98 | "insert_before_symbol"
99 | "insert_after_symbol"
100 | "rename_apply"
101 | "move_apply"
102 | "safe_delete_apply"
103 | "inline_apply"
104 | "reformat"
105 ),
106 shell_outcome: None,
107 content_blocks: None,
108 })
109 }
110}
111
112#[cfg(test)]
113mod schema_tests {
114 use super::*;
115 use crate::server::tool_trait::McpTool;
116
117 #[test]
118 fn schema_advertises_declaration_and_scope() {
119 let tool = CtxRefactorTool;
120 let def = tool.tool_def();
121 let schema = serde_json::to_string(&def).unwrap();
122 for needle in [
123 "declaration",
124 "\"scope\"",
125 "type_hierarchy",
126 "symbols_overview",
127 "\"direction\"",
128 "supertypes",
129 "subtypes",
130 "inspections",
131 "\"mode\"",
132 "replace_symbol_body",
133 "insert_before_symbol",
134 "insert_after_symbol",
135 "name_path",
136 "new_body",
137 "expected_hash",
138 "rename_preview",
139 "rename_apply",
140 "plan_hash",
141 "force",
142 "search_comments",
143 "search_text_occurrences",
144 "move_preview",
145 "move_apply",
146 "safe_delete_preview",
147 "safe_delete_apply",
148 "target_path",
149 "target_parent",
150 "propagate",
151 "inline_preview",
152 "inline_apply",
153 "reformat",
154 "keep_definition",
155 "optimize_imports",
156 ] {
157 assert!(schema.contains(needle), "schema missing {needle}: {schema}");
158 }
159 }
160}