chore: allow passing config file (#281)

This commit is contained in:
Pavel Feldman
2025-04-28 15:04:59 -07:00
committed by GitHub
parent 23704ace1f
commit 26779ceb20
6 changed files with 139 additions and 70 deletions

View File

@@ -32,7 +32,8 @@ import snapshot from './tools/snapshot';
import tabs from './tools/tabs';
import screen from './tools/screen';
import type { Tool, ToolCapability } from './tools/tool';
import type { Tool } from './tools/tool';
import type { Config } from '../config';
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
import type { LaunchOptions, BrowserContextOptions } from 'playwright';
@@ -64,22 +65,12 @@ const screenshotTools: Tool<any>[] = [
...tabs(false),
];
type Options = {
browser?: string;
userDataDir?: string;
headless?: boolean;
executablePath?: string;
cdpEndpoint?: string;
vision?: boolean;
capabilities?: ToolCapability[];
};
const packageJSON = require('../package.json');
export async function createServer(options?: Options): Promise<Server> {
export async function createServer(config?: Config): Promise<Server> {
let browserName: 'chromium' | 'firefox' | 'webkit';
let channel: string | undefined;
switch (options?.browser) {
switch (config?.browser?.type) {
case 'chrome':
case 'chrome-beta':
case 'chrome-canary':
@@ -90,7 +81,7 @@ export async function createServer(options?: Options): Promise<Server> {
case 'msedge-canary':
case 'msedge-dev':
browserName = 'chromium';
channel = options.browser;
channel = config.browser.type;
break;
case 'firefox':
browserName = 'firefox';
@@ -102,18 +93,18 @@ export async function createServer(options?: Options): Promise<Server> {
browserName = 'chromium';
channel = 'chrome';
}
const userDataDir = options?.userDataDir ?? await createUserDataDir(browserName);
const userDataDir = config?.browser?.userDataDir ?? await createUserDataDir(browserName);
const launchOptions: LaunchOptions & BrowserContextOptions = {
headless: !!(options?.headless ?? (os.platform() === 'linux' && !process.env.DISPLAY)),
headless: !!(config?.browser?.headless ?? (os.platform() === 'linux' && !process.env.DISPLAY)),
channel,
executablePath: options?.executablePath,
executablePath: config?.browser?.executablePath,
viewport: null,
...{ assistantMode: true },
};
const allTools = options?.vision ? screenshotTools : snapshotTools;
const tools = allTools.filter(tool => !options?.capabilities || tool.capability === 'core' || options.capabilities.includes(tool.capability));
const allTools = config?.vision ? screenshotTools : snapshotTools;
const tools = allTools.filter(tool => !config?.capabilities || tool.capability === 'core' || config.capabilities.includes(tool.capability));
return createServerWithTools({
name: 'Playwright',
version: packageJSON.version,
@@ -122,7 +113,7 @@ export async function createServer(options?: Options): Promise<Server> {
browserName,
userDataDir,
launchOptions,
cdpEndpoint: options?.cdpEndpoint,
cdpEndpoint: config?.browser?.cdpEndpoint,
});
}

View File

@@ -14,14 +14,17 @@
* limitations under the License.
*/
import fs from 'fs';
import { program } from 'commander';
import { createServer } from './index';
import { ServerList } from './server';
import { ToolCapability } from './tools/tool';
import { startHttpTransport, startStdioTransport } from './transport';
import type { Config, ToolCapability } from '../config';
const packageJSON = require('../package.json');
program
@@ -32,20 +35,29 @@ program
.option('--cdp-endpoint <endpoint>', 'CDP endpoint to connect to.')
.option('--executable-path <path>', 'Path to the browser executable.')
.option('--headless', 'Run browser in headless mode, headed by default')
.option('--user-data-dir <path>', 'Path to the 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('--user-data-dir <path>', 'Path to the user data directory')
.option('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
.option('--config <path>', 'Path to the configuration file.')
.action(async options => {
const serverList = new ServerList(() => createServer({
browser: options.browser,
userDataDir: options.userDataDir,
headless: options.headless,
executablePath: options.executablePath,
vision: !!options.vision,
cdpEndpoint: options.cdpEndpoint,
const cliOverrides: Config = {
browser: {
type: options.browser,
userDataDir: options.userDataDir,
headless: options.headless,
executablePath: options.executablePath,
cdpEndpoint: options.cdpEndpoint,
},
server: {
port: options.port,
host: options.host,
},
capabilities: options.caps?.split(',').map((c: string) => c.trim() as ToolCapability),
}));
vision: !!options.vision,
};
const config = await loadConfig(options.config, cliOverrides);
const serverList = new ServerList(() => createServer(config));
setupExitWatchdog(serverList);
if (options.port)
@@ -54,6 +66,30 @@ program
await startStdioTransport(serverList);
});
async function loadConfig(configFile: string | undefined, cliOverrides: Config): Promise<Config> {
if (!configFile)
return cliOverrides;
try {
const config = JSON.parse(await fs.promises.readFile(configFile, 'utf8'));
return {
...config,
...cliOverrides,
browser: {
...config.browser,
...cliOverrides.browser,
},
server: {
...config.server,
...cliOverrides.server,
},
};
} catch (e) {
console.error(`Error loading config file ${configFile}: ${e}`);
process.exit(1);
}
}
function setupExitWatchdog(serverList: ServerList) {
const handleExit = async () => {
setTimeout(() => process.exit(0), 15000);

View File

@@ -18,7 +18,7 @@ import type { ImageContent, TextContent } from '@modelcontextprotocol/sdk/types'
import type { z } from 'zod';
import type { Context } from '../context';
import type * as playwright from 'playwright';
export type ToolCapability = 'core' | 'tabs' | 'pdf' | 'history' | 'wait' | 'files' | 'install';
import type { ToolCapability } from '../../config';
export type ToolSchema<Input extends InputType> = {
name: string;