Skip to main content

Overview

The AOP (Agent Orchestration Platform) class enables you to deploy multiple Swarms agents as individual tools in an MCP server. It provides production-ready features including queue-based task execution, automatic restart capabilities, network monitoring, and comprehensive error handling.

Constructor

Create an AOP instance to manage and deploy agents as MCP tools.

Parameters

str
default:"AOP Cluster"
Name for the MCP server
str
Description of the AOP cluster
any
default:"None"
Optional list of agents to add initially
int
default:"8000"
Port for the MCP server
str
default:"streamable-http"
Transport type for the MCP server
bool
default:"False"
Enable verbose logging
bool
default:"True"
Enable traceback logging for errors
str
default:"localhost"
Host to bind the server to
bool
default:"True"
Enable queue-based task execution
int
default:"1"
Maximum number of workers per agent
int
default:"1000"
Maximum queue size per agent
int
default:"30"
Timeout for task processing in seconds
float
default:"1.0"
Delay between retries in seconds
bool
default:"False"
Enable automatic restart on shutdown (with failsafe)
int
default:"10"
Maximum number of restart attempts before giving up
float
default:"5.0"
Delay between restart attempts in seconds
bool
default:"True"
Enable network connection monitoring and retry
int
default:"5"
Maximum number of network reconnection attempts
float
default:"10.0"
Delay between network retry attempts in seconds
Literal['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
default:"INFO"
Logging level

Methods

add_agent

Add a single agent to the MCP server as a tool.
AgentType
required
The Swarms Agent instance to deploy
str
default:"None"
Name for the tool (defaults to agent.agent_name)
str
default:"None"
Description of the tool (defaults to agent.agent_description)
Dict[str, Any]
default:"None"
JSON schema for input parameters
Dict[str, Any]
default:"None"
JSON schema for output
int
default:"30"
Maximum execution time in seconds
int
default:"3"
Number of retries on failure
bool
default:"None"
Enable verbose logging for this tool (defaults to deployer’s verbose setting)
bool
default:"None"
Enable traceback logging for this tool
str
The tool name that was registered
Raises:
  • ValueError: If agent is None or tool_name already exists

add_agents_batch

Add multiple agents to the MCP server in batch.
List[Agent]
required
List of Swarms Agent instances
List[str]
default:"None"
Optional list of tool names (defaults to agent names)
List[str]
default:"None"
Optional list of tool descriptions
List[Dict[str, Any]]
default:"None"
Optional list of input schemas
List[Dict[str, Any]]
default:"None"
Optional list of output schemas
List[int]
default:"None"
Optional list of timeout values
List[int]
default:"None"
Optional list of max retry values
List[bool]
default:"None"
Optional list of verbose settings for each agent
List[bool]
default:"None"
Optional list of traceback settings for each agent
List[str]
List of tool names that were registered
Raises:
  • ValueError: If agents list is empty or contains None values

remove_agent

Remove an agent from the MCP server.
str
required
Name of the tool to remove
bool
True if agent was removed, False if not found

list_agents

Get a list of all registered agent tool names.
List[str]
List of tool names

get_agent_info

Get detailed information about a specific agent tool.
str
required
Name of the tool
Optional[Dict[str, Any]]
Dictionary containing agent information, or None if not found

get_queue_stats

Get queue statistics for agents.
str
default:"None"
Optional specific agent name. If None, returns stats for all agents.
Dict[str, Any]
Dictionary containing queue statistics

Queue Management

When queue_enabled=True, each agent gets its own task queue with the following features:

TaskQueue Features

  1. Priority-based execution: Tasks can be assigned priorities
  2. Automatic retries: Failed tasks are automatically retried
  3. Worker threads: Background workers process tasks concurrently
  4. Statistics tracking: Comprehensive metrics on task execution
  5. Pause/resume: Queues can be paused and resumed

Queue States

  • RUNNING: Queue is actively processing tasks
  • PAUSED: Queue is paused, workers wait for resume
  • STOPPED: Queue is stopped, workers are terminated

Task States

  • PENDING: Task is waiting in queue
  • PROCESSING: Task is currently being executed
  • COMPLETED: Task completed successfully
  • FAILED: Task failed after max retries
  • CANCELLED: Task was cancelled

Persistence & Network Monitoring

Persistence Mode

When persistence=True:
  • Server automatically restarts on shutdown
  • Configurable restart attempts and delays
  • Failsafe protection prevents infinite restart loops

Network Monitoring

When network_monitoring=True:
  • Automatic detection of network issues
  • Retry logic for network failures
  • Configurable retry attempts and delays

Complete Example

Best Practices

  1. Enable queuing: Use queue_enabled=True for production reliability
  2. Set timeouts: Configure appropriate timeouts based on task complexity
  3. Monitor queues: Regularly check queue statistics to identify bottlenecks
  4. Use persistence: Enable persistence mode for critical production deployments
  5. Configure workers: Adjust max_workers_per_agent based on load requirements
  6. Enable logging: Use verbose=True and appropriate log_level for debugging
  7. Handle failures: Set max_retries appropriately for your use case
  8. Network resilience: Enable network_monitoring for unstable connections
  9. Gradual scaling: Start with fewer agents and scale up based on metrics
  10. Health monitoring: Regular check get_queue_stats() for system health

Error Handling

The AOP class implements comprehensive error handling:

Performance Tuning