-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Add build/serve MCP tools #31667
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
amishne
wants to merge
3
commits into
angular:main
Choose a base branch
from
amishne:driver
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add build/serve MCP tools #31667
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /** | ||
| * @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 { ChildProcess } from 'child_process'; | ||
| import { Host } from './host'; | ||
|
|
||
| // Log messages that we want to catch to identify the build status. | ||
|
|
||
| const BUILD_SUCCEEDED_MESSAGE = 'Application bundle generation complete.'; | ||
| const BUILD_FAILED_MESSAGE = 'Application bundle generation failed.'; | ||
| const WAITING_FOR_CHANGES_MESSAGE = 'Watch mode enabled. Watching for file changes...'; | ||
| const CHANGES_DETECTED_START_MESSAGE = '❯ Changes detected. Rebuilding...'; | ||
| const CHANGES_DETECTED_SUCCESS_MESSAGE = '✔ Changes detected. Rebuilding...'; | ||
|
|
||
| const BUILD_START_MESSAGES = [CHANGES_DETECTED_START_MESSAGE]; | ||
| const BUILD_END_MESSAGES = [ | ||
| BUILD_SUCCEEDED_MESSAGE, | ||
| BUILD_FAILED_MESSAGE, | ||
| WAITING_FOR_CHANGES_MESSAGE, | ||
| CHANGES_DETECTED_SUCCESS_MESSAGE, | ||
| ]; | ||
|
|
||
| export type BuildStatus = 'success' | 'failure' | 'unknown'; | ||
|
|
||
| /** | ||
| * An Angular development server managed by the MCP server. | ||
| */ | ||
| export interface DevServer { | ||
| /** | ||
| * Launches the dev server and returns immediately. | ||
| * | ||
| * Throws if this server is already running. | ||
| */ | ||
| start(): void; | ||
|
|
||
| /** | ||
| * If the dev server is running, stops it. | ||
| */ | ||
| stop(): void; | ||
|
|
||
| /** | ||
| * Gets all the server logs so far (stdout + stderr). | ||
| */ | ||
| getServerLogs(): string[]; | ||
|
|
||
| /** | ||
| * Gets all the server logs from the latest build. | ||
| */ | ||
| getMostRecentBuild(): { status: BuildStatus; logs: string[] }; | ||
|
|
||
| /** | ||
| * Whether the dev server is currently being built, or is awaiting further changes. | ||
| */ | ||
| isBuilding(): boolean; | ||
|
|
||
| /** | ||
| * `ng serve` port to use. | ||
| */ | ||
| port: number; | ||
| } | ||
|
|
||
| export function devServerKey(project?: string) { | ||
| return project ?? '<default>'; | ||
| } | ||
|
|
||
| /** | ||
| * A local Angular development server managed by the MCP server. | ||
| */ | ||
| export class LocalDevServer implements DevServer { | ||
| readonly host: Host; | ||
| readonly port: number; | ||
| readonly project?: string; | ||
|
|
||
| private devServerProcess: ChildProcess | null = null; | ||
| private serverLogs: string[] = []; | ||
| private buildInProgress = false; | ||
| private latestBuildLogStartIndex?: number = undefined; | ||
| private latestBuildStatus: BuildStatus = 'unknown'; | ||
|
|
||
| constructor({ host, port, project }: { host: Host; port: number; project?: string }) { | ||
| this.host = host; | ||
| this.project = project; | ||
| this.port = port; | ||
| } | ||
|
|
||
| start() { | ||
| if (this.devServerProcess) { | ||
| throw Error('Dev server already started.'); | ||
| } | ||
|
|
||
| const args = ['serve']; | ||
| if (this.project) { | ||
| args.push(this.project); | ||
| } | ||
|
|
||
| args.push(`--port=${this.port}`); | ||
|
|
||
| this.devServerProcess = this.host.spawn('ng', args, { stdio: 'pipe' }); | ||
| this.devServerProcess.stdout?.on('data', (data) => { | ||
| this.addLog(data.toString()); | ||
| }); | ||
| this.devServerProcess.stderr?.on('data', (data) => { | ||
| this.addLog(data.toString()); | ||
| }); | ||
| this.devServerProcess.stderr?.on('close', () => { | ||
| this.stop(); | ||
| }); | ||
| this.buildInProgress = true; | ||
| } | ||
|
|
||
| private addLog(log: string) { | ||
| this.serverLogs.push(log); | ||
|
|
||
| if (BUILD_START_MESSAGES.some((message) => log.startsWith(message))) { | ||
| this.buildInProgress = true; | ||
| this.latestBuildLogStartIndex = this.serverLogs.length - 1; | ||
| } else if (BUILD_END_MESSAGES.some((message) => log.startsWith(message))) { | ||
| this.buildInProgress = false; | ||
| // We consider everything except a specific failure message to be a success. | ||
| this.latestBuildStatus = log.startsWith(BUILD_FAILED_MESSAGE) ? 'failure' : 'success'; | ||
| } | ||
| } | ||
|
|
||
| stop() { | ||
| this.devServerProcess?.kill(); | ||
| this.devServerProcess = null; | ||
| } | ||
|
|
||
| getServerLogs(): string[] { | ||
| return [...this.serverLogs]; | ||
| } | ||
|
|
||
| getMostRecentBuild() { | ||
| return { | ||
| status: this.latestBuildStatus, | ||
| logs: this.serverLogs.slice(this.latestBuildLogStartIndex), | ||
| }; | ||
| } | ||
|
|
||
| isBuilding() { | ||
| return this.buildInProgress; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/angular/cli/src/commands/mcp/testing/mock-host.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /** | ||
| * @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 { Host } from '../host'; | ||
|
|
||
| /** | ||
| * A mock implementation of the `Host` interface for testing purposes. | ||
| * This class allows spying on host methods and controlling their return values. | ||
| */ | ||
| export class MockHost implements Host { | ||
| runCommand = jasmine.createSpy('runCommand').and.resolveTo({ stdout: '', stderr: '' }); | ||
| stat = jasmine.createSpy('stat'); | ||
| existsSync = jasmine.createSpy('existsSync'); | ||
| spawn = jasmine.createSpy('spawn'); | ||
| getAvailablePort = jasmine.createSpy('getAvailablePort'); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider: Similar to my point on
ng build, would there be value in changing the output ofng serve(possibly behind an environment variable) to make it easier to consume? We probably don't want to tweak the general output, but maybe if we added something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed there as well. I like the environment variable approach, but I prefer to do that in a follow-up