cloudllm 0.15.9

A batteries-included Rust toolkit for building intelligent agents with LLM integration, multi-protocol tool support, multi-agent orchestration, and MentisDB-backed durable memory.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#![allow(dead_code)]

use cloudllm::tool_protocol::{
    ToolMetadata, ToolParameter, ToolParameterType, ToolRegistry, ToolResult,
};
use cloudllm::tool_protocols::{
    BashProtocol, CustomToolProtocol, HttpClientProtocol, McpClientProtocol, MemoryProtocol,
};
use cloudllm::tools::{BashTool, Calculator, FileSystemTool, HttpClient, Memory, Platform};
use serde_json::{json, Value};
use std::error::Error;
use std::path::PathBuf;
use std::sync::Arc;

pub async fn build_persistent_agent_registry(
    mentisdb_endpoint: &str,
    filesystem_root: PathBuf,
) -> Result<(ToolRegistry, Arc<McpClientProtocol>), Box<dyn Error + Send + Sync>> {
    let mentisdb_protocol =
        Arc::new(McpClientProtocol::new(mentisdb_endpoint.to_string()).with_cache_ttl(30));

    let mut registry = ToolRegistry::empty();
    registry
        .add_protocol("mentisdb", mentisdb_protocol.clone())
        .await?;

    let memory = Arc::new(Memory::new());
    registry
        .add_protocol("memory", Arc::new(MemoryProtocol::new(memory)))
        .await?;

    let bash_tool = Arc::new(BashTool::new(detect_platform()).with_timeout(30));
    registry
        .add_protocol("bash", Arc::new(BashProtocol::new(bash_tool)))
        .await?;

    let http_client = Arc::new(HttpClient::new());
    registry
        .add_protocol("http", Arc::new(HttpClientProtocol::new(http_client)))
        .await?;

    let custom = Arc::new(CustomToolProtocol::new());
    register_calculator_tool(custom.clone()).await;
    register_filesystem_tools(custom.clone(), filesystem_root).await;
    registry.add_protocol("custom", custom).await?;

    Ok((registry, mentisdb_protocol))
}

async fn register_calculator_tool(protocol: Arc<CustomToolProtocol>) {
    let calculator = Arc::new(Calculator::new());
    protocol
        .register_async_tool(
            ToolMetadata::new("calculator", "Evaluate a mathematical expression.").with_parameter(
                ToolParameter::new("expression", ToolParameterType::String)
                    .with_description("Expression to evaluate, e.g. 'sqrt(16) + mean([1,2,3])'.")
                    .required(),
            ),
            Arc::new(move |params| {
                let calculator = calculator.clone();
                Box::pin(async move {
                    let expression = required_string(&params, "expression")?;
                    match calculator.evaluate(&expression).await {
                        Ok(result) => Ok(ToolResult::success(json!({ "result": result }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;
}

async fn register_filesystem_tools(protocol: Arc<CustomToolProtocol>, filesystem_root: PathBuf) {
    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));

    protocol
        .register_async_tool(
            ToolMetadata::new(
                "read_file",
                "Read a text file inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the file.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    match filesystem.read_file(&path).await {
                        Ok(content) => Ok(ToolResult::success(json!({ "content": content }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "write_file",
                "Write a text file inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the file.")
                    .required(),
            )
            .with_parameter(
                ToolParameter::new("content", ToolParameterType::String)
                    .with_description("Text content to write.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    let content = required_string(&params, "content")?;
                    match filesystem.write_file(&path, &content).await {
                        Ok(()) => Ok(ToolResult::success(json!({ "status": "OK" }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "append_file",
                "Append text to a file inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the file.")
                    .required(),
            )
            .with_parameter(
                ToolParameter::new("content", ToolParameterType::String)
                    .with_description("Text content to append.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    let content = required_string(&params, "content")?;
                    match filesystem.append_file(&path, &content).await {
                        Ok(()) => Ok(ToolResult::success(json!({ "status": "OK" }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "list_directory",
                "List a directory inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the directory.")
                    .required(),
            )
            .with_parameter(
                ToolParameter::new("recursive", ToolParameterType::Boolean)
                    .with_description("Whether to recurse into subdirectories."),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    let recursive = params
                        .get("recursive")
                        .and_then(Value::as_bool)
                        .unwrap_or(false);
                    match filesystem.read_directory(&path, recursive).await {
                        Ok(entries) => Ok(ToolResult::success(json!({
                            "entries": entries
                                .iter()
                                .map(directory_entry_to_json)
                                .collect::<Vec<_>>()
                        }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "file_metadata",
                "Return file metadata inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the file.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    match filesystem.get_file_metadata(&path).await {
                        Ok(metadata) => Ok(ToolResult::success(json!({
                            "metadata": file_metadata_to_json(&metadata)
                        }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "create_directory",
                "Create a directory inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the directory.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    match filesystem.create_directory(&path).await {
                        Ok(()) => Ok(ToolResult::success(json!({ "status": "OK" }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "delete_file",
                "Delete a file inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the file.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    match filesystem.delete_file(&path).await {
                        Ok(()) => Ok(ToolResult::success(json!({ "status": "OK" }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "delete_directory",
                "Delete a directory inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the directory.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    match filesystem.delete_directory(&path).await {
                        Ok(()) => Ok(ToolResult::success(json!({ "status": "OK" }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "search_files",
                "Search for files by substring within the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("directory", ToolParameterType::String)
                    .with_description("Relative path to the directory to search.")
                    .required(),
            )
            .with_parameter(
                ToolParameter::new("pattern", ToolParameterType::String)
                    .with_description("Substring to search for in file names.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let directory = required_string(&params, "directory")?;
                    let pattern = required_string(&params, "pattern")?;
                    match filesystem.search_files(&directory, &pattern).await {
                        Ok(entries) => Ok(ToolResult::success(json!({
                            "entries": entries
                                .iter()
                                .map(directory_entry_to_json)
                                .collect::<Vec<_>>()
                        }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root.clone()));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "file_exists",
                "Check whether a path exists inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to check.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    match filesystem.file_exists(&path).await {
                        Ok(exists) => Ok(ToolResult::success(json!({ "exists": exists }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;

    let filesystem = Arc::new(FileSystemTool::new().with_root_path(filesystem_root));
    protocol
        .register_async_tool(
            ToolMetadata::new(
                "file_size",
                "Return a file size in bytes inside the configured workspace root.",
            )
            .with_parameter(
                ToolParameter::new("path", ToolParameterType::String)
                    .with_description("Relative path to the file.")
                    .required(),
            ),
            Arc::new(move |params| {
                let filesystem = filesystem.clone();
                Box::pin(async move {
                    let path = required_string(&params, "path")?;
                    match filesystem.get_file_size(&path).await {
                        Ok(size) => Ok(ToolResult::success(json!({ "size": size }))),
                        Err(error) => Ok(ToolResult::failure(error.to_string())),
                    }
                })
            }),
        )
        .await;
}

fn required_string(parameters: &Value, key: &str) -> Result<String, Box<dyn Error + Send + Sync>> {
    parameters
        .get(key)
        .and_then(Value::as_str)
        .map(ToOwned::to_owned)
        .ok_or_else(|| format!("'{key}' parameter is required").into())
}

fn detect_platform() -> Platform {
    #[cfg(target_os = "macos")]
    {
        Platform::macOS
    }
    #[cfg(not(target_os = "macos"))]
    {
        Platform::Linux
    }
}

fn directory_entry_to_json(entry: &cloudllm::tools::DirectoryEntry) -> Value {
    json!({
        "name": entry.name,
        "is_directory": entry.is_directory,
        "size": entry.size,
    })
}

fn file_metadata_to_json(metadata: &cloudllm::tools::FileMetadata) -> Value {
    json!({
        "name": metadata.name,
        "path": metadata.path,
        "size": metadata.size,
        "is_directory": metadata.is_directory,
        "modified": metadata.modified,
    })
}