From 557d6f13867ff4030da660ea83d37e2bb49a8500 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Wed, 16 Jul 2025 15:53:14 -0400 Subject: [PATCH] refactor(@angular/cli): add a get best practices guide MCP tool The Angular CLI's stdio-based MCP server now contains a tool to get an Angular best practices guide. This is in addition to a resource with the same content. The tool provides a description that strongly encourages the use of this tool and its content when performing Angular related tasks. This is useful in cases where MCP resource usage is not available or the resource would need to manually be added as context for specific use cases. --- .../cli/src/commands/mcp/mcp-server.ts | 3 ++ .../src/commands/mcp/tools/best-practices.ts | 45 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 packages/angular/cli/src/commands/mcp/tools/best-practices.ts diff --git a/packages/angular/cli/src/commands/mcp/mcp-server.ts b/packages/angular/cli/src/commands/mcp/mcp-server.ts index 13ba22fbc688..1ca13f8dde3b 100644 --- a/packages/angular/cli/src/commands/mcp/mcp-server.ts +++ b/packages/angular/cli/src/commands/mcp/mcp-server.ts @@ -12,6 +12,7 @@ import path from 'node:path'; import { z } from 'zod'; import type { AngularWorkspace } from '../../utilities/config'; import { VERSION } from '../../utilities/version'; +import { registerBestPracticesTool } from './tools/best-practices'; import { registerDocSearchTool } from './tools/doc-search'; export async function createMcpServer(context: { @@ -48,6 +49,8 @@ export async function createMcpServer(context: { }, ); + registerBestPracticesTool(server); + server.registerTool( 'list_projects', { diff --git a/packages/angular/cli/src/commands/mcp/tools/best-practices.ts b/packages/angular/cli/src/commands/mcp/tools/best-practices.ts new file mode 100644 index 000000000000..c6718a91e3ec --- /dev/null +++ b/packages/angular/cli/src/commands/mcp/tools/best-practices.ts @@ -0,0 +1,45 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.dev/license + */ + +import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'; +import { readFile } from 'node:fs/promises'; +import path from 'node:path'; + +export function registerBestPracticesTool(server: McpServer): void { + server.registerTool( + 'get_best_practices', + { + title: 'Get Angular Coding Best Practices Guide', + description: + 'You **MUST** use this tool to retrieve the Angular Best Practices Guide ' + + 'before any interaction with Angular code (creating, analyzing, modifying). ' + + 'It is mandatory to follow this guide to ensure all code adheres to ' + + 'modern standards, including standalone components, typed forms, and ' + + 'modern control flow. This is the first step for any Angular task.', + annotations: { + readOnlyHint: true, + openWorldHint: false, + }, + }, + async () => { + const text = await readFile( + path.join(__dirname, '..', 'instructions', 'best-practices.md'), + 'utf-8', + ); + + return { + content: [ + { + type: 'text', + text, + }, + ], + }; + }, + ); +}