feat: add dynamic session management for multiple browser instances#1241
Open
awsmadi wants to merge 4 commits intoChromeDevTools:mainfrom
Open
feat: add dynamic session management for multiple browser instances#1241awsmadi wants to merge 4 commits intoChromeDevTools:mainfrom
awsmadi wants to merge 4 commits intoChromeDevTools:mainfrom
Conversation
Add support for dynamically creating and connecting to browser sessions at runtime instead of being bound to a single browser instance configured at startup. Gated behind --category-session flag. New tools: create_session, list_sessions, select_session, close_session. Each session wraps its own browser connection and McpContext. Existing tools operate transparently on the active session via getContext(). - Remove browser.ts singleton pattern; make connect/launch pure factories - Add SessionManager class to manage multiple sessions - Add McpContext.close(mode) for browser-aware disposal - Add SESSION tool category and --category-session CLI flag - Wire SessionManager into index.ts, replacing inline browser init
- Add multi-session support to Key features - Add session management tools section (opt-in) with config example - Add session management concept section explaining usage - Add session tool reference docs (create_session, list_sessions, select_session, close_session) - Auto-generated --categorySession option via npm run gen - Formatting fixes from prettier
Re-add the server.oninitialized hook that records which MCP client is connected (Claude Code, Gemini CLI, etc.) in telemetry payloads. This was inadvertently dropped during the SessionManager refactor.
- SessionManager tests (18 cases): hasActiveSession, getActiveContext, launchSession, selectSession, selectSessionByName, closeSession, listSessions, disposeAll — covers state management, auto-selection, duplicate name rejection, and error cases - Session tools tests (14 cases): tool count/categories, create_session (launch/connect), list_sessions (empty/populated), select_session (by ID/name/missing), close_session (with/without remaining sessions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds session management support to chrome-devtools-mcp, enabling agents to work with multiple browser instances simultaneously. Sessions can be launched (new browser process) or connected (attach to existing browser via URL/WebSocket), switched between by ID or name, and independently closed — all through 4 new MCP tools gated behind the
--category-sessionflag.Motivation
The current architecture manages a single browser instance via a module-level singleton. This limits agents to one browser at a time, which is insufficient for workflows that require:
Changes
New:
SessionManagerclass (src/SessionManager.ts)launched: starts a new browser process, closed onclose_sessionconnected: attaches to an existing browser, disconnected onclose_session(browser keeps running)New: Session tools (
src/tools/sessions.ts)Four new tools, available when
--category-sessionis enabled:create_sessionname,headless,channel,isolated,browserUrl,wsEndpoint, andwsHeadersparameters.list_sessionsselect_sessionclose_sessionRefactored: Browser lifecycle (
src/browser.ts,src/index.ts)browsersingleton frombrowser.ts, makingensureBrowserConnectedandensureBrowserLaunchedstateless (each call creates a new connection)index.tstoSessionManager.createDefaultSession()getContext()now delegates toSessionManager.getActiveContext()New:
McpContext.close()method (src/McpContext.ts)close(mode: 'close' | 'disconnect')for proper session cleanupNew CLI flag
--categorySession/--category-session(default:false) — enables the session management toolsDocumentation
--categorySessionto auto-generated options (vianpm run gen)docs/tool-reference.mdBackward compatibility
--category-session(default:false)Test plan
npm run buildpassesnpm run genregenerates docs without errorstests/cli.test.tsupdated with newcategorySessiondefaults--category-sessionexposes 4 new tools via MCP inspectorcreate_sessionwithtype: "launch"starts a new browsercreate_sessionwithtype: "connect"attaches to a running browserselect_sessionswitches active context for subsequent tool callsclose_sessioncleans up browser and auto-selects next session