2025-03-25 14:46:39 -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.
|
|
|
|
*/
|
|
|
|
|
2025-04-15 16:39:52 -07:00
|
|
|
import path from 'path';
|
|
|
|
import os from 'os';
|
|
|
|
import fs from 'fs';
|
|
|
|
|
2025-03-25 14:46:39 -07:00
|
|
|
import { createServerWithTools } from './server';
|
2025-04-04 15:22:00 -07:00
|
|
|
import common from './tools/common';
|
2025-04-15 18:01:59 -07:00
|
|
|
import console from './tools/console';
|
2025-04-17 14:03:13 -07:00
|
|
|
import dialogs from './tools/dialogs';
|
2025-04-04 17:14:30 -07:00
|
|
|
import files from './tools/files';
|
2025-04-04 15:22:00 -07:00
|
|
|
import install from './tools/install';
|
|
|
|
import keyboard from './tools/keyboard';
|
|
|
|
import navigate from './tools/navigate';
|
2025-04-22 16:04:25 -07:00
|
|
|
import network from './tools/network';
|
2025-04-04 15:22:00 -07:00
|
|
|
import pdf from './tools/pdf';
|
|
|
|
import snapshot from './tools/snapshot';
|
|
|
|
import tabs from './tools/tabs';
|
|
|
|
import screen from './tools/screen';
|
2025-03-25 14:46:39 -07:00
|
|
|
|
2025-04-04 17:14:30 -07:00
|
|
|
import type { Tool, ToolCapability } from './tools/tool';
|
2025-03-25 14:46:39 -07:00
|
|
|
import type { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
|
|
import type { LaunchOptions } from 'playwright';
|
|
|
|
|
2025-04-22 13:24:38 +02:00
|
|
|
const snapshotTools: Tool<any>[] = [
|
2025-04-15 01:09:48 +02:00
|
|
|
...common(true),
|
2025-04-15 18:01:59 -07:00
|
|
|
...console,
|
2025-04-17 14:03:13 -07:00
|
|
|
...dialogs(true),
|
2025-04-04 17:14:30 -07:00
|
|
|
...files(true),
|
2025-04-04 15:22:00 -07:00
|
|
|
...install,
|
|
|
|
...keyboard(true),
|
|
|
|
...navigate(true),
|
2025-04-22 16:04:25 -07:00
|
|
|
...network,
|
2025-04-04 15:22:00 -07:00
|
|
|
...pdf,
|
|
|
|
...snapshot,
|
|
|
|
...tabs(true),
|
2025-03-25 14:46:39 -07:00
|
|
|
];
|
|
|
|
|
2025-04-22 13:24:38 +02:00
|
|
|
const screenshotTools: Tool<any>[] = [
|
2025-04-15 01:09:48 +02:00
|
|
|
...common(false),
|
2025-04-15 18:01:59 -07:00
|
|
|
...console,
|
2025-04-17 14:03:13 -07:00
|
|
|
...dialogs(false),
|
2025-04-04 17:14:30 -07:00
|
|
|
...files(false),
|
2025-04-04 15:22:00 -07:00
|
|
|
...install,
|
|
|
|
...keyboard(false),
|
|
|
|
...navigate(false),
|
2025-04-22 16:04:25 -07:00
|
|
|
...network,
|
2025-04-04 15:22:00 -07:00
|
|
|
...pdf,
|
|
|
|
...screen,
|
|
|
|
...tabs(false),
|
2025-03-25 14:46:39 -07:00
|
|
|
];
|
|
|
|
|
|
|
|
type Options = {
|
2025-04-15 16:39:52 -07:00
|
|
|
browser?: string;
|
2025-03-26 15:02:45 -07:00
|
|
|
userDataDir?: string;
|
2025-04-15 16:39:52 -07:00
|
|
|
headless?: boolean;
|
|
|
|
executablePath?: string;
|
2025-03-30 09:05:58 -07:00
|
|
|
cdpEndpoint?: string;
|
2025-03-26 15:02:45 -07:00
|
|
|
vision?: boolean;
|
2025-04-04 17:14:30 -07:00
|
|
|
capabilities?: ToolCapability[];
|
2025-03-25 14:46:39 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const packageJSON = require('../package.json');
|
|
|
|
|
2025-04-15 16:39:52 -07:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2025-04-04 17:14:30 -07:00
|
|
|
const allTools = options?.vision ? screenshotTools : snapshotTools;
|
|
|
|
const tools = allTools.filter(tool => !options?.capabilities || tool.capability === 'core' || options.capabilities.includes(tool.capability));
|
2025-03-26 15:02:45 -07:00
|
|
|
return createServerWithTools({
|
|
|
|
name: 'Playwright',
|
|
|
|
version: packageJSON.version,
|
|
|
|
tools,
|
2025-04-15 18:01:59 -07:00
|
|
|
resources: [],
|
2025-04-15 16:39:52 -07:00
|
|
|
browserName,
|
|
|
|
userDataDir,
|
|
|
|
launchOptions,
|
2025-03-30 09:05:58 -07:00
|
|
|
cdpEndpoint: options?.cdpEndpoint,
|
2025-03-26 15:02:45 -07:00
|
|
|
});
|
2025-03-25 14:46:39 -07:00
|
|
|
}
|
2025-04-15 16:39:52 -07:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|