OpenAI Swarm-compatible multi-agent handoffs with context preservation.
from mcp_agent.app import MCPApp from mcp_agent.agents.agent import Agent from mcp_agent.workflows.llm.augmented_llm_openai import OpenAIAugmentedLLM from mcp_agent.workflows.swarm.swarm_orchestrator import SwarmOrchestrator app = MCPApp(name="swarm_example") async with app.run() as context: # Create specialized agents sales_agent = Agent( name="sales", instruction="Handle sales inquiries and product recommendations.", server_names=["database", "fetch"] ) support_agent = Agent( name="support", instruction="Provide technical support and troubleshooting.", server_names=["knowledge_base", "ticketing"] ) billing_agent = Agent( name="billing", instruction="Handle billing, refunds, and account management.", server_names=["billing_system", "database"] ) # Define handoff conditions handoff_rules = { "sales": { "triggers": ["price", "buy", "purchase", "product"], "confidence_threshold": 0.7 }, "support": { "triggers": ["error", "bug", "help", "troubleshoot"], "confidence_threshold": 0.8 }, "billing": { "triggers": ["refund", "payment", "invoice", "subscription"], "confidence_threshold": 0.9 } } # Create swarm orchestrator swarm = SwarmOrchestrator( agents=[sales_agent, support_agent, billing_agent], handoff_rules=handoff_rules, llm_factory=OpenAIAugmentedLLM, initial_agent="sales" ) # Execute with automatic handoffs result = await swarm.generate_str( message="I'm interested in your premium plan but having trouble with payment" ) # Automatically hands off from sales -> billing agent