Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(mcp): add --ignore-roots option (#43532)
Pavel Feldman83b80cfb2cc1
diff --git a/packages/playwright-core/src/tools/mcp/config.d.ts b/packages/playwright-core/src/tools/mcp/config.d.tsindex 76234b472c..4548361789 100644--- a/packages/playwright-core/src/tools/mcp/config.d.ts+++ b/packages/playwright-core/src/tools/mcp/config.d.ts@@ -272,6 +272,13 @@ export type Config = {*/allowUnrestrictedFileAccess?: boolean;+ /**+ * Do not ask the client for its workspace roots, use the current working directory+ * as the workspace root instead. Use this with clients that advertise roots, but+ * can not answer the roots request.+ */+ ignoreRoots?: boolean;+/*** Specify the language to use for code generation.*/diff --git a/packages/playwright-core/src/tools/mcp/config.ts b/packages/playwright-core/src/tools/mcp/config.tsindex 8f9ecee16f..fbdd42b83e 100644--- a/packages/playwright-core/src/tools/mcp/config.ts+++ b/packages/playwright-core/src/tools/mcp/config.ts@@ -56,6 +56,7 @@ export type CLIOptions = {headless?: boolean;host?: string;ignoreHttpsErrors?: boolean;+ ignoreRoots?: boolean;initScript?: string[];initPage?: string[];isolated?: boolean;@@ -386,6 +387,7 @@ function configFromCLIOptions(cliOptions: CLIOptions): Config & { configFile?: sblockedOrigins: cliOptions.blockedOrigins,},allowUnrestrictedFileAccess: cliOptions.allowUnrestrictedFileAccess,+ ignoreRoots: cliOptions.ignoreRoots,codegen: cliOptions.codegen,saveSession: cliOptions.saveSession,secrets: cliOptions.secrets,@@ -444,6 +446,7 @@ export function configFromEnv(env?: NodeJS.ProcessEnv): Config & { configFile?:options.headless = envToBoolean(e.PLAYWRIGHT_MCP_HEADLESS);options.host = envToString(e.PLAYWRIGHT_MCP_HOST);options.ignoreHttpsErrors = envToBoolean(e.PLAYWRIGHT_MCP_IGNORE_HTTPS_ERRORS);+ options.ignoreRoots = envToBoolean(e.PLAYWRIGHT_MCP_IGNORE_ROOTS);const initPage = envToString(e.PLAYWRIGHT_MCP_INIT_PAGE);if (initPage)options.initPage = [initPage];diff --git a/packages/playwright-core/src/tools/mcp/configIni.ts b/packages/playwright-core/src/tools/mcp/configIni.tsindex c6e952ceb6..e965c4d68f 100644--- a/packages/playwright-core/src/tools/mcp/configIni.ts+++ b/packages/playwright-core/src/tools/mcp/configIni.ts@@ -164,6 +164,7 @@ const longhandTypes: Record<string, LonghandType> = {'imageResponses': 'string','filePaths': 'string','allowUnrestrictedFileAccess': 'boolean',+ 'ignoreRoots': 'boolean','codegen': 'string','testIdAttribute': 'string',diff --git a/packages/playwright-core/src/tools/mcp/index.ts b/packages/playwright-core/src/tools/mcp/index.tsindex f0be3c3596..1f82477654 100644--- a/packages/playwright-core/src/tools/mcp/index.ts+++ b/packages/playwright-core/src/tools/mcp/index.ts@@ -34,6 +34,7 @@ export async function createConnection(userConfig: Config = {}, contextGetter?:nameInConfig: 'api',version: packageJSON.version,toolSchemas: tools.map(tool => tool.schema),+ ignoreRoots: config.ignoreRoots,create: async (clientInfo: ClientInfo) => {const browser = contextGetter? new SimpleBrowser(await contextGetter())diff --git a/packages/playwright-core/src/tools/mcp/program.ts b/packages/playwright-core/src/tools/mcp/program.tsindex fdf3212463..acfbcbaa12 100644--- a/packages/playwright-core/src/tools/mcp/program.ts+++ b/packages/playwright-core/src/tools/mcp/program.ts@@ -57,6 +57,7 @@ export function decorateMCPCommand(command: Command) {.option('--host <host>', 'host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.').option('--idle-timeout <timeout>', 'close the browser after this many milliseconds without a completed tool call, the next tool call relaunches it. Defaults to one hour for headless browsers, never for headed ones, 0 disables.', numberParser).option('--ignore-https-errors', 'ignore https errors')+ .option('--ignore-roots', 'do not ask the client for its workspace roots, use the current working directory as the workspace root instead. Use this with clients that advertise roots, but can not answer the roots request.').option('--init-page <path...>', 'path to TypeScript file to evaluate on Playwright page object').option('--init-script <path...>', 'path to JavaScript file to add as an initialization script. The script will be evaluated in every page before any of the page\'s scripts. Can be specified multiple times.').option('--isolated', 'keep the browser profile in memory, do not save it to disk.')@@ -111,6 +112,7 @@ export function decorateMCPCommand(command: Command) {nameInConfig: 'playwright',version,toolSchemas: tools.map(tool => tool.schema),+ ignoreRoots: config.ignoreRoots,create: async (clientInfo: ClientInfo) => {if (useSharedBrowser && !sharedBrowserPromise) {const promise = createBrowserWithInfo(config, clientInfo, options, { title: clientInfo.clientName, workspaceDir: clientInfo.cwd }).then(shared => {diff --git a/packages/playwright-core/src/tools/utils/mcp/server.ts b/packages/playwright-core/src/tools/utils/mcp/server.tsindex 28317f27d4..e9d546d864 100644--- a/packages/playwright-core/src/tools/utils/mcp/server.ts+++ b/packages/playwright-core/src/tools/utils/mcp/server.ts@@ -52,6 +52,7 @@ export type ServerBackendFactory = {nameInConfig: string;version: string;toolSchemas: ToolSchema<any>[];+ ignoreRoots?: boolean;create: (clientInfo: ClientInfo) => Promise<ServerBackend>;};@@ -127,7 +128,7 @@ export function createServer(name: string, version: string, factory: ServerBackeconst initializeServer = async (server: ServerType, factory: ServerBackendFactory, transportInitialized: Promise<void>): Promise<ServerBackend> => {const capabilities = server.getClientCapabilities();let clientRoots: Root[] = [];- if (capabilities?.roots) {+ if (capabilities?.roots && !factory.ignoreRoots) {await Promise.race([transportInitialized, new Promise<void>(f => setTimeout(f, 5000))]);const { roots } = await server.listRoots().catch(e => {serverDebug(e);diff --git a/tests/mcp/roots.spec.ts b/tests/mcp/roots.spec.tsindex 96e8a2daf6..d7df73fe21 100644--- a/tests/mcp/roots.spec.ts+++ b/tests/mcp/roots.spec.ts@@ -16,6 +16,7 @@import crypto from 'crypto';import fs from 'fs';+import path from 'path';import { pathToFileURL } from 'url';import { test, expect } from './fixtures';@@ -128,3 +129,35 @@ test('should return relative paths when root is specified', async ({ startClientresult: expect.stringContaining(`[Screenshot of viewport](./screenshot.png)`),});});++test('should not ask for roots with --ignore-roots', async ({ startClient, server }, testInfo) => {+ const rootPath = testInfo.outputPath('workspace');+ const cwd = testInfo.outputPath('cwd');+ await fs.promises.mkdir(rootPath, { recursive: true });+ await fs.promises.mkdir(cwd, { recursive: true });++ const { client } = await startClient({+ args: ['--ignore-roots'],+ cwd,+ roots: [+ {+ name: 'workspace',+ uri: pathToFileURL(rootPath).toString(),+ },+ ],+ });++ await client.callTool({+ name: 'browser_navigate',+ arguments: { url: server.HELLO_WORLD },+ });++ expect(await client.callTool({+ name: 'browser_take_screenshot',+ arguments: { filename: 'screenshot.png' },+ })).toHaveResponse({+ result: expect.stringContaining(`[Screenshot of viewport](./screenshot.png)`),+ });+ expect(fs.existsSync(path.join(cwd, 'screenshot.png'))).toBe(true);+ expect(fs.existsSync(path.join(rootPath, 'screenshot.png'))).toBe(false);+});