noyalib-mcp 0.0.43

Model Context Protocol server exposing noyalib's lossless YAML editing to AI agents
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.

//! Library surface for `noyalib-mcp`.
//!
//! Hosts the JSON-RPC 2.0 dispatch logic and the tool implementations.
//! The `noyalib-mcp` binary in `main.rs` is a thin stdio loop that
//! drives [`handle_message`]; tests reach the same handlers
//! directly so coverage no longer depends on standing up a real
//! stdio process.
//!
//! # Cargo features
//!
//! This crate exposes no optional features; the MCP tool set
//! (`noyalib_get`, `noyalib_set`, `noyalib_set_multidoc`) is
//! fixed. Optional `noyalib`
//! features pulled in by a downstream packager (`schema`,
//! `parallel`, …) do not change the MCP wire surface — they
//! only affect what `noyalib::Error` messages can appear inside
//! tool-call error envelopes. The canonical `noyalib` feature
//! matrix lives in
//! [`crates/noyalib/src/lib.rs`](https://docs.rs/noyalib).
//!
//! # MSRV
//!
//! **Rust 1.86.0** stable — same as the core `noyalib` library
//! (and this crate's `rust-version`; an earlier revision of this
//! paragraph said 1.75.0 while the manifest said 1.86.0 — the
//! manifest is the contract).
//! The MCP wire surface is text-only JSON-RPC and pulls no
//! nightly-only deps. CI verifies the floor via the
//! `Per-crate MSRV` workflow job. See the workspace
//! [`POLICIES.md`](https://github.com/sebastienrousseau/noyalib/blob/main/docs/POLICIES.md#1-msrv-minimum-supported-rust-version)
//! for the bump policy.
//!
//! # Panics
//!
//! Public functions in this crate do not panic on well-formed
//! input. The MCP binary `unwrap`s once on stdin acquisition
//! during boot — that's deliberate, every caller invokes the
//! binary via a host process that controls the pipe.
//!
//! # Errors
//!
//! Tool calls return JSON-RPC error envelopes per the
//! [MCP specification](https://modelcontextprotocol.io). The
//! error code taxonomy lives in
//! [`docs/tools-reference.md`](https://github.com/sebastienrousseau/noyalib-mcp/blob/main/docs/tools-reference.md):
//! `-32000` (file I/O), `-32001` (parse), `-32002` (path not
//! found), `-32003` (set), `-32602` (missing arg), `-32601`
//! (unknown method).
//!
//! # Concurrency
//!
//! Each MCP request is processed sequentially on the binary's
//! stdio loop. The host (Claude Desktop, Cursor, Zed, …) is
//! responsible for not pipelining requests; if it does, the
//! tool execution is serialised by the loop's `BufRead` reader.
//!
//! # Platform support
//!
//! Tier-1 (CI-verified each PR): `aarch64-apple-darwin`,
//! `x86_64-unknown-linux-gnu`, `x86_64-pc-windows-msvc`.
//!
//! `noyalib_set` writes via an *atomic file replacement*
//! helper: write to a sibling temp file → `sync_all` →
//! `rename`. This is naturally atomic on POSIX; on Windows it
//! uses `MoveFileExW(MOVEFILE_REPLACE_EXISTING |
//! MOVEFILE_WRITE_THROUGH)` semantics so concurrent readers
//! always see either the old or the new contents — never a
//! half-write or a stale-page-cache observation. This was the
//! fix for the historical Windows-only `tool_call_set_preserves_comments`
//! flake.
//!
//! # Performance
//!
//! Each `tools/call` round-trip goes through one
//! `noyalib::cst::parse_document` (`O(n)` over input bytes)
//! and, for `noyalib_set`, one `Document::to_string` emit
//! (`O(n)` over output bytes). JSON-RPC line framing is
//! amortised constant-time per message. Tool calls do **not**
//! cache the parsed CST between requests — every call is a
//! fresh parse so concurrent edits from outside the MCP server
//! are always observed. Typical tool-call latency on a 100 KB
//! YAML file: 1–3 ms parse + emit on commodity hardware.
//!
//! # Security
//!
//! `#![forbid(unsafe_code)]`. No FFI. No network I/O —
//! `noyalib-mcp` is stdio-only by design; remote hosting goes
//! through a separate broker (see `examples/hosted-mcp-run.md`).
//! The server has no auth layer; restrict the working
//! directory of the spawned process via container mounts /
//! systemd `ReadWritePaths=` for production deployments.
//! Resource-limit gates are inherited from `noyalib`'s
//! `ParserConfig` defaults. Full posture:
//! [`SECURITY.md`](https://github.com/sebastienrousseau/noyalib/blob/main/SECURITY.md).
//!
//! # API stability and SemVer
//!
//! Pre-1.0 (`0.0.x`): the MCP wire contract (tool names,
//! input-schema shapes, error code ranges, the
//! [`SUPPORTED_PROTOCOL_VERSIONS`] set) is **stable** within a
//! 0.0.x line — bug fixes only.
//! Adding a new tool is allowed within a 0.0.x bump; removing
//! or renaming a tool, or repurposing an error code, is held
//! to a 0.x bump (e.g. 0.0.x → 0.1.0). The Rust library
//! surface (`handle_message`, `dispatch`, `error_str`,
//! `Request`, `Response`, `ErrorResponse`, `HandleOutcome`) is
//! covered by the workspace SemVer policy in
//! [`POLICIES.md`](https://github.com/sebastienrousseau/noyalib/blob/main/docs/POLICIES.md#2-semver--api-stability).
//! `cargo-semver-checks` runs in CI on every PR.
//!
//! # Documentation
//!
//! - **Engineering policies** — workspace
//!   [`POLICIES.md`](https://github.com/sebastienrousseau/noyalib/blob/main/docs/POLICIES.md).
//! - **MCP specification**: <https://modelcontextprotocol.io>.
//! - **Tools reference** (input schemas, error codes):
//!   [`docs/tools-reference.md`](https://github.com/sebastienrousseau/noyalib/blob/main/crates/noyalib-mcp/doc/tools-reference.md).
//! - **Host configurations** (Claude Desktop, Cursor,
//!   Continue.dev, Zed, hosted gateways):
//!   [`examples/`](https://github.com/sebastienrousseau/noyalib/tree/main/crates/noyalib-mcp/examples).

#![forbid(unsafe_code)]
#![warn(missing_docs)]
// Opt-in coverage exclusion (`NOYALIB_COVERAGE=1`) — see
// `build.rs` for the flag, individual `coverage(off)` annotations
// are below.
#![cfg_attr(noyalib_coverage, allow(unstable_features))]
#![cfg_attr(noyalib_coverage, feature(coverage_attribute))]

use serde::{Deserialize, Serialize};
use serde_json::{Value as JsonValue, json};

pub mod prompts;
pub mod resources;
pub mod tools;

/// Protocol revisions this server speaks, newest first. The server
/// is **dual-era** (MCP 2026-07-28 "Versioning and Compatibility"
/// terminology): a modern client declares its version per request in
/// `_meta` and may probe with `server/discover`; a legacy client
/// opens with an `initialize` handshake and negotiates
/// [`LEGACY_PROTOCOL_VERSION`].
pub const SUPPORTED_PROTOCOL_VERSIONS: [&str; 2] = ["2026-07-28", "2025-06-18"];

/// The newest handshake-based revision this server implements —
/// what `initialize` answers when the client's requested version is
/// not supported (per the 2025-06-18 negotiation rules, the server
/// then responds with a version it does support).
pub const LEGACY_PROTOCOL_VERSION: &str = "2025-06-18";

/// The `_meta` key a modern (2026-07-28+) client uses to declare the
/// protocol revision of each request.
pub const META_PROTOCOL_VERSION_KEY: &str = "io.modelcontextprotocol/protocolVersion";

/// `_meta` key under which each result identifies this server
/// (2026-07-28 "servers SHOULD identify themselves in each result's
/// `_meta`").
const META_SERVER_INFO_KEY: &str = "io.modelcontextprotocol/serverInfo";

/// `UnsupportedProtocolVersionError` code (2026-07-28 error-code
/// allocation: `-32020..=-32099` reserved for the MCP spec).
pub const UNSUPPORTED_PROTOCOL_VERSION: i32 = -32022;

/// One hour, in milliseconds — the `ttlMs` freshness hint on the
/// cacheable results (`tools/list`, `prompts/list`,
/// `resources/list`, `resources/templates/list`, `resources/read`).
/// The catalogue is fixed per binary, so any bound would do; an
/// hour keeps re-listing cheap without making a stale cache
/// survive a server upgrade for long.
const CACHE_TTL_MS: u64 = 3_600_000;

/// JSON-RPC 2.0 request envelope. Method-specific parameters live
/// in [`JsonValue`] to keep parsing flexible across the few methods
/// the MCP spec asks of a server.
#[derive(Debug, Deserialize)]
pub struct Request {
    /// JSON-RPC version. MCP requires `"2.0"`.
    pub jsonrpc: String,
    /// Method name, e.g. `tools/call`. Notifications have no `id`.
    pub method: String,
    /// Method parameters. Shape depends on `method`.
    #[serde(default)]
    pub params: JsonValue,
    /// Request id; absent on notifications.
    pub id: Option<JsonValue>,
}

/// JSON-RPC 2.0 success response envelope.
#[derive(Debug, Serialize)]
pub struct Response {
    /// Always `"2.0"`.
    pub jsonrpc: &'static str,
    /// The result payload.
    pub result: JsonValue,
    /// Echo of the corresponding request's id.
    pub id: JsonValue,
}

/// JSON-RPC 2.0 error envelope.
#[derive(Debug, Serialize)]
pub struct ErrorResponse {
    /// Always `"2.0"`.
    pub jsonrpc: &'static str,
    /// Error payload.
    pub error: ErrorObject,
    /// Echo of the corresponding request's id.
    pub id: JsonValue,
}

/// JSON-RPC 2.0 error object.
#[derive(Debug, Serialize)]
pub struct ErrorObject {
    /// Numeric error code per JSON-RPC convention.
    pub code: i32,
    /// Human-readable message.
    pub message: String,
    /// Optional structured detail — e.g.
    /// `UnsupportedProtocolVersionError` carries
    /// `{"supported": […], "requested": "…"}` so a modern client
    /// can pick a mutually supported revision and retry.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<JsonValue>,
}

/// What the stdio loop should do with a parsed message — write a
/// reply on stdout, or stay silent (notifications never receive a
/// response).
#[derive(Debug, PartialEq, Eq)]
pub enum HandleOutcome {
    /// Send the wrapped JSON payload back on stdout.
    Reply(String),
    /// Notification — no reply expected.
    Silent,
}

/// Process one newline-delimited JSON-RPC message. The stdio loop
/// in `main` calls this per line; tests call it with crafted
/// strings.
///
/// # Examples
///
/// ```
/// use noyalib_mcp::{handle_message, HandleOutcome};
/// let req = r#"{"jsonrpc":"2.0","method":"ping","id":1}"#;
/// match handle_message(req) {
///     // Every result carries the 2026-07-28 envelope fields.
///     HandleOutcome::Reply(s) => assert!(s.contains("\"resultType\":\"complete\"")),
///     HandleOutcome::Silent => panic!("expected reply"),
/// }
/// ```
#[must_use]
pub fn handle_message(raw: &str) -> HandleOutcome {
    let req: Request = match serde_json::from_str(raw) {
        Ok(r) => r,
        Err(e) => {
            return HandleOutcome::Reply(error_str(
                JsonValue::Null,
                -32700,
                format!("parse error: {e}"),
            ));
        }
    };
    if req.jsonrpc != "2.0" {
        return HandleOutcome::Reply(error_str(
            req.id.unwrap_or(JsonValue::Null),
            -32600,
            "invalid request: jsonrpc must be \"2.0\"".to_string(),
        ));
    }
    // Notifications (no id) get processed but never replied to.
    let id = req.id.clone();

    // Modern (2026-07-28) clients declare their protocol revision on
    // every request in `_meta`. An unsupported declaration MUST be
    // answered with `UnsupportedProtocolVersionError` listing what
    // the server does speak; an absent one means a legacy client (or
    // a modern client relying on the inline-retry flow) and the
    // request proceeds.
    if let Some(requested) = req
        .params
        .get("_meta")
        .and_then(|m| m.get(META_PROTOCOL_VERSION_KEY))
        .and_then(JsonValue::as_str)
    {
        if !SUPPORTED_PROTOCOL_VERSIONS.contains(&requested) {
            return match id {
                None => HandleOutcome::Silent,
                Some(id) => HandleOutcome::Reply(
                    serde_json::to_string(&ErrorResponse {
                        jsonrpc: "2.0",
                        error: ErrorObject {
                            code: UNSUPPORTED_PROTOCOL_VERSION,
                            message: "Unsupported protocol version".to_string(),
                            data: Some(json!({
                                "supported": SUPPORTED_PROTOCOL_VERSIONS,
                                "requested": requested,
                            })),
                        },
                        id,
                    })
                    .expect("infallible serialise"),
                ),
            };
        }
    }

    let result = dispatch(&req.method, req.params);
    match (id, result) {
        (None, _) => HandleOutcome::Silent,
        (Some(id), Ok(value)) => HandleOutcome::Reply(
            serde_json::to_string(&Response {
                jsonrpc: "2.0",
                result: decorate_result(value),
                id,
            })
            .expect("infallible serialise"),
        ),
        (Some(id), Err((code, msg))) => HandleOutcome::Reply(error_str(id, code, msg)),
    }
}

/// Stamp the 2026-07-28 result envelope fields onto a dispatch
/// result: `resultType: "complete"` (required on every result since
/// SEP-2322; legacy clients ignore unknown fields, and clients MUST
/// read an absent field as `"complete"`, so stamping is always
/// safe) and the server's identity in `_meta` (a SHOULD). Non-object
/// results — the `null` a legacy `initialized` sent *with* an id
/// gets back — pass through untouched.
fn decorate_result(mut value: JsonValue) -> JsonValue {
    if let JsonValue::Object(map) = &mut value {
        let _ = map.entry("resultType").or_insert_with(|| json!("complete"));
        let meta = map.entry("_meta").or_insert_with(|| json!({}));
        if let Some(meta) = meta.as_object_mut() {
            let _ = meta.entry(META_SERVER_INFO_KEY).or_insert_with(|| {
                json!({
                    "name": "noyalib-mcp",
                    "version": env!("CARGO_PKG_VERSION"),
                })
            });
        }
    }
    value
}

/// MCP method dispatcher. Returns the `result` payload on success
/// or a `(code, message)` pair for the error envelope.
///
/// # Examples
///
/// ```
/// use noyalib_mcp::dispatch;
/// use serde_json::Value;
/// let v = dispatch("ping", Value::Null).unwrap();
/// assert!(v.is_object());
/// ```
pub fn dispatch(method: &str, params: JsonValue) -> Result<JsonValue, (i32, String)> {
    match method {
        // Legacy (handshake-era) lifecycle. A dual-era server keeps
        // answering `initialize`: the 2026-07-28 stateless flow never
        // sends it, so its presence identifies a legacy client.
        "initialize" => {
            // 2025-06-18 negotiation: echo the requested version when
            // this server supports it; otherwise answer with the
            // newest handshake-based revision it does support and
            // let the client decide. The previous implementation
            // hard-coded the answer and ignored the request.
            let requested = params.get("protocolVersion").and_then(JsonValue::as_str);
            let negotiated = match requested {
                Some(v) if SUPPORTED_PROTOCOL_VERSIONS.contains(&v) => v,
                _ => LEGACY_PROTOCOL_VERSION,
            };
            Ok(json!({
                "protocolVersion": negotiated,
                "serverInfo": {
                    "name": "noyalib-mcp",
                    "version": env!("CARGO_PKG_VERSION"),
                },
                "capabilities": {
                    "tools": {},
                    "prompts": {},
                    "resources": {}
                }
            }))
        }
        "initialized" | "notifications/initialized" => Ok(JsonValue::Null),
        // Modern (2026-07-28) discovery — MUST be implemented; also
        // the stdio backward-compatibility probe a dual-era client
        // sends before deciding which era this server belongs to.
        "server/discover" => Ok(json!({
            "supportedVersions": SUPPORTED_PROTOCOL_VERSIONS,
            "capabilities": {
                "tools": {},
                "prompts": {},
                "resources": {}
            },
            "instructions": "Read and edit YAML files losslessly: \
                             noyalib_get reads the value at a path, \
                             noyalib_set / noyalib_set_multidoc \
                             rewrite one value while preserving all \
                             comments and formatting.",
            "ttlMs": CACHE_TTL_MS,
            "cacheScope": "public",
        })),
        "tools/list" => Ok(json!({
            "tools": tools::descriptors(),
            "ttlMs": CACHE_TTL_MS,
            "cacheScope": "public",
        })),
        "tools/call" => tools::call(params),
        "prompts/list" => Ok(json!({
            "prompts": prompts::descriptors(),
            "ttlMs": CACHE_TTL_MS,
            "cacheScope": "public",
        })),
        "prompts/get" => prompts::get(params),
        "resources/list" => Ok(json!({
            "resources": resources::descriptors(),
            "ttlMs": CACHE_TTL_MS,
            "cacheScope": "public",
        })),
        "resources/templates/list" => Ok(json!({
            "resourceTemplates": resources::templates(),
            "ttlMs": CACHE_TTL_MS,
            "cacheScope": "public",
        })),
        "resources/read" => resources::read(params).map(|mut v| {
            // `resources/read` is cacheable too (SEP-2549); the
            // served documents are fixed per binary.
            if let JsonValue::Object(map) = &mut v {
                let _ = map.entry("ttlMs").or_insert_with(|| json!(CACHE_TTL_MS));
                let _ = map.entry("cacheScope").or_insert_with(|| json!("public"));
            }
            v
        }),
        // Removed in 2026-07-28 but kept for legacy clients
        // (implementation-defined methods are grandfathered; a
        // modern client simply never sends it).
        "ping" => Ok(JsonValue::Object(serde_json::Map::new())),
        other => Err((-32601, format!("method not found: {other}"))),
    }
}

/// Render a JSON-RPC error envelope to a single line string.
///
/// # Examples
///
/// ```
/// use noyalib_mcp::error_str;
/// use serde_json::json;
/// let s = error_str(json!(1), -32601, "method not found".into());
/// assert!(s.contains("\"code\":-32601"));
/// ```
pub fn error_str(id: JsonValue, code: i32, message: String) -> String {
    serde_json::to_string(&ErrorResponse {
        jsonrpc: "2.0",
        error: ErrorObject {
            code,
            message,
            data: None,
        },
        id,
    })
    .expect("infallible serialise")
}

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

    fn parse_reply(out: HandleOutcome) -> JsonValue {
        match out {
            HandleOutcome::Reply(s) => serde_json::from_str(&s).unwrap(),
            HandleOutcome::Silent => panic!("expected Reply, got Silent"),
        }
    }

    // ── handle_message ─────────────────────────────────────────────────

    #[test]
    fn handle_message_returns_parse_error_on_bad_json() {
        let out = handle_message("not json {");
        let v = parse_reply(out);
        assert_eq!(v["error"]["code"].as_i64().unwrap(), -32700);
        assert!(
            v["error"]["message"]
                .as_str()
                .unwrap()
                .contains("parse error")
        );
        // Per JSON-RPC: parse errors carry `id: null`.
        assert!(v["id"].is_null());
    }

    #[test]
    fn handle_message_rejects_non_2_0_jsonrpc() {
        let req = json!({"jsonrpc": "1.0", "method": "ping", "id": 1});
        let out = handle_message(&req.to_string());
        let v = parse_reply(out);
        assert_eq!(v["error"]["code"].as_i64().unwrap(), -32600);
        assert_eq!(v["id"].as_i64().unwrap(), 1);
    }

    #[test]
    fn handle_message_returns_silent_for_notifications() {
        let req = json!({"jsonrpc": "2.0", "method": "ping"});
        let out = handle_message(&req.to_string());
        assert_eq!(out, HandleOutcome::Silent);
    }

    #[test]
    fn handle_message_returns_silent_for_notifications_initialized() {
        let req = json!({"jsonrpc": "2.0", "method": "notifications/initialized"});
        let out = handle_message(&req.to_string());
        assert_eq!(out, HandleOutcome::Silent);
    }

    #[test]
    fn handle_message_returns_unknown_method_error() {
        let req = json!({"jsonrpc": "2.0", "method": "frobnicate", "id": 7});
        let out = handle_message(&req.to_string());
        let v = parse_reply(out);
        assert_eq!(v["error"]["code"].as_i64().unwrap(), -32601);
        assert!(
            v["error"]["message"]
                .as_str()
                .unwrap()
                .contains("frobnicate")
        );
        assert_eq!(v["id"].as_i64().unwrap(), 7);
    }

    #[test]
    fn handle_message_returns_jsonrpc_error_when_jsonrpc_field_missing() {
        let req = json!({"method": "ping", "id": 1});
        let out = handle_message(&req.to_string());
        let v = parse_reply(out);
        // Either parse error (missing field) or invalid request — both
        // are valid envelopes; the contract is "you get an error".
        assert!(v["error"].is_object());
    }

    // ── dispatch ──────────────────────────────────────────────────────

    #[test]
    fn dispatch_initialize_returns_protocol_metadata() {
        let v = dispatch("initialize", JsonValue::Null).unwrap();
        assert_eq!(v["protocolVersion"].as_str().unwrap(), "2025-06-18");
        assert_eq!(v["serverInfo"]["name"].as_str().unwrap(), "noyalib-mcp");
        assert!(v["capabilities"]["tools"].is_object());
        assert!(v["capabilities"]["prompts"].is_object());
        assert!(v["capabilities"]["resources"].is_object());
    }

    // ── dual-era protocol (2026-07-28) ────────────────────────────────

    #[test]
    fn initialize_echoes_a_supported_requested_version() {
        for v in SUPPORTED_PROTOCOL_VERSIONS {
            let r = dispatch("initialize", json!({"protocolVersion": v})).unwrap();
            assert_eq!(r["protocolVersion"].as_str().unwrap(), v, "requested {v}");
        }
    }

    #[test]
    fn initialize_answers_legacy_for_an_unknown_version() {
        // Per the 2025-06-18 negotiation rules the server responds
        // with a version it does support; the client then decides.
        // The previous implementation ignored the request entirely.
        let r = dispatch("initialize", json!({"protocolVersion": "2024-11-05"})).unwrap();
        assert_eq!(
            r["protocolVersion"].as_str().unwrap(),
            LEGACY_PROTOCOL_VERSION
        );
    }

    #[test]
    fn server_discover_lists_versions_and_capabilities() {
        let v = dispatch("server/discover", JsonValue::Null).unwrap();
        let versions: Vec<&str> = v["supportedVersions"]
            .as_array()
            .unwrap()
            .iter()
            .map(|s| s.as_str().unwrap())
            .collect();
        assert_eq!(versions, SUPPORTED_PROTOCOL_VERSIONS);
        assert!(v["capabilities"]["tools"].is_object());
        assert!(v["ttlMs"].is_u64());
        assert_eq!(v["cacheScope"].as_str().unwrap(), "public");
    }

    #[test]
    fn results_carry_the_modern_envelope_fields() {
        let req = json!({"jsonrpc": "2.0", "method": "tools/list", "id": 7});
        let v = parse_reply(handle_message(&req.to_string()));
        assert_eq!(v["result"]["resultType"].as_str().unwrap(), "complete");
        assert_eq!(
            v["result"]["_meta"][META_SERVER_INFO_KEY]["name"]
                .as_str()
                .unwrap(),
            "noyalib-mcp"
        );
        assert!(v["result"]["ttlMs"].is_u64());
        assert_eq!(v["result"]["cacheScope"].as_str().unwrap(), "public");
    }

    #[test]
    fn a_supported_meta_version_is_served() {
        let req = json!({
            "jsonrpc": "2.0",
            "method": "tools/list",
            "id": 8,
            "params": {"_meta": {META_PROTOCOL_VERSION_KEY: "2026-07-28"}},
        });
        let v = parse_reply(handle_message(&req.to_string()));
        assert!(v["result"]["tools"].is_array());
    }

    #[test]
    fn an_unsupported_meta_version_is_refused_with_the_supported_list() {
        let req = json!({
            "jsonrpc": "2.0",
            "method": "tools/list",
            "id": 9,
            "params": {"_meta": {META_PROTOCOL_VERSION_KEY: "1900-01-01"}},
        });
        let v = parse_reply(handle_message(&req.to_string()));
        assert_eq!(
            v["error"]["code"].as_i64().unwrap(),
            i64::from(UNSUPPORTED_PROTOCOL_VERSION)
        );
        assert_eq!(v["error"]["data"]["requested"], "1900-01-01");
        let supported = v["error"]["data"]["supported"].as_array().unwrap();
        assert_eq!(supported.len(), SUPPORTED_PROTOCOL_VERSIONS.len());
    }

    #[test]
    fn resources_read_is_cacheable() {
        let v = dispatch("resources/read", json!({"uri": "noyalib://tools"})).unwrap();
        assert!(v["ttlMs"].is_u64());
        assert_eq!(v["cacheScope"].as_str().unwrap(), "public");
    }

    #[test]
    fn dispatch_prompts_list_returns_prompt_array() {
        let v = dispatch("prompts/list", JsonValue::Null).unwrap();
        let prompts = v["prompts"].as_array().unwrap();
        assert!(prompts.iter().any(|p| p["name"] == "format_and_lint_yaml"));
    }

    #[test]
    fn dispatch_prompts_get_returns_messages() {
        let v = dispatch("prompts/get", json!({"name": "format_and_lint_yaml"})).unwrap();
        assert!(v["messages"].as_array().unwrap().len() == 1);
    }

    #[test]
    fn dispatch_resources_list_returns_resource_array() {
        let v = dispatch("resources/list", JsonValue::Null).unwrap();
        let resources = v["resources"].as_array().unwrap();
        assert!(resources.iter().any(|r| r["uri"] == "noyalib://tools"));
    }

    #[test]
    fn dispatch_resources_templates_list_returns_templates() {
        let v = dispatch("resources/templates/list", JsonValue::Null).unwrap();
        let templates = v["resourceTemplates"].as_array().unwrap();
        assert!(
            templates
                .iter()
                .any(|t| t["uriTemplate"] == "noyalib://tool/{name}")
        );
    }

    #[test]
    fn dispatch_resources_read_returns_contents() {
        let v = dispatch("resources/read", json!({"uri": "noyalib://error-codes"})).unwrap();
        assert!(v["contents"].as_array().unwrap().len() == 1);
    }

    #[test]
    fn dispatch_initialized_returns_null() {
        let v = dispatch("initialized", JsonValue::Null).unwrap();
        assert!(v.is_null());
    }

    #[test]
    fn dispatch_notifications_initialized_returns_null() {
        let v = dispatch("notifications/initialized", JsonValue::Null).unwrap();
        assert!(v.is_null());
    }

    #[test]
    fn dispatch_tools_list_returns_descriptor_array() {
        let v = dispatch("tools/list", JsonValue::Null).unwrap();
        let tools = v["tools"].as_array().unwrap();
        assert!(tools.iter().any(|t| t["name"] == "noyalib_get"));
        assert!(tools.iter().any(|t| t["name"] == "noyalib_set"));
    }

    #[test]
    fn dispatch_ping_returns_empty_object() {
        let v = dispatch("ping", JsonValue::Null).unwrap();
        assert!(v.is_object());
        assert!(v.as_object().unwrap().is_empty());
    }

    #[test]
    fn dispatch_unknown_method_returns_method_not_found() {
        let err = dispatch("frobnicate", JsonValue::Null).unwrap_err();
        assert_eq!(err.0, -32601);
        assert!(err.1.contains("frobnicate"));
    }

    #[test]
    fn dispatch_tools_call_propagates_tools_errors() {
        // Missing `name` argument — tools::call returns -32602.
        let err = dispatch("tools/call", json!({})).unwrap_err();
        assert_eq!(err.0, -32602);
    }

    // ── error_str ─────────────────────────────────────────────────────

    #[test]
    fn error_str_renders_canonical_envelope() {
        let s = error_str(json!(42), -32000, "boom".into());
        let v: JsonValue = serde_json::from_str(&s).unwrap();
        assert_eq!(v["jsonrpc"].as_str().unwrap(), "2.0");
        assert_eq!(v["id"].as_i64().unwrap(), 42);
        assert_eq!(v["error"]["code"].as_i64().unwrap(), -32000);
        assert_eq!(v["error"]["message"].as_str().unwrap(), "boom");
    }

    #[test]
    fn error_str_handles_null_id() {
        let s = error_str(JsonValue::Null, -32700, "parse".into());
        let v: JsonValue = serde_json::from_str(&s).unwrap();
        assert!(v["id"].is_null());
    }
}