How to start multiple Codex browser sessions
To run multiple Codex sessions simultaneously, I wrote a browser launcher. Before actually starting, I investigated three questions: How does Chrome DevTools MCP inject into Codex, and can it be modified during runtime? Where is the Chrome profile specified? And how is the CDP port automatically connected? Once these three questions were clarified, the solution was pretty much finalized.
I initially thought this would just mean opening two more Chrome windows. However, in practice, one session connects to the browser in the other session; sometimes closing one Codex window also closes the other Chrome window. Even with two windows open, the connections at the bottom are still mixed up.
The final implementation is simple: each Codex session uses its own CDP port and Chrome profile, and the corresponding MCP configuration is passed to Codex at startup. These three items must be used together and cannot be mixed. The script also supports specifying or reusing profiles and retains Codex's resume capabilities.
First, draw the connection relationship clearly.
CDP (Chrome DevTools Protocol) is Chrome's debugging and automation interface. The port is its entry point. chrome-devtools-mcp It acts as a bridge between Codex and CDP, converting tool calls like "opening a page, executing scripts, and reading the DOM" into CDP requests.
The connection between the two sessions should be as follows:
Codex session A ──▶ MCP bridge A ──▶ Chrome A
CDP :49101 profile: work-a
Codex session B ──▶ MCP bridge B ──▶ Chrome B
CDP :49102 profile: work-b
What needs to be isolated here is the entire link. Opening multiple windows individually is useless; if there is a crossover in MCP, port, or profile, the two sessions will still affect each other.
First, a prerequisite: autoConnect comes from Chrome's official MCP.
autoConnect This connection method is not implemented by the launcher; it is chrome-devtools-mcp provided by Chrome itself.
According to the official Chrome documentation, Chrome versions 144 and above allow chrome://inspect/#remote-debugging enabling remote debugging, and then adding the following to the MCP server --autoConnect:
{
"mcpServers"
:
{
"chrome-devtools"
:
{
"command"
:
"npx"
,
"args"
:
[
"chrome-devtools-mcp@latest
,
"--autoConnect"
]
}
}
}
This approach is suitable for allowing the agent to take over an already opened local Chrome session, and can also directly use the existing login state. However, it has a prerequisite related to multiple sessions: if Chrome has multiple active profiles at the same time, MCP will connect to the default profile determined by Chrome and access all windows under that profile.
This is precisely the problem I was trying to solve when writing the launcher. What I wanted wasn't to "find a running Chrome," but rather to have each Codex session connect to a profile pre-assigned to it. So, instead of using a script --autoConnect, I launched a separate Chrome instance and then --browserUrl specified the connection target.
Where exactly is the MCP injected?
I initially misunderstood the term "injection." chrome-devtools-mcp It doesn't actually enter Chrome; it registers with Codex as an MCP server and then uses CDP to control Chrome.
-c Codex can receive this configuration at startup:
codex \
-c
'mcp_servers.chrome-devtools.command="node"'
\
-c
"mcp_servers.chrome-devtools.args=[..., "--browserUrl","
$devtools_url
"]"
The first line tells Codex how to start the MCP server. The second line --browserUrl tells it chrome-devtools-mcp which Chrome browser to connect to.
This also determines when the configuration should be passed in. If multiple sessions are running, each session should be passed in individually when starting Codex browserUrl. This MCP configuration only applies to the current Codex process.
profile is not a browser window
The second issue is profile.
Chrome's profile is essentially a writable directory. Cookies, cache, extension settings, Service Workers, and login state are all stored there. Chrome also locks the directory to prevent multiple processes from writing to it simultaneously.
When starting Chrome, you can --user-data-dir specify this directory:
chrome \
--remote-debugging-port=
"
$port
"
\
--user-data-dir=
"
$profile_dir
"
If two Chrome processes share the same directory, the second process will typically fail to start due to lock conflicts. Even if they manage to share it, login status and cache will be mixed together, making the problem even harder to troubleshoot.
The launcher assigns a separate directory to each session. Running ./codex-browser by itself picks a port and names the session after it, such as codex-49101. This is useful for a fresh, temporary session.
If I want to continue using my original login state on the next startup, I can fix it CODEX_BROWSER_SESSION:
CODEX_BROWSER_SESSION=work-a ./codex-browser
work-a Each profile will correspond to a fixed directory. After exiting the current session, restarting with the same session name will reuse the cookies, login status, and site data. CODEX_BROWSER_PROFILE_ROOT The storage location of these profiles can be modified, but this is generally not necessary.
Reuse occurs between two startups. Two concurrent sessions still cannot use the same CODEX_BROWSER_SESSION directory; otherwise, they will contend for it.
How to connect the starter to the correct CDP port
The third issue is the CDP port.
Chrome --remote-debugging-port exposes CDPs, for example:
By default, the script requests a currently available local port from the operating system. After Chrome starts, the script continues to request this port /json/version until it confirms that CDP is accessible. For everyday use, you don't need to worry about the specific port.
CODEX_CHROME_PORT It's possible to fix the port, but that's advanced usage. I only set it that way when external tools need to connect to a known CDP address, or when troubleshooting with a fixed port.
Once Chrome is ready, the launcher already knows the selected option $devtools_url and will pass it as --browserUrl the connection chrome-devtools-mcp. This step uses the official MCP's ability to specify a connection address, not --autoConnect...
The startup process can be broken down into three steps:
-
- The port is automatically selected by default; you only need to manually specify it when necessary.
-
- Launch Chrome and wait for CDP to be ready;
-
- Pass the same URL into the current session's MCP configuration.
To reuse a running Chrome instance, pass --browser-url or set CODEX_CHROME_DEVTOOLS_URL. The launcher checks /json/version to confirm the CDP endpoint works before it starts Codex.
The port can be selected automatically, but it must be explicitly bound after selection.
All three parameters must follow the same session.
Looking back at these three questions, they correspond to a complete link:
Codex session ↔ MCP browserUrl ↔ CDP port ↔ Chrome profile
The MCP configuration determines which browser the bridge connects to browserUrl. The CDP port points to a specific Chrome process. The profile then determines which browser state this Chrome instance uses.
The launcher's job is to bind these items together at startup. This way, each Codex session has a clearly defined browser affiliation, and MCP doesn't need to guess which Chrome to connect to on the fly.
Whoever launches Chrome is responsible for shutting it down.
After isolation of connections, there is another easily overlooked issue: which Chrome should be closed when exiting?
codex-browser Upon launching Chrome, it records the PID and registers it trap ... EXIT INT TERM. When Codex exits, it only closes the Chrome instance it created.
If I pass in an existing process --browser-url, the launcher will only handle the connection and will not close that Chrome instance, because it is not a process created for this launch.
The script should also fail immediately if it encounters a port or profile conflict. If a script secretly reuses an existing port, Codex might connect to someone else's browser. Providing an error message here is actually safer and makes troubleshooting easier.
How do I start it now?
The simplest way to use it is to run it directly:
./codex-browser
To open multiple sessions, run the same command in different terminals. Each startup will automatically select a port and create an independent profile.
The script also supports resuming Codex sessions:
./codex-browser
./codex-browser last
r It will switch to codex resume allowing you to choose which session to resume; last or it will switch codex resume --last to directly continuing the most recent session.
If a session needs to be used again later, simply assign it a fixed session name. The port will still be automatically assigned by the script.
CODEX_BROWSER_SESSION=work-a ./codex-browser
The next time the same name is used, work-a the corresponding profile will be opened. It can also resume be used in conjunction with [another tool/tool] to restore both the Codex session and browser state simultaneously.
CODEX_BROWSER_SESSION=work-a ./codex-browser r
Only specify the port when a fixed CDP address is truly required:
CODEX_CHROME_PORT=49101 CODEX_BROWSER_SESSION=work-a ./codex-browser
The real issue this time isn't whether Chrome can run multiple instances. Chrome can run multiple instances anyway. The challenge is ensuring that each Codex instance always finds its own dedicated instance and only cleans up its own resources upon exiting. By placing the MCP configuration, CDP port, and profile within the same session lifecycle, multiple browser sessions can run stably simultaneously.
I have already developed this launcher, and the code and instructions are available on GitHub: agent-browser-launchers.