Skip to content

feat: add extraHttpHeaders emulation to emulate tool#1176

Merged
OrKoN merged 7 commits into
ChromeDevTools:mainfrom
pedrodurek:feature/set-extra-http-headers
May 20, 2026
Merged

feat: add extraHttpHeaders emulation to emulate tool#1176
OrKoN merged 7 commits into
ChromeDevTools:mainfrom
pedrodurek:feature/set-extra-http-headers

Conversation

@pedrodurek
Copy link
Copy Markdown
Contributor

@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.

@pedrodurek pedrodurek requested a review from OrKoN April 13, 2026 23:47
@pedrodurek
Copy link
Copy Markdown
Contributor Author

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

Comment thread src/tools/emulation.ts Outdated
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! mostly looks good, one remaining comment about the format of the field.

Comment thread pnpm-lock.yaml Outdated
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 it looks good, but it appears some compilation and test issues have to be addressed.

@OrKoN OrKoN self-requested a review May 12, 2026 16:56
@pedrodurek pedrodurek force-pushed the feature/set-extra-http-headers branch from 0408821 to f3519bb Compare May 12, 2026 19:47
@OrKoN OrKoN changed the title feat: add set_extra_http_headers tool for Network.setExtraHTTPHeaders feat: add extraHttpHeaders emulation to emulate tool May 14, 2026
@OrKoN OrKoN force-pushed the feature/set-extra-http-headers branch 2 times, most recently from fbbc406 to 620c3df Compare May 14, 2026 05:58
Comment thread src/telemetry/tool_call_metrics.json Outdated
@OrKoN OrKoN self-requested a review May 14, 2026 05:59
Comment thread src/McpContext.ts Outdated
pedrodurek and others added 4 commits May 19, 2026 18:24
Extend the existing emulate tool with an extraHTTPHeaders parameter
that calls Puppeteer's page.setExtraHTTPHeaders() (which uses CDP
Network.setExtraHTTPHeaders under the hood).

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.

Per reviewer feedback, the parameter is a JSON string (not an object)
for CLI compatibility. Empty string clears the headers.

Closes ChromeDevTools#1175

Usage:
  // Set headers
  emulate({ extraHTTPHeaders: '{"X-Custom": "value"}' })

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

  // Combine with other emulation settings
  emulate({ extraHTTPHeaders: '{"X-Branch": "feature-1"}', userAgent: 'MyBot/1.0' })
@nroscino nroscino force-pushed the feature/set-extra-http-headers branch from 06cca8c to bc57d9c Compare May 19, 2026 16:30
Comment thread src/telemetry/tool_call_metrics.json Outdated
@nroscino nroscino requested review from OrKoN and yulunz May 20, 2026 09:19
@OrKoN OrKoN enabled auto-merge May 20, 2026 09:37
@OrKoN OrKoN added this pull request to the merge queue May 20, 2026
Merged via the queue into ChromeDevTools:main with commit 6992106 May 20, 2026
14 checks passed
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

4 participants