|
| 1 | +import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { z } from 'zod'; |
| 4 | +import type { AngularWorkspace } from '../../../utilities/config'; |
| 5 | + |
| 6 | +export function registerListProjectsTool( |
| 7 | + server: McpServer, |
| 8 | + context: { workspace?: AngularWorkspace }, |
| 9 | +): void { |
| 10 | + server.registerTool( |
| 11 | + 'list_projects', |
| 12 | + { |
| 13 | + title: 'List Angular Projects', |
| 14 | + description: |
| 15 | + 'Lists the names of all applications and libraries defined within an Angular workspace. ' + |
| 16 | + 'It reads the `angular.json` configuration file to identify the projects. ', |
| 17 | + annotations: { |
| 18 | + readOnlyHint: true, |
| 19 | + }, |
| 20 | + outputSchema: { |
| 21 | + projects: z.array( |
| 22 | + z.object({ |
| 23 | + name: z |
| 24 | + .string() |
| 25 | + .describe('The name of the project, as defined in the `angular.json` file.'), |
| 26 | + type: z |
| 27 | + .enum(['application', 'library']) |
| 28 | + .optional() |
| 29 | + .describe(`The type of the project, either 'application' or 'library'.`), |
| 30 | + root: z |
| 31 | + .string() |
| 32 | + .describe('The root directory of the project, relative to the workspace root.'), |
| 33 | + sourceRoot: z |
| 34 | + .string() |
| 35 | + .describe( |
| 36 | + `The root directory of the project's source files, relative to the workspace root.`, |
| 37 | + ), |
| 38 | + selectorPrefix: z |
| 39 | + .string() |
| 40 | + .optional() |
| 41 | + .describe( |
| 42 | + 'The prefix to use for component selectors.' + |
| 43 | + ` For example, a prefix of 'app' would result in selectors like '<app-my-component>'.`, |
| 44 | + ), |
| 45 | + }), |
| 46 | + ), |
| 47 | + }, |
| 48 | + }, |
| 49 | + async () => { |
| 50 | + const { workspace } = context; |
| 51 | + |
| 52 | + if (!workspace) { |
| 53 | + return { |
| 54 | + content: [ |
| 55 | + { |
| 56 | + type: 'text' as const, |
| 57 | + text: |
| 58 | + 'No Angular workspace found.' + |
| 59 | + ' An `angular.json` file, which marks the root of a workspace,' + |
| 60 | + ' could not be located in the current directory or any of its parent directories.', |
| 61 | + }, |
| 62 | + ], |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + const projects = []; |
| 67 | + // Convert to output format |
| 68 | + for (const [name, project] of workspace.projects.entries()) { |
| 69 | + projects.push({ |
| 70 | + name, |
| 71 | + type: project.extensions['projectType'] as 'application' | 'library' | undefined, |
| 72 | + root: project.root, |
| 73 | + sourceRoot: project.sourceRoot ?? path.posix.join(project.root, 'src'), |
| 74 | + selectorPrefix: project.extensions['prefix'] as string, |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + // The structuredContent field is newer and may not be supported by all hosts. |
| 79 | + // A text representation of the content is also provided for compatibility. |
| 80 | + return { |
| 81 | + content: [ |
| 82 | + { |
| 83 | + type: 'text' as const, |
| 84 | + text: `Projects in the Angular workspace:\n${JSON.stringify(projects)}`, |
| 85 | + }, |
| 86 | + ], |
| 87 | + structuredContent: { projects }, |
| 88 | + }; |
| 89 | + }, |
| 90 | + ); |
| 91 | +} |
0 commit comments