Skip to content

Commit 6c0653b

Browse files
committed
feat: support connecting an already launched browser instance
1 parent 1934369 commit 6c0653b

3 files changed

Lines changed: 28 additions & 9 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@privatewebai/chrome-devtools-mcp",
3-
"version": "0.12.6",
3+
"version": "0.12.7",
44
"description": "MCP server for Chrome DevTools",
55
"type": "module",
66
"bin": "./build/src/index.js",

src/browser.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,31 @@ export async function launch(options: McpLaunchOptions): Promise<Browser> {
236236
userDataDir &&
237237
(error as Error).message.includes('The browser is already running')
238238
) {
239-
throw new Error(
240-
`The browser is already running for ${userDataDir}. Use --isolated to run multiple browser instances.`,
241-
{
242-
cause: error,
243-
},
244-
);
239+
// Browser is already running, try to connect to it
240+
logger(`Browser already running for ${userDataDir}, attempting to connect...`);
241+
242+
// Check if a specific remote debugging port was specified in args
243+
const debugPortArg = args.find(arg => arg.startsWith('--remote-debugging-port='));
244+
const debugPort = debugPortArg ? parseInt(debugPortArg.split('=')[1], 10) : undefined;
245+
246+
try {
247+
const connectedBrowser = await ensureBrowserConnected({
248+
browserURL: debugPort ? `http://localhost:${debugPort}` : undefined,
249+
userDataDir: debugPort ? undefined : userDataDir,
250+
devtools: options.devtools,
251+
channel: options.channel,
252+
});
253+
logger('Successfully connected to existing browser');
254+
return connectedBrowser;
255+
} catch (connectError) {
256+
logger(`Failed to connect to existing browser:`, connectError);
257+
throw new Error(
258+
`The browser is already running for ${userDataDir}. Use --isolated to run multiple browser instances.`,
259+
{
260+
cause: error,
261+
},
262+
);
263+
}
245264
}
246265
throw error;
247266
}

0 commit comments

Comments
 (0)