Skip to content

Send Message Tool

The SendMessageTool facilitates communication between agents in the Bedrock Swarm framework. It provides a standardized way to send and receive messages between agents, with support for different message types and delivery options.

Class Documentation

Bases: BaseTool

Tool for sending messages between agents.

PARAMETER DESCRIPTION
valid_recipients

Optional list of valid recipient names

TYPE: Optional[List[str]] DEFAULT: None

description

Optional tool description

TYPE: Optional[str] DEFAULT: None

agency

Optional reference to the agency

TYPE: Optional[Agency] DEFAULT: None

Source code in src/bedrock_swarm/tools/send_message.py
def __init__(
    self,
    valid_recipients: Optional[List[str]] = None,
    description: Optional[str] = None,
    agency: Optional["Agency"] = None,
) -> None:
    """Initialize the send message tool.

    Args:
        valid_recipients: Optional list of valid recipient names
        description: Optional tool description
        agency: Optional reference to the agency
    """
    self._name = "SendMessage"
    self._description = description or "Send a message to another agent"
    self._valid_recipients = valid_recipients or []
    self._agency = agency

Attributes

name: str property

Get tool name.

description: str property

Get tool description.

Functions

get_schema() -> Dict[str, Any]

Get JSON schema for the send message tool.

Source code in src/bedrock_swarm/tools/send_message.py
def get_schema(self) -> Dict[str, Any]:
    """Get JSON schema for the send message tool."""
    return {
        "name": self.name,
        "description": self.description,
        "parameters": {
            "type": "object",
            "properties": {
                "recipient": {
                    "type": "string",
                    "description": f"Name of the recipient agent. Valid recipients: {', '.join(self._valid_recipients)}",
                },
                "message": {
                    "type": "string",
                    "description": "Message to send to the recipient",
                },
            },
            "required": ["recipient", "message"],
        },
    }

_execute_impl(*, recipient: str, message: str, **kwargs: Any) -> str

Execute the send message tool.

PARAMETER DESCRIPTION
recipient

Name of the recipient agent

TYPE: str

message

Message to send

TYPE: str

**kwargs

Additional keyword arguments

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
str

Response from the recipient agent

RAISES DESCRIPTION
ValueError

If recipient is not valid

Source code in src/bedrock_swarm/tools/send_message.py
def _execute_impl(self, *, recipient: str, message: str, **kwargs: Any) -> str:
    """Execute the send message tool.

    Args:
        recipient: Name of the recipient agent
        message: Message to send
        **kwargs: Additional keyword arguments

    Returns:
        Response from the recipient agent

    Raises:
        ValueError: If recipient is not valid
    """
    if recipient not in self._valid_recipients:
        raise ValueError(f"Invalid recipient: {recipient}")

    # Get thread from kwargs
    thread = kwargs.get("thread")
    if not thread:
        raise ValueError("No thread provided")

    # Process message through recipient's thread
    recipient_agent = self._agency.get_agent(recipient)
    if not recipient_agent:
        raise ValueError(f"Recipient agent {recipient} not found")

    return self._agency.get_completion(
        message=message,
        recipient_agent=recipient_agent,
        thread_id=thread.id,
    )

Features

The send message tool supports:

  1. Message Types:
  2. Text messages
  3. Structured data
  4. Binary content
  5. Event notifications

  6. Delivery Options:

  7. Synchronous sending
  8. Asynchronous delivery
  9. Broadcast messages
  10. Direct messages

  11. Message Properties:

  12. Priority levels
  13. Delivery status
  14. Message metadata
  15. Timestamps

Usage Examples

from bedrock_swarm.tools import SendMessageTool

message_tool = SendMessageTool()

# Send a simple message
await message_tool.send(
    recipient="agent_id",
    content="Hello, how are you?",
    priority="normal"
)

# Send structured data
data = {
    "type": "task_update",
    "status": "completed",
    "timestamp": "2024-02-29T15:30:45Z"
}
await message_tool.send(
    recipient="agent_id",
    content=data,
    priority="high"
)

# Broadcast message
await message_tool.broadcast(
    content="System maintenance in 5 minutes",
    priority="high"
)

Error Handling

The send message tool handles:

  1. Invalid recipients
  2. Message delivery failures
  3. Network errors
  4. Invalid message formats
  5. Timeout conditions

Implementation Details

The tool implementation includes:

  1. Message queuing system
  2. Delivery confirmation
  3. Retry mechanisms
  4. Priority handling
  5. Error recovery

Message Format

Messages follow this structure:

{
    "id": "msg_123",
    "sender": "agent_a",
    "recipient": "agent_b",
    "content": "Message content",
    "priority": "normal",
    "timestamp": "2024-02-29T15:30:45Z",
    "metadata": {
        "type": "text",
        "version": "1.0"
    }
}