Skip to content

Commit 18f483c

Browse files
3coinsmichaelnchin
andauthored
Bedrock AgentCore browser and code interpreter toolkits (crewAIInc#385)
* Added browser and code tools * Added dependencies, moved imports inside class * Added instructions in README * Updated imports * Updated imports * Updated dependencies * Fix 'get_current_page' utilities for Browser tools * Support browser session cleanup from synchronous code * Update browser tool examples for new changes * Manually override _run->_arun and set nested loop when in crew event loop * Browser async example * update examples with uv * Fix toolkit fields for code interpreter * Update code interpreter examples * update uv.lock * Move nest_asyncio import --------- Co-authored-by: Michael Chin <mchin188@yahoo.com>
1 parent 0cea482 commit 18f483c

File tree

12 files changed

+1870
-8
lines changed

12 files changed

+1870
-8
lines changed

crewai_tools/aws/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
from .s3 import S3ReaderTool, S3WriterTool
2-
from .bedrock import BedrockKBRetrieverTool, BedrockInvokeAgentTool
2+
from .bedrock import (
3+
BedrockKBRetrieverTool,
4+
BedrockInvokeAgentTool,
5+
create_browser_toolkit,
6+
create_code_interpreter_toolkit,
7+
)
38

49
__all__ = [
5-
'S3ReaderTool',
6-
'S3WriterTool',
7-
'BedrockKBRetrieverTool',
8-
'BedrockInvokeAgentTool'
9-
]
10+
"S3ReaderTool",
11+
"S3WriterTool",
12+
"BedrockKBRetrieverTool",
13+
"BedrockInvokeAgentTool",
14+
"create_browser_toolkit",
15+
"create_code_interpreter_toolkit"
16+
]

crewai_tools/aws/bedrock/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
from .knowledge_base.retriever_tool import BedrockKBRetrieverTool
22
from .agents.invoke_agent_tool import BedrockInvokeAgentTool
3+
from .browser import create_browser_toolkit
4+
from .code_interpreter import create_code_interpreter_toolkit
35

4-
__all__ = ["BedrockKBRetrieverTool", "BedrockInvokeAgentTool"]
6+
__all__ = [
7+
"BedrockKBRetrieverTool",
8+
"BedrockInvokeAgentTool",
9+
"create_browser_toolkit",
10+
"create_code_interpreter_toolkit"
11+
]
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# AWS Bedrock Browser Tools
2+
3+
This toolkit provides a set of tools for interacting with web browsers through AWS Bedrock Browser. It enables your CrewAI agents to navigate websites, extract content, click elements, and more.
4+
5+
## Features
6+
7+
- Navigate to URLs and browse the web
8+
- Extract text and hyperlinks from pages
9+
- Click on elements using CSS selectors
10+
- Navigate back through browser history
11+
- Get information about the current webpage
12+
- Multiple browser sessions with thread-based isolation
13+
14+
## Installation
15+
16+
Ensure you have the necessary dependencies:
17+
18+
```bash
19+
uv add crewai-tools bedrock-agentcore beautifulsoup4 playwright nest-asyncio
20+
```
21+
22+
## Usage
23+
24+
### Basic Usage
25+
26+
```python
27+
from crewai import Agent, Task, Crew, LLM
28+
from crewai_tools.aws.bedrock.browser import create_browser_toolkit
29+
30+
# Create the browser toolkit
31+
toolkit, browser_tools = create_browser_toolkit(region="us-west-2")
32+
33+
# Create the Bedrock LLM
34+
llm = LLM(
35+
model="bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0",
36+
region_name="us-west-2",
37+
)
38+
39+
# Create a CrewAI agent that uses the browser tools
40+
research_agent = Agent(
41+
role="Web Researcher",
42+
goal="Research and summarize web content",
43+
backstory="You're an expert at finding information online.",
44+
tools=browser_tools,
45+
llm=llm
46+
)
47+
48+
# Create a task for the agent
49+
research_task = Task(
50+
description="Navigate to https://example.com and extract all text content. Summarize the main points.",
51+
expected_output="A list of bullet points containing the most important information on https://example.com. Plus, a description of the tool calls used, and actions performed to get to the page.",
52+
agent=research_agent
53+
)
54+
55+
# Create and run the crew
56+
crew = Crew(
57+
agents=[research_agent],
58+
tasks=[research_task]
59+
)
60+
result = crew.kickoff()
61+
62+
print(f"\n***Final result:***\n\n{result}")
63+
64+
# Clean up browser resources when done
65+
toolkit.sync_cleanup()
66+
```
67+
68+
### Available Tools
69+
70+
The toolkit provides the following tools:
71+
72+
1. `navigate_browser` - Navigate to a URL
73+
2. `click_element` - Click on an element using CSS selectors
74+
3. `extract_text` - Extract all text from the current webpage
75+
4. `extract_hyperlinks` - Extract all hyperlinks from the current webpage
76+
5. `get_elements` - Get elements matching a CSS selector
77+
6. `navigate_back` - Navigate to the previous page
78+
7. `current_webpage` - Get information about the current webpage
79+
80+
### Advanced Usage (with async)
81+
82+
```python
83+
import asyncio
84+
from crewai import Agent, Task, Crew, LLM
85+
from crewai_tools.aws.bedrock.browser import create_browser_toolkit
86+
87+
async def main():
88+
89+
# Create the browser toolkit with specific AWS region
90+
toolkit, browser_tools = create_browser_toolkit(region="us-west-2")
91+
tools_by_name = toolkit.get_tools_by_name()
92+
93+
# Create the Bedrock LLM
94+
llm = LLM(
95+
model="bedrock/us.anthropic.claude-3-7-sonnet-20250219-v1:0",
96+
region_name="us-west-2",
97+
)
98+
99+
# Create agents with specific tools
100+
navigator_agent = Agent(
101+
role="Navigator",
102+
goal="Find specific information across websites",
103+
backstory="You navigate through websites to locate information.",
104+
tools=[
105+
tools_by_name["navigate_browser"],
106+
tools_by_name["click_element"],
107+
tools_by_name["navigate_back"]
108+
],
109+
llm=llm
110+
)
111+
112+
content_agent = Agent(
113+
role="Content Extractor",
114+
goal="Extract and analyze webpage content",
115+
backstory="You extract and analyze content from webpages.",
116+
tools=[
117+
tools_by_name["extract_text"],
118+
tools_by_name["extract_hyperlinks"],
119+
tools_by_name["get_elements"]
120+
],
121+
llm=llm
122+
)
123+
124+
# Create tasks for the agents
125+
navigation_task = Task(
126+
description="Navigate to https://example.com, then click on the the 'More information...' link.",
127+
expected_output="The status of the tool calls for this task.",
128+
agent=navigator_agent,
129+
)
130+
131+
extraction_task = Task(
132+
description="Extract all text from the current page and summarize it.",
133+
expected_output="The summary of the page, and a description of the tool calls used, and actions performed to get to the page.",
134+
agent=content_agent,
135+
)
136+
137+
# Create and run the crew
138+
crew = Crew(
139+
agents=[navigator_agent, content_agent],
140+
tasks=[navigation_task, extraction_task]
141+
)
142+
143+
result = await crew.kickoff_async()
144+
145+
# Clean up browser resources when done
146+
toolkit.sync_cleanup()
147+
148+
return result
149+
150+
if __name__ == "__main__":
151+
result = asyncio.run(main())
152+
print(f"\n***Final result:***\n\n{result}")
153+
```
154+
155+
## Requirements
156+
157+
- AWS account with access to Bedrock AgentCore API
158+
- Properly configured AWS credentials
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .browser_toolkit import BrowserToolkit, create_browser_toolkit
2+
3+
__all__ = ["BrowserToolkit", "create_browser_toolkit"]

0 commit comments

Comments
 (0)