oxml-mcp 0.0.7

Model Context Protocol server exposing oxml's XML parsing, XPath and XSD validation
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 oxml. All rights reserved.

//! A minimal JSON reader and writer.
//!
//! MCP speaks JSON-RPC over stdio. The messages involved are small and
//! their shapes are fixed, so this implements the subset needed rather
//! than taking a serialisation dependency — which for a server meant
//! to be audited is a worthwhile trade.

use std::collections::BTreeMap;
use std::fmt::Write as _;

/// A JSON value.
#[derive(Debug, Clone, PartialEq)]
pub enum Json {
    /// `null`
    Null,
    /// `true` or `false`
    Bool(bool),
    /// A number.
    Number(f64),
    /// A string.
    String(String),
    /// An array.
    Array(Vec<Json>),
    /// An object. Ordered so output is deterministic.
    Object(BTreeMap<String, Json>),
}

impl Json {
    /// Build an object from pairs.
    #[must_use]
    pub fn object(pairs: Vec<(&str, Self)>) -> Self {
        Self::Object(
            pairs.into_iter().map(|(k, v)| (k.to_owned(), v)).collect(),
        )
    }

    /// A string value.
    #[must_use]
    pub fn str(s: impl Into<String>) -> Self {
        Self::String(s.into())
    }

    /// Look up a key on an object.
    #[must_use]
    pub fn get(&self, key: &str) -> Option<&Self> {
        match self {
            Self::Object(m) => m.get(key),
            _ => None,
        }
    }

    /// The value as a string slice, if it is one.
    #[must_use]
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Self::String(s) => Some(s),
            _ => None,
        }
    }

    /// Serialise to JSON text.
    #[must_use]
    pub fn to_json(&self) -> String {
        let mut out = String::new();
        self.write(&mut out);
        out
    }

    fn write(&self, out: &mut String) {
        match self {
            Self::Null => out.push_str("null"),
            Self::Bool(b) => {
                out.push_str(if *b { "true" } else { "false" });
            }
            Self::Number(n) => {
                // JSON has one number type; printing an integral
                // value as `2` rather than `2.0` is what every other
                // encoder does, and the bound keeps the cast exact.
                #[allow(clippy::cast_possible_truncation)]
                if n.fract() == 0.0 && n.abs() < 1e15 {
                    let _ = write!(out, "{}", *n as i64);
                } else {
                    let _ = write!(out, "{n}");
                }
            }
            Self::String(s) => write_string(s, out),
            Self::Array(items) => {
                out.push('[');
                for (i, item) in items.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    item.write(out);
                }
                out.push(']');
            }
            Self::Object(map) => {
                out.push('{');
                for (i, (k, v)) in map.iter().enumerate() {
                    if i > 0 {
                        out.push(',');
                    }
                    write_string(k, out);
                    out.push(':');
                    v.write(out);
                }
                out.push('}');
            }
        }
    }
}

fn write_string(s: &str, out: &mut String) {
    out.push('"');
    for c in s.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\r' => out.push_str("\\r"),
            '\t' => out.push_str("\\t"),
            // Control characters must be escaped or the output is not
            // valid JSON, and XML documents do contain them.
            c if (c as u32) < 0x20 => {
                let _ = write!(out, "\\u{:04x}", c as u32);
            }
            c => out.push(c),
        }
    }
    out.push('"');
}

/// Parse JSON text.
///
/// # Errors
///
/// Returns a description if the input is not valid JSON.
pub fn parse(input: &str) -> Result<Json, String> {
    let chars: Vec<char> = input.chars().collect();
    let mut p = Parser {
        chars: &chars,
        pos: 0,
    };
    p.ws();
    let v = p.value()?;
    p.ws();
    if p.pos < chars.len() {
        return Err("trailing input after JSON value".to_owned());
    }
    Ok(v)
}

struct Parser<'a> {
    chars: &'a [char],
    pos: usize,
}

impl Parser<'_> {
    fn ws(&mut self) {
        while matches!(self.chars.get(self.pos), Some(' ' | '\t' | '\n' | '\r'))
        {
            self.pos += 1;
        }
    }

    fn value(&mut self) -> Result<Json, String> {
        self.ws();
        match self.chars.get(self.pos) {
            Some('{') => self.object(),
            Some('[') => self.array(),
            Some('"') => Ok(Json::String(self.string()?)),
            Some('t') => self.literal("true", Json::Bool(true)),
            Some('f') => self.literal("false", Json::Bool(false)),
            Some('n') => self.literal("null", Json::Null),
            Some(_) => self.number(),
            None => Err("unexpected end of JSON".to_owned()),
        }
    }

    fn literal(&mut self, word: &str, v: Json) -> Result<Json, String> {
        if self.chars[self.pos..]
            .starts_with(word.chars().collect::<Vec<_>>().as_slice())
        {
            self.pos += word.len();
            Ok(v)
        } else {
            Err(format!("expected `{word}`"))
        }
    }

    fn object(&mut self) -> Result<Json, String> {
        self.pos += 1;
        let mut map = BTreeMap::new();
        self.ws();
        if self.chars.get(self.pos) == Some(&'}') {
            self.pos += 1;
            return Ok(Json::Object(map));
        }
        loop {
            self.ws();
            let key = self.string()?;
            self.ws();
            if self.chars.get(self.pos) != Some(&':') {
                return Err("expected `:` in object".to_owned());
            }
            self.pos += 1;
            let value = self.value()?;
            let _ = map.insert(key, value);
            self.ws();
            match self.chars.get(self.pos) {
                Some(',') => self.pos += 1,
                Some('}') => {
                    self.pos += 1;
                    return Ok(Json::Object(map));
                }
                _ => return Err("expected `,` or `}`".to_owned()),
            }
        }
    }

    fn array(&mut self) -> Result<Json, String> {
        self.pos += 1;
        let mut items = Vec::new();
        self.ws();
        if self.chars.get(self.pos) == Some(&']') {
            self.pos += 1;
            return Ok(Json::Array(items));
        }
        loop {
            items.push(self.value()?);
            self.ws();
            match self.chars.get(self.pos) {
                Some(',') => self.pos += 1,
                Some(']') => {
                    self.pos += 1;
                    return Ok(Json::Array(items));
                }
                _ => return Err("expected `,` or `]`".to_owned()),
            }
        }
    }

    fn string(&mut self) -> Result<String, String> {
        if self.chars.get(self.pos) != Some(&'"') {
            return Err("expected a string".to_owned());
        }
        self.pos += 1;
        let mut out = String::new();
        loop {
            match self.chars.get(self.pos) {
                None => return Err("unterminated string".to_owned()),
                Some('"') => {
                    self.pos += 1;
                    return Ok(out);
                }
                Some('\\') => {
                    self.pos += 1;
                    let esc =
                        self.chars.get(self.pos).ok_or("trailing backslash")?;
                    self.pos += 1;
                    match esc {
                        'n' => out.push('\n'),
                        't' => out.push('\t'),
                        'r' => out.push('\r'),
                        'b' => out.push('\u{8}'),
                        'f' => out.push('\u{c}'),
                        'u' => {
                            let cp = self.hex4()?;
                            // A non-BMP character is escaped as a
                            // surrogate *pair*; neither half is a
                            // `char` on its own. Python's `json.dumps`
                            // escapes non-ASCII by default, so a client
                            // sending an emoji arrives this way.
                            let cp = if (0xD800..0xDC00).contains(&cp) {
                                if !matches!(
                                    self.chars.get(self.pos..self.pos + 2),
                                    Some(['\\', 'u'])
                                ) {
                                    return Err(
                                        "lone high surrogate".to_owned()
                                    );
                                }
                                self.pos += 2;
                                let lo = self.hex4()?;
                                if !(0xDC00..0xE000).contains(&lo) {
                                    return Err(
                                        "high surrogate not followed by a \
                                         low surrogate"
                                            .to_owned(),
                                    );
                                }
                                0x1_0000 + ((cp - 0xD800) << 10) + (lo - 0xDC00)
                            } else if (0xDC00..0xE000).contains(&cp) {
                                return Err("lone low surrogate".to_owned());
                            } else {
                                cp
                            };
                            out.push(
                                char::from_u32(cp).ok_or("bad code point")?,
                            );
                        }
                        c => out.push(*c),
                    }
                }
                Some(c) => {
                    out.push(*c);
                    self.pos += 1;
                }
            }
        }
    }

    /// Read exactly four hex digits of a `\\u` escape.
    fn hex4(&mut self) -> Result<u32, String> {
        let hex: String = self
            .chars
            .get(self.pos..self.pos + 4)
            .ok_or("truncated \\u escape")?
            .iter()
            .collect();
        self.pos += 4;
        u32::from_str_radix(&hex, 16).map_err(|_| "bad \\u escape".to_owned())
    }

    fn number(&mut self) -> Result<Json, String> {
        let start = self.pos;
        while matches!(
            self.chars.get(self.pos),
            Some(c) if c.is_ascii_digit()
                || matches!(c, '-' | '+' | '.' | 'e' | 'E')
        ) {
            self.pos += 1;
        }
        let text: String = self.chars[start..self.pos].iter().collect();
        text.parse()
            .map(Json::Number)
            .map_err(|_| format!("invalid number `{text}`"))
    }
}

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

    fn round_trip(text: &str) -> String {
        parse(text).expect("valid JSON").to_json()
    }

    #[test]
    fn scalars_round_trip() {
        assert_eq!(round_trip("null"), "null");
        assert_eq!(round_trip("true"), "true");
        assert_eq!(round_trip("false"), "false");
        assert_eq!(round_trip("\"hi\""), "\"hi\"");
    }

    #[test]
    fn integral_numbers_print_without_a_fraction() {
        // MCP ids are compared by clients; `2.0` where `2` was sent is
        // a different JSON value to a strict client.
        assert_eq!(round_trip("2"), "2");
        assert_eq!(round_trip("-17"), "-17");
        assert_eq!(round_trip("0"), "0");
        assert_eq!(round_trip("1.5"), "1.5");
        assert_eq!(round_trip("1e3"), "1000");
    }

    #[test]
    fn very_large_integral_numbers_do_not_wrap() {
        // Beyond 1e15 the i64 cast would be lossy, so the float path
        // must take over rather than silently truncating.
        let out = Json::Number(1e300).to_json();
        assert!(out.starts_with('1'), "{out}");
        assert!(parse(&out).is_ok(), "{out} did not re-parse");
    }

    #[test]
    fn objects_serialise_in_a_stable_order() {
        let text = round_trip(r#"{"b":1,"a":2}"#);
        assert_eq!(text, r#"{"a":2,"b":1}"#);
    }

    #[test]
    fn nesting_round_trips() {
        let src = r#"{"a":[1,[2,{"b":null}]],"c":{}}"#;
        assert_eq!(round_trip(src), src);
    }

    #[test]
    fn string_escapes_decode() {
        let v = parse(r#""a\nb\tc\"d\\e\/f\r\b\f""#).expect("valid");
        assert_eq!(v.as_str(), Some("a\nb\tc\"d\\e/f\r\u{8}\u{c}"));
    }

    #[test]
    fn control_characters_are_escaped_on_output() {
        // XML documents do contain these, and an unescaped control
        // character makes the whole response unparseable to the client.
        let out = Json::str("a\u{1}b\u{1f}").to_json();
        assert_eq!(out, r#""a\u0001b\u001f""#);
        assert_eq!(
            parse(&out).expect("re-parses").as_str(),
            Some("a\u{1}b\u{1f}")
        );
    }

    #[test]
    fn surrogate_pairs_decode_to_one_character() {
        // Python's `json.dumps` escapes non-ASCII by default, so a
        // client written against it sends emoji this way.
        assert_eq!(
            parse(r#""\ud83d\ude00""#).expect("valid").as_str(),
            Some("\u{1f600}")
        );
        assert_eq!(
            parse(r#""a\ud83d\ude00b""#).expect("valid").as_str(),
            Some("a\u{1f600}b")
        );
    }

    #[test]
    fn bmp_escapes_still_decode() {
        assert_eq!(
            parse(r#""\u00e9\u4e2d""#).expect("valid").as_str(),
            Some("é中")
        );
    }

    #[test]
    fn lone_surrogates_are_rejected() {
        // Rather than panicking or emitting an invalid character.
        assert!(parse(r#""\ud83d""#).is_err());
        assert!(parse(r#""\ude00""#).is_err());
        assert!(parse(r#""\ud83dx""#).is_err());
        assert!(parse(r#""\ud83d\u0041""#).is_err());
    }

    #[test]
    fn raw_utf8_passes_through_unescaped() {
        let src = "\"héllo 😀 中\"";
        assert_eq!(round_trip(src), src);
    }

    #[test]
    fn malformed_input_is_an_error_not_a_panic() {
        for bad in [
            "",
            "{",
            "[",
            "{\"a\"}",
            "{\"a\":}",
            "[1,",
            "\"unterminated",
            "tru",
            "nul",
            "\"\\",
            "\"\\u00\"",
            "\"\\uZZZZ\"",
            "@",
            "{\"a\":1}trailing",
            "1 2",
        ] {
            assert!(parse(bad).is_err(), "`{bad}` should not parse");
        }
    }

    #[test]
    fn whitespace_around_values_is_ignored() {
        assert_eq!(
            round_trip(" \n\t{ \"a\" : [ 1 , 2 ] }\r\n"),
            r#"{"a":[1,2]}"#
        );
    }

    #[test]
    fn get_and_as_str_are_type_safe() {
        let v = parse(r#"{"a":"x","b":1}"#).expect("valid");
        assert_eq!(v.get("a").and_then(Json::as_str), Some("x"));
        assert_eq!(v.get("b").and_then(Json::as_str), None);
        assert!(v.get("missing").is_none());
        assert!(Json::Null.get("a").is_none());
        assert!(Json::Null.as_str().is_none());
    }

    #[test]
    fn object_builder_matches_parsed_form() {
        let built =
            Json::object(vec![("a", Json::str("x")), ("b", Json::Number(1.0))]);
        assert_eq!(built.to_json(), r#"{"a":"x","b":1}"#);
    }

    #[test]
    fn deep_nesting_does_not_overflow_the_stack() {
        let depth = 200;
        let src = format!("{}{}", "[".repeat(depth), "]".repeat(depth));
        assert!(parse(&src).is_ok());
    }
}

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

    #[test]
    fn every_escape_survives_a_round_trip() {
        // Each of these has its own arm on both the reading and the
        // writing side, and a mismatch between the two corrupts the
        // document silently rather than failing.
        for s in [
            "back\\slash",
            "carriage\rreturn",
            "tab\there",
            "quote\"inside",
            "new\nline",
            "all\\ \" \n \r \t together",
        ] {
            let encoded = Json::str(s).to_json();
            assert_eq!(
                parse(&encoded).expect("re-parses").as_str(),
                Some(s),
                "round trip failed for {s:?} via {encoded}"
            );
        }
    }

    #[test]
    fn a_structural_error_names_what_was_expected() {
        // A bare "invalid JSON" would give a client nothing to act on.
        let e = parse(r#"{"a":1 "b":2}"#).expect_err("malformed");
        assert!(e.contains(',') || e.contains('}'), "{e}");

        let e = parse("[1 2]").expect_err("malformed");
        assert!(e.contains(',') || e.contains(']'), "{e}");
    }
}