suggest_refactoring
Improve code structure and readability by generating refactoring suggestions based on performance, maintainability, or readability focus for various programming languages.
Instructions
提供代码重构建议,改善代码结构和可读性
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | 要重构的代码 | |
| focus | No | 重构重点 | |
| language | Yes | 编程语言 |
Implementation Reference
- src/tools/refactoringSuggester.ts:6-56 (handler)The primary handler function that orchestrates refactoring analysis across different focus areas (performance, readability, maintainability) and language-specific rules, collecting and prioritizing suggestions.export async function suggestRefactoring( code: string, language: string, focus: 'performance' | 'readability' | 'maintainability' | 'all' = 'all' ): Promise<RefactoringResult> { const suggestions: RefactoringSuggestion[] = []; // 根据重构重点进行分析 if (focus === 'all' || focus === 'performance') { await analyzePerformanceIssues(code, language, suggestions); } if (focus === 'all' || focus === 'readability') { await analyzeReadabilityIssues(code, language, suggestions); } if (focus === 'all' || focus === 'maintainability') { await analyzeMaintainabilityIssues(code, language, suggestions); } // 语言特定重构建议 switch (language.toLowerCase()) { case 'javascript': case 'typescript': await analyzeJavaScriptRefactoring(code, suggestions); break; case 'python': await analyzePythonRefactoring(code, suggestions); break; case 'java': await analyzeJavaRefactoring(code, suggestions); break; } // 按优先级排序 suggestions.sort((a, b) => { const priorityOrder = { 'high': 3, 'medium': 2, 'low': 1 }; return priorityOrder[b.priority] - priorityOrder[a.priority]; }); return { language, focus, totalSuggestions: suggestions.length, highPriority: suggestions.filter(s => s.priority === 'high').length, mediumPriority: suggestions.filter(s => s.priority === 'medium').length, lowPriority: suggestions.filter(s => s.priority === 'low').length, suggestions, summary: generateRefactoringSummary(suggestions, focus) }; }
- src/types/index.ts:61-82 (schema)TypeScript interface definitions for RefactoringResult (output structure) and RefactoringSuggestion (individual suggestions).export interface RefactoringResult { language: string; focus: 'performance' | 'readability' | 'maintainability' | 'all'; totalSuggestions: number; highPriority: number; mediumPriority: number; lowPriority: number; suggestions: RefactoringSuggestion[]; summary: string; } export interface RefactoringSuggestion { type: 'performance' | 'readability' | 'maintainability' | 'modernization' | 'pythonic'; priority: 'high' | 'medium' | 'low'; title: string; description: string; line: number; originalCode: string; suggestedCode: string; impact: string; effort: 'low' | 'medium' | 'high'; }
- src/index.ts:110-127 (schema)JSON schema defining the input parameters for the suggest_refactoring tool (code, language, optional focus).inputSchema: { type: 'object', properties: { code: { type: 'string', description: '要重构的代码' }, language: { type: 'string', description: '编程语言' }, focus: { type: 'string', enum: ['performance', 'readability', 'maintainability', 'all'], description: '重构重点' } }, required: ['code', 'language']
- src/index.ts:107-129 (registration)Registration of the 'suggest_refactoring' tool in the MCP server's ListTools response, including name, description, and input schema.{ name: 'suggest_refactoring', description: '提供代码重构建议,改善代码结构和可读性', inputSchema: { type: 'object', properties: { code: { type: 'string', description: '要重构的代码' }, language: { type: 'string', description: '编程语言' }, focus: { type: 'string', enum: ['performance', 'readability', 'maintainability', 'all'], description: '重构重点' } }, required: ['code', 'language'] } },
- src/index.ts:244-262 (handler)MCP server request handler for 'suggest_refactoring' that validates input arguments using Zod, calls the core suggestRefactoring function, and formats the response.private async handleSuggestRefactoring(args: any) { const schema = z.object({ code: z.string(), language: z.string(), focus: z.enum(['performance', 'readability', 'maintainability', 'all']).default('all') }); const { code, language, focus } = schema.parse(args); const result = await suggestRefactoring(code, language, focus); return { content: [ { type: 'text', text: JSON.stringify(result, null, 2) } ] }; }