szal 1.2.0

Workflow engine — step/flow execution with branching, retry, rollback, and parallel stages
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
//! File system tools — read, write, list, stat, search.

use crate::mcp::{
    McpErrorCode, Tool, result_error_typed, result_ok, result_ok_json, tool_def, validate_path,
};
use bote::ToolDef as BoteToolDef;
use serde_json::json;
use std::path::Path;
use std::pin::Pin;
use tokio::io::AsyncWriteExt;

/// Maximum bytes to read from a file by default (1 MB).
const DEFAULT_MAX_READ_BYTES: usize = 1_048_576;
/// Maximum entries returned by directory listing.
const MAX_DIR_ENTRIES: u64 = 10_000;
/// Default entries returned by directory listing.
const DEFAULT_DIR_ENTRIES: u64 = 500;
/// Maximum recursion depth for directory traversal.
const MAX_DIR_DEPTH: usize = 20;

/// Read a file's contents.
pub struct FileRead;

impl Tool for FileRead {
    fn definition(&self) -> BoteToolDef {
        tool_def(
            "szal_file_read",
            "Read a file's contents as text",
            json!({
                "path": { "type": "string", "description": "File path to read" },
                "max_bytes": { "type": "integer", "description": "Max bytes to read (default: 1MB)" }
            }),
            vec!["path".into()],
        )
    }

    fn call(
        &self,
        args: serde_json::Value,
    ) -> Pin<Box<dyn std::future::Future<Output = serde_json::Value> + Send + '_>> {
        Box::pin(async move {
            let path = match args.get("path").and_then(|v| v.as_str()) {
                Some(p) => p,
                None => {
                    return result_error_typed(
                        McpErrorCode::Validation,
                        "missing required field: path",
                    );
                }
            };
            let path = match validate_path(path).await {
                Ok(p) => p.display().to_string(),
                Err(e) => return result_error_typed(McpErrorCode::PermissionDenied, e),
            };
            let path = path.as_str();
            let max_bytes = args
                .get("max_bytes")
                .and_then(|v| v.as_u64())
                .unwrap_or(DEFAULT_MAX_READ_BYTES as u64) as usize;

            match tokio::fs::read_to_string(path).await {
                Ok(content) => {
                    if content.len() > max_bytes {
                        let end = content[..max_bytes]
                            .char_indices()
                            .last()
                            .map(|(i, c)| i + c.len_utf8())
                            .unwrap_or(0);
                        result_ok(&content[..end])
                    } else {
                        result_ok(&content)
                    }
                }
                Err(e) => {
                    result_error_typed(McpErrorCode::IoError, format!("failed to read {path}: {e}"))
                }
            }
        })
    }
}

/// Write content to a file.
pub struct FileWrite;

impl Tool for FileWrite {
    fn definition(&self) -> BoteToolDef {
        tool_def(
            "szal_file_write",
            "Write text content to a file (creates or overwrites)",
            json!({
                "path": { "type": "string", "description": "File path to write" },
                "content": { "type": "string", "description": "Content to write" },
                "append": { "type": "boolean", "description": "Append instead of overwrite (default: false)" }
            }),
            vec!["path".into(), "content".into()],
        )
    }

    fn call(
        &self,
        args: serde_json::Value,
    ) -> Pin<Box<dyn std::future::Future<Output = serde_json::Value> + Send + '_>> {
        Box::pin(async move {
            let path = match args.get("path").and_then(|v| v.as_str()) {
                Some(p) => p,
                None => {
                    return result_error_typed(
                        McpErrorCode::Validation,
                        "missing required field: path",
                    );
                }
            };
            let path = match validate_path(path).await {
                Ok(p) => p.display().to_string(),
                Err(e) => return result_error_typed(McpErrorCode::PermissionDenied, e),
            };
            let path = path.as_str();
            let content = match args.get("content").and_then(|v| v.as_str()) {
                Some(c) => c,
                None => {
                    return result_error_typed(
                        McpErrorCode::Validation,
                        "missing required field: content",
                    );
                }
            };
            let append = args
                .get("append")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);

            let result = if append {
                let file = tokio::fs::OpenOptions::new()
                    .create(true)
                    .append(true)
                    .open(path)
                    .await;
                match file {
                    Ok(mut f) => f.write_all(content.as_bytes()).await,
                    Err(e) => Err(e),
                }
            } else {
                tokio::fs::write(path, content).await
            };

            match result {
                Ok(()) => result_ok(&format!("wrote {} bytes to {path}", content.len())),
                Err(e) => result_error_typed(
                    McpErrorCode::IoError,
                    format!("failed to write {path}: {e}"),
                ),
            }
        })
    }
}

/// List directory contents.
pub struct DirList;

impl Tool for DirList {
    fn definition(&self) -> BoteToolDef {
        tool_def(
            "szal_dir_list",
            "List files and directories in a path",
            json!({
                "path": { "type": "string", "description": "Directory path (default: current dir)" },
                "recursive": { "type": "boolean", "description": "Recurse into subdirectories (default: false)" },
                "max_entries": { "type": "integer", "description": "Max entries to return (default: 500)" }
            }),
            vec![],
        )
    }

    fn call(
        &self,
        args: serde_json::Value,
    ) -> Pin<Box<dyn std::future::Future<Output = serde_json::Value> + Send + '_>> {
        Box::pin(async move {
            let path = args.get("path").and_then(|v| v.as_str()).unwrap_or(".");
            let path = match validate_path(path).await {
                Ok(p) => p.display().to_string(),
                Err(e) => return result_error_typed(McpErrorCode::PermissionDenied, e),
            };
            let path = path.as_str();
            let recursive = args
                .get("recursive")
                .and_then(|v| v.as_bool())
                .unwrap_or(false);
            let max = args
                .get("max_entries")
                .and_then(|v| v.as_u64())
                .unwrap_or(DEFAULT_DIR_ENTRIES)
                .min(MAX_DIR_ENTRIES) as usize;

            let mut entries = Vec::new();
            if let Err(e) = collect_dir(Path::new(path), recursive, max, &mut entries, 0).await {
                return result_error_typed(
                    McpErrorCode::IoError,
                    format!("failed to list {path}: {e}"),
                );
            }

            result_ok_json(&json!(entries))
        })
    }
}

fn collect_dir<'a>(
    path: &'a Path,
    recursive: bool,
    max: usize,
    entries: &'a mut Vec<serde_json::Value>,
    depth: usize,
) -> Pin<Box<dyn std::future::Future<Output = std::io::Result<()>> + Send + 'a>> {
    Box::pin(async move {
        if depth > MAX_DIR_DEPTH {
            return Ok(());
        }
        let mut read_dir = tokio::fs::read_dir(path).await?;
        while let Some(entry) = read_dir.next_entry().await? {
            if entries.len() >= max {
                break;
            }
            let ft = entry.file_type().await?;
            let name = entry.file_name().to_string_lossy().to_string();
            let kind = if ft.is_dir() {
                "directory"
            } else if ft.is_symlink() {
                "symlink"
            } else {
                "file"
            };

            let mut info = json!({
                "name": name,
                "path": entry.path().display().to_string(),
                "type": kind,
            });

            if ft.is_file()
                && let Ok(meta) = entry.metadata().await
            {
                info["size"] = json!(meta.len());
            }

            entries.push(info);

            if recursive && ft.is_dir() && entries.len() < max {
                let entry_path = entry.path();
                if let Err(e) = collect_dir(&entry_path, true, max, entries, depth + 1).await {
                    tracing::debug!(path = %entry_path.display(), error = %e, "skipping unreadable subdirectory");
                }
            }
        }
        Ok(())
    })
}

/// Get file or directory metadata.
pub struct FileStat;

impl Tool for FileStat {
    fn definition(&self) -> BoteToolDef {
        tool_def(
            "szal_file_stat",
            "Get metadata for a file or directory (size, permissions, timestamps)",
            json!({ "path": { "type": "string", "description": "File or directory path" } }),
            vec!["path".into()],
        )
    }

    fn call(
        &self,
        args: serde_json::Value,
    ) -> Pin<Box<dyn std::future::Future<Output = serde_json::Value> + Send + '_>> {
        Box::pin(async move {
            let path = match args.get("path").and_then(|v| v.as_str()) {
                Some(p) => p,
                None => {
                    return result_error_typed(
                        McpErrorCode::Validation,
                        "missing required field: path",
                    );
                }
            };
            let path = match validate_path(path).await {
                Ok(p) => p.display().to_string(),
                Err(e) => return result_error_typed(McpErrorCode::PermissionDenied, e),
            };
            let path = path.as_str();

            match tokio::fs::symlink_metadata(path).await {
                Ok(meta) => {
                    let kind = if meta.is_dir() {
                        "directory"
                    } else if meta.is_symlink() {
                        "symlink"
                    } else {
                        "file"
                    };

                    let modified = meta.modified().ok().map(|t| {
                        let dt: chrono::DateTime<chrono::Utc> = t.into();
                        dt.to_rfc3339()
                    });

                    result_ok_json(&json!({
                        "path": path,
                        "type": kind,
                        "size": meta.len(),
                        "readonly": meta.permissions().readonly(),
                        "modified": modified,
                    }))
                }
                Err(e) => {
                    result_error_typed(McpErrorCode::IoError, format!("failed to stat {path}: {e}"))
                }
            }
        })
    }
}

/// Check if a path exists.
pub struct PathExists;

impl Tool for PathExists {
    fn definition(&self) -> BoteToolDef {
        tool_def(
            "szal_path_exists",
            "Check if a file or directory exists",
            json!({ "path": { "type": "string" } }),
            vec!["path".into()],
        )
    }

    fn call(
        &self,
        args: serde_json::Value,
    ) -> Pin<Box<dyn std::future::Future<Output = serde_json::Value> + Send + '_>> {
        Box::pin(async move {
            let path = match args.get("path").and_then(|v| v.as_str()) {
                Some(p) => p,
                None => {
                    return result_error_typed(
                        McpErrorCode::Validation,
                        "missing required field: path",
                    );
                }
            };
            let validated = match validate_path(path).await {
                Ok(p) => p,
                Err(e) => return result_error_typed(McpErrorCode::PermissionDenied, e),
            };
            let (exists, is_file, is_dir) = match tokio::fs::metadata(&validated).await {
                Ok(meta) => (true, meta.is_file(), meta.is_dir()),
                Err(_) => (false, false, false),
            };
            result_ok_json(&json!({
                "path": validated.display().to_string(),
                "exists": exists,
                "is_file": is_file,
                "is_dir": is_dir,
            }))
        })
    }
}

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

    /// Create a TempDir under the current working directory so paths pass validation.
    fn cwd_tempdir() -> TempDir {
        let cwd = std::env::current_dir().unwrap();
        TempDir::new_in(cwd).unwrap()
    }

    #[tokio::test]
    async fn file_read_write() {
        let tmp = cwd_tempdir();
        let path = tmp.path().join("test.txt");
        let path_str = path.display().to_string();

        let result = FileWrite
            .call(json!({"path": path_str, "content": "hello"}))
            .await;
        assert_eq!(result["isError"], false);

        let result = FileRead.call(json!({"path": path_str})).await;
        assert_eq!(result["isError"], false);
        assert_eq!(result["content"][0]["text"].as_str().unwrap(), "hello");
    }

    #[tokio::test]
    async fn file_write_append() {
        let tmp = cwd_tempdir();
        let path = tmp.path().join("append.txt");
        let path_str = path.display().to_string();

        FileWrite
            .call(json!({"path": path_str, "content": "a"}))
            .await;
        FileWrite
            .call(json!({"path": path_str, "content": "b", "append": true}))
            .await;

        let result = FileRead.call(json!({"path": path_str})).await;
        assert_eq!(result["content"][0]["text"].as_str().unwrap(), "ab");
    }

    #[tokio::test]
    async fn file_read_missing() {
        // Path outside cwd should be rejected
        let result = FileRead
            .call(json!({"path": "/nonexistent/file/xyz"}))
            .await;
        assert_eq!(result["isError"], true);
    }

    #[tokio::test]
    async fn file_read_rejects_path_traversal() {
        let result = FileRead.call(json!({"path": "/etc/passwd"})).await;
        assert_eq!(result["isError"], true);
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("outside working directory"));
    }

    #[tokio::test]
    async fn dir_list() {
        let tmp = cwd_tempdir();
        std::fs::write(tmp.path().join("a.txt"), "").unwrap();
        std::fs::write(tmp.path().join("b.txt"), "").unwrap();

        let result = DirList
            .call(json!({"path": tmp.path().display().to_string()}))
            .await;
        assert_eq!(result["isError"], false);
        let text = result["content"][0]["text"].as_str().unwrap();
        let entries: Vec<serde_json::Value> = serde_json::from_str(text).unwrap();
        assert_eq!(entries.len(), 2);
    }

    #[tokio::test]
    async fn file_stat() {
        let tmp = cwd_tempdir();
        let path = tmp.path().join("stat.txt");
        std::fs::write(&path, "12345").unwrap();

        let result = FileStat
            .call(json!({"path": path.display().to_string()}))
            .await;
        assert_eq!(result["isError"], false);
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("\"size\": 5"));
        assert!(text.contains("\"type\": \"file\""));
    }

    #[tokio::test]
    async fn path_exists_cwd() {
        // "." is always under cwd
        let result = PathExists.call(json!({"path": "."})).await;
        assert_eq!(result["isError"], false);
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("\"exists\": true"));
        assert!(text.contains("\"is_dir\": true"));
    }

    #[tokio::test]
    async fn path_exists_rejects_outside_cwd() {
        let result = PathExists.call(json!({"path": "/tmp"})).await;
        assert_eq!(result["isError"], true);
        let text = result["content"][0]["text"].as_str().unwrap();
        assert!(text.contains("outside working directory"));
    }
}