Skip to main content

Agent

The Agent class is the base class for defining AI agent behavior and capabilities. It provides the foundation for creating intelligent conversational agents with support for function tools, MCP servers, and advanced lifecycle management.

agent

Basic Usage

Simple Agent

This is how you can initialize a simple agent with the Agent class, where instructions defines how the agent should behave.

from videosdk.agents import Agent

class MyAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a helpful assistant."
)

Agent with Function Tools

Function tools allow your agent to perform actions and interact with external services, extending its capabilities beyond simple conversation. You can register tools that are defined either outside or inside your agent class.

External Tools

External tools are defined as standalone functions and are passed into the agent's constructor via the tools list. This is useful for sharing common tools across multiple agents.

from videosdk.agents import Agent, function_tool

# External tool defined outside the class
@function_tool(description="Get weather information")
def get_weather(location: str) -> str:
"""Get weather information for a specific location."""
# Weather logic here
return f"Weather in {location}: Sunny, 72°F"

class WeatherAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a weather assistant.",
tools=[get_weather] # Register the external tool
)

Internal Tools

Internal tools are defined as methods within your agent class and are decorated with @function_tool. This is useful for logic that is specific to the agent and needs access to its internal state (self).

from videosdk.agents import Agent, function_tool

class FinanceAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a helpful financial assistant."
)
self.portfolio = {"AAPL": 10, "GOOG": 5}

@function_tool
def get_portfolio_value(self) -> dict:
"""Get the current value of the user's stock portfolio."""
# In a real scenario, you'd fetch live stock prices
# This is a simplified example
return {"total_value": 5000, "holdings": self.portfolio}

Agent with MCP Server

MCPServerStdio enables your agent to communicate with external processes via standard input/output streams. This is ideal for integrating complex, standalone Python scripts or other local executables as tools.

import sys
from pathlib import Path
from videosdk.agents import Agent, MCPServerStdio

# Path to your external Python script that runs the MCP server
mcp_server_path = Path(__file__).parent / "mcp_server_script.py"

class MCPAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are an assistant that can leverage external tools via MCP.",
mcp_servers=[
MCPServerStdio(
executable_path=sys.executable,
process_arguments=[str(mcp_server_path)],
session_timeout=30
)
]
)

Agent Lifecycle and Methods

The Agent class provides lifecycle hooks and methods to manage state and behavior at critical points in the agent's session.

Lifecycle Hooks

These methods are designed to be overridden in your custom agent class to implement specific behaviors.

  • async def on_enter(self) -> None: Called once when the agent successfully joins the meeting. This is the ideal place for introductions or initial actions, such as greeting participants.
  • async def on_exit(self) -> None: Called when the agent is about to exit the meeting. Use this for cleanup tasks or for saying goodbye.
from videosdk.agents import Agent

class LifecycleAgent(Agent):
async def on_enter(self):
print("Agent has entered the meeting.")
await self.session.say("Hello everyone! I'm here to help.")

async def on_exit(self):
print("Agent is exiting the meeting.")
await self.session.say("It was a pleasure assisting you. Goodbye!")

Got a Question? Ask us on discord