|
| 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 { z } from 'zod'; |
| 10 | +import { createDevServerNotFoundError, getDevserverKey } from '../../devserver'; |
| 11 | +import { workspaceAndProjectOptions } from '../../shared-options'; |
| 12 | +import { createStructuredContentOutput } from '../../utils'; |
| 13 | +import { resolveWorkspaceAndProject } from '../../workspace-utils'; |
| 14 | +import { type McpToolContext, type McpToolDeclaration, declareTool } from '../tool-registry'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Maximum time to wait for the build logs of a completed rebuild to be received from the |
| 18 | + * dev server process output. |
| 19 | + */ |
| 20 | +const LOG_FLUSH_TIMEOUT = 2_000; // In milliseconds |
| 21 | + |
| 22 | +const LOG_FLUSH_INTERVAL = 50; // In milliseconds |
| 23 | + |
| 24 | +const devserverRebuildToolInputSchema = z.object({ |
| 25 | + ...workspaceAndProjectOptions, |
| 26 | +}); |
| 27 | + |
| 28 | +export type DevserverRebuildToolInput = z.infer<typeof devserverRebuildToolInputSchema>; |
| 29 | + |
| 30 | +const devserverRebuildToolOutputSchema = z.object({ |
| 31 | + status: z |
| 32 | + .enum(['success', 'failure', 'no-changes', 'cancelled']) |
| 33 | + .describe( |
| 34 | + 'The outcome of the rebuild. "no-changes" means no file changes were pending so no rebuild was performed. ' + |
| 35 | + '"cancelled" means the dev server stopped before the rebuild could complete.', |
| 36 | + ), |
| 37 | + logs: z |
| 38 | + .array(z.string()) |
| 39 | + .optional() |
| 40 | + .describe('The logs from the most recent build, if a rebuild was performed.'), |
| 41 | +}); |
| 42 | + |
| 43 | +export type DevserverRebuildToolOutput = z.infer<typeof devserverRebuildToolOutputSchema>; |
| 44 | + |
| 45 | +function wait(ms: number): Promise<void> { |
| 46 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 47 | +} |
| 48 | + |
| 49 | +export async function rebuildDevserver(input: DevserverRebuildToolInput, context: McpToolContext) { |
| 50 | + const { workspacePath, projectName } = await resolveWorkspaceAndProject({ |
| 51 | + host: context.host, |
| 52 | + server: context.server, |
| 53 | + workspacePathInput: input.workspace, |
| 54 | + projectNameInput: input.project, |
| 55 | + mcpWorkspace: context.workspace, |
| 56 | + }); |
| 57 | + const key = getDevserverKey(workspacePath, projectName); |
| 58 | + const devserver = context.devservers.get(key); |
| 59 | + |
| 60 | + if (!devserver) { |
| 61 | + throw createDevServerNotFoundError(context.devservers); |
| 62 | + } |
| 63 | + |
| 64 | + if (!devserver.manualRebuild) { |
| 65 | + throw new Error( |
| 66 | + `Development server for project '${projectName}' was not started in manual rebuild mode and ` + |
| 67 | + 'rebuilds automatically on file changes. Use "devserver_wait_for_build" instead, or restart it ' + |
| 68 | + 'with "devserver_start" and the "manualRebuild" option.', |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + const status = await devserver.requestRebuild(); |
| 73 | + if (status !== 'success' && status !== 'failure') { |
| 74 | + return createStructuredContentOutput<DevserverRebuildToolOutput>({ status, logs: undefined }); |
| 75 | + } |
| 76 | + |
| 77 | + // The rebuild has completed but its log output may still be in transit from the process. |
| 78 | + const deadline = Date.now() + LOG_FLUSH_TIMEOUT; |
| 79 | + while (devserver.isBuilding() && Date.now() < deadline) { |
| 80 | + await wait(LOG_FLUSH_INTERVAL); |
| 81 | + } |
| 82 | + |
| 83 | + return createStructuredContentOutput<DevserverRebuildToolOutput>({ |
| 84 | + status, |
| 85 | + logs: devserver.getMostRecentBuild().logs, |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +export const DEVSERVER_REBUILD_TOOL: McpToolDeclaration< |
| 90 | + typeof devserverRebuildToolInputSchema.shape, |
| 91 | + typeof devserverRebuildToolOutputSchema.shape |
| 92 | +> = declareTool({ |
| 93 | + name: 'devserver_rebuild', |
| 94 | + title: 'Rebuild Devserver', |
| 95 | + description: ` |
| 96 | +<Purpose> |
| 97 | +Triggers an incremental rebuild of a dev server that was started with the "devserver_start" tool in manual rebuild mode |
| 98 | +("manualRebuild" option), waits for it to complete, and reports the build status and logs. |
| 99 | +</Purpose> |
| 100 | +<Use Cases> |
| 101 | +* **Applying a batch of changes:** After finishing a set of file edits (possibly across many files), call this tool once to |
| 102 | + rebuild all of the changes together. This avoids repeated intermediate rebuilds while files are still being edited. |
| 103 | +* **Checking a change compiles:** The returned status and logs indicate whether the rebuild succeeded and contain any |
| 104 | + build errors to fix. |
| 105 | +</Use Cases> |
| 106 | +<Operational Notes> |
| 107 | +* This tool only works for dev servers started with "devserver_start" and "manualRebuild" set to true. Dev servers started |
| 108 | + without it rebuild automatically; use "devserver_wait_for_build" for those instead. |
| 109 | +* The dev server tracks file changes continuously, so only the files changed since the previous rebuild are recompiled. |
| 110 | +* If no files changed since the previous rebuild, the tool returns the "no-changes" status without rebuilding. |
| 111 | +</Operational Notes> |
| 112 | +`, |
| 113 | + isReadOnly: false, |
| 114 | + isLocalOnly: true, |
| 115 | + inputSchema: devserverRebuildToolInputSchema.shape, |
| 116 | + outputSchema: devserverRebuildToolOutputSchema.shape, |
| 117 | + factory: (context) => (input) => { |
| 118 | + return rebuildDevserver(input, context); |
| 119 | + }, |
| 120 | +}); |
0 commit comments