Add live web search to an AutoGen agent
AutoGen's AssistantAgent accepts any list of tools — MCP adapters from mcp_server_tools() for all five tools at once, or individual FunctionTool wrappers over the REST API for controlled spend.
Two paths
| Path | Best for | What you need |
|---|---|---|
| mcp_server_tools | All five tools, fully autonomous — agent pays per call via x402 | A funded Base wallet + npx |
| FunctionTool + API key | Explicit async tool functions, controlled spend | A free API key from /pricing |
Path 1: MCP via mcp_server_tools (recommended for autonomous agents)
pip install "autogen-agentchat" "autogen-ext[openai,mcp]"
import asyncio, os
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.tools.mcp import StdioServerParams, mcp_server_tools
from autogen_agentchat.agents import AssistantAgent
from autogen_core import CancellationToken
async def main() -> None:
server_params = StdioServerParams(
command="npx",
args=["-y", "superhighway-mcp"],
env={
"AGENT_PRIVATE_KEY": os.environ["AGENT_PRIVATE_KEY"],
"X402_NETWORK": "base",
},
)
# Returns a list of StdioMcpToolAdapter — one per available tool
tools = await mcp_server_tools(server_params)
agent = AssistantAgent(
name="search_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
tools=tools,
system_message="You are a research assistant with access to live web search.",
)
result = await agent.run(
task="What are the latest developments in AI agent frameworks?",
cancellation_token=CancellationToken(),
)
print(result.messages[-1].content)
asyncio.run(main())
AGENT_PRIVATE_KEY is the private key of a wallet with USDC on Base. Each call costs $0.001 and settles automatically via the x402 protocol — no signup, no API key, no human in the loop. mcp_server_tools() is async and returns one StdioMcpToolAdapter per tool, giving the agent all five tools: web_search, news_search, image_search, scrape, and research.
Path 2: FunctionTool with an API key
AutoGen's FunctionTool wraps any async (or sync) Python function. The description string — not the docstring — is what the model sees when deciding whether to call the tool.
pip install "autogen-agentchat" "autogen-ext[openai]" requests
import asyncio, os
import requests as _requests
from autogen_core.tools import FunctionTool
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_agentchat.agents import AssistantAgent
from autogen_core import CancellationToken
BASE = "https://superhighway.walls.sh"
HEADERS = {"X-Api-Key": os.environ["SUPERHIGHWAY_API_KEY"]}
async def web_search(query: str, limit: int = 5) -> dict:
return _requests.get(f"{BASE}/search", params={"q": query, "limit": limit}, headers=HEADERS).json()
async def news_search(query: str, limit: int = 5) -> dict:
return _requests.get(f"{BASE}/news", params={"q": query, "limit": limit}, headers=HEADERS).json()
async def research(query: str, pages: int = 3) -> dict:
return _requests.get(f"{BASE}/research", params={"q": query, "pages": pages}, headers=HEADERS).json()
async def main() -> None:
tools = [
FunctionTool(web_search, description="Search the live web for current information and return ranked results."),
FunctionTool(news_search, description="Search for recent news articles with published dates."),
FunctionTool(research, description="Search the web and read top pages for deep synthesis."),
]
agent = AssistantAgent(
name="search_agent",
model_client=OpenAIChatCompletionClient(model="gpt-4o"),
tools=tools,
system_message="You are a research assistant with access to live web search.",
)
result = await agent.run(
task="What changed in the latest Python release?",
cancellation_token=CancellationToken(),
)
print(result.messages[-1].content)
asyncio.run(main())
The key AutoGen pattern: FunctionTool(func, description="...") separates the tool description from the function's docstring — the model uses the description= argument, not the Python docstring. Functions can be sync or async. AssistantAgent takes model_client= (not model=).
Which tools are available
| Tool / endpoint | What it returns | Price |
|---|---|---|
web_search / /search | Ranked web results — title, URL, description | $0.001 |
news_search / /news | Recent news with published dates | $0.001 |
image_search / /images | Image URLs, thumbnails, source pages | $0.001 |
scrape / /scrape | Any URL → clean markdown text | $0.002 |
research / /research | Search + read top pages in one call | $0.005 |
Full API reference: /openapi.json. Get a free API key or learn the x402 wallet flow.