Skip to content
This repository was archived by the owner on Apr 16, 2026. It is now read-only.

Commit 7ffdc5e

Browse files
authored
feat: add take_memory_snapshot tool (ChromeDevTools#1023)
Refs: ChromeDevTools#406
1 parent 2cd9b95 commit 7ffdc5e

File tree

5 files changed

+92
-3
lines changed

5 files changed

+92
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,11 @@ If you run into any issues, checkout our [troubleshooting guide](./docs/troubles
433433
- **Emulation** (2 tools)
434434
- [`emulate`](docs/tool-reference.md#emulate)
435435
- [`resize_page`](docs/tool-reference.md#resize_page)
436-
- **Performance** (3 tools)
436+
- **Performance** (4 tools)
437437
- [`performance_analyze_insight`](docs/tool-reference.md#performance_analyze_insight)
438438
- [`performance_start_trace`](docs/tool-reference.md#performance_start_trace)
439439
- [`performance_stop_trace`](docs/tool-reference.md#performance_stop_trace)
440+
- [`take_memory_snapshot`](docs/tool-reference.md#take_memory_snapshot)
440441
- **Network** (2 tools)
441442
- [`get_network_request`](docs/tool-reference.md#get_network_request)
442443
- [`list_network_requests`](docs/tool-reference.md#list_network_requests)

docs/tool-reference.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<!-- AUTO GENERATED DO NOT EDIT - run 'npm run docs' to update-->
22

3-
# Chrome DevTools MCP Tool Reference (~6719 cl100k_base tokens)
3+
# Chrome DevTools MCP Tool Reference (~6885 cl100k_base tokens)
44

55
- **[Input automation](#input-automation)** (8 tools)
66
- [`click`](#click)
@@ -21,10 +21,11 @@
2121
- **[Emulation](#emulation)** (2 tools)
2222
- [`emulate`](#emulate)
2323
- [`resize_page`](#resize_page)
24-
- **[Performance](#performance)** (3 tools)
24+
- **[Performance](#performance)** (4 tools)
2525
- [`performance_analyze_insight`](#performance_analyze_insight)
2626
- [`performance_start_trace`](#performance_start_trace)
2727
- [`performance_stop_trace`](#performance_stop_trace)
28+
- [`take_memory_snapshot`](#take_memory_snapshot)
2829
- **[Network](#network)** (2 tools)
2930
- [`get_network_request`](#get_network_request)
3031
- [`list_network_requests`](#list_network_requests)
@@ -262,6 +263,16 @@
262263

263264
---
264265

266+
### `take_memory_snapshot`
267+
268+
**Description:** Capture a memory heapsnapshot of the currently selected page to memory leak debugging
269+
270+
**Parameters:**
271+
272+
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to save the heapsnapshot to.
273+
274+
---
275+
265276
## Network
266277

267278
### `get_network_request`

src/tools/memory.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import {zod} from '../third_party/index.js';
8+
9+
import {ToolCategory} from './categories.js';
10+
import {defineTool} from './ToolDefinition.js';
11+
12+
export const takeMemorySnapshot = defineTool({
13+
name: 'take_memory_snapshot',
14+
description: `Capture a memory heapsnapshot of the currently selected page to memory leak debugging`,
15+
annotations: {
16+
category: ToolCategory.PERFORMANCE,
17+
readOnlyHint: true,
18+
},
19+
schema: {
20+
filePath: zod
21+
.string()
22+
.describe('A path to a .heapsnapshot file to save the heapsnapshot to.')
23+
.endsWith('.heapsnapshot'),
24+
},
25+
handler: async (request, response, context) => {
26+
const page = context.getSelectedPage();
27+
28+
await page.captureHeapSnapshot({
29+
path: request.params.filePath,
30+
});
31+
32+
response.appendResponseLine(
33+
`Heap snapshot saved to ${request.params.filePath}`,
34+
);
35+
},
36+
});

src/tools/tools.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as consoleTools from './console.js';
88
import * as emulationTools from './emulation.js';
99
import * as extensionTools from './extensions.js';
1010
import * as inputTools from './input.js';
11+
import * as memoryTools from './memory.js';
1112
import * as networkTools from './network.js';
1213
import * as pagesTools from './pages.js';
1314
import * as performanceTools from './performance.js';
@@ -22,6 +23,7 @@ const tools = [
2223
...Object.values(emulationTools),
2324
...Object.values(extensionTools),
2425
...Object.values(inputTools),
26+
...Object.values(memoryTools),
2527
...Object.values(networkTools),
2628
...Object.values(pagesTools),
2729
...Object.values(performanceTools),

tests/tools/memory.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @license
3+
* Copyright 2026 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
import assert from 'node:assert';
8+
import {existsSync} from 'node:fs';
9+
import {rm} from 'node:fs/promises';
10+
import {tmpdir} from 'node:os';
11+
import {join} from 'node:path';
12+
import {describe, it} from 'node:test';
13+
14+
import {takeMemorySnapshot} from '../../src/tools/memory.js';
15+
import {withMcpContext} from '../utils.js';
16+
17+
describe('memory', () => {
18+
describe('take_memory_snapshot', () => {
19+
it('with default options', async () => {
20+
await withMcpContext(async (response, context) => {
21+
const filePath = join(tmpdir(), 'test-screenshot.heapsnapshot');
22+
try {
23+
await takeMemorySnapshot.handler(
24+
{params: {filePath}},
25+
response,
26+
context,
27+
);
28+
assert.equal(
29+
response.responseLines.at(0),
30+
`Heap snapshot saved to ${filePath}`,
31+
);
32+
assert.ok(existsSync(filePath));
33+
} finally {
34+
await rm(filePath, {force: true});
35+
}
36+
});
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)