Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(mcp): pass credentials from --proxy-server as proxy username/password (#43071)

Dmitry Gozman7ad3fba1aad9
diff --git a/packages/playwright-core/src/cli/browserActions.ts b/packages/playwright-core/src/cli/browserActions.ts
index 552abb9063..b4caea378f 100644
--- a/packages/playwright-core/src/cli/browserActions.ts
+++ b/packages/playwright-core/src/cli/browserActions.ts
@@ -24,6 +24,7 @@ import debug from 'debug';
import dotenv from 'dotenv';
import { program } from 'commander';
import { gracefullyProcessExitDoNotHang } from '@utils/processLauncher';
+import { proxySettingsFromString } from '@utils/network';
import { ManualPromise } from '@isomorphic/manualPromise';
import { playwright } from '../inprocess';
import type { Browser } from '../client/browser';
@@ -92,9 +93,7 @@ async function launchContext(options: Options, extraOptions: LaunchOptions): Pro
// Proxy
if (options.proxyServer) {
- launchOptions.proxy = {
- server: options.proxyServer
- };
+ launchOptions.proxy = proxySettingsFromString(options.proxyServer);
if (options.proxyBypass)
launchOptions.proxy.bypass = options.proxyBypass;
}
diff --git a/packages/playwright-core/src/cli/program.ts b/packages/playwright-core/src/cli/program.ts
index 7b4d685411..a6b047707d 100644
--- a/packages/playwright-core/src/cli/program.ts
+++ b/packages/playwright-core/src/cli/program.ts
@@ -279,7 +279,7 @@ function commandWithOpenOptions(command: string, description: string, options: a
.option('--ignore-https-errors', 'ignore https errors')
.option('--load-storage <filename>', 'load context storage state from the file, previously saved with --save-storage')
.option('--lang <language>', 'specify language / locale, for example "en-GB"')
- .option('--proxy-server <proxy>', 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"')
+ .option('--proxy-server <proxy>', 'specify proxy server, for example "http://myproxy:3128", "http://user:password@myproxy:3128" or "socks5://myproxy:8080"')
.option('--proxy-bypass <bypass>', 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"')
.option('--save-har <filename>', 'save HAR file with all network activity at the end')
.option('--save-har-glob <glob pattern>', 'filter entries in the HAR by matching url against this glob pattern')
diff --git a/packages/playwright-core/src/tools/mcp/config.ts b/packages/playwright-core/src/tools/mcp/config.ts
index 28062016e4..07dc1d3a83 100644
--- a/packages/playwright-core/src/tools/mcp/config.ts
+++ b/packages/playwright-core/src/tools/mcp/config.ts
@@ -20,6 +20,7 @@ import os from 'os';
import dotenv from 'dotenv';
import { isSystemDirectory } from '@utils/fileUtils';
+import { proxySettingsFromString } from '@utils/network';
import { playwright } from '../../inprocess';
import { configFromIniFile } from './configIni';
@@ -328,7 +329,7 @@ function configFromCLIOptions(cliOptions: CLIOptions): Config & { configFile?: s
const contextOptions: playwrightTypes.BrowserContextOptions = device ? playwright.devices[device] : {};
if (cliOptions.proxyServer) {
- const proxy: playwrightTypes.LaunchOptions['proxy'] = { server: cliOptions.proxyServer };
+ const proxy: playwrightTypes.LaunchOptions['proxy'] = proxySettingsFromString(cliOptions.proxyServer);
if (cliOptions.proxyBypass)
proxy.bypass = cliOptions.proxyBypass;
// Set on both to ensure CLI takes precedence over any proxy set in the config file
diff --git a/packages/playwright-core/src/tools/mcp/program.ts b/packages/playwright-core/src/tools/mcp/program.ts
index eec1b45158..86f448f4c5 100644
--- a/packages/playwright-core/src/tools/mcp/program.ts
+++ b/packages/playwright-core/src/tools/mcp/program.ts
@@ -68,7 +68,7 @@ export function decorateMCPCommand(command: Command) {
.option('--port <port>', 'port to listen on for SSE transport.')
.option('--profile-dir-name <name>', 'name of the profile directory in the user data dir to connect to with --extension, for example "Profile 1". Defaults to the last used profile that has the extension installed.')
.option('--proxy-bypass <bypass>', 'comma-separated domains to bypass proxy, for example ".com,chromium.org,.domain.com"')
- .option('--proxy-server <proxy>', 'specify proxy server, for example "http://myproxy:3128" or "socks5://myproxy:8080"')
+ .option('--proxy-server <proxy>', 'specify proxy server, for example "http://myproxy:3128", "http://user:password@myproxy:3128" or "socks5://myproxy:8080"')
.addOption(new ProgramOption('--remote-header <headers...>', 'headers to send with the remote endpoint connect request, multiple can be specified.').argParser(headerParser).hideHelp())
.option('--sandbox', 'enable the sandbox for all process types that are normally not sandboxed.')
.option('--save-session', 'Whether to save the Playwright MCP session into the output directory.')
diff --git a/packages/utils/network.ts b/packages/utils/network.ts
index d121ffda9e..5f9adb34f1 100644
--- a/packages/utils/network.ts
+++ b/packages/utils/network.ts
@@ -129,6 +129,22 @@ function normalizeProxyURL(proxy: string): URL {
return new URL(proxy);
}
+// Moves credentials from the proxy string, e.g. "http://user:pass@myproxy:3128", into explicit fields.
+export function proxySettingsFromString(proxy: string): ProxySettings {
+ try {
+ const url = normalizeProxyURL(proxy);
+ if (url.username || url.password) {
+ return {
+ server: url.protocol + '//' + url.host,
+ username: decodeURIComponent(url.username),
+ password: decodeURIComponent(url.password),
+ };
+ }
+ } catch {
+ }
+ return { server: proxy };
+}
+
export function createProxyAgent(proxy?: ProxySettings, forUrl?: URL) {
if (!proxy)
return;
diff --git a/tests/mcp/launch.spec.ts b/tests/mcp/launch.spec.ts
index 9e410e3c79..0b4e9599db 100644
--- a/tests/mcp/launch.spec.ts
+++ b/tests/mcp/launch.spec.ts
@@ -265,6 +265,31 @@ test('--proxy-server overrides contextOptions.proxy from config file', {
});
});
+test('--proxy-server should use credentials from the url', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43048' },
+}, async ({ startClient, server }) => {
+ server.setRoute('/target.html', (req, res) => {
+ const auth = req.headers['proxy-authorization'];
+ if (!auth) {
+ res.writeHead(407, 'Proxy Authentication Required', {
+ 'Proxy-Authenticate': 'Basic realm="Access to internal site"'
+ });
+ res.end();
+ } else {
+ res.end(`<html><title>${auth}</title></html>`);
+ }
+ });
+ const { client } = await startClient({
+ args: [`--proxy-server=http://user:se%40cret@${server.HOST}`],
+ });
+ expect(await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: 'http://non-existent.com/target.html' },
+ })).toHaveResponse({
+ page: expect.stringContaining('Basic ' + Buffer.from('user:se@cret').toString('base64')),
+ });
+});
+
test('proper launch error message for broken browser and persistent context', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright-mcp/issues/1305' }
}, async ({ startClient, server, mcpBrowser }, testInfo) => {