豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/McpContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
ElementHandle,
HTTPRequest,
Page,
ScreenRecorder,
SerializedAXNode,
PredefinedNetworkConditions,
Viewport,
Expand Down Expand Up @@ -121,6 +122,8 @@ export class McpContext implements Context {
#extensionRegistry = new ExtensionRegistry();

#isRunningTrace = false;
#screenRecorderData: {recorder: ScreenRecorder; filePath: string} | null =
null;
#networkConditionsMap = new WeakMap<Page, string>();
#cpuThrottlingRateMap = new WeakMap<Page, number>();
#geolocationMap = new WeakMap<Page, GeolocationOptions>();
Expand Down Expand Up @@ -372,6 +375,16 @@ export class McpContext implements Context {
return this.#isRunningTrace;
}

getScreenRecorder(): {recorder: ScreenRecorder; filePath: string} | null {
return this.#screenRecorderData;
}

setScreenRecorder(
data: {recorder: ScreenRecorder; filePath: string} | null,
): void {
this.#screenRecorderData = data;
}

isCruxEnabled(): boolean {
return this.#options.performanceCrux;
}
Expand Down
5 changes: 5 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ export const cliOptions = {
describe: 'Whether to enable interoperability tools',
hidden: true,
},
experimentalScreencast: {
type: 'boolean',
describe: 'Whether to enable screencast tools',
Comment thread
OrKoN marked this conversation as resolved.
Outdated
hidden: true,
Comment thread
OrKoN marked this conversation as resolved.
Outdated
},
chromeArg: {
type: 'array',
describe:
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ function registerTool(tool: ToolDefinition): void {
) {
return;
}
if (
tool.annotations.conditions?.includes('screencast') &&
!args.experimentalScreencast
) {
return;
}
server.registerTool(
tool.name,
{
Expand Down
5 changes: 5 additions & 0 deletions src/tools/ToolDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
Dialog,
ElementHandle,
Page,
ScreenRecorder,
Viewport,
} from '../third_party/index.js';
import type {InsightName, TraceResult} from '../trace-processing/parse.js';
Expand Down Expand Up @@ -146,6 +147,10 @@ export type Context = Readonly<{
* Returns a reqid for a cdpRequestId.
*/
resolveCdpElementId(cdpBackendNodeId: number): string | undefined;
getScreenRecorder(): {recorder: ScreenRecorder; filePath: string} | null;
setScreenRecorder(
data: {recorder: ScreenRecorder; filePath: string} | null,
): void;
installExtension(path: string): Promise<string>;
uninstallExtension(id: string): Promise<void>;
listExtensions(): InstalledExtension[];
Expand Down
108 changes: 108 additions & 0 deletions src/tools/screencast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';

import {zod} from '../third_party/index.js';
import type {ScreenRecorder} from '../third_party/index.js';

import {ToolCategory} from './categories.js';
import type {Context, Response} from './ToolDefinition.js';
import {defineTool} from './ToolDefinition.js';

async function generateTempFilePath(): Promise<string> {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'chrome-devtools-mcp-'));
return path.join(dir, `screencast.mp4`);
}

export const startScreencast = defineTool({
name: 'screencast_start',
description:
'Starts recording a screencast (video) of the selected page in mp4 format. Requires ffmpeg to be installed on the system.',
Comment thread
OrKoN marked this conversation as resolved.
Outdated
annotations: {
category: ToolCategory.DEBUGGING,
readOnlyHint: false,
conditions: ['screencast'],
},
schema: {
path: zod
.string()
.optional()
.describe(
'Output path. Uses mkdtemp to generate a unique path if not provided.',
),
},
handler: async (request, response, context) => {
if (context.getScreenRecorder() !== null) {
response.appendResponseLine(
'Error: a screencast recording is already in progress. Use screencast_stop to stop it before starting a new one.',
);
return;
}

const filePath = request.params.path ?? (await generateTempFilePath());
const resolvedPath = path.resolve(filePath);

const page = context.getSelectedPage();

let recorder: ScreenRecorder;
try {
recorder = await page.screencast({
path: resolvedPath as `${string}.mp4`,
format: 'mp4' as const,
});
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
if (message.includes('ENOENT') && message.includes('ffmpeg')) {
throw new Error(
'ffmpeg is required for screencast recording but was not found. ' +
'Install ffmpeg (https://ffmpeg.org/) and ensure it is available in your PATH.',
);
}
throw err;
}

context.setScreenRecorder({recorder, filePath: resolvedPath});

response.appendResponseLine(
`Screencast recording started. The recording will be saved to ${resolvedPath}. Use screencast_stop to stop recording.`,
Comment thread
OrKoN marked this conversation as resolved.
Outdated
);
},
});

export const stopScreencast = defineTool({
name: 'screencast_stop',
description: 'Stops the active screencast recording on the selected page.',
annotations: {
category: ToolCategory.DEBUGGING,
readOnlyHint: false,
conditions: ['screencast'],
},
schema: {},
handler: async (_request, response, context) => {
await stopScreencastAndAppendOutput(response, context);
},
});

async function stopScreencastAndAppendOutput(
response: Response,
context: Context,
): Promise<void> {
const data = context.getScreenRecorder();
if (!data) {
return;
}
try {
await data.recorder.stop();
response.appendResponseLine(
`The screencast recording has been stopped and saved to ${data.filePath}.`,
);
} finally {
context.setScreenRecorder(null);
}
}
2 changes: 2 additions & 0 deletions src/tools/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as inputTools from './input.js';
import * as networkTools from './network.js';
import * as pagesTools from './pages.js';
import * as performanceTools from './performance.js';
import * as screencastTools from './screencast.js';
import * as screenshotTools from './screenshot.js';
import * as scriptTools from './script.js';
import * as snapshotTools from './snapshot.js';
Expand All @@ -24,6 +25,7 @@ const tools = [
...Object.values(networkTools),
...Object.values(pagesTools),
...Object.values(performanceTools),
...Object.values(screencastTools),
...Object.values(screenshotTools),
...Object.values(scriptTools),
...Object.values(snapshotTools),
Expand Down
Loading
Loading