Skip to content

feat: add set_extra_http_headers tool for Network.setExtraHTTPHeaders#1176

Open
pedrodurek wants to merge 2 commits intoChromeDevTools:mainfrom
pedrodurek:feature/set-extra-http-headers
Open

feat: add set_extra_http_headers tool for Network.setExtraHTTPHeaders#1176
pedrodurek wants to merge 2 commits intoChromeDevTools:mainfrom
pedrodurek:feature/set-extra-http-headers

Conversation

@pedrodurek
Copy link
Copy Markdown

@pedrodurek pedrodurek commented Mar 14, 2026

Summary

Extend the existing emulate tool with an extraHTTPHeaders parameter that calls Puppeteer's page.setExtraHTTPHeaders() (which uses CDP Network.setExtraHTTPHeaders under the hood).

Closes #1175

Approach

Per feedback from @natorion, this integrates into the existing emulate tool rather than adding a standalone tool. The emulate tool is already the central hub for page-level state modifications (userAgent, viewport, networkConditions, geolocation, colorScheme), and custom HTTP headers fit naturally alongside them. This also avoids increasing the MCP tool count and LLM token overhead.

Changes

  • src/types.ts — Added extraHTTPHeaders?: Record<string, string> to EmulationSettings
  • src/tools/emulation.ts — Added extraHTTPHeaders as an optional zod parameter on the emulate tool
  • src/McpContext.ts — Added handler logic in the emulate() method:
    • Calls page.setExtraHTTPHeaders() when extraHTTPHeaders is provided
    • Clears from settings when an empty {} is passed
    • Preserves existing headers when the param is omitted (unlike other emulation settings that reset when omitted) — prevents emulate({colorScheme: "dark"}) from accidentally clearing previously-set headers
  • tests/tools/emulation.test.ts — Added 5 test cases:
    1. Sets extra headers on requests
    2. Clears headers with {}
    3. Headers persist across navigations
    4. Does not affect other emulation settings
    5. Reports correctly per-page (new page has no headers)

Use Case

This enables setting custom HTTP headers on all requests — including the initial document navigation and <script> tag loads — which initScript cannot do since it runs after the document is already fetched.

Usage

// Set headers
emulate({ extraHTTPHeaders: { "X-Custom": "value", "Authorization": "Bearer token" } })

// Clear headers
emulate({ extraHTTPHeaders: {} })

// Combine with other emulation settings
emulate({ extraHTTPHeaders: { "X-Branch": "feature-1" }, userAgent: "MyBot/1.0" })

@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 14, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown
Collaborator

@OrKoN OrKoN left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I think we should align with the suggestion in this comment #1175 (comment) and add this to the emulation tool.

Address review feedback to extend the existing emulate tool
instead of adding a standalone set_extra_http_headers tool.

- Add extraHTTPHeaders param to emulate tool schema
- Add handler logic in McpContext.emulate()
- Preserve existing headers when param is omitted
- Move tests from network.test.ts to emulation.test.ts
- Revert network.ts and network.test.ts to main
@pedrodurek pedrodurek requested a review from OrKoN April 13, 2026 23:47
@pedrodurek
Copy link
Copy Markdown
Author

@OrKoN I moved the functionality to the emulation tool. Let me know if further adjustments are needed.

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: set_extra_http_headers tool for Network.setExtraHTTPHeaders

2 participants