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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Integration tests demonstrating how Agents use the BashTool
//!
//! These tests showcase two integration patterns:
//! 1. **Local Adapter Pattern**: Agent directly uses BashTool via CustomToolProtocol
//! 2. **MCP Adapter Pattern**: Agent uses BashTool through an MCP server (simulated)
//!
//! This demonstrates the flexibility of the tool protocol system, where the same
//! agent code can work with tools whether they're local or accessed via MCP.

use cloudllm::tool_protocol::{
    ToolMetadata, ToolParameter, ToolParameterType, ToolProtocol, ToolResult,
};
use cloudllm::tool_protocols::CustomToolProtocol;
use cloudllm::tools::BashTool;
use cloudllm::tools::Platform;
use std::sync::Arc;

// Helper function to reduce boilerplate in tests
async fn register_bash_tool(adapter: &CustomToolProtocol, bash_tool: Arc<BashTool>) {
    let bash_clone = bash_tool.clone();
    adapter
        .register_async_tool(
            ToolMetadata::new("bash", "Execute bash commands safely").with_parameter(
                ToolParameter::new("command", ToolParameterType::String)
                    .with_description("Shell command to execute")
                    .required(),
            ),
            Arc::new(move |params| {
                let bash = bash_clone.clone();
                Box::pin(async move {
                    let command = params.get("command").and_then(|v| v.as_str()).ok_or_else(
                        || -> Box<dyn std::error::Error + Send + Sync> {
                            "Missing 'command' parameter".into()
                        },
                    )?;

                    let result = bash.execute(command).await.map_err(
                        |e| -> Box<dyn std::error::Error + Send + Sync> {
                            format!("Bash execution failed: {}", e).into()
                        },
                    )?;

                    Ok(ToolResult::success(serde_json::json!({
                        "stdout": result.stdout,
                        "stderr": result.stderr,
                        "exit_code": result.exit_code,
                        "duration_ms": result.duration_ms,
                    })))
                })
            }),
        )
        .await;
}

// ==============================================================================
// PATTERN 1: LOCAL ADAPTER - Agent directly uses BashTool
// ==============================================================================

/// Test showing agent executing a simple command through local adapter
#[tokio::test]
async fn test_agent_with_bash_tool_local_adapter() {
    let bash_tool = Arc::new(
        BashTool::new(Platform::Linux)
            .with_timeout(10)
            .with_denied_commands(vec!["rm".to_string(), "sudo".to_string()]),
    );

    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    let tool_result = adapter
        .execute(
            "bash",
            serde_json::json!({"command": "echo 'Agent says: Hello from local tool'"}),
        )
        .await;

    assert!(
        tool_result.is_ok(),
        "Agent should successfully call bash tool"
    );
    let result = tool_result.unwrap();
    assert!(result.success);
    assert!(result.output["stdout"]
        .as_str()
        .unwrap_or("")
        .contains("Agent says"));
}

/// Test showing agent can discover what tools are available
#[tokio::test]
async fn test_agent_discovers_tools_local_adapter() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    let tools = adapter.list_tools().await;
    assert!(tools.is_ok());

    let tool_list = tools.unwrap();
    assert_eq!(tool_list.len(), 1);
    assert_eq!(tool_list[0].name, "bash");
}

/// Test showing agent inspects tool metadata before using it
#[tokio::test]
async fn test_agent_inspects_tool_metadata_local_adapter() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    let metadata = adapter.get_tool_metadata("bash").await;
    assert!(metadata.is_ok());

    let meta = metadata.unwrap();
    assert_eq!(meta.name, "bash");
    assert_eq!(meta.parameters.len(), 1);
    assert_eq!(meta.parameters[0].name, "command");
}

/// Test showing agent executing multiple commands in sequence
#[tokio::test]
async fn test_agent_sequential_bash_commands_local_adapter() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    // Agent executes first command
    let result1 = adapter
        .execute("bash", serde_json::json!({"command": "echo step1"}))
        .await;
    assert!(result1.is_ok());
    assert!(result1.unwrap().output["stdout"]
        .as_str()
        .unwrap()
        .contains("step1"));

    // Agent uses output from first command and executes second
    let result2 = adapter
        .execute("bash", serde_json::json!({"command": "echo step2"}))
        .await;
    assert!(result2.is_ok());
    assert!(result2.unwrap().output["stdout"]
        .as_str()
        .unwrap()
        .contains("step2"));

    // Agent executes third command
    let result3 = adapter
        .execute("bash", serde_json::json!({"command": "echo step3"}))
        .await;
    assert!(result3.is_ok());
    assert!(result3.unwrap().output["stdout"]
        .as_str()
        .unwrap()
        .contains("step3"));
}

/// Test showing agent respects security constraints (denied commands)
#[tokio::test]
async fn test_agent_bash_security_with_denied_commands() {
    let bash_tool = Arc::new(
        BashTool::new(Platform::Linux)
            .with_timeout(10)
            .with_denied_commands(vec!["rm".to_string(), "sudo".to_string()]),
    );

    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    // Agent tries to execute dangerous command
    let result = adapter
        .execute("bash", serde_json::json!({"command": "rm -rf /"}))
        .await;

    assert!(
        result.is_err(),
        "Dangerous command should be blocked by adapter"
    );

    // But can execute safe commands
    let safe_result = adapter
        .execute("bash", serde_json::json!({"command": "echo safe"}))
        .await;

    assert!(safe_result.is_ok(), "Safe command should succeed");
}

/// Test showing agent can execute allowed commands only (allowlist mode)
#[tokio::test]
async fn test_agent_bash_with_allowed_commands_only() {
    let bash_tool = Arc::new(
        BashTool::new(Platform::Linux)
            .with_allowed_commands(vec!["echo".to_string(), "ls".to_string()]),
    );

    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    // Allowed command should work
    let result = adapter
        .execute("bash", serde_json::json!({"command": "echo allowed"}))
        .await;
    assert!(result.is_ok());

    // Disallowed command should fail
    let blocked = adapter
        .execute("bash", serde_json::json!({"command": "rm test.txt"}))
        .await;
    assert!(
        blocked.is_err(),
        "Non-allowlisted command should be blocked"
    );
}

/// Test showing agent gathering system information
#[tokio::test]
async fn test_agent_gathers_system_info_via_bash() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    // Agent queries for current directory
    let pwd_result = adapter
        .execute("bash", serde_json::json!({"command": "pwd"}))
        .await;

    assert!(pwd_result.is_ok());
    let pwd_output = pwd_result.unwrap();
    let pwd = pwd_output.output["stdout"].as_str().unwrap().trim();
    assert!(!pwd.is_empty(), "Should get current directory");

    // Agent queries for user info
    let user_result = adapter
        .execute("bash", serde_json::json!({"command": "whoami"}))
        .await;

    assert!(user_result.is_ok());
    let user_output = user_result.unwrap();
    let user = user_output.output["stdout"].as_str().unwrap().trim();
    assert!(!user.is_empty(), "Should get current user");
}

/// Test showing agent handles command failures gracefully
#[tokio::test]
async fn test_agent_handles_bash_errors_gracefully() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    // Agent tries a command that will fail
    let result = adapter
        .execute("bash", serde_json::json!({"command": "false"}))
        .await;

    assert!(result.is_ok(), "Tool call should succeed");
    let output = result.unwrap();
    assert!(output.success);
    assert_ne!(
        output.output["exit_code"].as_i64().unwrap(),
        0,
        "Command should have non-zero exit code"
    );

    // Agent can retry with different command
    let retry_result = adapter
        .execute("bash", serde_json::json!({"command": "echo 'retrying'"}))
        .await;

    assert!(retry_result.is_ok());
    assert_eq!(
        retry_result.unwrap().output["exit_code"].as_i64().unwrap(),
        0
    );
}

/// Test showing agent can pipe and chain commands
#[tokio::test]
async fn test_agent_chains_bash_commands() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    // Agent executes complex command with pipes
    let result = adapter
        .execute(
            "bash",
            serde_json::json!({"command": "echo -e '3\\n1\\n2' | sort"}),
        )
        .await;

    assert!(result.is_ok());
    let result_output = result.unwrap();
    let output = result_output.output["stdout"].as_str().unwrap();
    // Check that output contains sorted numbers
    let lines: Vec<&str> = output.trim().lines().collect();
    assert!(lines.len() >= 3, "Should have 3 lines of sorted output");
}

// ==============================================================================
// PATTERN 2: MCP ADAPTER - Simulating Agent access through MCP
// ==============================================================================

/// Test simulating Agent using BashTool through MCP protocol
/// Shows what MCP endpoints would look like
#[tokio::test]
async fn test_agent_with_bash_tool_mcp_adapter_simulation() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux).with_timeout(10));

    // Simulate what would be returned from MCP /tools endpoint
    let tool_metadata = ToolMetadata::new("bash", "Execute bash commands safely").with_parameter(
        ToolParameter::new("command", ToolParameterType::String)
            .with_description("Shell command to execute")
            .required(),
    );

    // Verify metadata is serializable (critical for MCP communication)
    let json = serde_json::to_value(&tool_metadata).unwrap();
    assert_eq!(json["name"], "bash");

    // Execute directly on bash tool (simulating MCP server behavior)
    let result = bash_tool
        .execute("echo 'From MCP'")
        .await
        .expect("Command should execute");

    // Simulate what would be returned from MCP /execute endpoint
    let execute_response = ToolResult::success(serde_json::json!({
        "stdout": result.stdout.trim(),
        "stderr": result.stderr,
        "exit_code": result.exit_code,
        "duration_ms": result.duration_ms,
    }));

    assert!(execute_response.success);
    assert!(execute_response.output["stdout"]
        .as_str()
        .unwrap()
        .contains("From MCP"));
}

/// Test showing agent security constraints work through MCP path
#[tokio::test]
async fn test_agent_bash_security_through_mcp_simulation() {
    let bash_tool = Arc::new(
        BashTool::new(Platform::Linux)
            .with_timeout(30)
            .with_denied_commands(vec![
                "rm".to_string(),
                "sudo".to_string(),
                "pkill".to_string(),
            ]),
    );

    // Simulate agent trying to execute dangerous command through MCP
    let dangerous_cmd = "rm -rf /";
    let result = bash_tool.execute(dangerous_cmd).await;

    assert!(result.is_err(), "Dangerous command should be blocked");

    // Simulate agent executing safe command through MCP
    let safe_cmd = "echo 'Safe command'";
    let result = bash_tool.execute(safe_cmd).await;

    assert!(result.is_ok(), "Safe command should succeed");
    assert!(result.unwrap().stdout.contains("Safe command"));
}

/// Test showing Agent with environment variable configuration through MCP
#[tokio::test]
async fn test_agent_bash_with_env_vars_mcp_simulation() {
    let bash_tool = Arc::new(
        BashTool::new(Platform::Linux)
            .with_env_var("AGENT_NAME".to_string(), "TestAgent".to_string())
            .with_env_var("MODE".to_string(), "production".to_string()),
    );

    // Simulate MCP request
    let result = bash_tool
        .execute("echo $AGENT_NAME in $MODE mode")
        .await
        .expect("Command should execute");

    assert!(result.stdout.contains("TestAgent"));
    assert!(result.stdout.contains("production"));
}

// ==============================================================================
// PATTERN 3: AGENT PLANNING & DISCOVERY
// ==============================================================================

/// Test showing agent planning phase - discovering and inspecting tools
#[tokio::test]
async fn test_agent_planning_phase_tool_discovery() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    // Agent planning phase 1: discover what tools exist
    let available_tools = adapter.list_tools().await.unwrap();
    assert_eq!(available_tools.len(), 1);

    // Agent planning phase 2: inspect each tool's metadata
    for tool in available_tools {
        let metadata = adapter.get_tool_metadata(&tool.name).await.unwrap();
        assert_eq!(metadata.name, "bash");

        // Verify parameters are documented
        for param in &metadata.parameters {
            assert!(!param.name.is_empty());
            assert!(param.required || param.description.is_some());
        }
    }
}

/// Test showing agent adaptive behavior based on tool capabilities
#[tokio::test]
async fn test_agent_adapts_behavior_based_on_tool_metadata() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();

    // Register tool
    register_bash_tool(&adapter, bash_tool).await;

    // Agent reads tool metadata to understand capabilities
    let metadata = adapter.get_tool_metadata("bash").await.unwrap();

    // Agent verifies timeout configuration is known
    assert!(!metadata.description.is_empty());
    assert_eq!(metadata.parameters.len(), 1);

    // Agent can now decide how to use the tool
    let has_command_param = metadata.parameters.iter().any(|p| p.name == "command");
    assert!(has_command_param, "Should have command parameter");
}

/// Test showing agent retrying logic after tool execution
#[tokio::test]
async fn test_agent_retry_logic_with_bash_tool() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    let mut attempt = 0;
    let max_attempts = 3;

    // Simulate agent retry loop
    while attempt < max_attempts {
        let result = adapter
            .execute("bash", serde_json::json!({"command": "echo attempt"}))
            .await;

        if let Ok(tool_result) = result {
            assert!(tool_result.success);
            break;
        }

        attempt += 1;
    }

    assert!(attempt < max_attempts, "Should succeed before max attempts");
}

/// Test showing multi-step agent workflow with bash tool
#[tokio::test]
async fn test_agent_multistep_workflow() {
    let bash_tool = Arc::new(BashTool::new(Platform::Linux));
    let adapter = CustomToolProtocol::new();
    register_bash_tool(&adapter, bash_tool).await;

    // Step 1: Check current state
    let state_check = adapter
        .execute(
            "bash",
            serde_json::json!({"command": "echo 'workflow started'"}),
        )
        .await;
    assert!(state_check.is_ok());

    // Step 2: Process data
    let process = adapter
        .execute(
            "bash",
            serde_json::json!({"command": "echo 'processing data'"}),
        )
        .await;
    assert!(process.is_ok());

    // Step 3: Verify results
    let verify = adapter
        .execute(
            "bash",
            serde_json::json!({"command": "echo 'verification complete'"}),
        )
        .await;
    assert!(verify.is_ok());

    let verify_result = verify.unwrap();
    let final_output = verify_result.output["stdout"].as_str().unwrap();
    assert!(final_output.contains("verification complete"));
}