CloudLLM
CloudLLM is a batteries-included Rust toolkit for building intelligent agents with LLM integration, multi-protocol tool support, and multi-agent orchestration. It provides:
- Agents with Tools: Create agents that connect to LLMs and execute actions through a flexible, multi-protocol tool system (local, remote MCP, Memory, custom protocols),
- Server Deployment: Easy standalone MCP server creation via
MCPServerBuilderwith HTTP, authentication, and IP filtering, - Flexible Tool Creation: From simple Rust closures to advanced custom protocol implementations,
- Stateful Sessions: A
LLMSessionfor managing conversation history with context trimming and token accounting, - Multi-Agent Orchestration: A
councilengine supporting Parallel, RoundRobin, Moderated, Hierarchical, and Debate collaboration patterns, - Provider Flexibility: Unified
ClientWrappertrait for OpenAI, Claude, Gemini, Grok, and custom OpenAI-compatible endpoints.
The entire public API is documented with compilable examples—run cargo doc --open to browse the
crate-level manual.
Installation
Add CloudLLM to your project:
[]
= "0.6.0"
The crate targets tokio 1.x and Rust 1.70+.
Quick start
1. Initialising a session
use Arc;
use ;
use ;
async
2. Streaming tokens in real time
use ;
use ;
use StreamExt;
use Arc;
async
Provider wrappers
CloudLLM ships wrappers for popular OpenAI-compatible services:
| Provider | Module | Notable constructors |
|---|---|---|
| OpenAI | cloudllm::clients::openai |
OpenAIClient::new_with_model_enum, OpenAIClient::new_with_base_url |
| Anthropic Claude | cloudllm::clients::claude |
ClaudeClient::new_with_model_enum |
| Google Gemini | cloudllm::clients::gemini |
GeminiClient::new_with_model_enum |
| xAI Grok | cloudllm::clients::grok |
GrokClient::new_with_model_enum |
Providers share the ClientWrapper
contract, so you can swap them without changing downstream code.
use ClientWrapper;
use ;
use ;
async
Every wrapper exposes token accounting via ClientWrapper::get_last_usage.
LLMSession: Stateful Conversations (The Foundation)
LLMSession is the core building block—it maintains conversation history with automatic context trimming and token accounting. Use it for simple stateful conversations with any LLM provider:
use Arc;
use ;
use ;
async
Agents: Building Intelligent Workers with Tools
Agents extend LLMSession by adding identity, expertise, and optional tools. They're the primary way to build sophisticated LLM interactions where you need the agent to take actions beyond conversation:
use Arc;
use Agent;
use ;
use ToolRegistry;
async
Tool Registry: Multi-Protocol Tool Access
Agents access tools through the ToolRegistry, which supports multiple simultaneous protocols. Use local tools, remote MCP servers, persistent Memory, or custom implementations—all transparently:
Adding Tools to a Registry
use Arc;
use ToolRegistry;
use ;
async
Key Benefits:
- Local + Remote: Mix tools from different sources in a single agent
- Transparent Routing: Registry automatically routes calls to the correct protocol
- Dynamic Management: Add/remove protocols at runtime
- Backward Compatible: Existing single-protocol code still works
Registry Modes
Multi-Protocol (New agents):
let mut registry = empty;
registry.add_protocol.await?;
Single-Protocol (Existing code):
let protocol = new;
let registry = new;
Deploying Tool Servers with MCPServerBuilder
Create standalone MCP servers exposing tools over HTTP. Perfect for microservices, integration testing, or sharing tools across your infrastructure:
use Arc;
use MCPServerBuilder;
use CustomToolProtocol;
use ;
async
Available on the mcp-server feature. Other agents connect via McpClientProtocol::new("http://localhost:8080").
Creating Tools: Simple to Advanced
CloudLLM provides a powerful, protocol-agnostic tool system that works seamlessly with agents and councils. Tools enable agents to take actions beyond conversation—calculate values, query databases, call APIs, or maintain state across sessions.
Simple Tool Creation: Rust Closures
Register Rust functions or closures as tools. Perfect for quick prototyping:
use Arc;
use CustomToolProtocol;
use ;
async
Advanced Tool Creation: Custom Protocol Implementation
For complex tools or external system integration, implement the ToolProtocol trait:
use async_trait;
use ;
use Error;
;
Using Tools with Agents
Agents use tools through a registry. Connect any tool source to an agent:
use Arc;
use Agent;
use ;
use CustomToolProtocol;
use ;
async
Protocol Implementations
1. CustomToolProtocol (Local Rust Functions)
Register local Rust closures or async functions as tools. Covered above under "Simple Tool Creation".
2. McpClientProtocol (Remote MCP Servers)
Connect to remote MCP servers:
use Arc;
use McpClientProtocol;
async
3. MemoryProtocol (Persistent Agent State)
For maintaining state across sessions within a single process:
use Arc;
use Memory;
use MemoryProtocol;
use ToolRegistry;
async
Built-in Tools
CloudLLM includes several production-ready tools that agents can use directly:
Calculator Tool
A fast, reliable scientific calculator for mathematical operations and statistical analysis. Perfect for agents that need to perform computations.
Features:
- Comprehensive arithmetic operations (
+,-,*,/,^,%) - Trigonometric functions (sin, cos, tan, csc, sec, cot, asin, acos, atan)
- Hyperbolic functions (sinh, cosh, tanh, csch, sech, coth)
- Logarithmic and exponential functions (ln, log, log2, exp)
- Statistical operations (mean, median, mode, std, stdpop, var, varpop, sum, count, min, max)
- Mathematical constants (pi, e)
Usage Example:
use Calculator;
async
More Examples:
sqrt(16)→ 4.0log(100)→ 2.0 (base 10)std([1, 2, 3, 4, 5])→ 1.581 (sample standard deviation)floor(3.7)→ 3.0
For comprehensive documentation, see Calculator API docs.
Memory Tool
A persistent, TTL-aware key-value store for maintaining agent state across sessions. Perfect for single agents to track progress or multi-agent councils to coordinate decisions.
Features:
- Key-value storage with optional TTL (time-to-live) expiration
- Automatic background expiration of stale entries (1-second cleanup)
- Metadata tracking (creation timestamp, expiration time)
- Succinct protocol for LLM communication (token-efficient)
- Thread-safe shared access across agents
- Designed specifically for agent communication (not a general database)
Basic Usage Example:
use Memory;
async
Using with Agents via Tool Protocol:
use Arc;
use Memory;
use MemoryProtocol;
use ToolRegistry;
use Agent;
use ;
async
Memory Protocol Commands (for agents):
The Memory tool uses a token-efficient protocol designed for LLM communication:
| Command | Syntax | Example | Use Case |
|---|---|---|---|
| Put | P <key> <value> [TTL:<seconds>] |
P task_status InProgress TTL:3600 |
Store state with 1-hour expiration |
| Get | G <key> [META] |
G task_status META |
Retrieve value + metadata |
| List | L [META] |
L META |
List all keys with metadata |
| Delete | D <key> |
D task_status |
Remove specific memory |
| Clear | C |
C |
Wipe all memories |
| Spec | SPEC |
SPEC |
Get protocol specification |
Use Case Examples:
-
Single-Agent Progress Tracking:
Agent stores: "P document_checkpoint Page 247 TTL:86400" Later: "G document_checkpoint" → retrieves current progress -
Multi-Agent Council Coordination:
Agent A stores: "P decision_consensus Approved TTL:3600" Agent B reads: "G decision_consensus" Agent C confirms: "L" → sees what's been decided -
Session Recovery:
Before shutdown: "P session_state {full_context} TTL:604800" (1 week) After restart: "G session_state" → resume from checkpoint -
Audit Trail:
Store each decision: "P milestone_v1 Completed TTL:2592000" (30 days) Track progress: "L META" → see timestamp and TTL of each milestone
Best Practices:
- Use TTL wisely: Temporary data (hours), permanent decisions (None)
- Clear old memories: Call
CorDto free space - Descriptive keys: Use clear, hierarchical names like
decision_inference_v2 - Batch operations: Use
L METAto understand stored state before updates - Monitor expiration: Check metadata to prevent unexpected data loss
Multi-Agent Memory Sharing:
use Arc;
use Memory;
use MemoryProtocol;
use ToolRegistry;
use ;
async
For comprehensive documentation and patterns, see Memory API docs.
HTTP Client Tool
A secure REST API client for calling external services with domain allowlist/blocklist protection. Perfect for agents that need to make HTTP requests to external APIs.
Features:
- All HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD)
- Domain security with allowlist/blocklist (blocklist takes precedence)
- Basic authentication and bearer token support
- Custom headers and query parameters with automatic URL encoding
- JSON response parsing
- Configurable request timeout and response size limits
- Thread-safe with connection pooling
- Builder pattern for chainable configuration
Usage Example:
use HttpClient;
use Duration;
async
Security Features:
- Allowlist: Restrict requests to trusted domains only
- Blocklist: Explicitly block malicious domains
- Precedence: Blocklist always takes precedence over allowlist
- No allowlist = All allowed: Empty allowlist means any domain is allowed (unless in blocklist)
More Examples:
- Basic auth:
client.with_basic_auth("username", "password") - Custom header:
client.with_header("X-API-Key", "secret123") - Query params:
client.with_query_param("page", "1").with_query_param("limit", "50") - Size limit:
client.with_max_response_size(50 * 1024 * 1024)(50MB) - Short timeout:
client.with_timeout(Duration::from_secs(5))
For comprehensive documentation and more examples, see HttpClient API docs and run cargo run --example http_client_example.
Using HTTP Client Tool with Agents
The HTTP Client tool can be exposed to agents through the MCP protocol, allowing agents to make API calls autonomously. Here's how to set it up:
Step 1: Create an MCP HTTP Server (expose via HTTP)
Create an HTTP server that exposes the HTTP Client tool via MCP protocol. This server can be accessed by agents over the network:
use Arc;
use HttpClient;
use CustomToolProtocol;
use ;
use json;
use ;
use SocketAddr;
async
Add to Cargo.toml:
= "0.7"
Usage:
Once running, other services/agents can call this MCP server:
# List available tools
# Use http_get tool
This MCP server can now be referenced by agents using McpClientProtocol::new("http://localhost:8080"), allowing them to access HTTP capabilities securely and with domain restrictions.
**Step 2: Create an Agent that Uses HTTP Client Tools**
```rust,no_run
use std::sync::Arc;
use cloudllm::council::Agent;
use cloudllm::clients::openai::{OpenAIClient, Model};
use cloudllm::tool_protocol::ToolRegistry;
use cloudllm::tool_protocols::CustomToolProtocol;
use cloudllm::tool_protocol::{ToolMetadata, ToolParameter, ToolParameterType, ToolResult};
use cloudllm::tools::HttpClient;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create HTTP client with security settings
let mut http_client = HttpClient::new();
// Configure security: only allow trusted domains
http_client.allow_domain("api.github.com");
http_client.allow_domain("api.example.com");
// Configure authentication
http_client.with_header("User-Agent", "CloudLLM-Agent/1.0");
let http_client = Arc::new(http_client);
// Wrap with CustomToolProtocol to expose to agents
let mut protocol = CustomToolProtocol::new();
// Register HTTP GET tool using the actual HttpClient
let client = http_client.clone();
protocol.register_async_tool(
ToolMetadata::new("get_json_api", "Fetch JSON data from an API endpoint")
.with_parameter(
ToolParameter::new("url", ToolParameterType::String)
.with_description("The URL to fetch (must be from allowed domains)")
.required()
)
.with_parameter(
ToolParameter::new("headers", ToolParameterType::Object)
.with_description("Optional custom headers")
),
Arc::new(move |params| {
let client = client.clone();
Box::pin(async move {
let url = params["url"]
.as_str()
.ok_or("url parameter is required")?;
// Use the actual HttpClient to make the request
match client.get(url).await {
Ok(response) => {
if response.is_success() {
// Try to parse as JSON
match response.json() {
Ok(json_data) => {
Ok(ToolResult::success(json!({
"status": response.status,
"data": json_data
})))
}
Err(_) => {
// Not JSON, return raw body
Ok(ToolResult::success(json!({
"status": response.status,
"body": response.body
})))
}
}
} else {
Ok(ToolResult::error(format!(
"HTTP {} error: {}",
response.status, response.body
)))
}
}
Err(e) => Ok(ToolResult::error(format!(
"Request failed: {}",
e
)))
}
})
})
).await;
// Register HTTP POST tool for sending data
let client = http_client.clone();
protocol.register_async_tool(
ToolMetadata::new("post_json_api", "Post JSON data to an API endpoint")
.with_parameter(
ToolParameter::new("url", ToolParameterType::String)
.with_description("The URL to POST to (must be from allowed domains)")
.required()
)
.with_parameter(
ToolParameter::new("data", ToolParameterType::Object)
.with_description("JSON data to send")
.required()
),
Arc::new(move |params| {
let client = client.clone();
Box::pin(async move {
let url = params["url"]
.as_str()
.ok_or("url parameter is required")?;
let data = params["data"].clone();
// Use the actual HttpClient to POST
match client.post(url, data).await {
Ok(response) => {
if response.is_success() {
Ok(ToolResult::success(json!({
"status": response.status,
"message": "Data posted successfully"
})))
} else {
Ok(ToolResult::error(format!(
"HTTP {} error: {}",
response.status, response.body
)))
}
}
Err(e) => Ok(ToolResult::error(format!(
"Request failed: {}",
e
)))
}
})
})
).await;
// Create tool registry
let registry = Arc::new(ToolRegistry::new(Arc::new(protocol)));
// Create agent with HTTP access
let mut agent = Agent::new(
"api-agent",
"API Integration Agent",
Arc::new(OpenAIClient::new_with_model_enum(
&std::env::var("OPEN_AI_SECRET")?,
Model::GPT41Mini
)),
)
.with_expertise("Makes HTTP requests to external APIs")
.with_tools(registry);
// Agent can now make authenticated, secure API calls!
println!("✓ Agent configured with HTTP tools");
println!("✓ Allowed domains: api.github.com, api.example.com");
println!("✓ Agent can now GET and POST to these APIs");
Ok(())
}
Step 3: Configure Agent System Prompt for HTTP Usage
Teach the agent about available HTTP tools via the system prompt:
You have access to HTTP tools for making API calls:
1. get_json_api(url: string, headers?: object)
- Fetches JSON data from an API endpoint
- Returns: {status: number, body: string}
- Security: Only allowed domains are accessible
- Use this to fetch real-time data from external services
2. post_json_api(url: string, data: object, headers?: object)
- Posts JSON data to an API endpoint
- Use this to submit data to external services
Always check the response status before processing the body.
When calling APIs, include appropriate headers like Content-Type.
Never share authentication tokens in logs.
Step 4: Multi-MCP Setup (Advanced)
Combine HTTP Client with other tools via multiple MCP servers:
use Arc;
use Agent;
use ;
use ToolRegistry;
use CustomToolProtocol;
async
Security Best Practices:
-
Domain Allowlist: Configure HTTP clients with domain allowlists to prevent unauthorized requests
let mut client = new; client.allow_domain; client.allow_domain; -
Deny Malicious Domains: Use blocklists as a second layer
client.deny_domain; -
Timeout Protection: Set reasonable timeouts to prevent hanging requests
use Duration; client.with_timeout; -
Size Limits: Limit response sizes to prevent memory exhaustion
client.with_max_response_size; // 10MB -
Authentication: Use appropriate auth methods when needed
client.with_basic_auth; // or client.with_header;
Bash Tool
Secure command execution on Linux and macOS with timeout and security controls. See BashTool API docs.
File System Tool
Safe file and directory operations with path traversal protection and optional extension filtering. Perfect for agents that need to read, write, and manage files within designated directories.
Key Features:
- Read, write, append, and delete files
- Directory creation, listing, and recursive deletion
- File metadata retrieval (size, modification time, is_directory)
- File search with pattern matching
- Path traversal prevention (
../../../etc/passwdis blocked) - Optional file extension filtering for security
- Root path restriction for sandboxing
Basic Usage:
use FileSystemTool;
use PathBuf;
// Create tool with root path restriction
let fs = new
.with_root_path
.with_allowed_extensions;
// Write a file
fs.write_file.await?;
// Read a file
let content = fs.read_file.await?;
// List directory contents
let entries = fs.read_directory.await?;
for entry in entries
// Get metadata
let metadata = fs.get_file_metadata.await?;
println!;
Security:
- All paths are normalized to prevent traversal attacks
- Root path restriction ensures operations stay within designated directory
- Extension filtering can prevent execution of dangerous file types
- Works safely with untrusted input
For comprehensive documentation and examples, see the FileSystemTool API docs and examples/filesystem_example.rs.
Creating Custom Protocol Adapters
Implement the ToolProtocol trait to support new protocols:
use async_trait;
use ;
use Error;
/// Example: Custom protocol adapter for a hypothetical service
Using Tools in Agent System Prompts
Teach agents about available tools via the system prompt:
You have access to the following tools:
1. Calculator (add, subtract, multiply)
- Use for mathematical operations
- Respond with: {"tool_call": {"name": "add", "parameters": {"a": 5, "b": 3}}}
2. Memory System
- Store important information
- Use command: P key value ttl
- Retrieve with: G key META
Always use tools when they can help answer the user's question. After using a tool,
incorporate the result into your response.
Best Practices for Tools
- Clear Names & Descriptions: Make tool purposes obvious to LLMs
- Comprehensive Parameters: Document all required and optional parameters
- Error Handling: Return meaningful error messages in ToolResult
- Atomicity: Each tool should do one thing well
- Documentation: Include examples in tool descriptions
- Testing: Test tool execution in isolation before integration
For more examples, see the examples/ directory and run cargo doc --open for complete API documentation.
Councils: multi-agent orchestration
The council module orchestrates conversations between agents built on any ClientWrapper.
Choose from parallel, round-robin, moderated, hierarchical, or debate modes.
use Arc;
use ;
use ;
async
For a deep dive, read COUNCIL_TUTORIAL.md which walks through each
collaboration mode with progressively sophisticated examples.
Examples
Clone the repository and run the provided examples:
Each example corresponds to a module in the documentation so you can cross-reference the code with explanations.
Support & contributing
Issues and pull requests are welcome via GitHub.
Please open focused pull requests against main and include tests or doc updates where relevant.
CloudLLM is released under the MIT License.
Happy orchestration! 🤖🤝🤖