feat: add multi-session support for concurrent Chrome instances#899
Open
RobertWsp wants to merge 4 commits intoChromeDevTools:mainfrom
Open
feat: add multi-session support for concurrent Chrome instances#899RobertWsp wants to merge 4 commits intoChromeDevTools:mainfrom
RobertWsp wants to merge 4 commits intoChromeDevTools:mainfrom
Conversation
Introduce SessionManager class that manages multiple isolated Chrome browser instances, each identified by a unique sessionId. Add three new session tools: create_session, list_sessions, and close_session. - Per-session Mutex ensures tool serialization within a session while allowing cross-session parallelism - Orphan prevention: browser is closed if McpContext creation fails - Auto-purge via browser 'disconnected' event listener - Graceful shutdown with #shuttingDown guard - SESSION category added to ToolCategory enum
Replace singleton browser/context with SessionManager. All existing browser tools now receive a mandatory sessionId parameter injected transparently via registerBrowserTool() wrapper — original tool handlers remain unmodified. - registerBrowserTool(): injects sessionId into Zod schema, resolves correct session's McpContext, acquires per-session mutex - registerSessionTool(): full implementations for create/list/close - Signal handling with process.once() and 10s shutdown timeout - Safety check rejects tools that define their own sessionId schema
17 tests covering session creation, retrieval, listing, closing, parallel session isolation, per-session mutex serialization, auto-purge on browser disconnect, and shutdown rejection.
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Author
|
I have signed the CLA. Please re-check. |
Collaborator
|
Thanks for the PR but we do not plan to support multiple sessions right now. Could you please file a feature request to help us better understand scenarios you plan to use it for? |
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
Add multi-session support allowing multiple independent Chrome browser instances to run simultaneously, each identified by a unique
sessionId. This enables parallel browser automation workflows (e.g., testing multiple pages side-by-side, comparing environments) without needing multiple MCP server processes.Motivation
The current architecture supports only a single Chrome browser instance per server. This limits use cases where an MCP client needs to interact with multiple browser contexts concurrently — for example, comparing two web pages, running parallel test suites, or managing multiple authenticated sessions.
Changes
New files
src/SessionManager.ts— Core session lifecycle manager with per-sessionMutex, auto-purge on browser disconnect, orphan prevention, and graceful shutdownsrc/tools/session.ts— Three new tools:create_session,list_sessions,close_sessiontests/SessionManager.test.ts— 17 E2E tests covering creation, retrieval, listing, closing, parallel isolation, mutex serialization, auto-purge, and shutdown rejectionModified files
src/main.ts— Replaced singletonbrowser/contextwithSessionManager. AddedregisterBrowserTool()wrapper that transparently injectssessionIdinto every existing tool's Zod schema and resolves the correct session'sMcpContext— original tool handlers remain unmodifiedsrc/tools/categories.ts— AddedSESSIONcategorysrc/tools/tools.ts— Registered session tools, exportedsessionToolNamessetDesign decisions
registerBrowserTool()wraps each tool, injectingsessionIdinto the schema and resolving the correctMcpContextbefore calling the unmodified handlerisolated: trueforced: All sessions use isolated Chrome profiles to prevent conflictsMap<string, SessionInfo>sessionIdschema (prevents conflicts)Error handling
McpContext.from()fails afterlaunch(), the browser is closedcloseSession()acquires the session mutex before closingprocess.once('SIGINT'/'SIGTERM')with#shuttingDownguard and 10s timeoutbrowser.on('disconnected')instanceof Error(no unsafeerr?.message)Test results
Breaking changes
All existing browser tools now require a mandatory
sessionIdparameter. Clients must callcreate_sessionfirst to obtain a session ID before using any other tool.