feat: add extraHttpHeaders emulation to emulate tool#1176
Conversation
|
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. |
There was a problem hiding this comment.
Thanks for the PR! I think we should align with the suggestion in this comment #1175 (comment) and add this to the emulation tool.
|
@OrKoN I moved the functionality to the emulation tool. Let me know if further adjustments are needed. |
OrKoN
left a comment
There was a problem hiding this comment.
Thanks! mostly looks good, one remaining comment about the format of the field.
OrKoN
left a comment
There was a problem hiding this comment.
Thanks it looks good, but it appears some compilation and test issues have to be addressed.
0408821 to
f3519bb
Compare
fbbc406 to
620c3df
Compare
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' })
06cca8c to
bc57d9c
Compare
Summary
Extend the existing
emulatetool with anextraHTTPHeadersparameter that calls Puppeteer'spage.setExtraHTTPHeaders()(which uses CDPNetwork.setExtraHTTPHeadersunder the hood).Closes #1175
Approach
Per feedback from @natorion, this integrates into the existing
emulatetool rather than adding a standalone tool. Theemulatetool 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— AddedextraHTTPHeaders?: Record<string, string>toEmulationSettingssrc/tools/emulation.ts— AddedextraHTTPHeadersas an optional zod parameter on theemulatetoolsrc/McpContext.ts— Added handler logic in theemulate()method:page.setExtraHTTPHeaders()whenextraHTTPHeadersis provided{}is passedemulate({colorScheme: "dark"})from accidentally clearing previously-set headerstests/tools/emulation.test.ts— Added 5 test cases:{}Use Case
This enables setting custom HTTP headers on all requests — including the initial document navigation and
<script>tag loads — whichinitScriptcannot do since it runs after the document is already fetched.Usage