Overview
MoFA provides native language bindings for Python, Java, Kotlin, Swift, and Go through Mozilla UniFFI. These bindings expose the full power of MoFA’s Rust core with zero-cost abstractions and idiomatic APIs for each language.Python
Native Python bindings with type hints and async support
Java
Java bindings with Maven/Gradle integration
Kotlin
Kotlin bindings with coroutine support
Swift
Swift bindings for iOS and macOS
Go
Go bindings with goroutine support
Architecture
MoFA’s binding architecture leverages UniFFI to generate language-specific interfaces from a single UDL (UniFFI Definition Language) file:Layered Architecture
Kernel Layer
Core Rust implementation in
mofa-kernel - agent lifecycle, metadata, communication primitivesFoundation Layer
Business logic in
mofa-foundation - LLM integration, persistence, agent abstractionsUniFFI Definition Language
Themofa.udl file defines the interface exposed to all languages:
Core Features
LLM Agent Interface
All bindings provide a consistent LLM agent API:- Builder Pattern: Fluent API for agent configuration
- Simple Q&A: Stateless question answering via
ask() - Multi-turn Chat: Contextual conversations via
chat() - History Management: Access and clear conversation history
- Multiple Providers: OpenAI, Azure, Anthropic, Gemini, Ollama, custom
Session Management
Persistent conversation sessions with storage backends:- In-Memory Sessions: Fast, ephemeral storage
- File-Based Sessions: JSONL persistence
- Metadata Support: Store custom key-value pairs
- Session Lifecycle: Create, retrieve, save, delete, list
Tool Registry
Define custom tools in your language that agents can invoke:- FfiToolCallback Interface: Define tools in Python, Java, Kotlin, Swift, or Go
- Dynamic Registration: Register tools at runtime
- JSON Schema: Automatic parameter validation
- Error Handling: Structured success/failure results
Type Conversions
Rust to Language Mappings
| Rust Type | Python | Java | Kotlin | Swift | Go |
|---|---|---|---|---|---|
String | str | String | String | String | string |
bool | bool | boolean | Boolean | Bool | bool |
u32 | int | int | Int | UInt32 | uint32 |
f32 | float | float | Float | Float | float32 |
Vec<T> | List[T] | List<T> | List<T> | [T] | []T |
Option<T> | Optional[T] | T / null | T? | T? | *T |
Result<T, E> | raises E | throws E | throws E | throws E | (T, error) |
FFI-Safe Types
UniFFI enforces FFI-safe type boundaries:Performance Characteristics
Zero-Copy Where Possible
UniFFI minimizes data copying:- String Views: Shared string data where safe
- Reference Counting:
Arc<T>for shared ownership - Direct FFI Calls: No intermediate serialization for primitives
Async Bridging
Rust’s async runtime is bridged to language-native async:Building from Source
Prerequisites
- All Languages
- Python
- Java/Kotlin
- Swift
- Go
Common Patterns
Error Handling
All bindings expose unified error types:Resource Management
UniFFI handles reference counting automatically:- Automatic Cleanup: Objects are freed when no longer referenced
- No Manual Memory Management: GC/ARC handles cleanup
- Thread-Safe: Internal
Arc<RwLock<T>>for concurrent access
Limitations
Current Constraints
Workarounds
- Callbacks: Use
FfiToolCallbacktrait for foreign tool implementations - Generics: Use JSON strings for flexible data exchange
- Streaming: Return batch results, not true streams (for now)
Next Steps
Python Guide
Complete Python binding documentation with examples
Java Guide
Java integration with Maven and Gradle
Kotlin Guide
Kotlin bindings with coroutines
Swift Guide
Swift bindings for Apple platforms
Go Guide
Go bindings with idiomatic Go patterns