Skip to content

Commit e2f3142

Browse files
committed
fix: update CLI to use generated definitions and bundle dependencies for distribution
1 parent 8dd62bd commit e2f3142

9 files changed

Lines changed: 749 additions & 1372 deletions

docs/tool-reference.md

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

3-
# Chrome DevTools MCP Tool Reference (~6940 cl100k_base tokens)
3+
# Chrome DevTools MCP Tool Reference (~6975 cl100k_base tokens)
44

55
- **[Input automation](#input-automation)** (9 tools)
66
- [`click`](#click)
@@ -393,6 +393,7 @@ in the DevTools Elements panel (if any).
393393

394394
**Parameters:**
395395

396+
- **diff** (boolean) _(optional)_: Return only the changes since the last snapshot. The cache is reset on navigation.
396397
- **filePath** (string) _(optional)_: The absolute path, or a path relative to the current working directory, to save the snapshot to instead of attaching it to the response.
397398
- **verbose** (boolean) _(optional)_: Whether to include all possible information available in the full a11y tree. Default is false.
398399

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"typecheck": "tsc --noEmit",
1717
"format": "eslint --cache --fix . && prettier --write --cache .",
1818
"check-format": "eslint --cache . && prettier --check --cache .;",
19-
"gen": "npm run build && npm run docs:generate && npm run cli:generate && npm run format",
19+
"gen": "npm run bundle && npm run docs:generate && npm run cli:generate && npm run format",
2020
"docs:generate": "node --experimental-strip-types scripts/generate-docs.ts",
2121
"start": "npm run build && node build/src/index.js",
2222
"start-debug": "DEBUG=mcp:* DEBUG_COLORS=false npm run build && node build/src/index.js",

scripts/post-build.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ function copySnapshotFiles() {
108108
const files = fs.readdirSync(testsDir);
109109
for (const file of files) {
110110
if (file.endsWith('.snapshot')) {
111-
fs.copyFileSync(path.join(testsDir, file), path.join(buildTestsDir, file));
111+
fs.copyFileSync(
112+
path.join(testsDir, file),
113+
path.join(buildTestsDir, file),
114+
);
112115
}
113116
}
114117

snapshot_improvement_plan.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Implementation Plan: Chrome DevTools CLI & MCP Optimizations
2+
3+
This plan implements optimizations for the `take_snapshot` tool to improve agent efficiency by reducing the volume of DOM data transmitted. It ensures full compatibility with both the Model Context Protocol (MCP) and the `chrome-devtools` CLI.
4+
5+
## Progress Overview
6+
7+
- [x] **Step 0: Save Plan to Project**
8+
- [x] Save this plan to `/Users/hablich/src/internal/chrome-devtools-mcp/snapshot_improvement_plan.md`
9+
- [x] **Step 0: Save Plan to Project**
10+
- [x] **PR 1: Native Semantic Filtering** (`feat/semantic-filtering`)
11+
- [x] Create branch and apply filtering changes
12+
- [x] Include `post-build.ts` fix (on `main`)
13+
- [x] Verify tests and CLI
14+
- [x] Commit
15+
- [x] **PR 2: Built-in "Interactive Only" Mode** (`feat/interactive-mode`)
16+
- [x] Create branch and apply interactive mode changes
17+
- [x] Include `post-build.ts` fix
18+
- [x] Verify tests and CLI
19+
- [x] Commit
20+
- [x] **PR 3: Session-Based Snapshot Diffs** (`feat/snapshot-diffs`)
21+
- [x] Create branch and apply diffing + UID stability changes
22+
- [x] Include `post-build.ts` fix
23+
- [x] Verify tests and CLI
24+
- [x] Commit
25+
- [x] **Verification**
26+
- [x] Check if each branch works independently from each other.
27+
28+
---
29+
30+
## PR 1: Native Semantic Filtering
31+
32+
**Objective**: Enable agents to request only specific types of elements from the accessibility tree.
33+
34+
- **Flags**: `role`, `name`, `text`
35+
- **Tasks**:
36+
- [ ] Update `SnapshotParams` in `src/tools/ToolDefinition.ts`.
37+
- [ ] Update `take_snapshot` tool schema in `src/tools/snapshot.ts`.
38+
- [ ] Implement `filterTree` in `McpContext.ts` to prune nodes that do not match and do not have matching descendants.
39+
- [ ] Update `createTextSnapshot` to use `filterTree`.
40+
- [ ] Add tests to `tests/McpContext.test.ts` verifying `role`, `name`, and `text` filters.
41+
42+
## PR 2: Built-in "Interactive Only" Mode
43+
44+
**Objective**: Strip non-actionable content from snapshots.
45+
46+
- **Flag**: `interactive`
47+
- **Tasks**:
48+
- [ ] Define "interactive" roles: `button`, `link`, `menuitem`, `checkbox`, `radio`, `textbox`, `searchbox`, `combobox`.
49+
- [ ] Implement `isInteractive` check in `McpContext.ts`.
50+
- [ ] Use `DOMDebugger.getEventListeners` to include nodes with event listeners.
51+
- [ ] Update `createTextSnapshot` to use this logic when `interactive: true`.
52+
- [ ] Add tests to `tests/tools/snapshot.test.ts` with complex HTML to verify pruning of static text.
53+
54+
## PR 3: Session-Based Snapshot Diffs
55+
56+
**Objective**: Send only changes since the last observation.
57+
58+
- **Flag**: `diff`
59+
- **Tasks**:
60+
- [ ] Update `McpPage` to store `lastSnapshot` and reset on `framenavigated` / `load`.
61+
- [ ] Implement semantic diffing by `uid`.
62+
- [ ] Update `SnapshotFormatter` to render diffs with `[+]`, `[-]`, `[*]`.
63+
- [ ] Add tests to `tests/McpContext.test.ts` for multiple snapshot calls, ensuring only deltas are returned and navigation resets the cache.
64+
65+
---
66+
67+
## Verification & Testing Strategy
68+
69+
- **Infrastructure**: Use `withMcpContext` in existing test files.
70+
- **MCP Verification**: Run the server and call `take_snapshot` with new parameters.
71+
- **CLI Verification**:
72+
- Run `npm run cli:generate` after each PR.
73+
- Run `chrome-devtools take_snapshot --help` to check for new flags.
74+
- Execute commands like `chrome-devtools take_snapshot --role button` and verify output.
75+
- **Daemon Verification**: Ensure that `diff` mode works correctly when calling the CLI multiple times (state should be preserved in the running daemon).

src/McpContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,13 +754,13 @@ export class McpContext implements Context {
754754
index = 0,
755755
): TextSnapshotNode => {
756756
let id = '';
757-
const nodeAny = node as any;
757+
const nodeAny = node as unknown as Record<string, unknown>;
758758
// StaticText nodes often have unstable backendNodeIds in some contexts,
759759
// or we might want to group them by their parent.
760760
const uniqueBackendId =
761761
nodeAny.backendNodeId && node.role !== 'StaticText'
762762
? `${nodeAny.loaderId}_${nodeAny.backendNodeId}`
763-
: `${nodeAny.loaderId}_${nodeAny.role}_${parentId}_${index}`;
763+
: `${nodeAny.loaderId}_${node.role}_${parentId}_${index}`;
764764

765765
if (uniqueBackendNodeIdToMcpId.has(uniqueBackendId)) {
766766
// Re-use MCP exposed ID if the uniqueId is the same.

0 commit comments

Comments
 (0)