|
| 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 { readFile } from 'node:fs/promises'; |
| 11 | +import path from 'node:path'; |
| 12 | +import type { AngularWorkspace } from '../../utilities/config'; |
| 13 | +import { VERSION } from '../../utilities/version'; |
| 14 | + |
| 15 | +export async function createMcpServer(context: { |
| 16 | + workspace?: AngularWorkspace; |
| 17 | +}): Promise<McpServer> { |
| 18 | + const server = new McpServer({ |
| 19 | + name: 'angular-cli-server', |
| 20 | + version: VERSION.full, |
| 21 | + capabilities: { |
| 22 | + resources: {}, |
| 23 | + tools: {}, |
| 24 | + }, |
| 25 | + }); |
| 26 | + |
| 27 | + server.registerResource( |
| 28 | + 'instructions', |
| 29 | + 'instructions://best-practices', |
| 30 | + { |
| 31 | + title: 'Angular System Instructions', |
| 32 | + description: |
| 33 | + 'A set of instructions to help LLMs generate correct code that follows Angular best practices.', |
| 34 | + mimeType: 'text/markdown', |
| 35 | + }, |
| 36 | + async () => { |
| 37 | + const text = await readFile( |
| 38 | + path.join(__dirname, 'instructions', 'best-practices.md'), |
| 39 | + 'utf-8', |
| 40 | + ); |
| 41 | + |
| 42 | + return { contents: [{ uri: 'instructions://best-practices', text }] }; |
| 43 | + }, |
| 44 | + ); |
| 45 | + |
| 46 | + server.registerTool( |
| 47 | + 'list_projects', |
| 48 | + { |
| 49 | + title: 'List projects', |
| 50 | + description: |
| 51 | + 'List projects within an Angular workspace.' + |
| 52 | + ' This information is read from the `angular.json` file at the root path of the Angular workspace', |
| 53 | + }, |
| 54 | + () => { |
| 55 | + if (!context.workspace) { |
| 56 | + return { |
| 57 | + content: [ |
| 58 | + { |
| 59 | + type: 'text', |
| 60 | + text: 'Not within an Angular project.', |
| 61 | + }, |
| 62 | + ], |
| 63 | + }; |
| 64 | + } |
| 65 | + |
| 66 | + return { |
| 67 | + content: [ |
| 68 | + { |
| 69 | + type: 'text', |
| 70 | + text: |
| 71 | + 'Projects in the Angular workspace: ' + |
| 72 | + [...context.workspace.projects.keys()].join(','), |
| 73 | + }, |
| 74 | + ], |
| 75 | + }; |
| 76 | + }, |
| 77 | + ); |
| 78 | + |
| 79 | + return server; |
| 80 | +} |
0 commit comments