use cloudllm::tool_protocol::ToolProtocol;
use cloudllm::tool_protocols::MemoryProtocol;
use cloudllm::tools::Memory;
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
cloudllm::init_logger();
println!("=== MCP Memory Server Example ===\n");
let memory = Arc::new(Memory::new());
println!("Server Configuration:");
println!(" Protocol: MCP (Model Context Protocol)");
println!(" Memory: TTL-aware persistent key-value store");
println!(" Expiration: Automatic background cleanup every 1 second\n");
let adapter = Arc::new(MemoryProtocol::new(memory.clone()));
println!("Available Tools:");
match adapter.list_tools().await {
Ok(tools) => {
for tool in tools {
println!(" - {} ({})", tool.name, tool.description);
}
}
Err(e) => println!(" Error: {}", e),
}
println!();
println!("=== Endpoint Documentation ===\n");
println!("GET /tools");
println!(" Returns: List of available tools");
println!(" Example response:");
println!(" [");
println!(" {{");
println!(" \"name\": \"memory\",");
println!(" \"description\": \"Persistent memory...\",");
println!(" \"parameters\": [");
println!(" {{");
println!(" \"name\": \"command\",");
println!(" \"param_type\": \"String\",");
println!(" \"description\": \"Memory protocol command\",");
println!(" \"required\": true");
println!(" }}");
println!(" ]");
println!(" }}");
println!(" ]\n");
println!("POST /execute");
println!(" Input: {{\"tool\": \"memory\", \"parameters\": {{\"command\": \"...\"}}}}");
println!(" Returns: {{\"success\": true/false, \"output\": {{...}}}}\n");
println!("=== Example Commands ===\n");
let example_commands = vec![
("P task1 important_data 3600", "Store task1 with 1-hour TTL"),
("P task2 another_value 7200", "Store task2 with 2-hour TTL"),
("G task1", "Retrieve task1 without metadata"),
("G task1 META", "Retrieve task1 with metadata"),
("L", "List all keys"),
("L META", "List all keys with metadata"),
("T A", "Get total memory usage"),
("T K", "Get keys memory usage"),
("T V", "Get values memory usage"),
("SPEC", "Get protocol specification"),
];
for (cmd, description) in &example_commands {
println!("Command: {}", cmd);
println!("Description: {}", description);
let params = serde_json::json!({"command": cmd});
match adapter.execute("memory", params).await {
Ok(result) => {
if result.success {
println!("Result: Success");
println!("Output: {}", result.output);
} else {
println!("Result: Failed");
println!("Error: {}", result.output);
}
}
Err(e) => println!("Error: {}", e),
}
println!();
}
println!("=== Deployment Guide ===\n");
println!("Local Development:");
println!(" cargo run --example mcp_memory_server");
println!(" Client connects to: http://localhost:8080\n");
println!("Docker Container:");
println!(" docker run -p 8080:8080 cloudllm-mcp-memory");
println!(" Client connects to: http://container-host:8080\n");
println!("Kubernetes Deployment:");
println!(" kubectl apply -f mcp-memory-server.yaml");
println!(" Service: mcp-memory-server.default.svc.cluster.local:8080");
println!(" Client connects to: http://mcp-memory-server.default.svc.cluster.local:8080\n");
println!("Multi-Agent Fleet Pattern:");
println!(" 1. Deploy single MCP Memory Server instance");
println!(" 2. Each agent gets a McpMemoryProtocol pointing to the server");
println!(" 3. Agents can coordinate through shared memory");
println!(" 4. Server manages TTL expiration automatically");
println!(" 5. Easy to scale by replicating server and using load balancer\n");
println!("=== Architecture Benefits ===\n");
println!("✓ Centralized State Management");
println!(" All agents access the same memory store");
println!(" Consistent coordination across distributed systems\n");
println!("✓ Language-Agnostic");
println!(" MCP HTTP interface can be used by any language");
println!(" Python, Go, JavaScript agents can use the same memory\n");
println!("✓ Process Boundaries");
println!(" Memory lives in a separate process");
println!(" Survives agent restarts (unless using RAM storage)\n");
println!("✓ Automatic Cleanup");
println!(" TTL-based expiration removes stale data");
println!(" No manual cleanup needed\n");
println!("✓ Production-Ready");
println!(" Token-efficient protocol minimizes overhead");
println!(" Thread-safe access with Arc<Mutex<>>");
println!(" Error handling for all edge cases\n");
println!("=== Testing ===\n");
println!("Test client connection:");
println!(" curl http://localhost:8080/tools\n");
println!("Test store operation:");
println!(" curl -X POST http://localhost:8080/execute \\");
println!(" -H 'Content-Type: application/json' \\");
println!(" -d '{{\"tool\": \"memory\", \"parameters\": {{\"command\": \"P test hello 3600\"}}}}'\n");
println!("Test retrieve operation:");
println!(" curl -X POST http://localhost:8080/execute \\");
println!(" -H 'Content-Type: application/json' \\");
println!(" -d '{{\"tool\": \"memory\", \"parameters\": {{\"command\": \"G test\"}}}}'\n");
println!("=== Notes ===\n");
println!("• This example demonstrates the API and command flow");
println!("• A full implementation would need an HTTP server (e.g., Axum)");
println!("• The MemoryProtocol handles all protocol command parsing");
println!("• Memory automatically expires entries based on TTL");
println!("• All operations are thread-safe and async-compatible\n");
Ok(())
}