Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/browserServerBackend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 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 { FullConfig } from './config.js';
import { Context } from './context.js';
import { logUnhandledError } from './log.js';
import { Response } from './response.js';
import { SessionLog } from './sessionLog.js';
import { filteredTools } from './tools.js';
import { packageJSON } from './package.js';

import type { BrowserContextFactory } from './browserContextFactory.js';
import type * as mcpServer from './mcp/server.js';
import type { ServerBackend } from './mcp/server.js';
import type { Tool } from './tools/tool.js';

export class BrowserServerBackend implements ServerBackend {
name = 'Playwright';
version = packageJSON.version;
private _tools: Tool[];
private _context: Context;
private _sessionLog: SessionLog | undefined;

constructor(config: FullConfig, browserContextFactory: BrowserContextFactory) {
this._tools = filteredTools(config);
this._context = new Context(this._tools, config, browserContextFactory);
}

async initialize() {
this._sessionLog = this._context.config.saveSession ? await SessionLog.create(this._context.config) : undefined;
}

tools(): mcpServer.ToolSchema<any>[] {
return this._tools.map(tool => tool.schema);
}

async callTool(schema: mcpServer.ToolSchema<any>, parsedArguments: any) {
const response = new Response(this._context, schema.name, parsedArguments);
const tool = this._tools.find(tool => tool.schema.name === schema.name)!;
await tool.handle(this._context, parsedArguments, response);
if (this._sessionLog)
await this._sessionLog.log(response);
return await response.serialize();
}

serverInitialized(version: mcpServer.ClientVersion | undefined) {
this._context.clientVersion = version;
}

serverClosed() {
void this._context.dispose().catch(logUnhandledError);
}
}
84 changes: 0 additions & 84 deletions src/connection.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/extension/cdpRelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import { spawn } from 'child_process';
import { WebSocket, WebSocketServer } from 'ws';
import debug from 'debug';
import * as playwright from 'playwright';
import { httpAddressToString, startHttpServer } from '../transport.js';
// @ts-ignore
const { registry } = await import('playwright-core/lib/server/registry/index');

import { httpAddressToString, startHttpServer } from '../httpServer.js';
import type { BrowserContextFactory } from '../browserContextFactory.js';
import type websocket from 'ws';

Expand Down
16 changes: 4 additions & 12 deletions src/extension/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@
* limitations under the License.
*/

import { startHttpServer, startHttpTransport, startStdioTransport } from '../transport.js';
import { Server } from '../server.js';
import { startCDPRelayServer } from './cdpRelay.js';
import { filteredTools } from '../tools.js';
import { BrowserServerBackend } from '../browserServerBackend.js';
import * as mcpTransport from '../mcp/transport.js';

import type { FullConfig } from '../config.js';

export async function runWithExtension(config: FullConfig) {
const contextFactory = await startCDPRelayServer(config.browser.launchOptions.channel || 'chrome');
const server = new Server(config, filteredTools(config), contextFactory);
server.setupExitWatchdog();

if (config.server.port !== undefined) {
const httpServer = await startHttpServer(config.server);
startHttpTransport(httpServer, server);
} else {
await startStdioTransport(server);
}
const serverBackendFactory = () => new BrowserServerBackend(config, contextFactory);
await mcpTransport.start(serverBackendFactory, config.server);
}
Loading
Loading