Skip to content

Commit 759770d

Browse files
committed
docs: clarify evaluate_script guardrails
1 parent 0a6aaa5 commit 759770d

4 files changed

Lines changed: 24 additions & 11 deletions

File tree

docs/tool-reference.md

Lines changed: 7 additions & 4 deletions
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 (~6962 cl100k_base tokens)
3+
# Chrome DevTools MCP Tool Reference (~7107 cl100k_base tokens)
44

55
- **[Input automation](#input-automation)** (9 tools)
66
- [`click`](#click)
@@ -317,8 +317,10 @@
317317

318318
### `evaluate_script`
319319

320-
**Description:** Evaluate a JavaScript function inside the currently selected page. Returns the response as JSON,
321-
so returned values have to be JSON-serializable.
320+
**Description:** Evaluate a JavaScript function inside the currently selected page or frame. Returns the response as JSON,
321+
so returned values have to be JSON-serializable. The function runs in the current page context only; do not use it to navigate to other pages.
322+
Use [`navigate_page`](#navigate_page) or [`new_page`](#new_page) for navigation, then run [`evaluate_script`](#evaluate_script) again after the new page loads. When querying the DOM, use standard browser APIs
323+
and valid native CSS selectors only. Uids from [`take_snapshot`](#take_snapshot) are not DOM attributes; pass them via the args parameter when you need the referenced elements.
322324

323325
**Parameters:**
324326

@@ -331,8 +333,9 @@ so returned values have to be JSON-serializable.
331333
Example with arguments: `(el) => {
332334
return el.innerText;
333335
}`
336+
Use only standard DOM APIs and valid native CSS selectors inside the function. Do not use snapshot uids in querySelector calls, and do not change window.location here to navigate across pages.
334337

335-
- **args** (array) _(optional)_: An optional list of arguments to pass to the function.
338+
- **args** (array) _(optional)_: An optional list of element uids from [`take_snapshot`](#take_snapshot). These are resolved to real element handles and passed as function arguments; they are not available as DOM attributes in the page.
336339

337340
---
338341

skills/chrome-devtools/SKILL.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ description: Uses Chrome DevTools via MCP for efficient debugging, troubleshooti
1111

1212
**Element interaction**: Use `take_snapshot` to get page structure with element `uid`s. Each element has a unique `uid` for interaction. If an element isn't found, take a fresh snapshot - the element may have been removed or the page changed.
1313

14+
**Snapshot uid scope**: `uid`s from `take_snapshot` are MCP references, not DOM attributes. Do not use them in `querySelector()` or other selector strings. When a script needs one of those elements, pass the `uid` through `evaluate_script` args so the tool resolves it to a real element handle.
15+
16+
**Script scope**: `evaluate_script` runs in the currently selected page or frame only. Do not try to navigate by setting `window.location` inside the evaluated function. Use `navigate_page` or `new_page`, wait for the destination page, then run another `evaluate_script`.
17+
1418
## Workflow Patterns
1519

1620
### Before interacting with a page
@@ -30,7 +34,7 @@ description: Uses Chrome DevTools via MCP for efficient debugging, troubleshooti
3034

3135
- **Automation/interaction**: `take_snapshot` (text-based, faster, better for automation)
3236
- **Visual inspection**: `take_screenshot` (when user needs to see visual state)
33-
- **Additional details**: `evaluate_script` for data not in accessibility tree
37+
- **Additional details**: `evaluate_script` for data not in accessibility tree. Use valid native DOM APIs/selectors only; avoid library-specific pseudo-classes like `:contains(...)`.
3438

3539
### Parallel execution
3640

src/bin/cliDefinitions.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,21 @@ export const commands: Commands = {
139139
},
140140
evaluate_script: {
141141
description:
142-
'Evaluate a JavaScript function inside the currently selected page. Returns the response as JSON,\nso returned values have to be JSON-serializable.',
142+
'Evaluate a JavaScript function inside the currently selected page or frame. Returns the response as JSON,\nso returned values have to be JSON-serializable. The function runs in the current page context only; do not use it to navigate to other pages.\nUse navigate_page or new_page for navigation, then run evaluate_script again after the new page loads. When querying the DOM, use standard browser APIs\nand valid native CSS selectors only. Uids from take_snapshot are not DOM attributes; pass them via the args parameter when you need the referenced elements.',
143143
category: 'Debugging',
144144
args: {
145145
function: {
146146
name: 'function',
147147
type: 'string',
148148
description:
149-
'A JavaScript function declaration to be executed by the tool in the currently selected page.\nExample without arguments: `() => {\n return document.title\n}` or `async () => {\n return await fetch("example.com")\n}`.\nExample with arguments: `(el) => {\n return el.innerText;\n}`\n',
149+
'A JavaScript function declaration to be executed by the tool in the currently selected page.\nExample without arguments: `() => {\n return document.title\n}` or `async () => {\n return await fetch("example.com")\n}`.\nExample with arguments: `(el) => {\n return el.innerText;\n}`\nUse only standard DOM APIs and valid native CSS selectors inside the function. Do not use snapshot uids in querySelector calls, and do not change window.location here to navigate across pages.\n',
150150
required: true,
151151
},
152152
args: {
153153
name: 'args',
154154
type: 'array',
155-
description: 'An optional list of arguments to pass to the function.',
155+
description:
156+
'An optional list of element uids from take_snapshot. These are resolved to real element handles and passed as function arguments; they are not available as DOM attributes in the page.',
156157
required: false,
157158
},
158159
},

src/tools/script.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ export type Evaluatable = Page | Frame | WebWorker;
1717
export const evaluateScript = defineTool(cliArgs => {
1818
return {
1919
name: 'evaluate_script',
20-
description: `Evaluate a JavaScript function inside the currently selected page. Returns the response as JSON,
21-
so returned values have to be JSON-serializable.`,
20+
description: `Evaluate a JavaScript function inside the currently selected page or frame. Returns the response as JSON,
21+
so returned values have to be JSON-serializable. The function runs in the current page context only; do not use it to navigate to other pages.
22+
Use navigate_page or new_page for navigation, then run evaluate_script again after the new page loads. When querying the DOM, use standard browser APIs
23+
and valid native CSS selectors only. Uids from take_snapshot are not DOM attributes; pass them via the args parameter when you need the referenced elements.`,
2224
annotations: {
2325
category: ToolCategory.DEBUGGING,
2426
readOnlyHint: false,
@@ -34,6 +36,7 @@ Example without arguments: \`() => {
3436
Example with arguments: \`(el) => {
3537
return el.innerText;
3638
}\`
39+
Use only standard DOM APIs and valid native CSS selectors inside the function. Do not use snapshot uids in querySelector calls, and do not change window.location here to navigate across pages.
3740
`,
3841
),
3942
args: zod
@@ -45,7 +48,9 @@ Example with arguments: \`(el) => {
4548
),
4649
)
4750
.optional()
48-
.describe(`An optional list of arguments to pass to the function.`),
51+
.describe(
52+
`An optional list of element uids from take_snapshot. These are resolved to real element handles and passed as function arguments; they are not available as DOM attributes in the page.`,
53+
),
4954
...(cliArgs?.experimentalPageIdRouting ? pageIdSchema : {}),
5055
...(cliArgs?.categoryExtensions
5156
? {

0 commit comments

Comments
 (0)