Skip to content

Commit 8a4cf38

Browse files
shanqueclaude
andcommitted
feat: add set_extra_http_headers tool for Network.setExtraHTTPHeaders
Add a new MCP tool that allows setting extra HTTP headers on all page requests via Puppeteer's page.setExtraHTTPHeaders(). Headers persist across navigations and can be cleared by passing an empty object. This addresses the need for injecting custom headers (e.g. swim-lane routing, A/B testing, feature flags) on all request types including initial document loads, which is not possible with initScript alone. Closes #1175 Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 8c2a7fc commit 8a4cf38

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

src/tools/network.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,34 @@ export const listNetworkRequests = definePageTool({
8686
},
8787
});
8888

89+
export const setExtraHttpHeaders = definePageTool({
90+
name: 'set_extra_http_headers',
91+
description: `Set extra HTTP headers that will be included in every request the page makes. These headers are applied to all resource types including document, script, stylesheet, image, fetch, and XHR requests. The headers persist across navigations until explicitly cleared by calling this tool with an empty headers object.`,
92+
annotations: {
93+
category: ToolCategory.NETWORK,
94+
readOnlyHint: false,
95+
},
96+
schema: {
97+
headers: zod
98+
.record(zod.string(), zod.string())
99+
.describe(
100+
'HTTP headers as key-value pairs to include in every request. Pass an empty object {} to clear previously set headers.',
101+
),
102+
},
103+
handler: async (request, response) => {
104+
const page = request.page;
105+
await page.pptrPage.setExtraHTTPHeaders(request.params.headers);
106+
const count = Object.keys(request.params.headers).length;
107+
if (count === 0) {
108+
response.appendResponseLine('Cleared all extra HTTP headers.');
109+
} else {
110+
response.appendResponseLine(
111+
`Set ${count} extra HTTP header(s): ${Object.keys(request.params.headers).join(', ')}`,
112+
);
113+
}
114+
},
115+
});
116+
89117
export const getNetworkRequest = definePageTool({
90118
name: 'get_network_request',
91119
description: `Gets a network request by an optional reqid, if omitted returns the currently selected request in the DevTools Network panel.`,

0 commit comments

Comments
 (0)