This guide explains how to use Mini Agent with an already-running browser instance to leverage existing authentication sessions, cookies, and logged-in states.
By default, Mini Agent launches a new browser instance. However, you can configure it to connect to an existing browser session, which is useful for:
- Accessing authenticated sessions without re-logging in
- Reusing existing cookies and login states
- Working with sites that require authentication
- Testing workflows in the context of a logged-in user
- Preserving browser extensions and user preferences
Mini Agent uses Chrome DevTools Protocol (CDP) to connect to an already-running browser instance. This allows it to reuse the browser's:
- Cookies and session data
- Login/authentication state
- Browser extensions
- User preferences and settings
- Cached data
Close all existing browser windows first, then launch your browser with the --remote-debugging-port flag:
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --remote-debugging-port=9222 --user-data-dir=$HOME/ChromeProfile/"/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" --remote-debugging-port=9222 --user-data-dir=$HOME/ChromeProfile/google-chrome --remote-debugging-port=9222 --user-data-dir=$HOME/ChromeProfile/
# or
chromium-browser --remote-debugging-port=9222 --user-data-dir=$HOME/ChromeProfile/"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" --remote-debugging-port=9222Navigate to the websites you want to access and log in as you normally would. Mini Agent will be able to reuse these sessions.
Update your config.yaml file to enable connecting to the existing browser:
# Browser Connection Settings
browser_options:
use_existing_browser: true # Set to true to connect to existing browser
cdp_url: "http://localhost:9222" # CDP endpoint (default)Alternatively, you can configure it programmatically:
from mini_agent.tools.browser_tool import get_browser_manager
# Connect to existing browser
manager = get_browser_manager(use_existing_browser=True, cdp_url="http://localhost:9222")
await manager.connect_to_existing_browser("http://localhost:9222")#!/usr/bin/env python3
import asyncio
from mini_agent import Agent
from mini_agent.llm import AnthropicClient
from mini_agent.tools import (
BrowserGotoTool,
BrowserScreenshotTool,
BrowserGetContentTool,
ReadTool,
)
async def main():
# Initialize with browser tools
llm = AnthropicClient(api_key="your-api-key", model="claude-3-5-sonnet-20241022")
tools = [
BrowserGotoTool(),
BrowserScreenshotTool(),
BrowserGetContentTool(),
ReadTool(),
]
agent = Agent(
llm_client=llm,
system_prompt="You can access my browser. I'm already logged into GitHub.",
tools=tools,
workspace_dir="./workspace",
)
# Agent will reuse your GitHub session
agent.add_user_message("Go to GitHub and check my notifications")
result = await agent.run()
if __name__ == "__main__":
asyncio.run(main())# The agent can access your Gmail without re-authentication
agent.add_user_message("Check my Gmail inbox and tell me about the latest email")
result = await agent.run()# Agent can access multiple authenticated sites in sequence
agent.add_user_message("""
1. Go to GitHub and check my pull requests
2. Then go to Gmail and check for any code review notifications
3. Take screenshots of both pages
""")
result = await agent.run()- All your logged-in accounts and sessions
- Cookies and authentication tokens
- Browser extensions and their data
- Autofill information (if enabled)
- Browsing history (if accessible)
-
Use a dedicated browser profile: Launch browser with
--user-data-dir=/path/to/profileto isolate sessionschrome --remote-debugging-port=9222 --user-data-dir=./agent-profile
-
Close sensitive tabs before connecting Mini Agent
-
Use for specific tasks only: Launch browser only when needed, close when done
-
Don't use your main browser: Consider using a separate browser installation or profile
-
Revoke sessions after use: Log out of sensitive accounts when done
Problem: Mini Agent can't connect to the browser
Solutions:
- Ensure browser was launched with
--remote-debugging-port=9222 - Check that port 9222 is not blocked by firewall
- Verify no other process is using port 9222
- Ensure browser is still running
Problem: Mini Agent launches a new browser instead of connecting
Solutions:
- Verify
use_existing_browser: trueis set in config - Check CDP URL matches the launch command
- Ensure config file is in correct location
- Confirm browser was launched before starting Mini Agent
Problem: Websites still ask for login despite existing session
Solutions:
- Check that you logged in AFTER launching with remote debugging
- Verify you're using the correct browser profile
- Some sites may require re-authentication for automation
- Check if cookies are being blocked or cleared
Problem: Error about port 9222 already being used
Solutions:
- Find and kill the process using the port:
# macOS/Linux lsof -ti:9222 | xargs kill -9 # Windows netstat -ano | findstr :9222 taskkill /PID <PID> /F
- Use a different port:
--remote-debugging-port=9333 - Update config.yaml with the new port
browser_options:
use_existing_browser: true
cdp_url: "http://localhost:9333" # Custom portMost Chromium-based browsers work:
- Google Chrome
- Microsoft Edge
- Chromium
- Brave (with some limitations)
Not recommended - headless mode typically can't reuse GUI browser sessions effectively.
See examples/08_browser_with_existing_session.py for a complete working example.
# 1. Launch browser with remote debugging
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9222
# 2. Log into websites
# 3. Run the example
python examples/08_browser_with_existing_session.py- Browser must be Chromium-based (Chrome, Edge, Chromium)
- Browser must be launched with remote debugging flag
- Session persistence depends on browser settings - some sites may still expire sessions
- Not all browser extensions work with CDP automation
- File downloads may be handled differently compared to normal browsing
- Certain browser features may behave differently under CDP control
Mini Agent uses Playwright's connect_over_cdp() method to connect to the browser. This connects via Chrome DevTools Protocol, which is the same protocol used by:
- Chrome Developer Tools
- Puppeteer
- Selenium WebDriver (ChromeDriver)
The connection allows full control over the browser while preserving the user's session state.