important-pharmacist-78974
09/09/2025, 9:57 PMtall-airline-16756
09/09/2025, 9:57 PMtall-airline-16756
09/09/2025, 9:58 PMUserData) that gets passed around between agents through the AgentSession. This way, every agent can easily access and update the context. For instance, you might initialize userdata when setting up the session and then update it within your @function_tool methods using context.userdata.
• Agent Handoff with function_tool: Another neat way is to use the @function_tool decorator to hand off control from one agent to another. When a function returns a different agent instance, the session smoothly transfers control while keeping the shared context intact.
• Preserving Conversation History: To keep the conversation flowing naturally, you can pass the current chat_ctx from one agent to the next. If you want to manage prompt size, using chat_ctx.copy() and truncate() methods can be really helpful.
These methods work together to make sure your agents share context seamlessly and keep the conversation continuous. If you'd like, I’m here to provide more details or examples to help you implement these. Just let me know!
Sources: examples/voice_agents/restaurant_agent.py | Workflows | LiveKit Docs | complex-agents/medical_office_triage/triage.py | Building Multi-Agent Architectures with LiveKit Agents | main.pyimportant-pharmacist-78974
09/09/2025, 9:59 PMtall-airline-16756
09/09/2025, 10:00 PMfrom livekit.agents import Agent, RunContext, JobContext
from livekit.plugins import openai, deepgram
from mcp_client import MCPServerSse
from dataclasses import dataclass
@dataclass
class UserData:
user_name: str = ""
location: str = ""
# Agent 1: Uses an MCP server for user data collection
class ResearchAgent(Agent):
def __init__(self):
mcp_server_1 = MCPServerSse(
params={"url": "https://mcp.example.com/crm"},
name="CRM Server"
)
super().__init__(
instructions="You are a research assistant. Use your tools to gather user information.",
mcp_servers=[mcp_server_1],
stt=deepgram.STT(),
llm=openai.LLM(),
tts=openai.TTS()
)
async def on_enter(self):
self.session.generate_reply()
async def on_leave(self):
self.session.update_agent(EditorAgent)
# Agent 2: Uses a different MCP server for content tools
class EditorAgent(Agent):
def __init__(self, context: RunContext[UserData]):
mcp_server_2 = MCPServerSse(
params={"url": "https://mcp.example.com/editor"},
name="Editor Server"
)
super().__init__(
instructions=f"You are an editor. The user's name is {context.userdata.user_name}, from {context.userdata.location}. Help write a story.",
mcp_servers=[mcp_server_2],
llm=openai.LLM(),
tts=openai.TTS(),
chat_ctx=context.chat_ctx # Inherit conversation history
)
async def on_enter(self):
self.session.generate_reply()
In this example:
• Agent 1: Uses an MCP server for CRM or profile lookup tools to gather user data.
• Agent 2: Uses a different MCP server for editorial tools to assist with content creation.
• Shared Context: User data (e.g., name, location) is passed via RunContext[UserData], and conversation history is preserved using chat_ctx.
If you have any questions or would like me to walk you through any part of this example, please don’t hesitate to ask. I’m here to help and make sure everything is clear for you!
Sources: README.md | examples/voice_agents/multi_agent.py | main.py | Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docsimportant-pharmacist-78974
09/09/2025, 10:04 PMtall-airline-16756
09/09/2025, 10:05 PMMCPToolsIntegration to dynamically load tools from external MCP servers. This way, your agents won’t be tied down to specific tools and can easily adjust to various setups.
• Shared UserData: Consider storing session-specific data in a UserData class. This approach keeps your agents stateless but still allows the important context to flow smoothly across agent transitions. For instance, user details or session state can be updated and accessed through context.userdata.
• Preserve Conversation History: Using ChatContext to keep and merge conversation history during agent handoffs is a great way to ensure your conversations stay seamless without embedding state directly in the agents.
• Agent Handoff: You can use @function_tool to hand over control between agents while sharing context and history. This helps keep your workflows modular and easy to scale.
These strategies can really help you build flexible, context-aware systems without locking yourself into specific tools. If you’d like me to walk you through any of these steps or provide examples, just let me know—I’m here to help!
Sources: mcp_client/agent_tools.py | livekit-agents/livekit/agents/voice/agent_session.py | Model Context Protocol Integration | livekit/livekit_composite | DeepWiki | Building Multi-Agent Architectures with LiveKit Agents