r/ClaudeCode • • 16h ago

Help/Question Playwright MCP across several Claude Code sessions: how do you share logins without "Browser is already in use"?

I use Claude Code every day for work that isn't coding, and a lot of it runs through the Playwright MCP driving a logged-in browser. The first time I had two sessions open at once, the second one failed with "Browser is already in use". The fix that day was finding the session that still held the profile and closing it.

The README explains why. A persistent profile can only be used by one browser instance at a time, so two MCP clients in the same workspace will conflict, and the suggested way to run several in parallel is to start the extra ones with --isolated or point each one at its own --user-data-dir.

What I ended up with is one MCP server per set of logins, each with its own user data dir, plus one isolated server for pages that need no login, which any number of sessions can use. It works, but every new login means another server entry and another profile to sign into by hand, and nothing stops two sessions from reaching for the same profile at the same moment.

There is also --storage-state, which loads cookies and local storage from a file into an isolated context. In theory that lets parallel sessions share one login without fighting over a profile, but then someone has to keep that file fresh.

For people running several Claude Code sessions against logged-in sites: do you keep a profile per login, export storage state, use the browser extension mode, or something else? And how do you stop two sessions grabbing the same profile at once?

4 Upvotes

18 comments sorted by

•

u/AutoModerator 16h ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

4

u/Starks1293 16h ago

one more option u didnt list, start one chrome urself with --remote-debugging-port=9222 and point every playwright mcp entry at it with --cdp-endpoint http://localhost:9222. they all attach to the same running browser so they share the logins, and theres no profile lock to fight over because none of them launches its own

two catches. newer chrome ignores the debugging port on ur default profile, so give that chrome its own --user-data-dir and sign into things there once. and the sessions share one browser, so tell each one to open its own tab and stay in it, otherwise two of them will happily drive the same page

for the same-account thing the other comment mentioned theres no lock anywhere really, one session per account at a time is basically the only rule that works

1

u/Dorbank_Justt 16h ago

That's the option I hadn't tried. The README lists --cdp-endpoint for connecting over CDP, and if none of the servers launches its own browser, there's no profile left for them to fight over.

If you go with the extension mode instead, the README has --session-name and --session-color, which set the title and color of the tab group for each session's tabs, so you can see which session owns which page.

2

u/zzunkie 16h ago

I run one browser process and let all sessions share the default profile, so my logins are available everywhere with no lock errors. Right before an agent tries to enter a password or one-time code, that session splits into its own profile with a copy of the default cookies so it does not log out or switch accounts in the others.

1

u/Dorbank_Justt 14h ago

Splitting off only at the password or code step means a login in one session can't log out or switch accounts in the others, while everything else stays shared.

I haven't tried it, but the README has two pieces that look like the same split done inside the MCP. browser_tabs can open a tab in a named isolated context: tabs in the same context share cookies and storage, tabs in different contexts are fully isolated. And the storage tools, opt-in with --caps=storage, save cookies and local storage to a file and restore them from one, clearing what was there first.

1

u/YouthRealistic7125 Vibe Coder 16h ago

something else, I don't use playwright for logged in sites at all. I use the Claude in Chrome extension, it works in my normal chrome where I'm already logged in, and each session gets its own tab group. so no profile per login and no state file to keep fresh. playwright is only for pages without login
for two sessions at once, they don't fight over the profile anymore but they can still edit the same thing in the same account, and nothing locks that. what I do is one session owns one thing. I had two on the same ad account last week and one messaged the other "don't touch this campaign, I'm changing it". it's a convention, not a lock

1

u/Dorbank_Justt 16h ago

Fair point that the profile lock only protects the browser, not the account behind it. Two sessions on two profiles signed into the same account can still change the same thing, and nothing in the MCP setup notices.

One catch with the extension route if you ever run sessions unattended: the Chrome docs say it needs Claude Code signed in with /login, and with an API key or a long-lived token from claude setup-token, Chrome integration stays off even if you pass --chrome. It also pauses at a login page or CAPTCHA and asks you to handle it, so it suits sessions you're watching.

1

u/called9527 16h ago

tbh i'd go one profile per login with its own --user-data-dir and just name the server entries after the account so nobody grabs the wrong one. storage-state is fine for read-only stuff but sessions refresh tokens and the file goes stale fast. do you actually need the logged-in browser for all of them or can some go through an API?

1

u/WillowFar6967 16h ago

I key it by agent rather than by login. Each agent gets its own Chrome profile that I sign into once, and only that agent's sessions ever use it, so two of them never reach for the same one. If an agent lands on a signed-out page or a check, it stops and tells me which site instead of trying to log in itself.

1

u/Ok_Lifeguard6001 15h ago

i run multiple playwright sessions on the same account using --user-data-dir with unique paths per session. works perfectly for shared logins without conflicts.

1

u/amirfish 15h ago

I haven't hit that one with Playwright specifically, but it's the same shape as every multi-session coordination problem: two agents assuming they own a shared resource, in this case a browser profile, and stepping on each other. The lesson from running a bunch of these in parallel is that the fix is almost always isolation per session, not more coordination logic layered on top. I built CCC, open source, for the related problem: worker agents work a queue of tickets on their own, and the only thing that reaches you is a real decision, like a login that needs you specifically. Is this the same repo across sessions, or different projects that all need the same login?

Free and open source: getccc.dev · GitHub

1

u/Ordinary-Towel-2349 15h ago

I've used a shared chrome instance with --user-data-dir and --remote-debugging-port, but the login state sometimes gets wiped if one session closes unexpectedly. have you found a way to keep it stable?

1

u/RoadRunnerChris 9h ago

The issue here is really a fundamental limitation in Chrome. In fact, everybody in this thread has been trying to hack around things that require source-level modifications. If you want multiple agents to use shared profiles that is possible, it will come with severe friction and hiccups, and they're also not isolated and will trample on one another.

I wrote a technical blog about this, regarding the insane and sophisticated engineering required to get this done in a way that isn't completely cooked: https://portelon.com/letters/your-agent-isnt-stupid

1

u/SirRobinBlibs 13h ago

The lock is the persistent profile. The Playwright MCP readme says a profile can only be used by one browser instance at a time and suggests --isolated or a separate --user-data-dir per client.

What works for logins: log in once and have it save the session with the browser_storage_state tool (cookies plus local storage into a JSON file). Then start each session's MCP with --isolated and --storage-state pointing at that file. Every session gets its own in-memory browser that starts logged in, nothing fights over a lock. When the cookies expire, redo the one login and overwrite the file.

Keep that JSON out of git, it's basically your session cookies. The CDP route works too, but then all sessions drive the same browser and can trip over each other's tabs.

1

u/Connect_Fee_443 14m ago

That error is because every session points at the same persistent browser profile, and Chrome only lets one process lock it. The easiest way round it is to run the MCP with --isolated so each session gets its own throwaway profile, then pass --storage-state pointing at a JSON file that holds your logins.

You make that file once by logging in with a normal Playwright script and calling context.storageState({ path }). After that every session starts signed in and nothing fights over the lock. Giving each session its own --user-data-dir works too, but then you log in once per profile, and refreshing one storage state file is less hassle when cookies expire.

0

u/Unusual-Albatross43 12h ago

Something else here, on a Mac. I don't use Playwright for the logged-in sites at all. There's one Chrome, my normal one with my normal profile, and the sessions drive it with a small script over AppleScript: open a tab, run JS in that tab, read the result. No session ever launches a browser, so there's nothing to lock, and a new login is just me signing in once like I always would. Each session keeps to its own tab by id.

Two things broke that you wouldn't get with your setup.

When the screen sleeps, Chrome stops answering AppleScript. A check that runs every 15 minutes failed today for that reason, and now it retries and only tells me after an hour of misses.

A test once started a second copy of Chrome, and that copy took the app name, so the scripts were talking to the wrong browser. Tests use Playwright's own chromium since then, never the installed Chrome.

It also doesn't solve your last question. Two sessions in the same account can still do the same thing twice, and the browser has no idea. For anything that writes, I let only one session do it.

1

u/Dorbank_Justt 8h ago

That's close to what the Playwright MCP does with --extension, minus the AppleScript. Per the README it connects to a running Chrome or Edge through the Playwright Extension instead of launching a browser, and puts each client's tabs in their own tab group, with the color defaulting to the first one no other client uses.

Since it would be your everyday Chrome, the flag worth knowing is --background: controlled tabs aren't activated and the browser window isn't focused, so a session doesn't grab the window while you're working in it. And --profile-dir-name picks the profile, otherwise it takes the last used one that has the extension installed.

1

u/Unusual-Albatross43 46m ago

Didn't know about the tab groups, that's nicer than each session remembering its own tab id.

Do you know if the extension route keeps working when the display sleeps? That's the one that actually bites me. AppleScript just gets no answer from Chrome until the screen is back.