rust-fs-mcp 0.2.3

Rust stdio MCP server compatible with fs-mcp public tool contracts.
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
//! mod.rs
//! tools::mod
//!
//! Registers the fs / git / inspect / search tool submodules and exposes the single dispatch_tool_call entry point.
//! Routes tool names to handlers and applies args_path resolution plus normalize_tool_result consistently.
//!

pub mod fs_tools;

pub mod git_tools;
pub mod inspect_tools;

pub mod search_tools;
pub mod web_tools;

use crate::core::args_ref::resolve_tool_args;
use crate::core::response::{RawResult, normalize_tool_result};
use crate::tools::fs_tools::{
    handle_dir_create, handle_dir_list, handle_file_edit, handle_file_edit_lines, handle_file_read,
    handle_file_read_line_range, handle_file_write, handle_path_copy, handle_path_move,
    handle_path_remove, handle_path_stat,
};
use crate::tools::git_tools::{
    handle_git_add, handle_git_amend, handle_git_commit, handle_git_diff, handle_git_show,
    handle_git_status,
};
use crate::tools::inspect_tools::handle_fs_inspect;
use crate::tools::search_tools::handle_fs_search;
use crate::tools::web_tools::{
    handle_download_to_file, handle_web_extract, handle_web_fetch, handle_web_render,
};
use serde_json::{Map, Value, json};
use std::time::Instant;

// 1. Tool dispatch ------------------------------------------------------------
pub fn dispatch_tool_call(tool_name: &str, args: Option<Value>) -> Value {
    let started = Instant::now();
    let result = match resolve_tool_args(args) {
        Ok(args) => {
            let args = absorb_arg_shape(tool_name, args);
            dispatch_resolved(tool_name, &args)
        }
        Err(error) => RawResult::error(error),
    };

    normalize_tool_result(tool_name, result, started.elapsed())
}
fn dispatch_resolved(tool_name: &str, args: &Value) -> RawResult {
    match tool_name {
        "file-read" => handle_file_read(args),
        "file-read-line-range" => handle_file_read_line_range(args),
        "file-write" => handle_file_write(args),
        "dir-create" => handle_dir_create(args),
        "dir-list" => handle_dir_list(args),
        "path-copy" => handle_path_copy(args),
        "path-move" => handle_path_move(args),
        "path-remove" => handle_path_remove(args),
        "fs-search" => handle_fs_search(args),
        "path-stat" => handle_path_stat(args),
        "file-edit" => handle_file_edit(args),
        "file-edit-lines" => handle_file_edit_lines(args),
        "git-add" => handle_git_add(args),
        "git-amend" => handle_git_amend(args),
        "git-commit" => handle_git_commit(args),
        "git-diff" => handle_git_diff(args),
        "git-show" => handle_git_show(args),
        "git-status" => handle_git_status(args),
        "fs-inspect" => handle_fs_inspect(args),
        "web-fetch" => handle_web_fetch(args),
        "web-render" => handle_web_render(args),
        "web-extract" => handle_web_extract(args),
        "download-to-file" => handle_download_to_file(args),
        _ => RawResult::error(format!("Unknown tool: {tool_name}")),
    }
}
// 2. Arg shape absorption ------------------------------------------------------
// 히스토리 로그 상 최다 서버 오류가 items[]/paths[] 래핑 누락과 아이템 키 이름 혼동이므로
// 디스패치 직전에 흡수한다: flat 단일 호출은 items/paths로 감싸고 별칭 키를 정규화.
fn absorb_arg_shape(tool_name: &str, args: Value) -> Value {
    let Value::Object(mut map) = args else {
        return args;
    };
    // 배열/객체 인수를 JSON 문자열로 마샬해 보내는 호출이 있어(paths:"[...]" 등) 원래 Value로 복원.
    coerce_json_string(&mut map, &["items", "paths"]);
    // items가 단일 객체로 오면 배열로 승격.
    if matches!(map.get("items"), Some(Value::Object(_))) && let Some(single) = map.remove("items") {
      map.insert("items".to_string(), Value::Array(vec![single]));
    }
    match tool_name {
        "file-read" => {
            alias_item_keys(&mut map, &[("file_path", "path")]);
            promote_single_read_item(&mut map, &["isUrl", "offset", "length"]);
        }
        "file-read-line-range" => {
            alias_item_keys(&mut map, &[("file_path", "path")]);
            promote_single_read_item(&mut map, &["start_line", "line_count"]);
        }
        "path-stat" | "dir-create" => normalize_paths_only(&mut map),
        "file-write" => wrap_flat_item(
            &mut map,
            &[("file_path", "path")],
            &["path"],
            &[
                "path",
                "content",
                "content_path",
                "content_offset",
                "content_length",
                "mode",
            ],
        ),
        "dir-list" => wrap_flat_item(
            &mut map,
            &[("file_path", "path")],
            &["path"],
            &[
                "path",
                "depth",
                "maxEntries",
                "excludePatterns",
                "includeFiles",
                "noDefaultExcludes",
            ],
        ),
        "path-remove" => {
            promote_paths_to_remove_items(&mut map);
            wrap_flat_item(&mut map, &[], &["path"], &["path", "recursive", "force"]);
        }
        "path-copy" => wrap_flat_item(
            &mut map,
            &[("from", "source"), ("to", "destination")],
            &["source", "destination"],
            &["source", "destination", "recursive", "force"],
        ),
        "path-move" => wrap_flat_item(
            &mut map,
            &[("from", "source"), ("to", "destination")],
            &["source", "destination"],
            &["source", "destination"],
        ),
        "fs-search" => wrap_flat_item(
            &mut map,
            &[("file_path", "path")],
            &["path", "pattern"],
            &[
                "path",
                "pattern",
                "pattern_path",
                "pattern_offset",
                "pattern_length",
                "filePattern",
                "ignoreCase",
                "maxResults",
                "includeHidden",
                "noDefaultExcludes",
                "contextLines",
                "timeout_ms",
            ],
        ),
        "file-edit" => wrap_flat_item(
            &mut map,
            &[("path", "file_path")],
            &["file_path", "old_string"],
            &[
                "file_path",
                "old_string",
                "old_string_path",
                "old_string_offset",
                "old_string_length",
                "new_string",
                "new_string_path",
                "new_string_offset",
                "new_string_length",
                "expected_replacements",
            ],
        ),
        "file-edit-lines" => wrap_flat_item(
            &mut map,
            &[("path", "file_path")],
            &["file_path", "start_line"],
            &[
                "file_path",
                "start_line",
                "end_line",
                "replacement",
                "replacement_path",
                "replacement_offset",
                "replacement_length",
                "after",
                "expected_lines",
            ],
        ),
        "web-extract" => wrap_flat_item(
            &mut map,
            &[],
            &["html", "path"],
            &["html", "path", "dump", "baseUrl"],
        ),
        _ => {}
    }
    Value::Object(map)
}
// 2a. 별칭 키 정규화 -----------------------------------------------------------
fn alias_keys(object: &mut Map<String, Value>, aliases: &[(&str, &str)]) {
    for (from, to) in aliases {
        if object.contains_key(*to) || !object.contains_key(*from) {
            continue;
        }
        if let Some(value) = object.remove(*from) {
            object.insert((*to).to_string(), value);
        }
    }
}
fn alias_item_keys(map: &mut Map<String, Value>, aliases: &[(&str, &str)]) {
    if aliases.is_empty() {
        return;
    }
    if let Some(Value::Array(items)) = map.get_mut("items") {
        for item in items {
            if let Value::Object(object) = item {
                alias_keys(object, aliases);
            }
        }
    }
}
// 2b. flat 단일 호출을 items:[{...}] 로 래핑 -----------------------------------
fn wrap_flat_item(
    map: &mut Map<String, Value>,
    aliases: &[(&str, &str)],
    markers: &[&str],
    item_keys: &[&str],
) {
    alias_item_keys(map, aliases);
    alias_keys(map, aliases);
    if map.contains_key("items") || !markers.iter().any(|key| map.contains_key(*key)) {
        return;
    }
    let mut item = Map::new();
    for key in item_keys {
        if let Some(value) = map.remove(*key) {
            item.insert((*key).to_string(), value);
        }
    }
    map.insert("items".to_string(), Value::Array(vec![Value::Object(item)]));
}
// 2c. 단일 path 를 read 계열 items 로 승격 --------------------------------------
fn promote_single_read_item(map: &mut Map<String, Value>, extra_keys: &[&str]) {
    if map.contains_key("paths") || map.contains_key("items") {
        return;
    }
    if !matches!(map.get("path"), Some(Value::String(_))) {
        return;
    }
    let Some(path) = map.remove("path") else {
        return;
    };
    let mut item = Map::new();
    item.insert("path".to_string(), path);
    for key in extra_keys {
        if let Some(value) = map.remove(*key) {
            item.insert((*key).to_string(), value);
        }
    }
    map.insert("items".to_string(), Value::Array(vec![Value::Object(item)]));
}
// 2d. paths 전용 도구의 items/path 흡수 ------------------------------------------
fn normalize_paths_only(map: &mut Map<String, Value>) {
    if map.contains_key("paths") {
        return;
    }
    match map.remove("items") {
        Some(Value::Array(items)) => {
            let paths = items
                .into_iter()
                .filter_map(|item| match item {
                    Value::String(text) => Some(Value::String(text)),
                    Value::Object(mut object) => match object.remove("path") {
                        Some(Value::String(text)) => Some(Value::String(text)),
                        _ => None,
                    },
                    _ => None,
                })
                .collect::<Vec<_>>();
            if !paths.is_empty() {
                map.insert("paths".to_string(), Value::Array(paths));
            }
            return;
        }
        Some(other) => {
            map.insert("items".to_string(), other);
        }
        None => {}
    }
    if !matches!(map.get("path"), Some(Value::String(_))) {
        return;
    }
    if let Some(path) = map.remove("path") {
        map.insert("paths".to_string(), json!([path]));
    }
}
// 2e. path-remove 편의 shape: paths:[...] 를 items:[{path, recursive?, force?}] 로 승격 ------
// path-stat/dir-create 는 이미 단순 paths 배열을 받지만 path-remove 는 items 만 받아, 에이전트가
// paths 로 호출하면 실패한다(히스토리 확인). 공유 recursive/force 플래그를 각 항목에 접어 넣는다.
fn promote_paths_to_remove_items(map: &mut Map<String, Value>) {
    if map.contains_key("items") {
        return;
    }
    if !matches!(map.get("paths"), Some(Value::Array(_))) {
        return;
    }
    let recursive = map.get("recursive").cloned();
    let force = map.get("force").cloned();
    let Some(Value::Array(paths)) = map.remove("paths") else {
        return;
    };
    let items: Vec<Value> = paths
        .iter()
        .filter_map(Value::as_str)
        .map(|path| {
            let mut item = Map::new();
            item.insert("path".to_string(), Value::String(path.to_string()));
            if let Some(recursive) = &recursive {
                item.insert("recursive".to_string(), recursive.clone());
            }
            if let Some(force) = &force {
                item.insert("force".to_string(), force.clone());
            }
            Value::Object(item)
        })
        .collect();
    if items.is_empty() {
        return;
    }
    map.remove("recursive");
    map.remove("force");
    map.insert("items".to_string(), Value::Array(items));
}
// 2f. 문자열로 마샬된 배열/객체 인수 복원 ---------------------------------------------------
// 에이전트가 items/paths 를 JSON 문자열로 직렬화해 보내면(스키마 오용 실패 원인) 파싱해 되돌린다.
fn coerce_json_string(map: &mut Map<String, Value>, keys: &[&str]) {
    for key in keys {
        let text = match map.get(*key) {
            Some(Value::String(text)) => text.clone(),
            _ => continue,
        };
        let trimmed = text.trim_start();
        if !(trimmed.starts_with('[') || trimmed.starts_with('{')) {
            continue;
        }
        if let Ok(parsed) = serde_json::from_str::<Value>(&text)
            && (parsed.is_array() || parsed.is_object())
        {
            map.insert((*key).to_string(), parsed);
        }
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn reports_unknown_tool_as_error() {
        let response = dispatch_tool_call("unknown", Some(json!({})));
        assert_eq!(response["isError"], true);
    }
    #[test]
    fn absorbs_flat_single_item_args() {
        let listed = absorb_arg_shape("dir-list", json!({ "path": "C:/x", "depth": 1 }));
        assert_eq!(listed["items"][0]["path"], "C:/x");
        assert_eq!(listed["items"][0]["depth"], 1);
        let written = absorb_arg_shape(
            "file-write",
            json!({ "file_path": "C:/x.txt", "content": "a" }),
        );
        assert_eq!(written["items"][0]["path"], "C:/x.txt");
        let edited = absorb_arg_shape(
            "file-edit",
            json!({ "path": "C:/x.txt", "old_string": "a", "new_string": "b" }),
        );
        assert_eq!(edited["items"][0]["file_path"], "C:/x.txt");
        assert!(edited.get("path").is_none());
    }
    #[test]
    fn aliases_item_keys_inside_items() {
        let written = absorb_arg_shape(
            "file-write",
            json!({ "items": [{ "file_path": "C:/y.txt", "content": "b" }] }),
        );
        assert_eq!(written["items"][0]["path"], "C:/y.txt");
        let copied = absorb_arg_shape(
            "path-copy",
            json!({ "items": [{ "from": "C:/a", "to": "C:/b" }] }),
        );
        assert_eq!(copied["items"][0]["source"], "C:/a");
        assert_eq!(copied["items"][0]["destination"], "C:/b");
    }
    #[test]
    fn promotes_single_read_path_with_range_keys() {
        let ranged = absorb_arg_shape(
            "file-read-line-range",
            json!({ "path": "C:/a.txt", "start_line": 3, "line_count": 2 }),
        );
        assert_eq!(ranged["items"][0]["path"], "C:/a.txt");
        assert_eq!(ranged["items"][0]["start_line"], 3);
        let read = absorb_arg_shape("file-read", json!({ "path": "C:/a.txt" }));
        assert_eq!(read["items"][0]["path"], "C:/a.txt");
    }
    #[test]
    fn normalizes_paths_only_tools() {
        let stat = absorb_arg_shape(
            "path-stat",
            json!({ "items": [{ "path": "C:/a" }, "C:/b"] }),
        );
        assert_eq!(stat["paths"], json!(["C:/a", "C:/b"]));
        let created = absorb_arg_shape("dir-create", json!({ "path": "C:/new" }));
        assert_eq!(created["paths"], json!(["C:/new"]));
    }
    #[test]
    fn single_items_object_becomes_array() {
        let shaped = absorb_arg_shape(
            "file-write",
            json!({ "items": { "path": "C:/z.txt", "content": "c" } }),
        );
        assert_eq!(shaped["items"][0]["path"], "C:/z.txt");
    }
    #[test]
    fn leaves_proper_batch_args_untouched() {
        let args = json!({ "items": [{ "path": "C:/a" }, { "path": "C:/b" }], "allowMissing": true });
        let shaped = absorb_arg_shape("dir-list", args.clone());
        assert_eq!(shaped, args);
    }
    #[test]
    fn path_remove_accepts_simple_paths_array() {
        // path-stat/dir-create 처럼 단순 paths 배열을 받아 items 로 승격하고 공유 플래그를 접어 넣는다.
        let shaped = absorb_arg_shape(
            "path-remove",
            json!({ "paths": ["C:/a", "C:/b"], "recursive": true }),
        );
        assert_eq!(shaped["items"][0]["path"], "C:/a");
        assert_eq!(shaped["items"][1]["path"], "C:/b");
        assert_eq!(shaped["items"][0]["recursive"], true);
        assert_eq!(shaped["items"][1]["recursive"], true);
        assert!(shaped.get("paths").is_none());
        assert!(shaped.get("recursive").is_none());
    }
    #[test]
    fn coerces_json_string_encoded_arrays() {
        // 에이전트가 배열을 JSON 문자열로 보내도(스키마 오용 실패 재현) 파싱해 정상 shape 로 흡수한다.
        let removed = absorb_arg_shape("path-remove", json!({ "paths": "[\"C:/a\", \"C:/b\"]" }));
        assert_eq!(removed["items"][0]["path"], "C:/a");
        assert_eq!(removed["items"][1]["path"], "C:/b");
        let stat = absorb_arg_shape("path-stat", json!({ "paths": "[\"C:/x\"]" }));
        assert_eq!(stat["paths"][0], "C:/x");
    }
    #[test]
    fn leaves_non_json_string_paths_for_handler_error() {
        // JSON 이 아닌 문자열은 파싱하지 않고 그대로 둬 핸들러가 명확히 오류를 낸다.
        let shaped = absorb_arg_shape("path-stat", json!({ "paths": "C:/single" }));
        assert_eq!(shaped["paths"], "C:/single");
    }
}