Skip to main content
Glama

mcp__gemini__refactor_genius

Refactor code intelligently with automated testing, safety validation, and rollback capabilities. Enhance code readability, maintainability, and performance while ensuring high safety standards.

Instructions

Intelligent code refactoring with automated testing, rollback capabilities, and safety validation

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
codeYesCode to refactor
generate_testsNoGenerate test cases
languageNoProgramming languagejavascript
refactor_goalsNoRefactoring goals
rollback_planNoCreate rollback plan
safety_levelNoSafety levelhigh

Implementation Reference

  • The async handler function implementing the core logic of the 'mcp__gemini__refactor_genius' tool. It destructures input args, performs safety analysis via AI, generates refactoring plan and code, optionally creates tests and rollback strategy using multiple AI client calls, and returns a formatted result with all sections.
    handler: async (args) => { const { code, language = 'javascript', refactor_goals = ['readability', 'maintainability', 'performance'], safety_level = 'high', generate_tests = true, rollback_plan = true } = args; validateString(code, 'code', 15000); const timer = performanceMonitor.startTimer('refactor_genius'); const goals = Array.isArray(refactor_goals) ? refactor_goals : [refactor_goals]; // Safety analysis first const safetyPrompt = `Analyze this ${language} code for refactoring safety: \`\`\`${language} ${code} \`\`\` Assess: 1. **Refactoring Safety Score** (1-10, where 10 is safest) 2. **Risk Factors** that could break functionality 3. **Critical Dependencies** that must be preserved 4. **External Interfaces** that cannot change 5. **Hidden Coupling** that might be affected 6. **Complexity Hotspots** requiring careful handling Safety Level Required: ${safety_level} Provide safety assessment and recommendations.`; const safetyAnalysis = await aiClient.call(safetyPrompt, 'review'); // Main refactoring analysis const refactorPrompt = `Perform intelligent refactoring for this ${language} code: \`\`\`${language} ${code} \`\`\` **Goals**: ${goals.join(', ')} **Safety Level**: ${safety_level} Provide: 1. **Refactoring Plan** - Step-by-step refactoring approach - Order of operations for safety - Risk mitigation at each step 2. **Refactored Code** - Complete refactored implementation - Preserve all functionality - Maintain external interfaces 3. **Improvement Analysis** - Before/after comparison - Quantified improvements - Goal achievement assessment 4. **Change Summary** - What was changed and why - Performance implications - Maintainability improvements ${safety_level === 'high' ? 'Apply conservative refactoring with maximum safety guarantees.' : ''}`; const refactorAnalysis = await aiClient.call(refactorPrompt, 'coding', { complexity: 'complex', maxTokens: 4000 }); let result = `♻️ **Refactor Genius** (${language}) **Goals**: ${goals.join(', ')} **Safety Level**: ${safety_level} --- 🛡️ **Safety Analysis** ${safetyAnalysis} --- 🔧 **Refactoring Implementation** ${refactorAnalysis}`; // Generate test cases if requested if (generate_tests) { const testPrompt = `Generate comprehensive test cases for this refactoring: **Original Code:** \`\`\`${language} ${code} \`\`\` **Refactored Code:** (from analysis above) Create: 1. **Unit Tests** for individual functions/methods 2. **Integration Tests** for component interactions 3. **Regression Tests** to ensure no functionality breaks 4. **Performance Tests** to validate improvements 5. **Edge Case Tests** for boundary conditions Use appropriate testing framework for ${language}. Focus on verifying that refactored code behaves identically to original.`; const testCases = await aiClient.call(testPrompt, 'testing'); result += ` --- 🧪 **Test Suite** ${testCases}`; } // Create rollback plan if requested if (rollback_plan) { const rollbackPrompt = `Create a rollback plan for this refactoring: **Original Code:** \`\`\`${language} ${code} \`\`\` Provide: 1. **Rollback Strategy** - Step-by-step rollback process - Recovery checkpoints - Validation steps 2. **Risk Mitigation** - What could go wrong - Early warning signs - Emergency procedures 3. **Monitoring Plan** - Key metrics to watch - Performance indicators - Alert thresholds 4. **Backup Recommendations** - What to backup before refactoring - Version control strategy - Documentation requirements`; const rollbackStrategy = await aiClient.call(rollbackPrompt, 'analysis'); result += ` --- 🔄 **Rollback Plan** ${rollbackStrategy}`; } timer.end(); return result; }
  • Input schema defining the parameters for the tool, including required 'code' string, optional language, array of refactor_goals, safety_level, and booleans for generating tests and rollback plan.
    parameters: { code: { type: 'string', description: 'Code to refactor', required: true }, language: { type: 'string', description: 'Programming language', default: 'javascript' }, refactor_goals: { type: 'array', description: 'Refactoring goals', default: ['readability', 'maintainability', 'performance'] }, safety_level: { type: 'string', description: 'Safety level', default: 'high' }, generate_tests: { type: 'boolean', description: 'Generate test cases', default: true }, rollback_plan: { type: 'boolean', description: 'Create rollback plan', default: true } },
  • Registration block in ToolRegistry where the enhancedTools module (containing 'mcp__gemini__refactor_genius') is registered via registerToolsFromModule(enhancedTools), which iterates over all tools in the module and calls registerTool for each.
    this.registerToolsFromModule(codeTools); this.registerToolsFromModule(analysisTools); this.registerToolsFromModule(enhancedTools); this.registerToolsFromModule(businessTools); this.registerToolsFromModule(licenseTools);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/emmron/gemini-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server