Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/playwright-core/src/tools/mcp/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/playwright-core/src/tools/mcp/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export type CLIOptions = {
headless?: boolean;
host?: string;
ignoreHttpsErrors?: boolean;
ignoreRoots?: boolean;
initScript?: string[];
initPage?: string[];
isolated?: boolean;
Expand Down Expand Up @@ -386,6 +387,7 @@ function configFromCLIOptions(cliOptions: CLIOptions): Config & { configFile?: s
blockedOrigins: cliOptions.blockedOrigins,
},
allowUnrestrictedFileAccess: cliOptions.allowUnrestrictedFileAccess,
ignoreRoots: cliOptions.ignoreRoots,
codegen: cliOptions.codegen,
saveSession: cliOptions.saveSession,
secrets: cliOptions.secrets,
Expand Down Expand Up @@ -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];
Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/tools/mcp/configIni.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ const longhandTypes: Record<string, LonghandType> = {
'imageResponses': 'string',
'filePaths': 'string',
'allowUnrestrictedFileAccess': 'boolean',
'ignoreRoots': 'boolean',
'codegen': 'string',
'testIdAttribute': 'string',

Expand Down
1 change: 1 addition & 0 deletions packages/playwright-core/src/tools/mcp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions packages/playwright-core/src/tools/mcp/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down Expand Up @@ -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 => {
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright-core/src/tools/utils/mcp/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export type ServerBackendFactory = {
nameInConfig: string;
version: string;
toolSchemas: ToolSchema<any>[];
ignoreRoots?: boolean;
create: (clientInfo: ClientInfo) => Promise<ServerBackend>;
};

Expand Down Expand Up @@ -127,7 +128,7 @@ export function createServer(name: string, version: string, factory: ServerBacke
const 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);
Expand Down
33 changes: 33 additions & 0 deletions tests/mcp/roots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -128,3 +129,35 @@ test('should return relative paths when root is specified', async ({ startClient
result: 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);
});