smart_refactor
Rename symbols consistently across multiple files to maintain code clarity and accuracy. Identifies and updates all occurrences of a specified name in a given set of files.
Instructions
Intelligently refactor code by renaming symbols across multiple files
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | List of files to search and refactor | |
| newName | Yes | The new symbol name | |
| oldName | Yes | The symbol name to replace |
Input Schema (JSON Schema)
{
"properties": {
"files": {
"description": "List of files to search and refactor",
"type": "array"
},
"newName": {
"description": "The new symbol name",
"type": "string"
},
"oldName": {
"description": "The symbol name to replace",
"type": "string"
}
},
"required": [
"files",
"oldName",
"newName"
],
"type": "object"
}
Implementation Reference
- src/index.ts:335-362 (registration)Registration of the 'smart_refactor' MCP tool including its schema, description, and annotations.mcpServer.registerTool({ name: 'smart_refactor', description: 'Intelligently refactor code by renaming symbols across multiple files', inputSchema: { type: 'object', properties: { files: { type: 'array', description: 'List of files to search and refactor' }, oldName: { type: 'string', description: 'The symbol name to replace' }, newName: { type: 'string', description: 'The new symbol name' } }, required: ['files', 'oldName', 'newName'] }, annotations: { readOnlyHint: false, destructiveHint: true, idempotentHint: true, openWorldHint: false } });
- src/index.ts:338-354 (schema)Input schema definition for the 'smart_refactor' tool specifying files, oldName, and newName parameters.inputSchema: { type: 'object', properties: { files: { type: 'array', description: 'List of files to search and refactor' }, oldName: { type: 'string', description: 'The symbol name to replace' }, newName: { type: 'string', description: 'The new symbol name' } }, required: ['files', 'oldName', 'newName']
- src/router/operation-router.ts:503-530 (handler)Handler logic for 'smart_refactor' operation: searches for oldName in affected files using fileSystemManager, identifies files with matches, and performs multi-file replace using editInstanceManager.case 'smart_refactor': // Use file system to find occurrences, then Edit for precision const searchResults = await Promise.all( operation.affectedFiles.map(file => this.fileSystemManager.findInFile( file, new RegExp(operation.params.oldName, 'g') ) ) ); // If we found occurrences, use Edit to refactor const filesToEdit = operation.affectedFiles.filter((file, index) => searchResults[index].length > 0); if (filesToEdit.length > 0) { return this.editInstanceManager.coordinateMultiFileEdit({ files: filesToEdit, operation: { type: 'replace', params: { pattern: operation.params.oldName, replacement: operation.params.newName } } }); } return { message: 'No occurrences found to refactor' };
- Categorizes 'smart_refactor' as a hybrid operation in complexity analysis, leading to hybrid executor routing.const hybridOperations = [ 'smart_refactor', 'validate_and_edit', 'backup_and_edit', 'atomic_multi_file_edit'
- src/http/http-server.ts:279-299 (helper)REST API endpoint wrapper for calling the 'smart_refactor' MCP tool.this.app.post('/api/refactor', async (req, res) => { try { const refactorRequest = parseMessage({ jsonrpc: '2.0', method: 'tools/call', params: { name: 'smart_refactor', arguments: { files: req.body.files, oldName: req.body.oldName, newName: req.body.newName } }, id: 'rest-' + Date.now() }); const response = await this.mcpServer.handleMessage(refactorRequest); res.json(response); } catch (error: any) { res.status(500).json({ error: error.message }); } });