|
| 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 { ChildProcess, spawn } from 'child_process'; |
| 10 | +import { z } from 'zod'; |
| 11 | +import { McpToolContext, declareTool } from './tool-registry'; |
| 12 | + |
| 13 | +let devServerProcess: ChildProcess | null = null; |
| 14 | +const serverLogs: string[] = []; |
| 15 | + |
| 16 | +const serveToolInputSchema = z.object({ |
| 17 | + command: z.enum(['start_devserver', 'stop_devserver']).describe('The subcommand to execute.'), |
| 18 | + project: z |
| 19 | + .string() |
| 20 | + .optional() |
| 21 | + .describe( |
| 22 | + 'Which project to serve in a monorepo context. If not provided, serves the top-level project.', |
| 23 | + ), |
| 24 | + configuration: z |
| 25 | + .string() |
| 26 | + .optional() |
| 27 | + .default('development') |
| 28 | + .describe('Which build configuration to use. Defaults to "development".'), |
| 29 | +}); |
| 30 | + |
| 31 | +export type ServeToolInput = z.infer<typeof serveToolInputSchema>; |
| 32 | + |
| 33 | +const serveToolOutputSchema = z.object({ |
| 34 | + message: z.string().describe('A message indicating the result of the operation.'), |
| 35 | + logs: z.array(z.string()).optional().describe('The logs from the dev server.'), |
| 36 | +}); |
| 37 | + |
| 38 | +export type ServeToolOutput = z.infer<typeof serveToolOutputSchema>; |
| 39 | + |
| 40 | +function serveToolFactory(context: McpToolContext) { |
| 41 | + return (input: ServeToolInput) => { |
| 42 | + switch (input.command) { |
| 43 | + case 'start_devserver': |
| 44 | + if (devServerProcess) { |
| 45 | + return { |
| 46 | + structuredContent: { |
| 47 | + message: 'Development server is already running.', |
| 48 | + }, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + const args = ['serve']; |
| 53 | + if (input.project) { |
| 54 | + args.push(input.project); |
| 55 | + } |
| 56 | + if (input.configuration) { |
| 57 | + args.push(`--configuration=${input.configuration}`); |
| 58 | + } |
| 59 | + |
| 60 | + serverLogs.length = 0; // Clear previous logs |
| 61 | + devServerProcess = spawn('ng', args, { stdio: 'pipe' }); |
| 62 | + |
| 63 | + devServerProcess.stdout?.on('data', (data) => { |
| 64 | + serverLogs.push(data.toString()); |
| 65 | + }); |
| 66 | + devServerProcess.stderr?.on('data', (data) => { |
| 67 | + serverLogs.push(data.toString()); |
| 68 | + }); |
| 69 | + |
| 70 | + devServerProcess.on('close', () => { |
| 71 | + devServerProcess = null; |
| 72 | + }); |
| 73 | + |
| 74 | + return { |
| 75 | + structuredContent: { |
| 76 | + message: 'Development server started.', |
| 77 | + }, |
| 78 | + }; |
| 79 | + |
| 80 | + case 'stop_devserver': |
| 81 | + if (!devServerProcess) { |
| 82 | + return { |
| 83 | + structuredContent: { |
| 84 | + message: 'Development server is not running.', |
| 85 | + }, |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + devServerProcess.kill('SIGTERM'); |
| 90 | + devServerProcess = null; |
| 91 | + |
| 92 | + return { |
| 93 | + structuredContent: { |
| 94 | + message: 'Development server stopped.', |
| 95 | + logs: serverLogs, |
| 96 | + }, |
| 97 | + }; |
| 98 | + } |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +export const SERVE_TOOL = declareTool({ |
| 103 | + name: 'serve', |
| 104 | + title: 'Serve Tool', |
| 105 | + description: ` |
| 106 | +<Purpose> |
| 107 | +Manages the Angular development server ("ng serve"). It allows you to start and stop the server as a background process. |
| 108 | +</Purpose> |
| 109 | +<Use Cases> |
| 110 | +* **Starting the Server:** Use the 'start_devserver' command to begin serving the application. The tool will return immediately while the server runs in the background. |
| 111 | +* **Stopping the Server:** Use the 'stop_devserver' command to terminate the running development server and retrieve the logs. |
| 112 | +</Use Cases> |
| 113 | +<Operational Notes> |
| 114 | +* This tool manages a single, shared development server instance. |
| 115 | +* 'start_devserver' is asynchronous. Subsequent commands can be run while the server is active. |
| 116 | +* 'stop_devserver' should be called to gracefully shut down the server and access the full log output. |
| 117 | +</Operational Notes> |
| 118 | +`, |
| 119 | + isReadOnly: false, |
| 120 | + isLocalOnly: true, |
| 121 | + inputSchema: serveToolInputSchema.shape, |
| 122 | + outputSchema: serveToolOutputSchema.shape, |
| 123 | + factory: serveToolFactory, |
| 124 | +}); |
0 commit comments