transform_code
Apply syntax and structural code transformations for renaming, performance optimization, framework migration, and modernization. Supports multiple programming languages and preserves business logic.
Instructions
🔧 Apply code transformations including syntax changes, structural reorganization, framework migration, and modernization. Combines syntax-level and structural changes.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Source code to transform | |
| language | Yes | Programming language of the code | |
| options | No | ||
| target_framework | No | Target framework (for framework migration) | |
| target_language | No | Target language (for language conversion) | |
| transformations | Yes | List of transformations to apply |
Implementation Reference
- src/services/refactor.ts:1086-1119 (handler)Core implementation of the transform_code tool. Applies a series of transformations (naming, modernize, etc.) to the input code and returns the transformed code with changes and warnings.async transformCode(code: string, transformations: any[], language: string, target_language?: string, options: any = {}): Promise<any> { let transformedCode = code; const changes = []; const warnings = []; for (const transformation of transformations) { switch (transformation.type) { case 'naming': const namingResult = await this.transformNamingConvention(transformedCode, transformation.options); transformedCode = namingResult.code; changes.push(...namingResult.changes); break; case 'modernize': const modernizeResult = await this.modernizeCode(transformedCode, language); transformedCode = modernizeResult.refactoredCode; changes.push(...modernizeResult.changes); break; case 'performance': warnings.push('Performance optimization not implemented'); break; case 'security': warnings.push('Security transformation not implemented'); break; } } return { originalCode: code, transformedCode, changes, warnings, instructions: ['Review transformed code for correctness'], }; }
- src/tools/consolidated.ts:522-592 (schema)Input schema definition for the transform_code tool, specifying parameters like code, transformations list, language, and options.inputSchema: { type: 'object', properties: { code: { type: 'string', description: 'Source code to transform', }, transformations: { type: 'array', items: { type: 'object', properties: { type: { type: 'string', enum: ['naming', 'modernize', 'framework', 'performance', 'security', 'structure', 'migration'], description: 'Type of transformation', }, options: { type: 'object', description: 'Transformation-specific options', }, }, required: ['type'], }, description: 'List of transformations to apply', }, language: { type: 'string', description: 'Programming language of the code', }, target_language: { type: 'string', description: 'Target language (for language conversion)', }, target_framework: { type: 'string', description: 'Target framework (for framework migration)', }, options: { type: 'object', properties: { preserve_comments: { type: 'boolean', description: 'Preserve code comments', default: true, }, preserve_logic: { type: 'boolean', description: 'Preserve business logic during transformation', default: true, }, update_imports: { type: 'boolean', description: 'Update import paths automatically', default: true, }, include_instructions: { type: 'boolean', description: 'Include transformation instructions', default: true, }, validate_syntax: { type: 'boolean', description: 'Validate syntax after transformation', default: true, }, }, }, }, required: ['code', 'transformations', 'language'], },
- src/index.ts:285-287 (registration)Tool dispatch registration in the main switch statement handling CallToolRequestSchema.case 'transform_code': result = await handleTransformCode(args); break;
- src/index.ts:559-577 (handler)Wrapper handler function in index.ts that extracts arguments and delegates to refactorService.transformCode, handling response formatting and errors.async function handleTransformCode(args: any) { try { const { code, transformations, language, target_language, options = {} } = args; const result = await refactorService.transformCode( code, transformations, language, target_language, options ); const response = createResponse(result); return formatToolResponse(response); } catch (error) { const response = createResponse(null, error); return formatToolResponse(response); } }