Skip to content

Conversation

lupuletic
Copy link

@lupuletic lupuletic commented Sep 25, 2025

Summary

  • add an opt-in headers capability for Playwright MCP
  • expose a browser_set_headers tool that persists custom HTTP headers via
    setExtraHTTPHeaders
  • document the new capability and cover it with targeted MCP tests

Testing

  • npm run test-mcp -- tests/mcp/headers.spec.ts

Closes #37575


Seed tests provide a ready-to-use `page` context to bootstrap execution.

### Custom headers for multi-tenant testing
Copy link
Member

Choose a reason for hiding this comment

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

This file is for the test generation scenario. Please move this section into https://github.com/microsoft/playwright-mcp/blob/main/README.md

}

const invalidHeader = entries.find(([name]) => !name.trim());
if (invalidHeader) {
Copy link
Member

Choose a reason for hiding this comment

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

Playwright will validate this, no need to duplicate the logic.

Copy link
Author

Choose a reason for hiding this comment

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

I tried removing this check, but it turns out Playwright doesn’t reject whitespace-only header names. Chromium throws, but Firefox/WebKit happily accept ' ' : value, and the next navigation crashes our MCP static server with “ParseError: Unexpected whitespace after header value”, killing the session. Keeping the guard here prevents that across all browsers.

See the CI run below: https://github.com/microsoft/playwright/actions/runs/18037953508/job/51329266937?pr=37583#step:3:549

return;
}

const normalizedHeaders = Object.fromEntries(entries.map(([name, value]) => [name.trim(), value]));
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this normalization makes any difference, drop it?

@lupuletic lupuletic requested a review from yury-s September 26, 2025 08:29
@lupuletic
Copy link
Author

Hi @yury-s 👋, I’ve updated the PR based on your feedback.

Could you please take another look when you have some time? The team is very excited for this feature for some of the automations we got in our pipeline.

Thanks a lot for your help!

return;
}

const invalidHeader = entries.find(([name]) => !name.trim());
Copy link
Member

Choose a reason for hiding this comment

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

nit: ideally this check should be done in the setExtraHTTPHeaders implementation.

expect(secondRequest.headers['x-tenant-id']).toBe('tenant-123');
});

test('browser_set_headers applies to all requests from the context', async ({ startClient, server }) => {
Copy link
Member

Choose a reason for hiding this comment

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

nit: we already have it covered by the tests for context.setExtraHttpHeaders, I'd only test that the headers are sent at all.

@yury-s
Copy link
Member

yury-s commented Oct 2, 2025

Tests on Windows seem unrelated but they keep failing restarting the job. Let me try again.

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]: Add support for custom headers to enable multi-tenant testing #925
2 participants