Tools & Extensions

Give your AutoGen agents superpowers! Learn how to add tools so agents can do more than just chat.

What are Tools?

🤖 Think of it Like This:

Imagine you have a smart assistant who can only talk. That's useful, but limited!

Now imagine giving that assistant tools like:

  • 🔍 A search engine to find information
  • 🧮 A calculator to do math
  • 📧 Access to send emails
  • 🌐 A web browser to visit websites

That's exactly what Tools do in AutoGen! They let your AI agents perform actual actions, not just have conversations.

Without Tools

Agent can only chat and respond with text

Add Tools

Give agents abilities to perform actions

With Tools

Agent can search, calculate, code, and more!

Your First Tool: A Simple Calculator

Let's create a tool that lets your agent do math. It's easier than you think!

📝 Step-by-Step:
1
Write a Python function

Just a regular function that does what you want

2
Wrap it with FunctionTool

Tell AutoGen "this is a tool"

3
Give it to your agent

Now your agent can use it!

Here's the Complete Code:
# Step 1: Import what you need
from autogen_agentchat.agents import AssistantAgent
from autogen_core.tools import FunctionTool
from autogen_ext.models.openai import OpenAIChatCompletionClient
import asyncio

# Step 2: Write a simple function
def add_numbers(a: int, b: int) -> int:
    """Add two numbers together"""
    return a + b

# Step 3: Turn it into a tool
calculator_tool = FunctionTool(
    add_numbers,
    description="Adds two numbers"
)

# Step 4: Use it with your agent
model_client = OpenAIChatCompletionClient(model="gpt-4o")
agent = AssistantAgent(
    name="MathBot",
    model_client=model_client,
    tools=[calculator_tool]  # Give the tool to your agent
)

# Now your agent can do math!
# When you ask "What is 5 + 3?" the agent will use this tool

async def main():
    result = await agent.run(task="What is 5 + 3?")
    # Extract the final answer from the last message
    final_answer = result.messages[-1].content
    print(f"Answer: {final_answer}")

if __name__ == "__main__":
    asyncio.run(main())
That's It!

Your agent can now add numbers! You just created your first tool in 4 simple steps.

Common Tools You'll Use

Custom Functions

What: Any Python function you write

Use for: Weather lookups, calculations, database queries

Example:

def get_weather(city: str) -> str:
    return f"Sunny in {city}"

weather_tool = FunctionTool(get_weather)
Code Execution

What: Let agents write and run Python code

Use for: Data analysis, creating charts, complex calculations

Example:

from autogen_ext.code_executors import DockerCommandLineCodeExecutor

# Safe code execution in Docker
executor = DockerCommandLineCodeExecutor()

What are Extensions?

Extensions are pre-built tools and features created by the AutoGen community.

🎁 It's Like an App Store for Your Agents!

Instead of building everything from scratch, you can install ready-made extensions:

  • autogen-ext[openai] - Connect to OpenAI models
  • autogen-ext[docker] - Run code safely in containers
  • autogen-ext[langchain] - Use LangChain tools
  • autogen-ext-email - Send emails from your agents
  • autogen-openaiext-client - Connect to other LLMs like Gemini through OpenAI API
  • autogen-contextplus - Enhanced context with auto-summarization

Installing is Easy:

pip install "autogen-ext[openai]"
Example: Using the Email Extension

Here's how easy it is to create an agent that can send emails using an extension:

# Step 1: Install the Email extension
# pip install autogen-ext-email

from autogen_ext_email import EmailAgent, EmailConfig
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_agentchat.conditions import TextMentionTermination
import asyncio

# Step 2: Create an OpenAI model client
model_client = OpenAIChatCompletionClient(
    model="gpt-4o",
    # Your OpenAI API key (or set OPENAI_API_KEY environment variable)
)

# Step 3: Configure the email agent
email_agent = EmailAgent(
    name='EmailBot',
    model_client=model_client,
    email_config=EmailConfig(
        email='',           # Your email address
        password='',        # Your email server password/token (not regular password)
        server='',          # Your SMTP server
        port=587            # Your SMTP port
    )
)

# Step 4: Use the agent to send emails!
async def main():
    text_termination = TextMentionTermination("TERMINATE")
    team = RoundRobinGroupChat([email_agent], termination_condition=text_termination)

    result = await team.run(
        task="Send an email from LearnAutoGen to John at his email john*****@gmail.com with subject 'Meeting Reminder' and message 'Don't forget our meeting at 3 PM today!'"
    )

if __name__ == "__main__":
    asyncio.run(main())

# Your agent can now generate and send emails automatically!
What Just Happened?

You used an extension (autogen-ext-email) to:

  • Create an agent that can compose and send emails
  • Connect to Gmail's SMTP server for email delivery
  • Automate email communication with natural language commands

Key Takeaways

✅ Remember:
  • Tools = Special abilities for agents
  • Any Python function can become a tool
  • Use FunctionTool to wrap functions
  • Give tools to agents when creating them
🚀 Next Steps:
  • Try creating a simple calculator tool
  • Build a weather bot with your own tool
  • Explore extensions for more features
  • Experiment with code execution tools

Ready to Practice?

Now that you understand tools, try building your own!