This commit is contained in:
Tommy D. Rossi
2025-11-13 19:51:52 +01:00
parent fbacaf8832
commit ca2485f3d5
3 changed files with 44 additions and 5 deletions
+22 -3
View File
@@ -22,6 +22,8 @@
* - /extension/guid - Extension connection for chrome.debugger forwarding
*/
import net from 'net'
import { spawn } from 'child_process';
import http from 'http';
@@ -29,14 +31,13 @@ import { debug, ws, wsServer } from 'playwright-core/lib/utilsBundle';
import { registry } from 'playwright-core/lib/server/registry/index';
import { ManualPromise } from 'playwright-core/lib/utils';
import { httpAddressToString } from '../sdk/http.js';
import { logUnhandledError } from '../log.js';
import * as protocol from './protocol.js';
import type websocket from 'ws';
import type { ClientInfo } from '../sdk/server.js';
import type { ExtensionCommand, ExtensionEvents } from './protocol.js';
import type { WebSocket, WebSocketServer } from 'playwright-core/lib/utilsBundle';
import { ClientInfo } from './types.js';
const debugLogger = debug('pw:mcp:relay');
@@ -219,6 +220,7 @@ export class CDPRelayServer {
void this._extensionConnectionPromise.catch(logUnhandledError);
}
private _closePlaywrightConnection(reason: string) {
if (this._playwrightConnection?.readyState === ws.OPEN)
this._playwrightConnection.close(1000, reason);
@@ -422,3 +424,20 @@ class ExtensionConnection {
this._callbacks.clear();
}
}
export function httpAddressToString(address: string | net.AddressInfo | null): string {
if (!address)
throw new Error('Invalid null address passeds to httpAddressToString');
if (typeof address === 'string')
return address;
const resolvedPort = address.port;
let resolvedHost = address.family === 'IPv4' ? address.address : `[${address.address}]`;
if (resolvedHost === '0.0.0.0' || resolvedHost === '[::]')
resolvedHost = 'localhost';
return `http://${resolvedHost}:${resolvedPort}`;
}
function logUnhandledError(e: unknown) {
debugLogger('Unhandled promise rejection:', e instanceof Error ? e.stack || e.message : e);
}
@@ -21,9 +21,9 @@ import http from 'http';
import net from 'net'
import { CDPRelayServer } from './cdpRelay.js';
import { BrowserContextFactory, ClientInfo } from './types.js';
import type { BrowserContextFactory } from '../browser/browserContextFactory.js';
import type { ClientInfo } from '../sdk/server';
const debugLogger = debug('pw:mcp:relay');
+20
View File
@@ -0,0 +1,20 @@
import * as playwright from 'playwright-core';
export type BrowserContextFactoryResult = {
browserContext: playwright.BrowserContext;
close: (afterClose: () => Promise<void>) => Promise<void>;
};
export interface BrowserContextFactory {
createContext(clientInfo: ClientInfo, abortSignal: AbortSignal, toolName: string | undefined): Promise<BrowserContextFactoryResult>;
}
export type ClientInfo = {
name: string;
version: string;
roots: Root[];
timestamp: number;
};
type Root = never // something mcp