Skip to main content

Multi-Agent Collaboration Patterns

MoFA provides a comprehensive set of collaboration protocols that enable intelligent multi-agent coordination. All collaboration patterns support optional LLM integration for intelligent decision-making and natural language understanding.

Architecture

The collaboration system follows a layered architecture:

Core Collaboration Modes

MoFA supports seven standard collaboration patterns, each optimized for specific use cases:

Request-Response

One-to-one deterministic tasks with synchronous replies

Publish-Subscribe

One-to-many broadcast tasks with multiple receivers

Consensus

Multi-round negotiation and voting for decision-making

Debate

Agents alternate speaking to iteratively refine results

Parallel

Simultaneous execution with automatic result aggregation

Sequential

Pipeline execution where output flows to the next agent

Secretary Pattern

Human-in-the-loop workflow management with 5-phase execution

Key Features

LLM-Driven Collaboration

All protocols support optional LLM integration:
  • Intelligent Protocol Selection: LLM analyzes task description and selects the most appropriate collaboration mode
  • Natural Language Processing: Protocols can process and understand natural language messages
  • Decision Context Recording: LLM reasoning and decision-making process is tracked
use mofa_sdk::collaboration::{
    RequestResponseProtocol, LLMClient
};
use std::sync::Arc;

// Create LLM-enabled protocol
let protocol = RequestResponseProtocol::with_llm(
    "agent_001",
    llm_client.clone()
);

// Or create protocol without LLM (fast path)
let protocol = RequestResponseProtocol::new("agent_001");

Protocol Registry

Manage and discover available collaboration protocols:
use mofa_sdk::collaboration::{
    LLMDrivenCollaborationManager,
    RequestResponseProtocol,
    PublishSubscribeProtocol
};

let manager = LLMDrivenCollaborationManager::new("agent_001");

// Register protocols
manager.register_protocol(Arc::new(
    RequestResponseProtocol::new("agent_001")
)).await?;

manager.register_protocol(Arc::new(
    PublishSubscribeProtocol::new("agent_001")
)).await?;

// Execute task with specific protocol
let result = manager.execute_task_with_protocol(
    "request_response",
    "Process this data: [1, 2, 3]"
).await?;

Collaboration Messages

Standardized message format for inter-agent communication:
use mofa_sdk::collaboration::{
    CollaborationMessage,
    CollaborationContent,
    CollaborationMode
};

// Create a collaboration message
let msg = CollaborationMessage::new(
    "agent_001".to_string(),
    CollaborationContent::Text("Analyze this dataset".to_string()),
    CollaborationMode::RequestResponse
)
.with_receiver("agent_002".to_string())
.with_metadata("priority".to_string(), "high".to_string());

// Send via protocol
protocol.send_message(msg).await?;

When to Use Each Pattern

Best for:
  • Data query and processing
  • Deterministic task execution
  • Simple Q&A scenarios
  • Status retrieval
Characteristics:
  • Synchronous communication
  • One sender, one receiver
  • Guaranteed response
Best for:
  • Creative generation tasks
  • Event broadcasting
  • Notification distribution
  • Multi-party collaboration
Characteristics:
  • Asynchronous communication
  • One publisher, multiple subscribers
  • Topic-based routing
Best for:
  • Decision-making processes
  • Voting and evaluation
  • Proposal selection
  • Quality review
Characteristics:
  • Multi-round negotiation
  • Threshold-based agreement
  • Democratic decision-making
Best for:
  • Code review
  • Solution optimization
  • Dispute resolution
  • Iterative quality improvement
Characteristics:
  • Turn-based discussion
  • Multiple rounds of refinement
  • Constructive criticism
Best for:
  • Data analysis
  • Batch processing
  • Distributed search
  • Independent subtask execution
Characteristics:
  • Simultaneous execution
  • Result aggregation
  • No dependencies between tasks
Best for:
  • Pipeline processing
  • Dependent task chains
  • Step-by-step workflows
  • Phased execution
Characteristics:
  • Serial execution
  • Output flows to next agent
  • Dependency management
Best for:
  • Complex project management
  • Human-in-the-loop workflows
  • Multi-phase task orchestration
  • Critical decision points
Characteristics:
  • 5-phase workflow
  • Human intervention points
  • Task tracking and reporting
  • Intelligent agent scheduling

Complete Example

use mofa_sdk::collaboration::{
    LLMDrivenCollaborationManager,
    RequestResponseProtocol,
    PublishSubscribeProtocol,
    ConsensusProtocol,
    CollaborationContent
};
use mofa_sdk::llm::{LLMClient, openai_from_env};
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create LLM client (optional)
    let provider = openai_from_env()?;
    let llm_client = Arc::new(LLMClient::new(Arc::new(provider)));
    
    // Create collaboration manager
    let manager = LLMDrivenCollaborationManager::new("agent_001");
    
    // Register LLM-enabled protocols
    manager.register_protocol(Arc::new(
        RequestResponseProtocol::with_llm("agent_001", llm_client.clone())
    )).await?;
    
    manager.register_protocol(Arc::new(
        PublishSubscribeProtocol::with_llm("agent_001", llm_client.clone())
    )).await?;
    
    manager.register_protocol(Arc::new(
        ConsensusProtocol::with_llm("agent_001", llm_client.clone())
    )).await?;
    
    // Execute task with automatic protocol selection
    let result = manager.execute_task_with_protocol(
        "request_response",
        CollaborationContent::Text(
            "Analyze customer feedback data and provide insights".to_string()
        )
    ).await?;
    
    println!("Success: {}", result.success);
    println!("Mode: {:?}", result.mode);
    println!("Duration: {}ms", result.duration_ms);
    
    if let Some(decision_ctx) = result.decision_context {
        println!("LLM Reasoning: {}", decision_ctx.reasoning);
        println!("Confidence: {}", decision_ctx.confidence);
    }
    
    Ok(())
}

Next Steps

Request-Response Pattern

Learn about one-to-one deterministic communication

Publish-Subscribe Pattern

Explore one-to-many event broadcasting

Consensus Mechanism

Implement multi-agent decision-making

Secretary Pattern

Build human-in-the-loop workflows

Build docs developers (and LLMs) love