Skip to content

refactor(@angular/cli): declaratively register MCP server tools #30858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions packages/angular/cli/src/commands/mcp/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,43 @@ export default class McpCommandModule extends CommandModule implements CommandMo
longDescriptionPath = undefined;

builder(localYargs: Argv): Argv {
return localYargs;
return localYargs
.option('read-only', {
type: 'boolean',
default: false,
describe: 'Only register read-only tools.',
})
.option('local-only', {
type: 'boolean',
default: false,
describe: 'Only register tools that do not require internet access.',
})
.option('experimental-tool', {
type: 'string',
alias: 'E',
array: true,
describe: 'Enable an experimental tool.',
});
}

async run(): Promise<void> {
async run(options: {
readOnly: boolean;
localOnly: boolean;
experimentalTool: string[] | undefined;
}): Promise<void> {
if (isTTY()) {
this.context.logger.info(INTERACTIVE_MESSAGE);

return;
}

const server = await createMcpServer(
{ workspace: this.context.workspace },
{
workspace: this.context.workspace,
readOnly: options.readOnly,
localOnly: options.localOnly,
experimentalTools: options.experimentalTool,
},
this.context.logger,
);
const transport = new StdioServerTransport();
Expand Down
69 changes: 48 additions & 21 deletions packages/angular/cli/src/commands/mcp/mcp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,23 @@
*/

import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import type { AngularWorkspace } from '../../utilities/config';
import { VERSION } from '../../utilities/version';
import { registerInstructionsResource } from './resources/instructions';
import { registerBestPracticesTool } from './tools/best-practices';
import { registerDocSearchTool } from './tools/doc-search';
import { registerFindExampleTool } from './tools/examples';
import { registerModernizeTool } from './tools/modernize';
import { registerListProjectsTool } from './tools/projects';
import { BEST_PRACTICES_TOOL } from './tools/best-practices';
import { DOC_SEARCH_TOOL } from './tools/doc-search';
import { FIND_EXAMPLE_TOOL } from './tools/examples';
import { MODERNIZE_TOOL } from './tools/modernize';
import { LIST_PROJECTS_TOOL } from './tools/projects';
import { McpToolDeclaration, registerTools } from './tools/tool-registry';

export async function createMcpServer(
context: {
workspace?: AngularWorkspace;
readOnly?: boolean;
localOnly?: boolean;
experimentalTools?: string[];
},
logger: { warn(text: string): void },
): Promise<McpServer> {
Expand All @@ -42,28 +45,52 @@ export async function createMcpServer(
);

registerInstructionsResource(server);
registerBestPracticesTool(server);
registerModernizeTool(server);

// If run outside an Angular workspace (e.g., globally) skip the workspace specific tools.
if (context.workspace) {
registerListProjectsTool(server, context);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let toolDeclarations: McpToolDeclaration<any, any>[] = [
BEST_PRACTICES_TOOL,
DOC_SEARCH_TOOL,
LIST_PROJECTS_TOOL,
];
const experimentalToolDeclarations = [FIND_EXAMPLE_TOOL, MODERNIZE_TOOL];

if (context.readOnly) {
toolDeclarations = toolDeclarations.filter((tool) => tool.isReadOnly);
}

await registerDocSearchTool(server);
if (context.localOnly) {
toolDeclarations = toolDeclarations.filter((tool) => tool.isLocalOnly);
}

const enabledExperimentalTools = new Set(context.experimentalTools);
if (process.env['NG_MCP_CODE_EXAMPLES'] === '1') {
// sqlite database support requires Node.js 22.16+
const [nodeMajor, nodeMinor] = process.versions.node.split('.', 2).map(Number);
if (nodeMajor < 22 || (nodeMajor === 22 && nodeMinor < 16)) {
logger.warn(
`MCP tool 'find_examples' requires Node.js 22.16 (or higher). ` +
' Registration of this tool has been skipped.',
);
} else {
await registerFindExampleTool(server, path.join(__dirname, '../../../lib/code-examples.db'));
enabledExperimentalTools.add('find_examples');
}

if (enabledExperimentalTools.size > 0) {
const experimentalToolsMap = new Map(
experimentalToolDeclarations.map((tool) => [tool.name, tool]),
);

for (const toolName of enabledExperimentalTools) {
const tool = experimentalToolsMap.get(toolName);
if (tool) {
toolDeclarations.push(tool);
} else {
logger.warn(`Unknown experimental tool: ${toolName}`);
}
}
}

await registerTools(
server,
{
workspace: context.workspace,
logger,
exampleDatabasePath: path.join(__dirname, '../../../lib/code-examples.db'),
},
toolDeclarations,
);

return server;
}
40 changes: 18 additions & 22 deletions packages/angular/cli/src/commands/mcp/tools/best-practices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,25 @@
* 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';
import { declareTool } from './tool-registry';

export function registerBestPracticesTool(server: McpServer): void {
let bestPracticesText;
export const BEST_PRACTICES_TOOL = declareTool({
name: '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.',
isReadOnly: true,
isLocalOnly: true,
factory: () => {
let bestPracticesText: string;

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 () => {
return async () => {
bestPracticesText ??= await readFile(
path.join(__dirname, '..', 'instructions', 'best-practices.md'),
'utf-8',
Expand All @@ -46,6 +42,6 @@ export function registerBestPracticesTool(server: McpServer): void {
},
],
};
},
);
}
};
},
});
Loading