playwright-mcp/src/program.ts

73 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-03-21 10:58:58 -07:00
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { program } from 'commander';
import { startHttpTransport, startStdioTransport } from './transport.js';
import { resolveConfig } from './config.js';
import type { Connection } from './connection.js';
import { packageJSON } from './context.js';
2025-03-21 10:58:58 -07:00
program
.version('Version ' + packageJSON.version)
.name(packageJSON.name)
.option('--browser <browser>', 'Browser or chrome channel to use, possible values: chrome, firefox, webkit, msedge.')
.option('--caps <caps>', 'Comma-separated list of capabilities to enable, possible values: tabs, pdf, history, wait, files, install. Default is all.')
.option('--cdp-endpoint <endpoint>', 'CDP endpoint to connect to.')
.option('--executable-path <path>', 'Path to the browser executable.')
2025-03-21 10:58:58 -07:00
.option('--headless', 'Run browser in headless mode, headed by default')
.option('--device <device>', 'Device to emulate, for example: "iPhone 15"')
2025-05-12 16:42:47 -07:00
.option('--user-data-dir <path>', 'Path to the user data directory. If not specified, a temporary directory will be created.')
.option('--in-memory', 'Use in-memory storage for user data directory.')
.option('--port <port>', 'Port to listen on for SSE transport.')
.option('--host <host>', 'Host to bind server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces.')
.option('--allowed-origins <origins>', 'Semicolon-separated list of origins to allow the browser to request. Default is to allow all.', semicolonSeparatedList)
.option('--blocked-origins <origins>', 'Semicolon-separated list of origins to block the browser from requesting. Blocklist is evaluated before allowlist. If used without the allowlist, requests not matching the blocklist are still allowed.', semicolonSeparatedList)
2025-03-21 10:58:58 -07:00
.option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
.option('--no-image-responses', 'Do not send image responses to the client.')
.option('--output-dir <path>', 'Path to the directory for output files.')
.option('--config <path>', 'Path to the configuration file.')
2025-03-21 10:58:58 -07:00
.action(async options => {
const config = await resolveConfig(options);
const connectionList: Connection[] = [];
setupExitWatchdog(connectionList);
if (options.port)
startHttpTransport(config, +options.port, options.host, connectionList);
else
await startStdioTransport(config, connectionList);
2025-03-21 10:58:58 -07:00
});
function setupExitWatchdog(connectionList: Connection[]) {
const handleExit = async () => {
2025-03-21 10:58:58 -07:00
setTimeout(() => process.exit(0), 15000);
for (const connection of connectionList)
await connection.close();
2025-03-21 10:58:58 -07:00
process.exit(0);
};
process.stdin.on('close', handleExit);
process.on('SIGINT', handleExit);
process.on('SIGTERM', handleExit);
2025-03-21 10:58:58 -07:00
}
function semicolonSeparatedList(value: string): string[] {
return value.split(';').map(v => v.trim());
}
2025-03-21 10:58:58 -07:00
program.parse(process.argv);