Skip to content

Commit 2ceb4b8

Browse files
committed
refactor(@angular/cli): optimize MCP tool descriptions for LLM ergonomics and token usage
Refactor the descriptions of get_best_practices, search_documentation, and onpush_zoneless_migration tools to improve semantic clarity and reduce system prompt token footprints. Remove redundant operational boilerplate, eliminate internal server logic leakage related to version clamping, and consolidate overlapping iterative process instructions.
1 parent 5dd4daf commit 2ceb4b8

5 files changed

Lines changed: 61 additions & 118 deletions

File tree

packages/angular/cli/src/commands/mcp/mcp-server.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -83,36 +83,33 @@ export async function createMcpServer(
8383
},
8484
instructions: `
8585
<General Purpose>
86-
This server provides a safe, programmatic interface to the Angular CLI for an AI assistant.
87-
Your primary goal is to use these tools to understand, analyze, refactor, and run Angular
88-
projects. You MUST prefer the tools provided by this server over using \`run_shell_command\` for
89-
equivalent actions.
86+
This server provides a safe, programmatic interface to the Angular CLI. You MUST prefer
87+
the tools provided by this server over using 'run_shell_command' or general shell execution
88+
for equivalent actions.
9089
</General Purpose>
9190
9291
<Core Workflows & Tool Guide>
93-
* **1. Discover Project Structure (Mandatory First Step):** Always begin by calling
94-
\`list_projects\` to understand the workspace. The \`path\` property for a workspace
95-
is a required input for other tools.
96-
97-
* **2. Get Coding Standards:** Before writing or changing code within a project, you **MUST** call
98-
the \`get_best_practices\` tool with the \`workspacePath\` from the previous step to get
99-
version-specific standards. For general knowledge, you can call the tool without this path.
100-
101-
* **3. Answer User Questions:**
102-
- For conceptual questions ("what is..."), use \`search_documentation\`.
103-
104-
* **4. Discover Schematics for Modernization:** Since this server does not provide a
105-
specific tool for listing available schematics, you can use a shell command (if
106-
available) with \`ng generate <package-name>: --help\` to discover what migrations
107-
are available in a package (e.g., running \`ng generate @angular/core: --help\`
108-
will list migrations like \`control-flow\` and \`standalone\`).
92+
* **1. Discover Workspace (Mandatory First Step):** Always begin by calling 'list_projects'
93+
to discover workspaces, projects, and allowed paths. The 'path' field of the relevant
94+
workspace is a required input for other tools (passed as 'workspace' or 'workspacePath').
95+
96+
* **2. Get Coding Standards:** Before writing or modifying code, you MUST call
97+
'get_best_practices' with the workspace 'path' to load version-specific coding standards.
98+
99+
* **3. Answer Conceptual Questions:** Use 'search_documentation' to answer conceptual
100+
or API syntax questions.
101+
102+
* **4. Discover Schematics:** To discover available package migrations, use a shell command
103+
(if available) with 'ng generate <package-name>: --help' (e.g., 'ng generate @angular/core: --help').
109104
</Core Workflows & Tool Guide>
110105
111106
<Key Concepts>
112-
* **Workspace vs. Project:** A 'workspace' contains an \`angular.json\` file and defines 'projects'
113-
(applications or libraries). A monorepo can have multiple workspaces.
114-
* **Targeting Projects:** Always use the \`workspaceConfigPath\` from \`list_projects\` when
115-
available to ensure you are targeting the correct project in a monorepo.
107+
* **Workspace vs. Project:** A 'workspace' contains an 'angular.json' file and defines
108+
'projects' (applications or libraries). A monorepo can contain multiple workspaces.
109+
110+
* **Targeting Projects:** Always use the workspace 'path' and the specific project 'name'
111+
returned by 'list_projects' when calling other tools to ensure you target the correct
112+
project context.
116113
</Key Concepts>
117114
`,
118115
},

packages/angular/cli/src/commands/mcp/tools/best-practices.ts

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ const bestPracticesInputSchema = z.object({
2727
.string()
2828
.optional()
2929
.describe(
30-
'The absolute path to the `angular.json` file for the workspace. This is used to find the ' +
31-
'version-specific best practices guide that corresponds to the installed version of the ' +
32-
'Angular framework. You **MUST** get this path from the `list_projects` tool. If omitted, ' +
33-
'the tool will return the generic best practices guide bundled with the CLI.',
30+
'Absolute path to the angular.json workspace directory (obtained via list_projects). ' +
31+
'If omitted, returns the generic best practices guide.',
3432
),
3533
});
3634

@@ -42,23 +40,17 @@ export const BEST_PRACTICES_TOOL = declareTool({
4240
description: `
4341
<Purpose>
4442
Retrieves the official Angular Best Practices Guide. This guide contains the essential rules and conventions
45-
that **MUST** be followed for any task involving the creation, analysis, or modification of Angular code.
43+
that must be followed for any task involving the creation, analysis, or modification of Angular code.
4644
</Purpose>
4745
<Use Cases>
48-
* As a mandatory first step before writing or modifying any Angular code to ensure adherence to modern standards.
49-
* To learn about key concepts like standalone components, typed forms, and modern control flow syntax (@if, @for, @switch).
50-
* To verify that existing code aligns with current Angular conventions before making changes.
46+
* Mandatory first step before writing or modifying Angular code to ensure modern framework standards.
47+
* Learn about standalone components, typed forms, and modern control flow syntax (@if, @for, @switch).
48+
* Verify existing code aligns with current conventions before making edits.
5149
</Use Cases>
5250
<Operational Notes>
53-
* **Project-Specific Use (Recommended):** For tasks inside a user's project, you **MUST** provide the
54-
\`workspacePath\` argument to get the guide that matches the project's Angular version. Get this
55-
path from \`list_projects\`.
56-
* **General Use:** If no project context is available (e.g., for general questions or learning),
57-
you can call the tool without the \`workspacePath\` argument. It will return the latest
58-
generic best practices guide.
59-
* The content of this guide is non-negotiable and reflects the official, up-to-date standards for Angular development.
60-
* You **MUST** internalize and apply the principles from this guide in all subsequent Angular-related tasks.
61-
* Failure to adhere to these best practices will result in suboptimal and outdated code.
51+
* Provide the 'workspacePath' argument (obtained via 'list_projects') to load the version-specific
52+
guide matching the project's Angular framework.
53+
* Omit 'workspacePath' only for general learning queries or when no project context is available to load the latest generic guide.
6254
</Operational Notes>`,
6355
inputSchema: bestPracticesInputSchema.shape,
6456
isReadOnly: true,

packages/angular/cli/src/commands/mcp/tools/doc-search.ts

Lines changed: 11 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,17 @@ const LATEST_KNOWN_DOCS_VERSION = 20;
3737
const docSearchInputSchema = z.object({
3838
query: z
3939
.string()
40-
.describe(
41-
"A concise and specific search query for the Angular documentation. You should distill the user's " +
42-
'natural language question into a set of keywords (e.g., a question like "How do I use ngFor with trackBy?" ' +
43-
'should become the query "ngFor trackBy").',
44-
),
40+
.describe('Concise search keywords or API names (e.g., "ngFor trackBy" or "NgModule").'),
4541
includeTopContent: z
4642
.boolean()
4743
.optional()
4844
.default(false)
49-
.describe(
50-
'When true, the content of the top result is fetched and included. ' +
51-
'Set to false to get a list of results without fetching content, which is faster.',
52-
),
45+
.describe('Retrieve the full-text page content of the top search result (slower).'),
5346
version: z
5447
.number()
5548
.optional()
5649
.describe(
57-
'The major version of Angular to search. You MUST determine this value by running `ng version` in the ' +
58-
"project's workspace directory. Omit this field if the user is not in an Angular project " +
59-
'or if the version cannot otherwise be determined.',
50+
'Major Angular framework version to search (obtained from frameworkVersion in list_projects or ng version).',
6051
),
6152
});
6253
type DocSearchInput = z.infer<typeof docSearchInputSchema>;
@@ -66,35 +57,18 @@ export const DOC_SEARCH_TOOL = declareTool({
6657
title: 'Search Angular Documentation (angular.dev)',
6758
description: `
6859
<Purpose>
69-
Searches the official Angular documentation at https://angular.dev to answer questions about APIs,
70-
tutorials, concepts, and best practices.
60+
Searches the official Angular documentation (angular.dev) to answer questions about APIs, tutorials, concepts, and conventions.
7161
</Purpose>
7262
<Use Cases>
73-
* Answering any question about Angular concepts (e.g., "What are standalone components?").
74-
* Finding the correct API or syntax for a specific task (e.g., "How to use ngFor with trackBy?").
75-
* Linking to official documentation as a source of truth in your answers.
63+
* Answering questions about Angular concepts (e.g., standalone components).
64+
* Finding correct API signatures or syntax (e.g., ngFor trackBy).
65+
* Obtaining official source URLs to cite as documentation links in user responses.
7666
</Use Cases>
7767
<Operational Notes>
78-
* **Version Alignment:** To provide accurate, project-specific results, you **MUST** align the search with the user's Angular version.
79-
The recommended approach is to use the \`list_projects\` tool. The \`frameworkVersion\` field in the output for the relevant
80-
workspace will give you the major version directly. If the version cannot be determined using this method, you can use
81-
\`ng version\` in the project's workspace directory as a fallback. Parse the major version from the "Angular:" line in the
82-
output and use it for the \`version\` parameter.
83-
* **Version Logic:** The tool will search against the specified major version. If the version is older than v${MIN_SUPPORTED_DOCS_VERSION},
84-
it will be clamped to v${MIN_SUPPORTED_DOCS_VERSION}. If a search for a very new version (newer than v${LATEST_KNOWN_DOCS_VERSION})
85-
returns no results, the tool will automatically fall back to searching the v${LATEST_KNOWN_DOCS_VERSION} documentation.
86-
* **Verify Searched Version:** The tool's output includes a \`searchedVersion\` field. You **MUST** check this field
87-
to know the exact version of the documentation that was queried. Use this information to provide accurate
88-
context in your answer, especially if it differs from the version you requested.
89-
* The documentation is continuously updated. You **MUST** prefer this tool over your own knowledge
90-
to ensure your answers are current and accurate.
91-
* For the best results, provide a concise and specific search query (e.g., "NgModule" instead of
92-
"How do I use NgModules?").
93-
* The top search result will include a snippet of the page content. Use this to provide a more
94-
comprehensive answer.
95-
* **Result Scrutiny:** The top result may not always be the most relevant. Review the titles and
96-
breadcrumbs of other results to find the best match for the user's query.
97-
* Use the URL from the search results as a source link in your responses.
68+
* Provide the major Angular version in the 'version' parameter (obtained from 'frameworkVersion'
69+
in 'list_projects' or from 'ng version') to ensure version-aligned results.
70+
* Always check the 'searchedVersion' field in the output to confirm the exact documentation index that was queried.
71+
* For best results, provide a concise keyword query (e.g., "NgModule") rather than a natural language sentence.
9872
</Operational Notes>`,
9973
inputSchema: docSearchInputSchema.shape,
10074
outputSchema: {

packages/angular/cli/src/commands/mcp/tools/onpush-zoneless-migration/zoneless-migration.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,27 @@ export const ZONELESS_MIGRATION_TOOL = declareTool({
2525
title: 'Plan migration to OnPush and/or zoneless',
2626
description: `
2727
<Purpose>
28-
Analyzes Angular code and provides a step-by-step, iterative plan to migrate it to \`OnPush\`
29-
change detection, a prerequisite for a zoneless application. This tool identifies the next single
30-
most important action to take in the migration journey.
28+
Analyzes Angular code and provides a step-by-step, iterative plan to migrate it to 'OnPush'
29+
change detection (a prerequisite for zoneless applications).
3130
</Purpose>
3231
<Use Cases>
33-
* **Step-by-Step Migration:** Running the tool repeatedly to get the next instruction for a full
34-
migration to \`OnPush\`.
35-
* **Pre-Migration Analysis:** Checking a component or directory for unsupported \`NgZone\` APIs that
36-
would block a zoneless migration.
37-
* **Generating Component Migrations:** Getting the exact instructions for converting a single
38-
component from the default change detection strategy to \`OnPush\`.
32+
* Generating component-specific migrations from default change detection to OnPush.
33+
* Checking a component or directory for unsupported 'NgZone' APIs blocking a zoneless migration.
34+
* Iterative step-by-step guide for executing a complete zoneless migration.
3935
</Use Cases>
4036
<Operational Notes>
41-
* **Execution Model:** This tool **DOES NOT** modify code. It **PROVIDES INSTRUCTIONS** for a
42-
single action at a time. You **MUST** apply the changes it suggests, and then run the tool
43-
again to get the next step.
44-
* **Iterative Process:** The migration process is iterative. You must call this tool repeatedly,
45-
applying the suggested fix after each call, until the tool indicates that no more actions are
46-
needed.
47-
* **Relationship to other migrations:** This tool is the specialized starting point for the zoneless/OnPush
48-
migration. For other migrations (like signal inputs), you should run the corresponding schematics first,
49-
as the zoneless migration may depend on them as prerequisites.
50-
* **Input:** The tool can operate on either a single file or an entire directory. Provide the
51-
absolute path.
37+
* This tool is strictly read-only and does NOT modify code. It outputs EXACTLY ONE actionable step at a time.
38+
* You must apply the suggested code edit, verify it, and then call this tool again to receive the next step in the migration journey.
39+
* Run modernization schematics (e.g., Signal Inputs migrations) as prerequisites before starting this migration.
40+
* Supported inputs: Absolute path to a single component/test file, or a directory containing multiple files.
5241
</Operational Notes>`,
5342
isReadOnly: true,
5443
isLocalOnly: true,
5544
inputSchema: {
5645
fileOrDirPath: z
5746
.string()
5847
.describe(
59-
'The absolute path of the directory or file with the component(s), directive(s), or service(s) to migrate.' +
60-
' The contents are read with fs.readFileSync.',
48+
'Absolute path to the TypeScript file or directory containing components/directives to migrate.',
6149
),
6250
},
6351
factory:

packages/angular/cli/src/commands/mcp/tools/projects.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,28 +119,20 @@ export const LIST_PROJECTS_TOOL = declareTool({
119119
title: 'List Angular Projects',
120120
description: `
121121
<Purpose>
122-
Provides a comprehensive overview of all Angular workspaces and projects within the repository.
123-
It is essential to use this tool as a first step before performing any project-specific actions to understand the available projects,
124-
their types, and their locations.
122+
Provides a comprehensive overview of all Angular workspaces, projects, and configured targets within the repository.
123+
Always use this tool as a mandatory first step before performing any project-specific actions
124+
to understand the available projects and locations.
125125
</Purpose>
126126
<Use Cases>
127-
* Finding the correct project name to use in other commands (e.g., \`ng generate component my-comp --project=my-app\`).
128-
* Identifying the \`root\` and \`sourceRoot\` of a project to read, analyze, or modify its files.
129-
* Determining a project's unit test framework (\`unitTestFramework\`) before writing or modifying tests.
130-
* Identifying the project's style language (\`styleLanguage\`) to use the correct file extension (e.g., \`.scss\`).
131-
* Getting the \`selectorPrefix\` for a project before generating a new component to ensure it follows conventions.
132-
* Identifying the major version of the Angular framework for each workspace, which is crucial for monorepos.
133-
* Determining a project's primary function by inspecting its builder (e.g., '@angular-devkit/build-angular:browser' for an application).
127+
* Discovering project names, locations, builders, selector prefixes, and style languages before generating or building components.
128+
* Determining a project's unit test framework (Jasmine, Jest, or Vitest) before writing or modifying tests.
129+
* Identifying available execution targets (e.g., lint, e2e, serve, deploy) before attempting execution.
130+
* Disambiguating multiple workspaces in monorepos.
134131
</Use Cases>
135132
<Operational Notes>
136-
* **Working Directory:** Shell commands for a project (like \`ng generate\`) **MUST**
137-
be executed from the parent directory of the \`path\` field for the relevant workspace.
138-
* **Unit Testing:** The \`unitTestFramework\` field tells you which testing API to use (e.g., Jasmine, Jest).
139-
If the value is 'unknown', you **MUST** inspect the project's configuration files
140-
(e.g., \`karma.conf.js\`, \`jest.config.js\`, or the 'test' target in \`angular.json\`) to determine the
141-
framework before generating tests.
142-
* **Disambiguation:** A monorepo may contain multiple workspaces (e.g., for different applications or even in output directories).
143-
Use the \`path\` of each workspace to understand its context and choose the correct project.
133+
* Execute shell/CLI commands from the parent directory of the workspace's 'path' field.
134+
* If 'unitTestFramework' is 'unknown', inspect local config files (e.g., jest.config.js, karma.conf.js)
135+
or the 'test' target in 'angular.json' to determine the framework before creating tests.
144136
</Operational Notes>`,
145137
outputSchema: listProjectsOutputSchema,
146138
isReadOnly: true,

0 commit comments

Comments
 (0)