Skip to content

feat: add filePath parameter to take_snapshot and evaluate_script#248

Closed
Coquinate wants to merge 2 commits into
ChromeDevTools:mainfrom
Coquinate:feat/save-to-file
Closed

feat: add filePath parameter to take_snapshot and evaluate_script#248
Coquinate wants to merge 2 commits into
ChromeDevTools:mainfrom
Coquinate:feat/save-to-file

Conversation

@Coquinate

Copy link
Copy Markdown
Contributor

Summary

Adds optional filePath parameter to take_snapshot and evaluate_script tools, enabling direct file output instead of response inclusion.

Resolves #153

Features

  • take_snapshot can save accessibility tree to file
  • evaluate_script can save JSON result to file
  • Reduces token usage for large outputs
  • Enables offline analysis and processing
  • Fully backwards compatible

Implementation

  • Added optional filePath parameter to both tool schemas
  • Write formatted output using writeFile from node:fs/promises
  • Updated Context interface to expose createTextSnapshot and getTextSnapshot
  • Follows same pattern as take_screenshot tool

Testing

  • ✅ New test: saves snapshot to file
  • ✅ New test: saves script result to file
  • ✅ All existing tests still pass
  • ✅ Backwards compatibility verified
  • ✅ Manual testing with local MCP server

Test Plan

  • Build succeeds
  • All existing tests pass
  • New file-saving tests pass
  • Documentation generated correctly

@Lightning00Blade

Copy link
Copy Markdown
Collaborator

Hey,
We landed some changes in - #250
regarding the way we want to go about saving files via the MCP.

Could you please incorporate those in this PR and will review after.

@Coquinate

Copy link
Copy Markdown
Contributor Author

Updated to use context.saveFile() following the pattern from #250. All tests passing. ✅

aberemia24 and others added 2 commits October 10, 2025 13:43
Add optional filePath parameter to save tool output directly to files instead of including in response.

Features:
- take_snapshot can save accessibility tree to file
- evaluate_script can save JSON result to file
- Reduces token usage for large outputs
- Enables offline analysis and processing
- Backwards compatible - tools work as before when filePath is omitted

Implementation:
- Added filePath optional parameter to both tool schemas
- Write formatted output to file using writeFile from node:fs/promises
- Updated Context interface to expose createTextSnapshot and getTextSnapshot
- Includes comprehensive tests for file saving functionality

Resolves ChromeDevTools#153
Updates take_snapshot and evaluate_script to use context.saveFile()
instead of direct writeFile() calls, following the pattern established
in PR ChromeDevTools#250.

Changes:
- Removed direct fs/promises imports
- Convert string data to Uint8Array using TextEncoder
- Use context.saveFile() for consistent file writing
- Update response messages to use returned filename

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@OrKoN OrKoN force-pushed the feat/save-to-file branch from f4b451c to acaabdc Compare October 10, 2025 11:43
@OrKoN

OrKoN commented Oct 10, 2025

Copy link
Copy Markdown
Collaborator

Could you please sign the CLA (see the failing check) https://github.com/ChromeDevTools/chrome-devtools-mcp/pull/248/checks?check_run_id=52444256994?

@OrKoN

OrKoN commented Nov 19, 2025

Copy link
Copy Markdown
Collaborator

Closing due to conflicts. It is partially addressed by other PRs and we have tracking issues for remaining feature requests.

@OrKoN OrKoN closed this Nov 19, 2025
pull Bot pushed a commit to Mu-L/chrome-devtools-mcp that referenced this pull request May 14, 2026
## Summary

Adds an optional `filePath` parameter to `evaluate_script` that saves
the script output to a file instead of returning it inline.

Refs #153

## Motivation

Issue #153 requested `filePath` support for `take_snapshot` and
`evaluate_script`. `take_snapshot` was addressed in ChromeDevTools#463. PR ChromeDevTools#248
previously attempted this but was closed due to conflicts. This PR
implements the same feature on the current codebase, completing the
remaining piece.

## Changes

- Add optional `filePath` parameter to the `evaluate_script` schema
- Add `context.validatePath(filePath)` call for path validation
- Pass `{filePath, context}` options to `performEvaluation()`
- In `performEvaluation()`: when `filePath` is provided, save output via
`context.saveFile()` with `.json` extension; otherwise return inline as
before
- Update `docs/tool-reference.md` via `npm run docs:generate`
- Add unit test for file output

## Key design decisions

- **Same pattern as existing tools**: Follows the `context.saveFile()`
pattern established by `take_snapshot` (ChromeDevTools#463), `take_screenshot`,
`get_network_request` (ChromeDevTools#795), and performance tools (ChromeDevTools#686).
- **Minimal change surface**: Only `performEvaluation()` gains an
optional `options` parameter. No new interfaces or abstractions.
- **Backwards compatible**: `filePath` is optional. When omitted,
behavior is identical to before.

## Testing

**Unit test added** (`tests/tools/script.test.ts`):
- Call `evaluate_script` with `filePath` set to a temp file
- Assert response contains "Output saved to"
- Assert file content matches the JSON-serialized return value
- Clean up temp file in `finally` block

**Manual testing performed**:
- `() => document.title` with `filePath: /tmp/test.json` → file contains
`"Example Domain"`
- `() => document.title` without `filePath` → inline ```json block
returned (no regression)
- `() => Array.from({length: 100}, ...)` with `filePath` → 100-item
array saved correctly
- `filePath` pointing to non-existent directory → directory
auto-created, file saved
- Relative path (`test.json`) → resolved to CWD, absolute path shown in
response
- Function that throws → error returned, no partial file created
- Existing file as `filePath` → file overwritten completely

---------

Co-authored-by: Alex Rudenko <alexrudenko@chromium.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Add Save to File for take_snapshot and evaluate_script tools

4 participants