Skip to content

Conversation

@lupuletic
Copy link
Contributor

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

@lupuletic lupuletic requested a review from yury-s October 8, 2025 19:40
Copy link
Member

@yury-s yury-s left a comment

Choose a reason for hiding this comment

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

Please fix lint errors, otherwise looks good!

@lupuletic
Copy link
Contributor Author

lupuletic commented Oct 15, 2025

Please fix lint errors, otherwise looks good!

Hey, I am not sure what I am doing wrong, but lint is passing for me locally for that file -- would it be possible to retry the job, please?

@yury-s
Copy link
Member

yury-s commented Oct 16, 2025

Please fix lint errors, otherwise looks good!

Hey, I am not sure what I am doing wrong, but lint is passing for me locally for that file -- would it be possible to retry the job, please?

You need to rebase on the tip of tree, I believe the type has changed. I will retry the job too.

@yury-s yury-s merged commit 1ca9f66 into microsoft:main Oct 17, 2025
3 of 6 checks passed
yury-s added a commit to yury-s/playwright that referenced this pull request Oct 17, 2025
@github-actions
Copy link
Contributor

Test results for "MCP"

13 failed
❌ [chrome] › mcp/generator.spec.ts:23 › generator tools intent @mcp-macos-15
❌ [chromium] › mcp/generator.spec.ts:23 › generator tools intent @mcp-macos-15
❌ [firefox] › mcp/generator.spec.ts:23 › generator tools intent @mcp-macos-15
❌ [webkit] › mcp/generator.spec.ts:23 › generator tools intent @mcp-macos-15
❌ [chrome] › mcp/generator.spec.ts:23 › generator tools intent @mcp-ubuntu-latest
❌ [chromium] › mcp/generator.spec.ts:23 › generator tools intent @mcp-ubuntu-latest
❌ [firefox] › mcp/generator.spec.ts:23 › generator tools intent @mcp-ubuntu-latest
❌ [webkit] › mcp/generator.spec.ts:23 › generator tools intent @mcp-ubuntu-latest
❌ [chrome] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [chromium] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [firefox] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [webkit] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest
❌ [msedge] › mcp/generator.spec.ts:23 › generator tools intent @mcp-windows-latest

2622 passed, 108 skipped


Merge workflow run.

yury-s added a commit to yury-s/playwright that referenced this pull request Oct 22, 2025
yury-s added a commit to yury-s/playwright that referenced this pull request Oct 22, 2025
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