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 "new_body": { "type": "string", "description": "Full replacement declaration text" },
52 "text": { "type": "string", "description": "Sibling text to insert (auto-indented)" },
53 "end_line": { "type": "integer", "description": "1-based last line (path+line fallback)" },
54 "expected_hash": { "type": "string", "description": "BLAKE3 hex of current range (TOCTOU guard)" },
55 "plan_hash": { "type": "string", "description": "BLAKE3 plan hash from rename_preview" },
56 "force": { "type": "boolean", "description": "Override refactoring conflicts" },
57 "search_comments": { "type": "boolean", "description": "Rename in comments/strings" },
58 "search_text_occurrences": { "type": "boolean", "description": "Rename in non-code text" },
59 "target_path": { "type": "string", "description": "Destination directory/file (project-relative)" },
60 "target_parent": { "type": "string", "description": "Destination parent symbol for member move" },
61 "propagate": { "type": "boolean", "description": "Delete unreferenced dependencies" },
62 "keep_definition": { "type": "boolean", "description": "Keep declaration after inline" },
63 "optimize_imports": { "type": "boolean", "description": "Remove unused imports" }
64 },
65 "required": ["action"]
66 }),
67 )
68 }
69
70 fn handle(
71 &self,
72 args: &Map<String, Value>,
73 ctx: &ToolContext,
74 ) -> Result<ToolOutput, ErrorData> {
75 let has_path = args.get("path").and_then(Value::as_str).is_some();
78 let abs_path = if has_path {
79 require_resolved_path(ctx, args, "path")?
80 } else {
81 String::new()
82 };
83
84 let args_value = Value::Object(args.clone());
85 let result = crate::tools::ctx_refactor::handle(&args_value, &ctx.project_root, &abs_path);
86
87 let action = get_str(args, "action").unwrap_or_default();
88 Ok(ToolOutput {
89 text: result,
90 original_tokens: 0,
91 saved_tokens: 0,
92 mode: Some(action.clone()),
93 path: get_str(args, "path"),
94 changed: matches!(
95 action.as_str(),
96 "replace_symbol_body"
97 | "insert_before_symbol"
98 | "insert_after_symbol"
99 | "rename_apply"
100 | "move_apply"
101 | "safe_delete_apply"
102 | "inline_apply"
103 | "reformat"
104 ),
105 shell_outcome: None,
106 })
107 }
108}
109
110#[cfg(test)]
111mod schema_tests {
112 use super::*;
113 use crate::server::tool_trait::McpTool;
114
115 #[test]
116 fn schema_advertises_declaration_and_scope() {
117 let tool = CtxRefactorTool;
118 let def = tool.tool_def();
119 let schema = serde_json::to_string(&def).unwrap();
120 for needle in [
121 "declaration",
122 "\"scope\"",
123 "type_hierarchy",
124 "symbols_overview",
125 "\"direction\"",
126 "supertypes",
127 "subtypes",
128 "inspections",
129 "\"mode\"",
130 "replace_symbol_body",
131 "insert_before_symbol",
132 "insert_after_symbol",
133 "name_path",
134 "new_body",
135 "expected_hash",
136 "rename_preview",
137 "rename_apply",
138 "plan_hash",
139 "force",
140 "search_comments",
141 "search_text_occurrences",
142 "move_preview",
143 "move_apply",
144 "safe_delete_preview",
145 "safe_delete_apply",
146 "target_path",
147 "target_parent",
148 "propagate",
149 "inline_preview",
150 "inline_apply",
151 "reformat",
152 "keep_definition",
153 "optimize_imports",
154 ] {
155 assert!(schema.contains(needle), "schema missing {needle}: {schema}");
156 }
157 }
158}