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

Commit f8f2e26

Browse files
committed
feat: add take_memory_snapshot tool
1 parent c402b43 commit f8f2e26

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
@@ -440,10 +440,11 @@ If you run into any issues, checkout our [troubleshooting guide](./docs/troubles
440440
- **Network** (2 tools)
441441
- [`get_network_request`](docs/tool-reference.md#get_network_request)
442442
- [`list_network_requests`](docs/tool-reference.md#list_network_requests)
443-
- **Debugging** (5 tools)
443+
- **Debugging** (6 tools)
444444
- [`evaluate_script`](docs/tool-reference.md#evaluate_script)
445445
- [`get_console_message`](docs/tool-reference.md#get_console_message)
446446
- [`list_console_messages`](docs/tool-reference.md#list_console_messages)
447+
- [`take_memory_snapshot`](docs/tool-reference.md#take_memory_snapshot)
447448
- [`take_screenshot`](docs/tool-reference.md#take_screenshot)
448449
- [`take_snapshot`](docs/tool-reference.md#take_snapshot)
449450

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 (~6889 cl100k_base tokens)
44

55
- **[Input automation](#input-automation)** (8 tools)
66
- [`click`](#click)
@@ -28,10 +28,11 @@
2828
- **[Network](#network)** (2 tools)
2929
- [`get_network_request`](#get_network_request)
3030
- [`list_network_requests`](#list_network_requests)
31-
- **[Debugging](#debugging)** (5 tools)
31+
- **[Debugging](#debugging)** (6 tools)
3232
- [`evaluate_script`](#evaluate_script)
3333
- [`get_console_message`](#get_console_message)
3434
- [`list_console_messages`](#list_console_messages)
35+
- [`take_memory_snapshot`](#take_memory_snapshot)
3536
- [`take_screenshot`](#take_screenshot)
3637
- [`take_snapshot`](#take_snapshot)
3738

@@ -335,6 +336,16 @@ so returned values have to be JSON-serializable.
335336

336337
---
337338

339+
### `take_memory_snapshot`
340+
341+
**Description:** Capture a memory heapsnapshot of the currently seelcted page to memory leak debuggin
342+
343+
**Parameters:**
344+
345+
- **filePath** (string) **(required)**: A path to a .heapsnapshot file to save the heapsnapshot to.
346+
347+
---
348+
338349
### `take_screenshot`
339350

340351
**Description:** Take a screenshot of the page or element.

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 seelcted page to memory leak debuggin`,
15+
annotations: {
16+
category: ToolCategory.DEBUGGING,
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)