|
| 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, VideoFormat} 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(format: VideoFormat): Promise<string> { |
| 19 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'chrome-devtools-mcp-')); |
| 20 | + return path.join(dir, `screencast.${format}`); |
| 21 | +} |
| 22 | + |
| 23 | +export const startScreencast = defineTool({ |
| 24 | + name: 'screencast_start', |
| 25 | + description: |
| 26 | + 'Starts recording a screencast (video) of the selected page. 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 | + 'The absolute file path, or a file path relative to the current working directory, to save the screencast to. For example, recording.webm. If not specified, a temporary file will be created.', |
| 38 | + ), |
| 39 | + format: zod |
| 40 | + .enum(['webm', 'mp4', 'gif']) |
| 41 | + .default('webm') |
| 42 | + .describe('Specifies the output file format. Default is "webm".'), |
| 43 | + quality: zod |
| 44 | + .number() |
| 45 | + .min(0) |
| 46 | + .max(63) |
| 47 | + .optional() |
| 48 | + .describe( |
| 49 | + 'Recording quality (CRF) between 0-63. Lower values mean better quality but larger files. Default is 30.', |
| 50 | + ), |
| 51 | + fps: zod |
| 52 | + .number() |
| 53 | + .optional() |
| 54 | + .describe('Frame rate in frames per second. Default is 30 (20 for GIF).'), |
| 55 | + scale: zod |
| 56 | + .number() |
| 57 | + .optional() |
| 58 | + .describe( |
| 59 | + 'Scales the output video. For example, 0.5 will halve the dimensions. Default is 1.', |
| 60 | + ), |
| 61 | + speed: zod |
| 62 | + .number() |
| 63 | + .optional() |
| 64 | + .describe( |
| 65 | + 'Playback speed multiplier. For example, 2 will double the speed. Default is 1.', |
| 66 | + ), |
| 67 | + }, |
| 68 | + handler: async (request, response, context) => { |
| 69 | + if (context.getScreenRecorder() !== null) { |
| 70 | + response.appendResponseLine( |
| 71 | + 'Error: a screencast recording is already in progress. Use screencast_stop to stop it before starting a new one.', |
| 72 | + ); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const format = request.params.format as VideoFormat; |
| 77 | + const filePath = |
| 78 | + request.params.filePath ?? (await generateTempFilePath(format)); |
| 79 | + const resolvedPath = path.resolve(filePath); |
| 80 | + |
| 81 | + const page = context.getSelectedPage(); |
| 82 | + |
| 83 | + let recorder: ScreenRecorder; |
| 84 | + try { |
| 85 | + recorder = await page.screencast({ |
| 86 | + path: resolvedPath as `${string}.${VideoFormat}`, |
| 87 | + format, |
| 88 | + quality: request.params.quality, |
| 89 | + fps: request.params.fps, |
| 90 | + scale: request.params.scale, |
| 91 | + speed: request.params.speed, |
| 92 | + }); |
| 93 | + } catch (err) { |
| 94 | + const message = err instanceof Error ? err.message : String(err); |
| 95 | + if (message.includes('ENOENT') && message.includes('ffmpeg')) { |
| 96 | + throw new Error( |
| 97 | + 'ffmpeg is required for screencast recording but was not found. ' + |
| 98 | + 'Install ffmpeg (https://ffmpeg.org/) and ensure it is available in your PATH.', |
| 99 | + ); |
| 100 | + } |
| 101 | + throw err; |
| 102 | + } |
| 103 | + |
| 104 | + context.setScreenRecorder({recorder, filePath: resolvedPath}); |
| 105 | + |
| 106 | + response.appendResponseLine( |
| 107 | + `Screencast recording started. The recording will be saved to ${resolvedPath}. Use screencast_stop to stop recording.`, |
| 108 | + ); |
| 109 | + }, |
| 110 | +}); |
| 111 | + |
| 112 | +export const stopScreencast = defineTool({ |
| 113 | + name: 'screencast_stop', |
| 114 | + description: 'Stops the active screencast recording on the selected page.', |
| 115 | + annotations: { |
| 116 | + category: ToolCategory.DEBUGGING, |
| 117 | + readOnlyHint: false, |
| 118 | + conditions: ['screencast'], |
| 119 | + }, |
| 120 | + schema: {}, |
| 121 | + handler: async (_request, response, context) => { |
| 122 | + await stopScreencastAndAppendOutput(response, context); |
| 123 | + }, |
| 124 | +}); |
| 125 | + |
| 126 | +async function stopScreencastAndAppendOutput( |
| 127 | + response: Response, |
| 128 | + context: Context, |
| 129 | +): Promise<void> { |
| 130 | + const data = context.getScreenRecorder(); |
| 131 | + if (!data) { |
| 132 | + return; |
| 133 | + } |
| 134 | + try { |
| 135 | + await data.recorder.stop(); |
| 136 | + response.appendResponseLine( |
| 137 | + `The screencast recording has been stopped and saved to ${data.filePath}.`, |
| 138 | + ); |
| 139 | + } finally { |
| 140 | + context.setScreenRecorder(null); |
| 141 | + } |
| 142 | +} |
0 commit comments