apply_refactoring
Apply code refactoring patterns to modify files in your codebase. Use this tool to implement structural improvements after previewing changes.
Instructions
Apply a refactoring to the codebase.
This actually modifies files. Use preview_refactoring first to see what changes will be made. Changes can be reverted with git.
Args: refactoring: Name of the refactoring (e.g., 'extract-method') target: Target in language-native format (e.g., 'src/order.py::Order::calculate#L10-L15') params: Refactoring-specific parameters
Returns: TOON-formatted string with results of the applied refactoring.
Example: apply_refactoring( refactoring="rename-method", target="src/order.py::Order::calc_total", params={"new_name": "calculate_total"} )
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| refactoring | Yes | ||
| target | Yes | ||
| params | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| result | Yes |
Implementation Reference
- The actual implementation of the apply_refactoring tool handler. It validates the refactoring and delegates the work to a language-specific adapter.
async def apply_refactoring(params: ApplyRefactoringInput) -> str: """Apply a refactoring to the codebase. This actually modifies files. Use preview_refactoring first to see what changes will be made. Args: params: Input parameters including refactoring name, target, and options Returns: TOON-formatted string with results of the applied refactoring """ # Detect language from target path language = detect_language(params.target) if language is None: return encode_toon({ "status": "error", "error": f"Could not detect language from target: {params.target}", "hint": "Ensure the target path has a recognized file extension (.py, .rb, .java, .go)", }) # Get adapter for the language adapter = get_adapter(language) if adapter is None: return encode_toon({ "status": "error", "error": f"No backend available for language: {language}", "hint": f"Install the {language} refactoring backend", }) # Verify the refactoring is supported supported_names = {spec.name for spec in adapter.list_refactorings()} if params.refactoring not in supported_names: return encode_toon({ "status": "error", "error": f"Unknown refactoring: {params.refactoring}", "hint": "Use list_refactorings to see available refactorings", "available": sorted(supported_names), }) # Execute the refactoring result = await adapter.apply_refactoring( refactoring=params.refactoring, target=params.target, params=params.params, ) # Format response response: dict[str, Any] = { "result": { "refactoring": result.refactoring, "target": params.target, "status": result.status, "files_modified": result.files_modified, } } if result.error: response["result"]["error"] = result.error if result.files: response["files"] = [ { "path": f.path, "action": f.action, } for f in result.files ] return encode_toon(response) - Input schema for the apply_refactoring tool.
class ApplyRefactoringInput(BaseModel): """Input for applying a refactoring.""" model_config = ConfigDict(str_strip_whitespace=True) refactoring: str = Field( ..., description="Name of the refactoring (e.g., 'extract-method')", min_length=1, ) target: str = Field( ..., description="Target specification in language-native format (e.g., 'src/order.py::Order::calculate#L10-L15')", min_length=1, ) params: dict[str, Any] = Field( default_factory=dict, description="Refactoring-specific parameters (e.g., {'name': 'calculate_tax'})", ) - src/mcp_refactoring/server.py:118-158 (registration)Registration of the 'apply_refactoring' tool in the MCP server. It wraps the implementation logic.
@mcp.tool( name="apply_refactoring", annotations={ "title": "Apply Refactoring", "readOnlyHint": False, "destructiveHint": False, # Reversible via git "idempotentHint": False, "openWorldHint": False, }, ) async def apply_refactoring( refactoring: str, target: str, params: Optional[dict] = None, ) -> str: """Apply a refactoring to the codebase. This actually modifies files. Use preview_refactoring first to see what changes will be made. Changes can be reverted with git. Args: refactoring: Name of the refactoring (e.g., 'extract-method') target: Target in language-native format (e.g., 'src/order.py::Order::calculate#L10-L15') params: Refactoring-specific parameters Returns: TOON-formatted string with results of the applied refactoring. Example: apply_refactoring( refactoring="rename-method", target="src/order.py::Order::calc_total", params={"new_name": "calculate_total"} ) """ input_params = ApplyRefactoringInput( refactoring=refactoring, target=target, params=params or {}, ) return await _apply_refactoring(input_params)