mirror of
https://github.com/microsoft/playwright-mcp.git
synced 2025-07-26 08:32:26 +08:00
chore: update exported types (#192)
Fixes https://github.com/microsoft/playwright-mcp/issues/186
This commit is contained in:
parent
bc48600a49
commit
e4331313f9
61
index.d.ts
vendored
61
index.d.ts
vendored
@ -15,33 +15,46 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { LaunchOptions } from 'playwright';
|
|
||||||
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
||||||
|
|
||||||
type ToolCapability = 'core' | 'tabs' | 'pdf' | 'history' | 'wait' | 'files' | 'install';
|
type ToolCapability = 'core' | 'tabs' | 'pdf' | 'history' | 'wait' | 'files' | 'install';
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
/**
|
/**
|
||||||
* Path to the user data directory.
|
* The browser to use (e.g., 'chrome', 'chromium', 'firefox', 'webkit', 'msedge').
|
||||||
*/
|
*/
|
||||||
userDataDir?: string;
|
browser?: string;
|
||||||
|
/**
|
||||||
/**
|
* Path to a user data directory for browser profile persistence.
|
||||||
* Launch options for the browser.
|
*/
|
||||||
*/
|
userDataDir?: string;
|
||||||
launchOptions?: LaunchOptions;
|
/**
|
||||||
|
* Whether to run the browser in headless mode (default: true).
|
||||||
/**
|
*/
|
||||||
* Use screenshots instead of snapshots. Less accurate, reliable and overall
|
headless?: boolean;
|
||||||
* slower, but contains visual representation of the page.
|
/**
|
||||||
* @default false
|
* Path to a custom browser executable.
|
||||||
*/
|
*/
|
||||||
vision?: boolean;
|
executablePath?: string;
|
||||||
|
/**
|
||||||
/**
|
* Chrome DevTools Protocol endpoint to connect to an existing browser instance.
|
||||||
* Capabilities to enable.
|
*/
|
||||||
*/
|
cdpEndpoint?: string;
|
||||||
capabilities?: ToolCapability[];
|
/**
|
||||||
|
* Enable vision capabilities (e.g., visual automation or OCR).
|
||||||
|
*/
|
||||||
|
vision?: boolean;
|
||||||
|
/**
|
||||||
|
* List of enabled tool capabilities. Possible values:
|
||||||
|
* - 'core': Core browser automation features.
|
||||||
|
* - 'tabs': Tab management features.
|
||||||
|
* - 'pdf': PDF generation and manipulation.
|
||||||
|
* - 'history': Browser history access.
|
||||||
|
* - 'wait': Wait and timing utilities.
|
||||||
|
* - 'files': File upload/download support.
|
||||||
|
* - 'install': Browser installation utilities.
|
||||||
|
*/
|
||||||
|
capabilities?: ToolCapability[];
|
||||||
};
|
};
|
||||||
|
export declare function createServer(options?: Options): Promise<Server>;
|
||||||
export function createServer(options?: Options): Server;
|
export {};
|
||||||
|
67
src/index.ts
67
src/index.ts
@ -14,6 +14,10 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import path from 'path';
|
||||||
|
import os from 'os';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
import { createServerWithTools } from './server';
|
import { createServerWithTools } from './server';
|
||||||
import common from './tools/common';
|
import common from './tools/common';
|
||||||
import files from './tools/files';
|
import files from './tools/files';
|
||||||
@ -58,9 +62,10 @@ const resources: Resource[] = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
browserName?: 'chromium' | 'firefox' | 'webkit';
|
browser?: string;
|
||||||
userDataDir?: string;
|
userDataDir?: string;
|
||||||
launchOptions?: LaunchOptions;
|
headless?: boolean;
|
||||||
|
executablePath?: string;
|
||||||
cdpEndpoint?: string;
|
cdpEndpoint?: string;
|
||||||
vision?: boolean;
|
vision?: boolean;
|
||||||
capabilities?: ToolCapability[];
|
capabilities?: ToolCapability[];
|
||||||
@ -68,7 +73,42 @@ type Options = {
|
|||||||
|
|
||||||
const packageJSON = require('../package.json');
|
const packageJSON = require('../package.json');
|
||||||
|
|
||||||
export function createServer(options?: Options): Server {
|
export async function createServer(options?: Options): Promise<Server> {
|
||||||
|
let browserName: 'chromium' | 'firefox' | 'webkit';
|
||||||
|
let channel: string | undefined;
|
||||||
|
switch (options?.browser) {
|
||||||
|
case 'chrome':
|
||||||
|
case 'chrome-beta':
|
||||||
|
case 'chrome-canary':
|
||||||
|
case 'chrome-dev':
|
||||||
|
case 'msedge':
|
||||||
|
case 'msedge-beta':
|
||||||
|
case 'msedge-canary':
|
||||||
|
case 'msedge-dev':
|
||||||
|
browserName = 'chromium';
|
||||||
|
channel = options.browser;
|
||||||
|
break;
|
||||||
|
case 'chromium':
|
||||||
|
browserName = 'chromium';
|
||||||
|
break;
|
||||||
|
case 'firefox':
|
||||||
|
browserName = 'firefox';
|
||||||
|
break;
|
||||||
|
case 'webkit':
|
||||||
|
browserName = 'webkit';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
browserName = 'chromium';
|
||||||
|
channel = 'chrome';
|
||||||
|
}
|
||||||
|
const userDataDir = options?.userDataDir ?? await createUserDataDir(browserName);
|
||||||
|
|
||||||
|
const launchOptions: LaunchOptions = {
|
||||||
|
headless: !!(options?.headless ?? (os.platform() === 'linux' && !process.env.DISPLAY)),
|
||||||
|
channel,
|
||||||
|
executablePath: options?.executablePath,
|
||||||
|
};
|
||||||
|
|
||||||
const allTools = options?.vision ? screenshotTools : snapshotTools;
|
const allTools = options?.vision ? screenshotTools : snapshotTools;
|
||||||
const tools = allTools.filter(tool => !options?.capabilities || tool.capability === 'core' || options.capabilities.includes(tool.capability));
|
const tools = allTools.filter(tool => !options?.capabilities || tool.capability === 'core' || options.capabilities.includes(tool.capability));
|
||||||
return createServerWithTools({
|
return createServerWithTools({
|
||||||
@ -76,9 +116,24 @@ export function createServer(options?: Options): Server {
|
|||||||
version: packageJSON.version,
|
version: packageJSON.version,
|
||||||
tools,
|
tools,
|
||||||
resources,
|
resources,
|
||||||
browserName: options?.browserName,
|
browserName,
|
||||||
userDataDir: options?.userDataDir ?? '',
|
userDataDir,
|
||||||
launchOptions: options?.launchOptions,
|
launchOptions,
|
||||||
cdpEndpoint: options?.cdpEndpoint,
|
cdpEndpoint: options?.cdpEndpoint,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function createUserDataDir(browserName: 'chromium' | 'firefox' | 'webkit') {
|
||||||
|
let cacheDirectory: string;
|
||||||
|
if (process.platform === 'linux')
|
||||||
|
cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
|
||||||
|
else if (process.platform === 'darwin')
|
||||||
|
cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
|
||||||
|
else if (process.platform === 'win32')
|
||||||
|
cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
|
||||||
|
else
|
||||||
|
throw new Error('Unsupported platform: ' + process.platform);
|
||||||
|
const result = path.join(cacheDirectory, 'ms-playwright', `mcp-${browserName}-profile`);
|
||||||
|
await fs.promises.mkdir(result, { recursive: true });
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
@ -15,9 +15,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import http from 'http';
|
import http from 'http';
|
||||||
import fs from 'fs';
|
|
||||||
import os from 'os';
|
|
||||||
import path from 'path';
|
|
||||||
|
|
||||||
import { program } from 'commander';
|
import { program } from 'commander';
|
||||||
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
||||||
@ -27,7 +24,6 @@ import { SSEServerTransport } from '@modelcontextprotocol/sdk/server/sse.js';
|
|||||||
import { createServer } from './index';
|
import { createServer } from './index';
|
||||||
import { ServerList } from './server';
|
import { ServerList } from './server';
|
||||||
|
|
||||||
import type { LaunchOptions } from 'playwright';
|
|
||||||
import assert from 'assert';
|
import assert from 'assert';
|
||||||
import { ToolCapability } from './tools/tool';
|
import { ToolCapability } from './tools/tool';
|
||||||
|
|
||||||
@ -45,46 +41,11 @@ program
|
|||||||
.option('--user-data-dir <path>', 'Path to the user data directory')
|
.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('--vision', 'Run server that uses screenshots (Aria snapshots are used by default)')
|
||||||
.action(async options => {
|
.action(async options => {
|
||||||
let browserName: 'chromium' | 'firefox' | 'webkit';
|
|
||||||
let channel: string | undefined;
|
|
||||||
switch (options.browser) {
|
|
||||||
case 'chrome':
|
|
||||||
case 'chrome-beta':
|
|
||||||
case 'chrome-canary':
|
|
||||||
case 'chrome-dev':
|
|
||||||
case 'msedge':
|
|
||||||
case 'msedge-beta':
|
|
||||||
case 'msedge-canary':
|
|
||||||
case 'msedge-dev':
|
|
||||||
browserName = 'chromium';
|
|
||||||
channel = options.browser;
|
|
||||||
break;
|
|
||||||
case 'chromium':
|
|
||||||
browserName = 'chromium';
|
|
||||||
break;
|
|
||||||
case 'firefox':
|
|
||||||
browserName = 'firefox';
|
|
||||||
break;
|
|
||||||
case 'webkit':
|
|
||||||
browserName = 'webkit';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
browserName = 'chromium';
|
|
||||||
channel = 'chrome';
|
|
||||||
}
|
|
||||||
|
|
||||||
const launchOptions: LaunchOptions = {
|
|
||||||
headless: !!(options.headless ?? (os.platform() === 'linux' && !process.env.DISPLAY)),
|
|
||||||
channel,
|
|
||||||
executablePath: options.executablePath,
|
|
||||||
};
|
|
||||||
|
|
||||||
const userDataDir = options.userDataDir ?? await createUserDataDir(browserName);
|
|
||||||
|
|
||||||
const serverList = new ServerList(() => createServer({
|
const serverList = new ServerList(() => createServer({
|
||||||
browserName,
|
browser: options.browser,
|
||||||
userDataDir,
|
userDataDir: options.userDataDir,
|
||||||
launchOptions,
|
headless: options.headless,
|
||||||
|
executablePath: options.executablePath,
|
||||||
vision: !!options.vision,
|
vision: !!options.vision,
|
||||||
cdpEndpoint: options.cdpEndpoint,
|
cdpEndpoint: options.cdpEndpoint,
|
||||||
capabilities: options.caps?.split(',').map((c: string) => c.trim() as ToolCapability),
|
capabilities: options.caps?.split(',').map((c: string) => c.trim() as ToolCapability),
|
||||||
@ -113,21 +74,6 @@ function setupExitWatchdog(serverList: ServerList) {
|
|||||||
|
|
||||||
program.parse(process.argv);
|
program.parse(process.argv);
|
||||||
|
|
||||||
async function createUserDataDir(browserName: 'chromium' | 'firefox' | 'webkit') {
|
|
||||||
let cacheDirectory: string;
|
|
||||||
if (process.platform === 'linux')
|
|
||||||
cacheDirectory = process.env.XDG_CACHE_HOME || path.join(os.homedir(), '.cache');
|
|
||||||
else if (process.platform === 'darwin')
|
|
||||||
cacheDirectory = path.join(os.homedir(), 'Library', 'Caches');
|
|
||||||
else if (process.platform === 'win32')
|
|
||||||
cacheDirectory = process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local');
|
|
||||||
else
|
|
||||||
throw new Error('Unsupported platform: ' + process.platform);
|
|
||||||
const result = path.join(cacheDirectory, 'ms-playwright', `mcp-${browserName}-profile`);
|
|
||||||
await fs.promises.mkdir(result, { recursive: true });
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function startSSEServer(port: number, serverList: ServerList) {
|
async function startSSEServer(port: number, serverList: ServerList) {
|
||||||
const sessions = new Map<string, SSEServerTransport>();
|
const sessions = new Map<string, SSEServerTransport>();
|
||||||
const httpServer = http.createServer(async (req, res) => {
|
const httpServer = http.createServer(async (req, res) => {
|
||||||
|
@ -89,14 +89,14 @@ export function createServerWithTools(options: Options): Server {
|
|||||||
|
|
||||||
export class ServerList {
|
export class ServerList {
|
||||||
private _servers: Server[] = [];
|
private _servers: Server[] = [];
|
||||||
private _serverFactory: () => Server;
|
private _serverFactory: () => Promise<Server>;
|
||||||
|
|
||||||
constructor(serverFactory: () => Server) {
|
constructor(serverFactory: () => Promise<Server>) {
|
||||||
this._serverFactory = serverFactory;
|
this._serverFactory = serverFactory;
|
||||||
}
|
}
|
||||||
|
|
||||||
async create() {
|
async create() {
|
||||||
const server = this._serverFactory();
|
const server = await this._serverFactory();
|
||||||
this._servers.push(server);
|
this._servers.push(server);
|
||||||
return server;
|
return server;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user