|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import fs from 'node:fs/promises'; |
| 8 | +import os from 'node:os'; |
| 9 | +import path from 'node:path'; |
| 10 | + |
| 11 | +import {zod} from '../third_party/index.js'; |
| 12 | +import type {ScreenRecorder} from '../third_party/index.js'; |
| 13 | + |
| 14 | +import {ToolCategory} from './categories.js'; |
| 15 | +import type {Context, Response} from './ToolDefinition.js'; |
| 16 | +import {defineTool} from './ToolDefinition.js'; |
| 17 | + |
| 18 | +async function generateTempFilePath(): Promise<string> { |
| 19 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'chrome-devtools-mcp-')); |
| 20 | + return path.join(dir, `screencast.mp4`); |
| 21 | +} |
| 22 | + |
| 23 | +export const startScreencast = defineTool({ |
| 24 | + name: 'screencast_start', |
| 25 | + description: |
| 26 | + 'Starts recording a screencast (video) of the selected page in mp4 format. Requires ffmpeg to be installed on the system.', |
| 27 | + annotations: { |
| 28 | + category: ToolCategory.DEBUGGING, |
| 29 | + readOnlyHint: false, |
| 30 | + conditions: ['screencast'], |
| 31 | + }, |
| 32 | + schema: { |
| 33 | + filePath: zod |
| 34 | + .string() |
| 35 | + .optional() |
| 36 | + .describe( |
| 37 | + 'Output path. Uses mkdtemp to generate a unique path if not provided.', |
| 38 | + ), |
| 39 | + }, |
| 40 | + handler: async (request, response, context) => { |
| 41 | + if (context.getScreenRecorder() !== null) { |
| 42 | + response.appendResponseLine( |
| 43 | + 'Error: a screencast recording is already in progress. Use screencast_stop to stop it before starting a new one.', |
| 44 | + ); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + const filePath = |
| 49 | + request.params.filePath ?? (await generateTempFilePath()); |
| 50 | + const resolvedPath = path.resolve(filePath); |
| 51 | + |
| 52 | + const page = context.getSelectedPage(); |
| 53 | + |
| 54 | + let recorder: ScreenRecorder; |
| 55 | + try { |
| 56 | + recorder = await page.screencast({ |
| 57 | + path: resolvedPath as `${string}.mp4`, |
| 58 | + format: 'mp4' as const, |
| 59 | + }); |
| 60 | + } catch (err) { |
| 61 | + const message = err instanceof Error ? err.message : String(err); |
| 62 | + if (message.includes('ENOENT') && message.includes('ffmpeg')) { |
| 63 | + throw new Error( |
| 64 | + 'ffmpeg is required for screencast recording but was not found. ' + |
| 65 | + 'Install ffmpeg (https://ffmpeg.org/) and ensure it is available in your PATH.', |
| 66 | + ); |
| 67 | + } |
| 68 | + throw err; |
| 69 | + } |
| 70 | + |
| 71 | + context.setScreenRecorder({recorder, filePath: resolvedPath}); |
| 72 | + |
| 73 | + response.appendResponseLine( |
| 74 | + `Screencast recording started. The recording will be saved to ${resolvedPath}. Use screencast_stop to stop recording.`, |
| 75 | + ); |
| 76 | + }, |
| 77 | +}); |
| 78 | + |
| 79 | +export const stopScreencast = defineTool({ |
| 80 | + name: 'screencast_stop', |
| 81 | + description: 'Stops the active screencast recording on the selected page.', |
| 82 | + annotations: { |
| 83 | + category: ToolCategory.DEBUGGING, |
| 84 | + readOnlyHint: false, |
| 85 | + conditions: ['screencast'], |
| 86 | + }, |
| 87 | + schema: {}, |
| 88 | + handler: async (_request, response, context) => { |
| 89 | + await stopScreencastAndAppendOutput(response, context); |
| 90 | + }, |
| 91 | +}); |
| 92 | + |
| 93 | +async function stopScreencastAndAppendOutput( |
| 94 | + response: Response, |
| 95 | + context: Context, |
| 96 | +): Promise<void> { |
| 97 | + const data = context.getScreenRecorder(); |
| 98 | + if (!data) { |
| 99 | + return; |
| 100 | + } |
| 101 | + try { |
| 102 | + await data.recorder.stop(); |
| 103 | + response.appendResponseLine( |
| 104 | + `The screencast recording has been stopped and saved to ${data.filePath}.`, |
| 105 | + ); |
| 106 | + } finally { |
| 107 | + context.setScreenRecorder(null); |
| 108 | + } |
| 109 | +} |
0 commit comments