rust-bash 0.3.0

A sandboxed bash interpreter for AI Agents with a virtual filesystem
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! MCP (Model Context Protocol) server over stdio.
//!
//! Implements the minimal MCP subset: `initialize`, `tools/list`, `tools/call`,
//! and `notifications/initialized`. Communicates via newline-delimited JSON-RPC
//! over stdin/stdout.

use crate::{RustBash, RustBashBuilder};
use serde_json::{Value, json};
use std::collections::HashMap;
use std::io::{self, BufRead, Write};

const MAX_OUTPUT_LEN: usize = 100_000;

/// Run the MCP server loop, reading JSON-RPC messages from stdin and writing
/// responses to stdout. Each line is one JSON-RPC message.
pub fn run_mcp_server() -> Result<(), Box<dyn std::error::Error>> {
    let builder = RustBashBuilder::new()
        .env(HashMap::from([
            ("HOME".to_string(), "/home".to_string()),
            ("USER".to_string(), "user".to_string()),
            ("PWD".to_string(), "/".to_string()),
        ]))
        .cwd("/");
    let mut shell = builder.build()?;

    let stdin = io::stdin();
    let stdout = io::stdout();
    let mut stdout = stdout.lock();

    for line in stdin.lock().lines() {
        let line = line?;
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        let request: Value = match serde_json::from_str(trimmed) {
            Ok(v) => v,
            Err(e) => {
                let error_response = json!({
                    "jsonrpc": "2.0",
                    "id": null,
                    "error": {
                        "code": -32700,
                        "message": format!("Parse error: {e}")
                    }
                });
                write_response(&mut stdout, &error_response)?;
                continue;
            }
        };

        if let Some(response) = handle_message(&mut shell, &request) {
            write_response(&mut stdout, &response)?;
        }
        // Notifications (no "id") that we don't respond to just get dropped
    }

    Ok(())
}

fn write_response(stdout: &mut impl Write, response: &Value) -> io::Result<()> {
    let serialized = serde_json::to_string(response).expect("JSON serialization should not fail");
    writeln!(stdout, "{serialized}")?;
    stdout.flush()
}

fn handle_message(shell: &mut RustBash, request: &Value) -> Option<Value> {
    let id = request.get("id");

    // Notifications have no "id" — we don't respond to them
    if id.is_none() || id == Some(&Value::Null) {
        return None;
    }

    let id = id.unwrap().clone();

    let method = match request.get("method").and_then(|v| v.as_str()) {
        Some(m) => m,
        None => {
            return Some(json!({
                "jsonrpc": "2.0",
                "id": id,
                "error": {
                    "code": -32600,
                    "message": "Invalid Request: missing or non-string method"
                }
            }));
        }
    };

    let result = match method {
        "initialize" => handle_initialize(),
        "tools/list" => handle_tools_list(),
        "tools/call" => handle_tools_call(shell, request.get("params")),
        _ => {
            return Some(json!({
                "jsonrpc": "2.0",
                "id": id,
                "error": {
                    "code": -32601,
                    "message": format!("Method not found: {method}")
                }
            }));
        }
    };

    match result {
        Ok(value) => Some(json!({
            "jsonrpc": "2.0",
            "id": id,
            "result": value
        })),
        Err(e) => Some(json!({
            "jsonrpc": "2.0",
            "id": id,
            "error": {
                "code": -32603,
                "message": e
            }
        })),
    }
}

fn handle_initialize() -> Result<Value, String> {
    Ok(json!({
        "protocolVersion": "2024-11-05",
        "capabilities": {
            "tools": {}
        },
        "serverInfo": {
            "name": "rust-bash",
            "version": env!("CARGO_PKG_VERSION")
        }
    }))
}

fn handle_tools_list() -> Result<Value, String> {
    Ok(json!({
        "tools": [
            {
                "name": "bash",
                "description": "Execute bash commands in a sandboxed environment with an in-memory virtual filesystem. Supports standard Unix utilities including grep, sed, awk, jq, cat, echo, and more. All file operations are isolated within the sandbox. State persists between calls.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "command": {
                            "type": "string",
                            "description": "The bash command to execute"
                        }
                    },
                    "required": ["command"]
                }
            },
            {
                "name": "write_file",
                "description": "Write content to a file in the sandboxed virtual filesystem. Creates parent directories automatically.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "path": {
                            "type": "string",
                            "description": "The absolute path to write to"
                        },
                        "content": {
                            "type": "string",
                            "description": "The content to write"
                        }
                    },
                    "required": ["path", "content"]
                }
            },
            {
                "name": "read_file",
                "description": "Read the contents of a file from the sandboxed virtual filesystem.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "path": {
                            "type": "string",
                            "description": "The absolute path to read"
                        }
                    },
                    "required": ["path"]
                }
            },
            {
                "name": "list_directory",
                "description": "List the contents of a directory in the sandboxed virtual filesystem.",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "path": {
                            "type": "string",
                            "description": "The absolute path of the directory to list"
                        }
                    },
                    "required": ["path"]
                }
            }
        ]
    }))
}

fn truncate_output(s: &str) -> String {
    if s.len() <= MAX_OUTPUT_LEN {
        return s.to_string();
    }
    // Find a valid UTF-8 char boundary at or before MAX_OUTPUT_LEN
    let mut end = MAX_OUTPUT_LEN;
    while end > 0 && !s.is_char_boundary(end) {
        end -= 1;
    }
    format!("{}\n... (truncated, {} total chars)", &s[..end], s.len())
}

fn handle_tools_call(shell: &mut RustBash, params: Option<&Value>) -> Result<Value, String> {
    let params = params.ok_or("Missing params")?;
    let tool_name = params
        .get("name")
        .and_then(|v| v.as_str())
        .ok_or("Missing tool name")?;
    let empty_obj = Value::Object(Default::default());
    let arguments = params.get("arguments").unwrap_or(&empty_obj);

    match tool_name {
        "bash" => {
            let command = arguments
                .get("command")
                .and_then(|v| v.as_str())
                .ok_or("Missing 'command' argument")?;

            match shell.exec(command) {
                Ok(result) => {
                    let stdout = truncate_output(&result.stdout);
                    let stderr = truncate_output(&result.stderr);
                    let text = format!(
                        "stdout:\n{stdout}\nstderr:\n{stderr}\nexit_code: {}",
                        result.exit_code
                    );
                    let is_error = result.exit_code != 0;
                    Ok(json!({
                        "content": [{ "type": "text", "text": text }],
                        "isError": is_error
                    }))
                }
                Err(e) => Ok(json!({
                    "content": [{ "type": "text", "text": format!("Error: {e}") }],
                    "isError": true
                })),
            }
        }
        "write_file" => {
            let path = arguments
                .get("path")
                .and_then(|v| v.as_str())
                .ok_or("Missing 'path' argument")?;
            let content = arguments
                .get("content")
                .and_then(|v| v.as_str())
                .ok_or("Missing 'content' argument")?;

            match shell.write_file(path, content.as_bytes()) {
                Ok(()) => Ok(json!({
                    "content": [{ "type": "text", "text": format!("Written {path}") }]
                })),
                Err(e) => Ok(json!({
                    "content": [{ "type": "text", "text": format!("Error: {e}") }],
                    "isError": true
                })),
            }
        }
        "read_file" => {
            let path = arguments
                .get("path")
                .and_then(|v| v.as_str())
                .ok_or("Missing 'path' argument")?;

            match shell.read_file(path) {
                Ok(bytes) => {
                    let text = String::from_utf8_lossy(&bytes).into_owned();
                    Ok(json!({
                        "content": [{ "type": "text", "text": text }]
                    }))
                }
                Err(e) => Ok(json!({
                    "content": [{ "type": "text", "text": format!("Error: {e}") }],
                    "isError": true
                })),
            }
        }
        "list_directory" => {
            let path = arguments
                .get("path")
                .and_then(|v| v.as_str())
                .ok_or("Missing 'path' argument")?;

            match shell.readdir(path) {
                Ok(entries) => {
                    let listing: Vec<String> = entries
                        .iter()
                        .map(|e| {
                            let suffix = match e.node_type {
                                crate::vfs::NodeType::Directory => "/",
                                _ => "",
                            };
                            format!("{}{suffix}", e.name)
                        })
                        .collect();
                    let text = listing.join("\n");
                    Ok(json!({
                        "content": [{ "type": "text", "text": text }]
                    }))
                }
                Err(e) => Ok(json!({
                    "content": [{ "type": "text", "text": format!("Error: {e}") }],
                    "isError": true
                })),
            }
        }
        _ => Err(format!("Unknown tool: {tool_name}")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_initialize_response() {
        let result = handle_initialize().unwrap();
        assert_eq!(result["protocolVersion"], "2024-11-05");
        assert!(result["serverInfo"]["name"].as_str().unwrap() == "rust-bash");
        assert!(result["capabilities"]["tools"].is_object());
    }

    #[test]
    fn test_tools_list_returns_four_tools() {
        let result = handle_tools_list().unwrap();
        let tools = result["tools"].as_array().unwrap();
        assert_eq!(tools.len(), 4);

        let names: Vec<&str> = tools.iter().map(|t| t["name"].as_str().unwrap()).collect();
        assert!(names.contains(&"bash"));
        assert!(names.contains(&"write_file"));
        assert!(names.contains(&"read_file"));
        assert!(names.contains(&"list_directory"));
    }

    #[test]
    fn test_tools_list_schemas_have_required_fields() {
        let result = handle_tools_list().unwrap();
        let tools = result["tools"].as_array().unwrap();
        for tool in tools {
            assert!(tool["name"].is_string());
            assert!(tool["description"].is_string());
            assert!(tool["inputSchema"]["type"].as_str().unwrap() == "object");
            assert!(tool["inputSchema"]["properties"].is_object());
            assert!(tool["inputSchema"]["required"].is_array());
        }
    }

    fn create_test_shell() -> RustBash {
        RustBashBuilder::new()
            .cwd("/")
            .env(HashMap::from([
                ("HOME".to_string(), "/home".to_string()),
                ("USER".to_string(), "user".to_string()),
            ]))
            .build()
            .unwrap()
    }

    #[test]
    fn test_bash_tool_call() {
        let mut shell = create_test_shell();
        let params = json!({
            "name": "bash",
            "arguments": { "command": "echo hello" }
        });
        let result = handle_tools_call(&mut shell, Some(&params)).unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("hello"));
        assert!(text.contains("exit_code: 0"));
    }

    #[test]
    fn test_write_and_read_file_tool() {
        let mut shell = create_test_shell();

        // Write a file
        let write_params = json!({
            "name": "write_file",
            "arguments": { "path": "/test.txt", "content": "test content" }
        });
        let result = handle_tools_call(&mut shell, Some(&write_params)).unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("Written"));

        // Read it back
        let read_params = json!({
            "name": "read_file",
            "arguments": { "path": "/test.txt" }
        });
        let result = handle_tools_call(&mut shell, Some(&read_params)).unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        assert_eq!(text, "test content");
    }

    #[test]
    fn test_list_directory_tool() {
        let mut shell = create_test_shell();

        // Create a file first
        shell.write_file("/mydir/a.txt", b"a").unwrap();
        shell.write_file("/mydir/b.txt", b"b").unwrap();

        let params = json!({
            "name": "list_directory",
            "arguments": { "path": "/mydir" }
        });
        let result = handle_tools_call(&mut shell, Some(&params)).unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("a.txt"));
        assert!(text.contains("b.txt"));
    }

    #[test]
    fn test_read_nonexistent_file_returns_error() {
        let mut shell = create_test_shell();
        let params = json!({
            "name": "read_file",
            "arguments": { "path": "/nonexistent.txt" }
        });
        let result = handle_tools_call(&mut shell, Some(&params)).unwrap();
        assert_eq!(result["isError"], true);
    }

    #[test]
    fn test_unknown_tool_returns_error() {
        let mut shell = create_test_shell();
        let params = json!({
            "name": "unknown_tool",
            "arguments": {}
        });
        let result = handle_tools_call(&mut shell, Some(&params));
        assert!(result.is_err());
    }

    #[test]
    fn test_handle_message_initialize() {
        let mut shell = create_test_shell();
        let request = json!({
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {}
        });
        let response = handle_message(&mut shell, &request).unwrap();
        assert_eq!(response["id"], 1);
        assert!(response["result"]["serverInfo"].is_object());
    }

    #[test]
    fn test_handle_message_notification_returns_none() {
        let mut shell = create_test_shell();
        let request = json!({
            "jsonrpc": "2.0",
            "method": "notifications/initialized"
        });
        let response = handle_message(&mut shell, &request);
        assert!(response.is_none());
    }

    #[test]
    fn test_handle_message_unknown_method() {
        let mut shell = create_test_shell();
        let request = json!({
            "jsonrpc": "2.0",
            "id": 5,
            "method": "unknown/method",
            "params": {}
        });
        let response = handle_message(&mut shell, &request).unwrap();
        assert!(response["error"]["code"].as_i64().unwrap() == -32601);
    }

    #[test]
    fn test_bash_error_command_returns_is_error() {
        let mut shell = create_test_shell();
        let params = json!({
            "name": "bash",
            "arguments": { "command": "cat /nonexistent_file_404" }
        });
        let result = handle_tools_call(&mut shell, Some(&params)).unwrap();
        assert_eq!(result["isError"], true);
    }

    #[test]
    fn test_stateful_session() {
        let mut shell = create_test_shell();

        // Set a variable
        let params1 = json!({
            "name": "bash",
            "arguments": { "command": "export MY_VAR=hello123" }
        });
        handle_tools_call(&mut shell, Some(&params1)).unwrap();

        // Read it back
        let params2 = json!({
            "name": "bash",
            "arguments": { "command": "echo $MY_VAR" }
        });
        let result = handle_tools_call(&mut shell, Some(&params2)).unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("hello123"));
    }

    #[test]
    fn test_handle_message_missing_method_with_id() {
        let mut shell = create_test_shell();
        let request = json!({
            "jsonrpc": "2.0",
            "id": 7
        });
        let response = handle_message(&mut shell, &request).unwrap();
        assert_eq!(response["error"]["code"], -32600);
    }

    #[test]
    fn test_handle_message_non_string_method_with_id() {
        let mut shell = create_test_shell();
        let request = json!({
            "jsonrpc": "2.0",
            "id": 8,
            "method": 42
        });
        let response = handle_message(&mut shell, &request).unwrap();
        assert_eq!(response["error"]["code"], -32600);
    }

    #[test]
    fn test_truncate_output_short() {
        let s = "hello world";
        assert_eq!(truncate_output(s), s);
    }

    #[test]
    fn test_truncate_output_long() {
        let s = "x".repeat(MAX_OUTPUT_LEN + 100);
        let result = truncate_output(&s);
        assert!(result.len() < s.len());
        assert!(result.contains("truncated"));
    }
}