|
| 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 { z } from 'zod'; |
| 10 | +import { createStructuredContentOutput } from '../../utils'; |
| 11 | +import { type McpToolContext, type McpToolDeclaration, declareTool } from '../tool-registry'; |
| 12 | +import { loadSchematicsMetadata } from './utils'; |
| 13 | + |
| 14 | +const listInputSchema = z.object({ |
| 15 | + workspacePath: z |
| 16 | + .string() |
| 17 | + .describe('Path to the workspace angular.json (or any file within the workspace).'), |
| 18 | +}); |
| 19 | + |
| 20 | +const listOutputSchema = z.object({ |
| 21 | + schematics: z.array( |
| 22 | + z.object({ |
| 23 | + name: z.string(), |
| 24 | + aliases: z.array(z.string()).optional(), |
| 25 | + description: z.string().optional(), |
| 26 | + hidden: z.boolean().optional(), |
| 27 | + private: z.boolean().optional(), |
| 28 | + required: z.array(z.string()).optional(), |
| 29 | + options: z |
| 30 | + .array( |
| 31 | + z.object({ |
| 32 | + name: z.string(), |
| 33 | + description: z.string().optional(), |
| 34 | + type: z.union([z.string(), z.array(z.string())]).optional(), |
| 35 | + enum: z.array(z.any()).optional(), |
| 36 | + default: z.any().optional(), |
| 37 | + required: z.boolean().optional(), |
| 38 | + alias: z.string().optional(), |
| 39 | + prompt: z.string().optional(), |
| 40 | + }), |
| 41 | + ) |
| 42 | + .optional(), |
| 43 | + }), |
| 44 | + ), |
| 45 | +}); |
| 46 | + |
| 47 | +export type ListSchematicsInput = z.infer<typeof listInputSchema>; |
| 48 | +export type ListSchematicsOutput = z.infer<typeof listOutputSchema>; |
| 49 | + |
| 50 | +async function handleListSchematics(input: ListSchematicsInput, context: McpToolContext) { |
| 51 | + // Always use injected loader if present (for tests) |
| 52 | + const { meta } = await loadSchematicsMetadata( |
| 53 | + input.workspacePath, |
| 54 | + context.logger, |
| 55 | + typeof context.schematicMetaLoader === 'function' ? context.schematicMetaLoader : undefined, |
| 56 | + ); |
| 57 | + const serialized = meta.map((m) => ({ |
| 58 | + name: m.name, |
| 59 | + aliases: m.aliases, |
| 60 | + description: m.description, |
| 61 | + hidden: m.hidden, |
| 62 | + private: m.private, |
| 63 | + required: m.required, |
| 64 | + options: m.options?.map((o) => ({ |
| 65 | + name: o.name, |
| 66 | + description: o.description, |
| 67 | + type: o.type, |
| 68 | + enum: o.enum, |
| 69 | + default: o.default, |
| 70 | + required: o.required, |
| 71 | + alias: o.alias, |
| 72 | + prompt: o.prompt, |
| 73 | + })), |
| 74 | + })); |
| 75 | + |
| 76 | + return createStructuredContentOutput<ListSchematicsOutput>({ |
| 77 | + schematics: serialized, |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +export const LIST_SCHEMATICS_TOOL: McpToolDeclaration< |
| 82 | + typeof listInputSchema.shape, |
| 83 | + typeof listOutputSchema.shape |
| 84 | +> = declareTool({ |
| 85 | + name: 'list_schematics', |
| 86 | + title: 'List Angular Schematics', |
| 87 | + description: ` |
| 88 | +<Purpose> |
| 89 | +Enumerates all schematics provided by @schematics/angular with full option metadata (types, defaults, enums, prompts, required flags). |
| 90 | +</Purpose> |
| 91 | +<Use Cases> |
| 92 | +* Discover generators available before planning code changes. |
| 93 | +* Inspect required options for a schematic prior to execution. |
| 94 | +* Provide intelligent suggestions to users based on option types and prompts. |
| 95 | +</Use Cases> |
| 96 | +<Operational Notes> |
| 97 | +* Resolution uses Node's module loader. If a schematic collection cannot be resolved, this tool returns an empty list. |
| 98 | +* Hidden/private schematics are included for transparency. |
| 99 | +* Use 'run_schematic' to actually execute a schematic after reviewing options here. |
| 100 | + * Official docs: https://angular.dev/tools/cli/schematics and https://angular.dev/cli/generate |
| 101 | +</Operational Notes> |
| 102 | +`, |
| 103 | + inputSchema: listInputSchema.shape, |
| 104 | + outputSchema: listOutputSchema.shape, |
| 105 | + isLocalOnly: true, |
| 106 | + isReadOnly: true, |
| 107 | + factory: (context) => (input) => handleListSchematics(input, context), |
| 108 | +}); |
0 commit comments