MoFAAgent Trait
TheMoFAAgent trait is the unified core interface that all agents in the MoFA framework must implement. It provides the essential methods for agent lifecycle management, task execution, and state management.
Trait Definition
mofa-kernel/src/agent/core.rs:129
Required Methods
Identification Methods
Returns the unique identifier for this agent. The ID must remain constant throughout the agent’s entire lifecycle and is used for:
- Agent registration and discovery
- Logging and monitoring
- Message routing between agents
Returns a human-readable name for display and logging. Unlike the ID, the name does not need to be unique.
Returns the agent’s capability description, used for:
- Agent discovery and matching
- Task routing based on required capabilities
- Multi-agent coordination
Core Lifecycle Methods
Initializes the agent before task execution. Called to:
- Load resources and configuration
- Establish connections (database, network, etc.)
- Initialize internal state
Created → Initializing → ReadyParameters:ctx: Execution context providing configuration, event bus, etc.
execute
async fn(&mut self, input: AgentInput, ctx: &AgentContext) -> AgentResult<AgentOutput>
required
The primary execution method that processes input and returns output. This is the core method where the agent performs its work.State Transition:
Ready → Executing → ReadyParameters:input: Input data supporting multiple formats (text, JSON, binary, etc.)ctx: Execution context providing state, events, interrupts
Gracefully shuts down the agent, releasing resources:
- Save state
- Close connections
- Clean up resources
* → ShuttingDown → ShutdownSends an interrupt signal to the agent. The agent can choose how to respond:
- Stop immediately
- Complete the current step then stop
- Ignore the interrupt
InterruptResult::AcknowledgedState Transition: Executing → InterruptedReturns: Interrupt handling result indicating how the agent respondedState Query Method
Returns the agent’s current state, used for:
- Health checks
- Status monitoring
- State transition verification
Example Implementation
Real-World Example
Fromexamples/react_agent/src/main.rs:
Architecture Design
TheMoFAAgent trait follows microkernel architecture principles:
Extension Traits
The coreMoFAAgent trait can be extended with optional traits for additional functionality:
- AgentLifecycle - Adds
pause()andresume()methods - AgentMessaging - Adds message and event handling
- AgentPluginSupport - Adds plugin registration and management
See Also
- AgentInput & AgentOutput - Input and output types
- AgentContext - Execution context
- AgentCapabilities - Capability discovery
- AgentLifecycle - Lifecycle extension trait