File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import os from 'node:os';
99import path from 'node:path' ;
1010import { fileURLToPath , pathToFileURL } from 'node:url' ;
1111
12+ import { disconnectBrowser } from './browser.js' ;
1213import type { TargetUniverse } from './DevtoolsUtils.js' ;
1314import { UniverseManager } from './DevtoolsUtils.js' ;
1415import { HeapSnapshotManager } from './HeapSnapshotManager.js' ;
@@ -146,6 +147,11 @@ export class McpContext implements Context {
146147 this . #isolatedContexts. clear ( ) ;
147148 }
148149
150+ async disconnect ( ) : Promise < void > {
151+ this . dispose ( ) ;
152+ await disconnectBrowser ( ) ;
153+ }
154+
149155 static async from (
150156 browser : Browser ,
151157 logger : Debugger ,
Original file line number Diff line number Diff line change @@ -270,4 +270,16 @@ export async function ensureBrowserLaunched(
270270 return browser ;
271271}
272272
273+ export async function disconnectBrowser ( ) : Promise < void > {
274+ const current = browser ;
275+ browser = undefined ;
276+ if ( current ?. connected ) {
277+ try {
278+ await current . disconnect ( ) ;
279+ } catch ( err ) {
280+ logger ( 'Error while disconnecting browser' , err ) ;
281+ }
282+ }
283+ }
284+
273285export type Channel = 'stable' | 'canary' | 'beta' | 'dev' ;
Original file line number Diff line number Diff line change @@ -179,6 +179,7 @@ export type Context = Readonly<{
179179 isCruxEnabled ( ) : boolean ;
180180 recordedTraces ( ) : TraceResult [ ] ;
181181 storeTraceRecording ( result : TraceResult ) : void ;
182+ disconnect ( ) : Promise < void > ;
182183 getPageById ( pageId : number ) : ContextPage ;
183184 newPage (
184185 background ?: boolean ,
Original file line number Diff line number Diff line change 1+ /**
2+ * @license
3+ * Copyright 2026 Google LLC
4+ * SPDX-License-Identifier: Apache-2.0
5+ */
6+
7+ import { ToolCategory } from './categories.js' ;
8+ import { defineTool } from './ToolDefinition.js' ;
9+
10+ export const disconnectBrowser = defineTool ( {
11+ name : 'disconnect_browser' ,
12+ description :
13+ 'Release the current connection to Chrome without exiting the MCP server.' ,
14+ annotations : {
15+ category : ToolCategory . NAVIGATION ,
16+ readOnlyHint : false ,
17+ } ,
18+ schema : { } ,
19+ blockedByDialog : false ,
20+ handler : async ( _request , response , context ) => {
21+ await context . disconnect ( ) ;
22+ response . appendResponseLine (
23+ 'Browser connection released. The next tool call will reconnect.' ,
24+ ) ;
25+ } ,
26+ } ) ;
Original file line number Diff line number Diff line change 66
77import type { ParsedArguments } from '../bin/chrome-devtools-mcp-cli-options.js' ;
88
9+ import * as connectionTools from './connection.js' ;
910import * as consoleTools from './console.js' ;
1011import * as emulationTools from './emulation.js' ;
1112import * as extensionTools from './extensions.js' ;
@@ -28,6 +29,7 @@ export const createTools = (args: ParsedArguments) => {
2829 const rawTools = args . slim
2930 ? Object . values ( slimTools )
3031 : [
32+ ...Object . values ( connectionTools ) ,
3133 ...Object . values ( consoleTools ) ,
3234 ...Object . values ( emulationTools ) ,
3335 ...Object . values ( extensionTools ) ,
You can’t perform that action at this time.
0 commit comments