address
This commit is contained in:
@@ -28,7 +28,7 @@ export class VSCodeMCPFactory implements MCPFactory {
|
|||||||
if (typeof options.connectionString !== 'string')
|
if (typeof options.connectionString !== 'string')
|
||||||
throw new Error('Missing options.connectionString');
|
throw new Error('Missing options.connectionString');
|
||||||
if (typeof options.lib !== 'string')
|
if (typeof options.lib !== 'string')
|
||||||
throw new Error('Missing options.library');
|
throw new Error('Missing options.lib');
|
||||||
|
|
||||||
return new StdioClientTransport({
|
return new StdioClientTransport({
|
||||||
command: process.execPath,
|
command: process.execPath,
|
||||||
|
|||||||
@@ -21,26 +21,28 @@ import * as mcpServer from '../mcp/server.js';
|
|||||||
import { BrowserServerBackend } from '../browserServerBackend.js';
|
import { BrowserServerBackend } from '../browserServerBackend.js';
|
||||||
import { BrowserContextFactory, ClientInfo } from '../browserContextFactory.js';
|
import { BrowserContextFactory, ClientInfo } from '../browserContextFactory.js';
|
||||||
|
|
||||||
const config: FullConfig = JSON.parse(process.argv[2]);
|
|
||||||
const connectionString = new URL(process.argv[3]);
|
|
||||||
const lib = process.argv[4];
|
|
||||||
|
|
||||||
const playwright = await import(lib).then(mod => mod.default ?? mod) as typeof import('playwright');
|
|
||||||
|
|
||||||
class VSCodeBrowserContextFactory implements BrowserContextFactory {
|
class VSCodeBrowserContextFactory implements BrowserContextFactory {
|
||||||
name = 'vscode';
|
name = 'vscode';
|
||||||
description = 'Connect to a browser running in the Playwright VS Code extension';
|
description = 'Connect to a browser running in the Playwright VS Code extension';
|
||||||
|
|
||||||
|
constructor(private _config: FullConfig, private _playwright: typeof import('playwright'), private _connectionString: string) {}
|
||||||
|
|
||||||
async createContext(clientInfo: ClientInfo, abortSignal: AbortSignal): Promise<{ browserContext: BrowserContext; close: () => Promise<void>; }> {
|
async createContext(clientInfo: ClientInfo, abortSignal: AbortSignal): Promise<{ browserContext: BrowserContext; close: () => Promise<void>; }> {
|
||||||
connectionString.searchParams.set('launch-options', JSON.stringify({
|
let launchOptions: any = this._config.browser.launchOptions;
|
||||||
...config.browser.launchOptions,
|
if (this._config.browser.userDataDir) {
|
||||||
...config.browser.contextOptions,
|
launchOptions = {
|
||||||
userDataDir: config.browser.userDataDir,
|
...launchOptions,
|
||||||
}));
|
...this._config.browser.contextOptions,
|
||||||
|
userDataDir: this._config.browser.userDataDir,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
const connectionString = new URL(this._connectionString);
|
||||||
|
connectionString.searchParams.set('launch-options', JSON.stringify(launchOptions));
|
||||||
|
|
||||||
const browser = await playwright.chromium.connect(connectionString.toString());
|
const browserType = this._playwright.chromium; // it could also be firefox or webkit, we just need some browser type to call `connect` on
|
||||||
|
const browser = await browserType.connect(connectionString.toString());
|
||||||
|
|
||||||
const context = browser.contexts()[0] ?? await browser.newContext(config.browser.contextOptions);
|
const context = browser.contexts()[0] ?? await browser.newContext(this._config.browser.contextOptions);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
browserContext: context,
|
browserContext: context,
|
||||||
@@ -51,8 +53,18 @@ class VSCodeBrowserContextFactory implements BrowserContextFactory {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function main(config: FullConfig, connectionString: string, lib: string) {
|
||||||
|
const playwright = await import(lib).then(mod => mod.default ?? mod);
|
||||||
|
const factory = new VSCodeBrowserContextFactory(config, playwright, connectionString);
|
||||||
await mcpServer.connect(
|
await mcpServer.connect(
|
||||||
() => new BrowserServerBackend(config, new VSCodeBrowserContextFactory()),
|
() => new BrowserServerBackend(config, factory),
|
||||||
new StdioServerTransport(),
|
new StdioServerTransport(),
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await main(
|
||||||
|
JSON.parse(process.argv[2]),
|
||||||
|
process.argv[3],
|
||||||
|
process.argv[4]
|
||||||
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user