diff --git a/README.md b/README.md index b94217b2b..1853ed754 100644 --- a/README.md +++ b/README.md @@ -433,10 +433,11 @@ If you run into any issues, checkout our [troubleshooting guide](./docs/troubles - **Emulation** (2 tools) - [`emulate`](docs/tool-reference.md#emulate) - [`resize_page`](docs/tool-reference.md#resize_page) -- **Performance** (3 tools) +- **Performance** (4 tools) - [`performance_analyze_insight`](docs/tool-reference.md#performance_analyze_insight) - [`performance_start_trace`](docs/tool-reference.md#performance_start_trace) - [`performance_stop_trace`](docs/tool-reference.md#performance_stop_trace) + - [`take_memory_snapshot`](docs/tool-reference.md#take_memory_snapshot) - **Network** (2 tools) - [`get_network_request`](docs/tool-reference.md#get_network_request) - [`list_network_requests`](docs/tool-reference.md#list_network_requests) diff --git a/docs/tool-reference.md b/docs/tool-reference.md index 52c1e9c6c..776859acf 100644 --- a/docs/tool-reference.md +++ b/docs/tool-reference.md @@ -1,6 +1,6 @@ -# Chrome DevTools MCP Tool Reference (~6719 cl100k_base tokens) +# Chrome DevTools MCP Tool Reference (~6885 cl100k_base tokens) - **[Input automation](#input-automation)** (8 tools) - [`click`](#click) @@ -21,10 +21,11 @@ - **[Emulation](#emulation)** (2 tools) - [`emulate`](#emulate) - [`resize_page`](#resize_page) -- **[Performance](#performance)** (3 tools) +- **[Performance](#performance)** (4 tools) - [`performance_analyze_insight`](#performance_analyze_insight) - [`performance_start_trace`](#performance_start_trace) - [`performance_stop_trace`](#performance_stop_trace) + - [`take_memory_snapshot`](#take_memory_snapshot) - **[Network](#network)** (2 tools) - [`get_network_request`](#get_network_request) - [`list_network_requests`](#list_network_requests) @@ -262,6 +263,16 @@ --- +### `take_memory_snapshot` + +**Description:** Capture a memory heapsnapshot of the currently selected page to memory leak debugging + +**Parameters:** + +- **filePath** (string) **(required)**: A path to a .heapsnapshot file to save the heapsnapshot to. + +--- + ## Network ### `get_network_request` diff --git a/src/tools/memory.ts b/src/tools/memory.ts new file mode 100644 index 000000000..9c13be230 --- /dev/null +++ b/src/tools/memory.ts @@ -0,0 +1,36 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import {zod} from '../third_party/index.js'; + +import {ToolCategory} from './categories.js'; +import {defineTool} from './ToolDefinition.js'; + +export const takeMemorySnapshot = defineTool({ + name: 'take_memory_snapshot', + description: `Capture a memory heapsnapshot of the currently selected page to memory leak debugging`, + annotations: { + category: ToolCategory.PERFORMANCE, + readOnlyHint: true, + }, + schema: { + filePath: zod + .string() + .describe('A path to a .heapsnapshot file to save the heapsnapshot to.') + .endsWith('.heapsnapshot'), + }, + handler: async (request, response, context) => { + const page = context.getSelectedPage(); + + await page.captureHeapSnapshot({ + path: request.params.filePath, + }); + + response.appendResponseLine( + `Heap snapshot saved to ${request.params.filePath}`, + ); + }, +}); diff --git a/src/tools/tools.ts b/src/tools/tools.ts index 2aed860f4..96bb033a3 100644 --- a/src/tools/tools.ts +++ b/src/tools/tools.ts @@ -8,6 +8,7 @@ import * as consoleTools from './console.js'; import * as emulationTools from './emulation.js'; import * as extensionTools from './extensions.js'; import * as inputTools from './input.js'; +import * as memoryTools from './memory.js'; import * as networkTools from './network.js'; import * as pagesTools from './pages.js'; import * as performanceTools from './performance.js'; @@ -22,6 +23,7 @@ const tools = [ ...Object.values(emulationTools), ...Object.values(extensionTools), ...Object.values(inputTools), + ...Object.values(memoryTools), ...Object.values(networkTools), ...Object.values(pagesTools), ...Object.values(performanceTools), diff --git a/tests/tools/memory.test.ts b/tests/tools/memory.test.ts new file mode 100644 index 000000000..16fc0876e --- /dev/null +++ b/tests/tools/memory.test.ts @@ -0,0 +1,39 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import assert from 'node:assert'; +import {existsSync} from 'node:fs'; +import {rm} from 'node:fs/promises'; +import {tmpdir} from 'node:os'; +import {join} from 'node:path'; +import {describe, it} from 'node:test'; + +import {takeMemorySnapshot} from '../../src/tools/memory.js'; +import {withMcpContext} from '../utils.js'; + +describe('memory', () => { + describe('take_memory_snapshot', () => { + it('with default options', async () => { + await withMcpContext(async (response, context) => { + const filePath = join(tmpdir(), 'test-screenshot.heapsnapshot'); + try { + await takeMemorySnapshot.handler( + {params: {filePath}}, + response, + context, + ); + assert.equal( + response.responseLines.at(0), + `Heap snapshot saved to ${filePath}`, + ); + assert.ok(existsSync(filePath)); + } finally { + await rm(filePath, {force: true}); + } + }); + }); + }); +});