rust-fs-mcp 0.1.6

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
//! args_ref.rs
//! core::args_ref
//!
//! Resolver for large JSON argument indirection via args_path / args_offset / args_length.
//! Reads a slice from a file so tool handlers see the same input shape as an inline argument.
//! Also absorbs host marshaling variants that send schema arrays, booleans, or numbers as JSON strings.
//!

use serde_json::{Map, Value};
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;

const READ_SLICE_CHUNK: usize = 64 * 1024;

// Schema-array keys that never carry body text, so re-parsing a string value cannot corrupt a
// genuine string field (for example file-write content).
const ARRAY_ARG_KEYS: [&str; 5] = ["items", "paths", "sessionIds", "requests", "filesToStage"];
// Schema-boolean keys; a "true" / "false" string is promoted, anything else stays as received.
const BOOL_ARG_KEYS: [&str; 22] = [
    "after",
    "all",
    "allowEmpty",
    "allowMissing",
    "amend",
    "force",
    "ignoreCase",
    "includeFiles",
    "includeHidden",
    "includeUntracked",
    "initializeIfNotPresent",
    "isUrl",
    "literal",
    "literalSearch",
    "nameOnly",
    "noVerify",
    "recursive",
    "resetAuthor",
    "staged",
    "stat",
    "update",
    "validateGitRepo",
];
// Schema-number keys; only a string that fully parses as a number is promoted. Free-text keys
// whose values can look numeric (object, pattern, content, ...) are intentionally not listed.
const NUM_ARG_KEYS: [&str; 31] = [
    "args_length",
    "args_offset",
    "content_length",
    "content_offset",
    "contextLines",
    "depth",
    "end_line",
    "expected_lines",
    "expected_replacements",
    "length",
    "line_count",
    "maxEntries",
    "maxMatches",
    "maxResults",
    "maxSnippetChars",
    "maxSnippets",
    "messageLength",
    "messageOffset",
    "new_string_length",
    "new_string_offset",
    "offset",
    "old_string_length",
    "old_string_offset",
    "pattern_length",
    "pattern_offset",
    "replacement_length",
    "replacement_offset",
    "start_line",
    "timeout_ms",
    "value_length",
    "value_offset",
];

// 1. Resolve tool args reference -----------------------------------------------------------
// Resolve args_path indirection with host-marshaling coercion on both sides. Claude Code sends
// schema arrays as JSON strings (items: "[{\"path\":...}]") and scalars as strings
// ("allowMissing": "true"), so handlers reading via as_array / as_bool / as_u64 would silently
// drop them; Codex sends real JSON values and is untouched. Coercion runs before args_path
// resolution so stringified args_offset / args_length parse, and again afterward so inline
// overrides merged from the file go through the same absorption.
pub fn resolve_tool_args(args: Option<Value>) -> Result<Value, String> {
    let args = match args {
        Some(Value::Object(mut map)) => {
            coerce_stringified_args(&mut map);
            Some(Value::Object(map))
        }
        other => other,
    };
    let resolved = resolve_args_path(args)?;
    let Value::Object(mut map) = resolved else {
        return Ok(resolved);
    };
    coerce_stringified_args(&mut map);
    Ok(Value::Object(map))
}

// 1a. Coerce stringified argument fields ---------------------------------------------------
// Keys are whitelisted per schema type; a value that does not parse exactly stays as received.
fn coerce_stringified_args(map: &mut Map<String, Value>) {
    for key in ARRAY_ARG_KEYS {
        let parsed = match map.get(key) {
            Some(Value::String(text)) => serde_json::from_str::<Value>(text).ok(),
            _ => None,
        };
        if let Some(value @ Value::Array(_)) = parsed {
            map.insert(key.to_string(), value);
        }
    }
    coerce_scalar_fields(map);
    for key in ["items", "requests"] {
        if let Some(Value::Array(elements)) = map.get_mut(key) {
            for element in elements {
                if let Value::Object(item) = element {
                    coerce_scalar_fields(item);
                }
            }
        }
    }
}

fn coerce_scalar_fields(map: &mut Map<String, Value>) {
    for key in BOOL_ARG_KEYS {
        let promoted = match map.get(key) {
            Some(Value::String(text)) => match text.as_str() {
                "true" => Some(true),
                "false" => Some(false),
                _ => None,
            },
            _ => None,
        };
        if let Some(value) = promoted {
            map.insert(key.to_string(), Value::Bool(value));
        }
    }
    for key in NUM_ARG_KEYS {
        let promoted = match map.get(key) {
            Some(Value::String(text)) => parse_json_number(text),
            _ => None,
        };
        if let Some(value) = promoted {
            map.insert(key.to_string(), Value::Number(value));
        }
    }
}

fn parse_json_number(text: &str) -> Option<serde_json::Number> {
    if let Ok(value) = text.parse::<i64>() {
        return Some(serde_json::Number::from(value));
    }
    text.parse::<f64>()
        .ok()
        .and_then(serde_json::Number::from_f64)
}

// 1b. Resolve args_path indirection ---------------------------------------------------------
fn resolve_args_path(args: Option<Value>) -> Result<Value, String> {
    let Some(Value::Object(map)) = args else {
        return Ok(args.unwrap_or(Value::Object(Map::new())));
    };
    let Some(args_path) = map.get("args_path") else {
        return Ok(Value::Object(map));
    };
    let Some(args_path) = args_path.as_str() else {
        return Err("args_path must be a string".to_string());
    };
    let offset = optional_usize(&map, "args_offset")?.unwrap_or(0);
    let length = optional_usize(&map, "args_length")?;
    let args_text = read_text_slice(args_path, offset, length)?;
    let parsed: Value = serde_json::from_str(&args_text)
        .map_err(|error| format!("args_path must contain valid JSON: {error}"))?;
    let inline: Map<String, Value> = map
        .into_iter()
        .filter(|(key, _)| key != "args_path" && key != "args_offset" && key != "args_length")
        .collect();
    if inline.is_empty() {
        return Ok(parsed);
    }
    let Value::Object(mut parsed_map) = parsed else {
        return Err(
            "args_path JSON must be an object when inline overrides are provided".to_string(),
        );
    };
    for (key, value) in inline {
        parsed_map.insert(key, value);
    }
    Ok(Value::Object(parsed_map))
}

// 2. Read text slice -----------------------------------------------------------------------
pub fn read_text_slice(
    path: impl AsRef<Path>,
    offset: usize,
    length: Option<usize>,
) -> Result<String, String> {
    if length == Some(0) {
        return Ok(String::new());
    }

    let path = path.as_ref();
    let file =
        File::open(path).map_err(|error| format!("Failed to read {}: {error}", path.display()))?;
    let mut reader = BufReader::with_capacity(READ_SLICE_CHUNK, file);
    let mut buffer = [0u8; READ_SLICE_CHUNK];
    let mut pending = Vec::new();
    let mut skip = offset;
    let mut take = length;
    let mut result = String::new();

    loop {
        let read = reader
            .read(&mut buffer)
            .map_err(|error| format!("Failed to read {}: {error}", path.display()))?;
        if read == 0 {
            break;
        }
        if pending.is_empty()
            && buffer[..read].is_ascii()
            && append_ascii_slice(&buffer[..read], &mut skip, &mut take, &mut result)
        {
            return Ok(result);
        }
        pending.extend_from_slice(&buffer[..read]);
        let valid_end = match std::str::from_utf8(&pending) {
            Ok(_) => pending.len(),
            Err(error) if error.error_len().is_none() => error.valid_up_to(),
            Err(_) => {
                return Err(format!(
                    "Failed to read {}: stream did not contain valid UTF-8",
                    path.display()
                ));
            }
        };
        let text = std::str::from_utf8(&pending[..valid_end]).unwrap();
        if append_text_slice(text, &mut skip, &mut take, &mut result) {
            return Ok(result);
        }
        let remaining = pending[valid_end..].to_vec();
        pending.clear();
        pending.extend_from_slice(&remaining);
    }

    if !pending.is_empty() {
        return Err(format!(
            "Failed to read {}: stream did not contain valid UTF-8",
            path.display()
        ));
    }
    Ok(result)
}

fn append_ascii_slice(
    bytes: &[u8],
    skip: &mut usize,
    take: &mut Option<usize>,
    result: &mut String,
) -> bool {
    let start = (*skip).min(bytes.len());
    *skip -= start;
    if *skip > 0 {
        return false;
    }

    let available = bytes.len() - start;
    let count = match take {
        Some(remaining) => {
            let count = (*remaining).min(available);
            *remaining -= count;
            count
        }
        None => available,
    };
    if count > 0 {
        result.push_str(std::str::from_utf8(&bytes[start..start + count]).unwrap());
    }

    matches!(take, Some(0))
}

fn append_text_slice(
    text: &str,
    skip: &mut usize,
    take: &mut Option<usize>,
    result: &mut String,
) -> bool {
    for ch in text.chars() {
        if *skip > 0 {
            *skip -= 1;
            continue;
        }
        if let Some(remaining) = take {
            if *remaining == 0 {
                return true;
            }
            result.push(ch);
            *remaining -= 1;
            if *remaining == 0 {
                return true;
            }
        } else {
            result.push(ch);
        }
    }
    false
}

// 3. Optional usize ------------------------------------------------------------------------
fn optional_usize(map: &Map<String, Value>, key: &str) -> Result<Option<usize>, String> {
    match map.get(key) {
        Some(value) => value
            .as_u64()
            .map(|value| Some(value as usize))
            .ok_or_else(|| format!("{key} must be a non-negative integer")),
        None => Ok(None),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;
    use std::fs;
    use std::time::{SystemTime, UNIX_EPOCH};

    #[test]
    fn resolves_inline_args() {
        let args = resolve_tool_args(Some(json!({"x": 1}))).unwrap();
        assert_eq!(args["x"], 1);
    }

    #[test]
    fn coerces_stringified_items_array() {
        // Claude Code marshals the items array as a JSON string; it must parse back to an array.
        let args = resolve_tool_args(Some(json!({ "items": "[{\"path\": \"a.txt\"}]" }))).unwrap();
        assert!(args["items"].is_array());
        assert_eq!(args["items"][0]["path"], "a.txt");
    }

    #[test]
    fn coerces_stringified_paths_array() {
        let args = resolve_tool_args(Some(json!({ "paths": "[\"a.txt\", \"b.txt\"]" }))).unwrap();
        assert!(args["paths"].is_array());
        assert_eq!(args["paths"][1], "b.txt");
    }

    #[test]
    fn leaves_non_json_string_field_untouched() {
        // A bare path is not valid JSON and must stay a string so no genuine value is corrupted.
        let args = resolve_tool_args(Some(json!({ "items": "C:/not/json" }))).unwrap();
        assert!(args["items"].is_string());
    }

    #[test]
    fn leaves_real_array_untouched() {
        let args = resolve_tool_args(Some(json!({ "paths": ["a", "b"] }))).unwrap();
        assert_eq!(args["paths"][0], "a");
    }

    #[test]
    fn coerces_stringified_bool_and_number() {
        let args = resolve_tool_args(Some(json!({
            "allowMissing": "true",
            "stat": "false",
            "maxResults": "10",
            "paths": ["a"]
        })))
        .unwrap();
        assert_eq!(args["allowMissing"], true);
        assert_eq!(args["stat"], false);
        assert_eq!(args["maxResults"], 10);
    }

    #[test]
    fn coerces_scalars_inside_items_elements() {
        let args = resolve_tool_args(Some(json!({
            "items": [{
                "path": "a.txt",
                "isUrl": "false",
                "offset": "2",
                "line_count": "3"
            }]
        })))
        .unwrap();
        assert_eq!(args["items"][0]["isUrl"], false);
        assert_eq!(args["items"][0]["offset"], 2);
        assert_eq!(args["items"][0]["line_count"], 3);
    }

    #[test]
    fn coerces_stringified_requests_array() {
        let args = resolve_tool_args(Some(json!({
            "requests": "[{\"op\": \"git-status\", \"path\": \".\", \"recursive\": \"true\"}]"
        })))
        .unwrap();
        assert!(args["requests"].is_array());
        assert_eq!(args["requests"][0]["recursive"], true);
    }

    #[test]
    fn leaves_free_text_and_partial_numbers_untouched() {
        // object is free text (a revision named 123456 must stay a string) and values that do
        // not parse exactly keep their original form.
        let args = resolve_tool_args(Some(json!({
            "object": "123456",
            "maxResults": "10x",
            "allowMissing": "yes"
        })))
        .unwrap();
        assert!(args["object"].is_string());
        assert!(args["maxResults"].is_string());
        assert!(args["allowMissing"].is_string());
    }

    #[test]
    fn reads_text_slice_by_char_offset() {
        let path = std::env::current_dir()
            .unwrap()
            .join("target")
            .join(format!(
                "rust-fs-mcp-args-slice-{}",
                SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap()
                    .as_nanos()
            ));
        fs::write(&path, "ab한글cd").unwrap();

        let sliced = read_text_slice(&path, 2, Some(2)).unwrap();
        assert_eq!(sliced, "한글");

        fs::remove_file(path).unwrap();
    }

    #[test]
    fn reads_ascii_slice_fast_path() {
        let path = std::env::current_dir()
            .unwrap()
            .join("target")
            .join(format!(
                "rust-fs-mcp-args-ascii-slice-{}",
                SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .unwrap()
                    .as_nanos()
            ));
        fs::write(&path, "abcdef").unwrap();

        let sliced = read_text_slice(&path, 2, Some(3)).unwrap();
        assert_eq!(sliced, "cde");

        fs::remove_file(path).unwrap();
    }
}