playwright-selector-finder
Version:
A tool for finding and interacting with elements using Playwright's vision locators
75 lines (74 loc) • 2.63 kB
JavaScript
;
/**
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.MCPServer = void 0;
const test_1 = require("@playwright/test");
const context_1 = require("./context");
const common_1 = require("./tools/common");
class MCPServer {
context;
tools;
options;
constructor(options = {}) {
this.options = {
headless: options.headless ?? true,
visionTimeout: options.visionTimeout ?? 5000,
visionConfidence: options.visionConfidence ?? 0.7
};
this.tools = new Map([
['browser_navigate', common_1.NavigateTool],
['browser_go_back', common_1.GoBackTool],
['browser_go_forward', common_1.GoForwardTool],
['browser_click', common_1.ClickTool],
['browser_hover', common_1.HoverTool],
['browser_type', common_1.TypeTool],
['browser_press_key', common_1.PressKeyTool],
['browser_wait', common_1.WaitTool],
['browser_save_as_pdf', common_1.SaveAsPDFTool],
['browser_close', common_1.CloseTool],
]);
}
async start() {
const browser = await test_1.chromium.launch({
headless: this.options.headless
});
this.context = new context_1.Context(browser, this.options);
}
async stop() {
await this.context?.close();
this.context = undefined;
}
async listTools() {
const tools = Array.from(this.tools.values()).map(tool => ({
name: tool.schema.name,
description: tool.schema.description,
inputSchema: tool.schema.inputSchema
}));
return {
tools
};
}
async callTool(name, params) {
if (!this.context)
throw new Error('Server not started');
const tool = this.tools.get(name);
if (!tool)
throw new Error(`Unknown tool: ${name}`);
return await tool.handle(this.context, params);
}
}
exports.MCPServer = MCPServer;