agents/agent_demo.ipynb
This notebook demonstrates an agent using multiple tool types with simplified connection management:
import os
import sys
import nest_asyncio
nest_asyncio.apply()
parent_dir = os.path.dirname(os.getcwd())
sys.path.insert(0, parent_dir)
from agents.agent import Agent, ModelConfig
from agents.tools.think import ThinkTool
from agents.tools.web_search import WebSearchServerTool
from agents.tools.code_execution import CodeExecutionServerTool
# Standard Python tool
think_tool = ThinkTool()
# Python MCP server
calculator_server_path = os.path.abspath(os.path.join(os.getcwd(), "tools/calculator_mcp.py"))
calculator_server = {
"type": "stdio",
"command": "python",
"args": [calculator_server_path]
}
print(f"Calculator server configured: {'Yes' if calculator_server else 'No'}")
# Brave MCP server written in TypeScript
brave_api_key = os.environ.get("BRAVE_API_KEY_BASE_DATA", "")
print(f"Brave API key available: {'Yes' if brave_api_key else 'No'}")
brave_search_server = {
"type": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-brave-search"],
"env": {
"BRAVE_API_KEY": brave_api_key,
"PATH": f"{os.path.dirname('npx')}:" + os.environ.get("PATH", "")
}
}
print(f"Brave search server configured: {'Yes' if brave_search_server else 'No'}")
# Create agent config
system_prompt = """
You are a helpful assistant with access to:
1. Web search (brave_web_search, brave_local_search)
2. Mathematical calculator (calculate)
3. A tool to think and reason (think)
Always use the most appropriate tool for each task.
"""
# Initialize agent with standard tools and MCP servers
agent = Agent(
name="Multi-Tool Agent",
system=system_prompt,
tools=[think_tool],
mcp_servers=[brave_search_server, calculator_server],
config=ModelConfig(
model="claude-3-7-sonnet-20250219",
max_tokens=4096,
temperature=1.0
),
verbose=True
)
# Example query
agent.run("What's the square root of the OKC population in 2022")
await agent.run_async("How many bananas will fit in an Toyota GR86?")
This example demonstrates using Anthropic's native server tools for web search and code execution.
# Create Anthropic server tools
web_search_tool = WebSearchServerTool(
name="web_search",
max_uses=5, # Limit to 5 searches per request
blocked_domains=["example.com"] # Example of blocking specific domains
)
code_execution_tool = CodeExecutionServerTool()
# Initialize agent with server tools
server_agent = Agent(
name="Server Tools Agent",
system="""
You are a helpful assistant with access to:
1. Web search for finding current information
2. Code execution for running Python code
3. Think tool for complex reasoning
Use these tools effectively to answer questions that require current data or calculations.
""",
tools=[think_tool, web_search_tool, code_execution_tool],
config=ModelConfig(
model="claude-sonnet-4-20250514",
max_tokens=4096,
temperature=0.7
),
verbose=True
)
# Example 1: Use web search to find current information and code execution for analysis
server_agent.run("""
Search for the current population of Tokyo, Japan.
Then write and execute Python code to calculate how many people that would be per square kilometer,
given that Tokyo's area is approximately 2,194 square kilometers.
""")