|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 10 | +import { z } from 'zod'; |
| 11 | + |
| 12 | +interface Transformation { |
| 13 | + name: string; |
| 14 | + description: string; |
| 15 | + documentationUrl: string; |
| 16 | + instructions?: string; |
| 17 | +} |
| 18 | + |
| 19 | +const TRANSFORMATIONS: Array<Transformation> = [ |
| 20 | + { |
| 21 | + name: 'control-flow-migration', |
| 22 | + description: |
| 23 | + 'Migrates from `*ngIf`, `*ngFor`, and `*ngSwitch` to the new `@if`, `@for`, and `@switch` block syntax in templates.', |
| 24 | + documentationUrl: 'https://angular.dev/reference/migrations/control-flow', |
| 25 | + }, |
| 26 | + { |
| 27 | + name: 'self-closing-tags-migration', |
| 28 | + description: |
| 29 | + 'Converts tags for elements with no content to be self-closing (e.g., `<app-foo></app-foo>` becomes `<app-foo />`).', |
| 30 | + documentationUrl: 'https://angular.dev/reference/migrations/self-closing-tags', |
| 31 | + }, |
| 32 | + { |
| 33 | + name: 'test-bed-get', |
| 34 | + description: |
| 35 | + 'Updates `TestBed.get` to the preferred and type-safe `TestBed.inject` in TypeScript test files.', |
| 36 | + documentationUrl: 'https://angular.dev/guide/testing/dependency-injection', |
| 37 | + }, |
| 38 | + { |
| 39 | + name: 'inject-flags', |
| 40 | + description: |
| 41 | + 'Updates `inject` calls from using the InjectFlags enum to a more modern and readable options object.', |
| 42 | + documentationUrl: 'https://angular.dev/reference/migrations/inject-function', |
| 43 | + }, |
| 44 | + { |
| 45 | + name: 'output-migration', |
| 46 | + description: 'Converts `@Output` declarations to the new functional `output()` syntax.', |
| 47 | + documentationUrl: 'https://angular.dev/reference/migrations/outputs', |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'signal-input-migration', |
| 51 | + description: 'Migrates `@Input` declarations to the new signal-based `input()` syntax.', |
| 52 | + documentationUrl: 'https://angular.dev/reference/migrations/signal-inputs', |
| 53 | + }, |
| 54 | + { |
| 55 | + name: 'signal-queries-migration', |
| 56 | + description: |
| 57 | + 'Migrates `@ViewChild` and `@ContentChild` queries to their signal-based `viewChild` and `contentChild` versions.', |
| 58 | + documentationUrl: 'https://angular.dev/reference/migrations/signal-queries', |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'standalone', |
| 62 | + description: |
| 63 | + 'Converts the application to use standalone components, directives, and pipes. This is a ' + |
| 64 | + 'three-step process. After each step, you should verify that your application builds and ' + |
| 65 | + 'runs correctly.', |
| 66 | + instructions: |
| 67 | + 'This migration requires running a cli schematic multiple times. Run the commands in the ' + |
| 68 | + 'order listed below, verifying that your code builds and runs between each step:\n\n' + |
| 69 | + '1. Run `ng g @angular/core:standalone` and select "Convert all components, directives and pipes to standalone"\n' + |
| 70 | + '2. Run `ng g @angular/core:standalone` and select "Remove unnecessary NgModule classes"\n' + |
| 71 | + '3. Run `ng g @angular/core:standalone` and select "Bootstrap the project using standalone APIs"', |
| 72 | + documentationUrl: 'https://angular.dev/reference/migrations/standalone', |
| 73 | + }, |
| 74 | + { |
| 75 | + name: 'zoneless', |
| 76 | + description: 'Migrates the application to be zoneless.', |
| 77 | + documentationUrl: 'https://angular.dev/guide/zoneless', |
| 78 | + }, |
| 79 | +]; |
| 80 | + |
| 81 | +const modernizeInputSchema = z.object({ |
| 82 | + // Casting to [string, ...string[]] since the enum definition requires a nonempty array. |
| 83 | + transformations: z |
| 84 | + .array(z.enum(TRANSFORMATIONS.map((t) => t.name) as [string, ...string[]])) |
| 85 | + .optional(), |
| 86 | +}); |
| 87 | + |
| 88 | +export type ModernizeInput = z.infer<typeof modernizeInputSchema>; |
| 89 | + |
| 90 | +function generateInstructions(transformationNames: string[]): string[] { |
| 91 | + if (transformationNames.length === 0) { |
| 92 | + return [ |
| 93 | + 'See https://angular.dev/best-practices for Angular best practices. ' + |
| 94 | + 'You can call this tool if you have specific transformation you want to run.', |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + const instructions: string[] = []; |
| 99 | + const transformationsToRun = TRANSFORMATIONS.filter((t) => transformationNames?.includes(t.name)); |
| 100 | + |
| 101 | + for (const transformation of transformationsToRun) { |
| 102 | + let transformationInstructions = ''; |
| 103 | + if (transformation.instructions) { |
| 104 | + transformationInstructions = transformation.instructions; |
| 105 | + } else { |
| 106 | + // If no instructions are included, default to running a cli schematic with the transformation name. |
| 107 | + const command = `ng generate @angular/core:${transformation.name}`; |
| 108 | + transformationInstructions = `To run the ${transformation.name} migration, execute the following command: \`${command}\`.`; |
| 109 | + } |
| 110 | + if (transformation.documentationUrl) { |
| 111 | + transformationInstructions += `\nFor more information, see ${transformation.documentationUrl}.`; |
| 112 | + } |
| 113 | + instructions.push(transformationInstructions); |
| 114 | + } |
| 115 | + |
| 116 | + return instructions; |
| 117 | +} |
| 118 | + |
| 119 | +export async function runModernization(input: ModernizeInput) { |
| 120 | + const structuredContent = { instructions: generateInstructions(input.transformations ?? []) }; |
| 121 | + |
| 122 | + return { |
| 123 | + content: [{ type: 'text' as const, text: JSON.stringify(structuredContent) }], |
| 124 | + structuredContent, |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +export function registerModernizeTool(server: McpServer): void { |
| 129 | + server.registerTool( |
| 130 | + 'modernize', |
| 131 | + { |
| 132 | + title: 'Modernize Angular Code', |
| 133 | + description: |
| 134 | + '<Purpose>\n' + |
| 135 | + 'This tool modernizes Angular code by applying the latest best practices and syntax improvements, ' + |
| 136 | + 'ensuring it is idiomatic, readable, and maintainable.\n\n' + |
| 137 | + '</Purpose>\n' + |
| 138 | + '<Use Cases>\n' + |
| 139 | + '* After generating new code: Run this tool immediately after creating new Angular components, directives, ' + |
| 140 | + 'or services to ensure they adhere to modern standards.\n' + |
| 141 | + '* On existing code: Apply to existing TypeScript files (.ts) and Angular templates (.ng.html) to update ' + |
| 142 | + 'them with the latest features, such as the new built-in control flow syntax.\n\n' + |
| 143 | + '* When the user asks for a specific transformation: When the transformation list is populated, ' + |
| 144 | + 'these specific ones will be ran on the inputs.\n' + |
| 145 | + '</Use Cases>\n' + |
| 146 | + '<Transformations>\n' + |
| 147 | + TRANSFORMATIONS.map((t) => `* ${t.name}: ${t.description}`).join('\n') + |
| 148 | + '\n</Transformations>\n', |
| 149 | + annotations: { |
| 150 | + readOnlyHint: true, |
| 151 | + }, |
| 152 | + inputSchema: modernizeInputSchema.shape, |
| 153 | + outputSchema: { |
| 154 | + instructions: z |
| 155 | + .array(z.string()) |
| 156 | + .optional() |
| 157 | + .describe('A list of instructions on how to run the migrations.'), |
| 158 | + }, |
| 159 | + }, |
| 160 | + (input) => runModernization(input), |
| 161 | + ); |
| 162 | +} |
0 commit comments