Overview
Concrete session manager for thread-isolated browser instances via AWS Bedrock provided by CrewAI.
Description
The BrowserSessionManager class manages browser sessions across different threads, maintaining separate browser instances per thread to enable concurrent browser usage in multi-threaded environments. It uses AWS bedrock_agentcore BrowserClient to create browser sessions and connects to them via Playwright (both async and sync APIs) over Chrome DevTools Protocol (CDP) WebSocket connections. Sessions are stored in separate dictionaries for async and sync modes, keyed by thread_id. Browsers are created lazily only when first requested by a tool. The manager provides methods to get or create browsers (get_async_browser, get_sync_browser), close individual sessions (close_async_browser, close_sync_browser), and close all sessions at once (close_all_browsers). Comprehensive error handling ensures proper cleanup of both the Playwright browser and the BrowserClient when creation or closure fails.
Usage
Import and instantiate BrowserSessionManager when building browser-based tools for CrewAI that require concurrent browser access across multiple agents or tasks. Each thread receives its own isolated browser instance, preventing race conditions and state corruption during parallel execution.
Code Reference
Source Location
- Repository: CrewAI
- File: lib/crewai-tools/src/crewai_tools/aws/bedrock/browser/browser_session_manager.py
- Lines: 1-255
Signature
class BrowserSessionManager:
def __init__(self, region: str = "us-west-2"):
Import
from crewai_tools.aws.bedrock.browser.browser_session_manager import BrowserSessionManager
I/O Contract
Inputs (Constructor)
| Name |
Type |
Required |
Description
|
| region |
str |
No |
AWS region for the browser client (default: "us-west-2")
|
Inputs (get_async_browser / get_sync_browser)
| Name |
Type |
Required |
Description
|
| thread_id |
str |
Yes |
Unique identifier for the thread requesting the browser
|
Outputs
| Name |
Type |
Description
|
| get_async_browser() |
AsyncBrowser |
Playwright async browser instance specific to the thread
|
| get_sync_browser() |
SyncBrowser |
Playwright sync browser instance specific to the thread
|
| close_async_browser() |
None |
Closes and cleans up the async browser session for a thread
|
| close_sync_browser() |
None |
Closes and cleans up the sync browser session for a thread
|
| close_all_browsers() |
None |
Closes all async and sync browser sessions
|
Usage Examples
Basic Usage
from crewai_tools.aws.bedrock.browser.browser_session_manager import BrowserSessionManager
# Initialize manager
manager = BrowserSessionManager(region="us-east-1")
# Get a sync browser for a thread
browser = manager.get_sync_browser(thread_id="agent-1")
# Use the browser with Playwright
page = browser.contexts[0].new_page()
page.goto("https://example.com")
content = page.content()
# Clean up when done
manager.close_sync_browser(thread_id="agent-1")
Async Multi-Thread Usage
import asyncio
from crewai_tools.aws.bedrock.browser.browser_session_manager import BrowserSessionManager
manager = BrowserSessionManager()
async def browse(thread_id, url):
browser = await manager.get_async_browser(thread_id)
page = await browser.contexts[0].new_page()
await page.goto(url)
return await page.content()
# Multiple agents browsing concurrently
results = await asyncio.gather(
browse("agent-1", "https://example.com"),
browse("agent-2", "https://other.com"),
)
# Clean up all sessions
await manager.close_all_browsers()
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.