mcport 0.6.1

Fast, bounded, Tokio-free MCP stdio runtime with FastMCP-like Rust tools
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
743
744
745
746
747
748
749
750
751
752
753
use crate::{
    schema, serve, serve_streams, serve_streams_with_config, serve_streams_with_limits,
    serve_with_config, serve_with_limits, PreparedTool, SchemaDefect, ServerIdentity,
    ToolDefinition, ToolReply, ToolServer, TransportConfig, TransportLimits,
};
use blazingly_json::{from_str, Map, RawJson, RawValue, Value};
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::io::{self, BufRead, Write};

type ValueHandler<S> = Box<dyn FnMut(&mut S, Value) -> ToolReply>;
type RawHandler<S> = Box<dyn FnMut(&mut S, &str) -> ToolReply>;

enum Handler<S> {
    Value(ValueHandler<S>),
    Raw(RawHandler<S>),
}

/// Builder-style MCP server for applications that do not need a custom
/// [`ToolServer`] implementation.
pub struct McpServer<S = ()> {
    identity: ServerIdentity,
    state: S,
    catalog: Value,
    catalog_raw: Option<Box<RawValue>>,
    tool_page_size: Option<usize>,
    tools: HashMap<String, Handler<S>>,
    schema_defects: Vec<SchemaDefect>,
    registration_errors: Vec<String>,
    strict_schemas: bool,
}

impl McpServer<()> {
    /// Creates a stateless server.
    #[must_use]
    pub fn new(name: impl Into<String>, version: impl Into<String>) -> Self {
        Self::from_identity(ServerIdentity::new(name, version, ""))
    }

    /// Creates a stateless server from a complete identity.
    #[must_use]
    pub fn from_identity(identity: ServerIdentity) -> Self {
        Self::with_identity_and_state(identity, ())
    }

    /// Registers a stateless tool that receives an owned JSON value.
    #[must_use]
    pub fn tool(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: Value,
        mut handler: impl FnMut(Value) -> ToolReply + 'static,
    ) -> Self {
        self.tool_with_state(name, description, input_schema, move |(), arguments| {
            handler(arguments)
        })
    }

    /// Registers a stateless tool that receives validated raw JSON.
    #[must_use]
    pub fn raw_tool(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: Value,
        mut handler: impl FnMut(&str) -> ToolReply + 'static,
    ) -> Self {
        self.raw_tool_with_state(name, description, input_schema, move |(), arguments| {
            handler(arguments)
        })
    }

    /// Registers a stateless typed tool without constructing an intermediate
    /// JSON DOM for its arguments.
    #[must_use]
    pub fn typed_tool<I>(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: Value,
        mut handler: impl FnMut(I) -> ToolReply + 'static,
    ) -> Self
    where
        I: DeserializeOwned + 'static,
    {
        self.typed_tool_with_state(name, description, input_schema, move |(), arguments| {
            handler(arguments)
        })
    }
}

impl<S> McpServer<S> {
    /// Creates a server whose tool handlers share mutable state.
    #[must_use]
    pub fn with_state(name: impl Into<String>, version: impl Into<String>, state: S) -> Self {
        Self::with_identity_and_state(ServerIdentity::new(name, version, ""), state)
    }

    /// Creates a stateful server from a complete identity.
    #[must_use]
    pub fn with_identity_and_state(identity: ServerIdentity, state: S) -> Self {
        Self {
            identity,
            state,
            catalog: Value::Array(Vec::new()),
            catalog_raw: None,
            tool_page_size: None,
            tools: HashMap::new(),
            schema_defects: Vec::new(),
            registration_errors: Vec::new(),
            strict_schemas: false,
        }
    }

    /// Sets the instructions reported during initialization.
    #[must_use]
    pub fn instructions(mut self, instructions: impl Into<String>) -> Self {
        self.identity.instructions = instructions.into();
        self
    }

    /// Enables cursor pagination for `tools/list`.
    #[must_use]
    pub fn tool_page_size(mut self, page_size: usize) -> Self {
        self.tool_page_size = Some(page_size.max(1));
        self
    }

    /// Refuses to serve a catalog whose advertised schemas do not describe
    /// what their tools accept.
    ///
    /// Registration stays infallible, so this cannot break an existing
    /// builder chain. The `serve*` methods return
    /// [`io::ErrorKind::InvalidInput`] listing every defect, which fails at
    /// startup rather than letting a client discover the gaps one rejected
    /// call at a time. See [`crate::validate_tool_schema`] for the rules.
    #[must_use]
    pub fn strict_schemas(mut self) -> Self {
        self.strict_schemas = true;
        self
    }

    /// Advertised schemas that do not describe what their tool accepts.
    ///
    /// Always populated, so a server can assert on it in its own tests
    /// without opting into [`McpServer::strict_schemas`].
    #[must_use]
    pub fn schema_defects(&self) -> &[SchemaDefect] {
        &self.schema_defects
    }

    /// Registers a tool with access to shared state and owned JSON arguments.
    #[must_use]
    pub fn tool_with_state(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: Value,
        handler: impl FnMut(&mut S, Value) -> ToolReply + 'static,
    ) -> Self {
        let name = name.into();
        self.register_descriptor(ToolDefinition::new(name.clone(), description, input_schema));
        self.tools.insert(name, Handler::Value(Box::new(handler)));
        self
    }

    /// Registers a tool with access to shared state and validated raw JSON.
    #[must_use]
    pub fn raw_tool_with_state(
        mut self,
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: Value,
        handler: impl FnMut(&mut S, &str) -> ToolReply + 'static,
    ) -> Self {
        let name = name.into();
        self.register_descriptor(ToolDefinition::new(name.clone(), description, input_schema));
        self.tools.insert(name, Handler::Raw(Box::new(handler)));
        self
    }

    /// Registers a typed tool with shared state and no intermediate JSON DOM.
    #[must_use]
    pub fn typed_tool_with_state<I>(
        self,
        name: impl Into<String>,
        description: impl Into<String>,
        input_schema: Value,
        mut handler: impl FnMut(&mut S, I) -> ToolReply + 'static,
    ) -> Self
    where
        I: DeserializeOwned + 'static,
    {
        let name = name.into();
        let error_name = name.clone();
        self.raw_tool_with_state(name, description, input_schema, move |state, arguments| {
            match from_str::<I>(arguments) {
                Ok(arguments) => handler(state, arguments),
                Err(error) => {
                    ToolReply::error(format!("invalid arguments for {error_name}: {error}"))
                }
            }
        })
    }

    /// Registers prepared tools generated by `#[tool]` / `tools!`.
    ///
    /// Duplicate names from this path are recorded and refuse startup. The
    /// older `.tool(...)` methods still replace an existing name.
    #[must_use]
    pub fn tools(mut self, tools: impl IntoIterator<Item = PreparedTool<S>>) -> Self
    where
        S: 'static,
    {
        for tool in tools {
            self = self.register_prepared(tool);
        }
        self
    }

    /// Registers one complete descriptor plus handler without a generated adapter.
    #[must_use]
    pub fn define_tool(
        self,
        definition: ToolDefinition,
        handler: impl FnMut(&mut S, Value) -> ToolReply + 'static,
    ) -> Self {
        let mut server = self;
        let name = definition.name.clone();
        server.register_descriptor(definition);
        server.tools.insert(name, Handler::Value(Box::new(handler)));
        server
    }

    fn register_prepared(mut self, tool: PreparedTool<S>) -> Self
    where
        S: 'static,
    {
        let name = tool.definition.name.clone();
        if !tool.replace && self.tools.contains_key(&name) {
            self.registration_errors
                .push(format!("duplicate tool name '{name}'"));
            return self;
        }
        let PreparedTool {
            definition, invoke, ..
        } = tool;
        self.register_descriptor(definition);
        self.tools.insert(
            name,
            Handler::Raw(Box::new(move |state, arguments| match invoke {
                crate::tool::PreparedInvoke::Stateless(invoke) => invoke(arguments),
                crate::tool::PreparedInvoke::Stateful(invoke) => invoke(state, arguments),
            })),
        );
        self
    }

    /// Returns shared state.
    #[must_use]
    pub const fn state(&self) -> &S {
        &self.state
    }

    /// Returns mutable shared state.
    #[must_use]
    pub fn state_mut(&mut self) -> &mut S {
        &mut self.state
    }

    /// Runs the server over process stdin/stdout until EOF.
    ///
    /// # Errors
    ///
    /// Returns only stdio failures.
    pub fn serve(&mut self) -> io::Result<()> {
        serve(self)
    }

    /// Runs the server with explicit request and response byte budgets.
    ///
    /// # Errors
    ///
    /// Returns stdio failures or an invalid limits configuration.
    pub fn serve_with_limits(&mut self, limits: TransportLimits) -> io::Result<()> {
        serve_with_limits(self, limits)
    }

    /// Runs the server with complete byte and flush policy.
    ///
    /// # Errors
    ///
    /// Returns transport or configuration failures.
    pub fn serve_with_config(&mut self, config: TransportConfig) -> io::Result<()> {
        serve_with_config(self, config)
    }

    /// Runs the server over injectable streams until EOF.
    ///
    /// # Errors
    ///
    /// Returns only stream failures.
    pub fn serve_streams(&mut self, reader: impl BufRead, writer: impl Write) -> io::Result<()> {
        serve_streams(self, reader, writer)
    }

    /// Runs the server over injectable streams with explicit byte budgets.
    ///
    /// # Errors
    ///
    /// Returns stream failures or an invalid limits configuration.
    pub fn serve_streams_with_limits(
        &mut self,
        reader: impl BufRead,
        writer: impl Write,
        limits: TransportLimits,
    ) -> io::Result<()> {
        serve_streams_with_limits(self, reader, writer, limits)
    }

    /// Runs the server over injectable streams with complete transport policy.
    ///
    /// # Errors
    ///
    /// Returns transport or configuration failures.
    pub fn serve_streams_with_config(
        &mut self,
        reader: impl BufRead,
        writer: impl Write,
        config: TransportConfig,
    ) -> io::Result<()> {
        serve_streams_with_config(self, reader, writer, config)
    }

    fn register_descriptor(&mut self, definition: ToolDefinition) {
        let name = definition.name;
        // A re-registration replaces the descriptor, so its defects go with it.
        self.schema_defects
            .retain(|defect| defect.tool.as_deref() != Some(name.as_str()));
        self.schema_defects
            .extend(schema::defects_in(Some(&name), &definition.input_schema));

        let mut descriptor = Map::new();
        descriptor.insert("name".to_owned(), Value::String(name.clone()));
        descriptor.insert(
            "description".to_owned(),
            Value::String(definition.description),
        );
        if let Some(title) = definition.title {
            descriptor.insert("title".to_owned(), Value::String(title));
        }
        descriptor.insert("inputSchema".to_owned(), definition.input_schema);
        if let Some(output_schema) = definition.output_schema {
            descriptor.insert("outputSchema".to_owned(), output_schema);
        }
        if let Some(annotations) = definition.annotations.to_value() {
            descriptor.insert("annotations".to_owned(), annotations);
        }
        let descriptor = Value::Object(descriptor);

        let Value::Array(catalog) = &mut self.catalog else {
            unreachable!("builder catalog is always an array");
        };
        if let Some(existing) = catalog
            .iter_mut()
            .find(|tool| tool.get("name").and_then(Value::as_str) == Some(name.as_str()))
        {
            *existing = descriptor;
        } else {
            catalog.push(descriptor);
        }
        self.catalog_raw = None;
    }

    fn call_handler(&mut self, name: &str, arguments: Value) -> ToolReply {
        let Some(handler) = self.tools.get_mut(name) else {
            return ToolReply::error(format!("unknown tool: {name}"));
        };
        match handler {
            Handler::Value(handler) => handler(&mut self.state, arguments),
            Handler::Raw(handler) => match blazingly_json::to_string(&arguments) {
                Ok(arguments) => handler(&mut self.state, &arguments),
                Err(error) => ToolReply::error(format!("invalid arguments for {name}: {error}")),
            },
        }
    }

    fn call_raw_handler(&mut self, name: &str, arguments: RawJson<'_>) -> ToolReply {
        let Some(handler) = self.tools.get_mut(name) else {
            return ToolReply::error(format!("unknown tool: {name}"));
        };
        match handler {
            Handler::Value(handler) => match arguments.deserialize::<Value>() {
                Ok(arguments) => handler(&mut self.state, arguments),
                Err(error) => ToolReply::error(format!("invalid arguments for {name}: {error}")),
            },
            Handler::Raw(handler) => handler(&mut self.state, arguments.get()),
        }
    }
}

impl<S> ToolServer for McpServer<S> {
    fn identity(&self) -> ServerIdentity {
        self.identity.clone()
    }

    fn identity_ref(&self) -> Option<&ServerIdentity> {
        Some(&self.identity)
    }

    fn catalog(&mut self) -> Value {
        self.catalog.clone()
    }

    fn catalog_ref(&mut self) -> Option<&Value> {
        Some(&self.catalog)
    }

    fn catalog_raw_ref(&mut self) -> Option<&RawValue> {
        if self.catalog_raw.is_none() {
            self.catalog_raw = blazingly_json::to_raw_value(&self.catalog).ok();
        }
        self.catalog_raw.as_deref()
    }

    fn registration_errors(&self) -> &[String] {
        &self.registration_errors
    }

    fn catalog_is_paginated(&self) -> bool {
        self.tool_page_size.is_some()
    }

    fn catalog_page(&mut self, cursor: Option<&str>) -> Result<crate::ToolPage, String> {
        paginate_catalog(&self.catalog, self.tool_page_size, cursor)
    }

    fn has_tool(&self, name: &str) -> Option<bool> {
        Some(self.tools.contains_key(name))
    }

    fn strict_schema_defects(&self) -> &[SchemaDefect] {
        if self.strict_schemas {
            &self.schema_defects
        } else {
            &[]
        }
    }

    fn call(&mut self, name: &str, arguments: Value) -> ToolReply {
        self.call_handler(name, arguments)
    }

    fn call_raw(&mut self, name: &str, arguments: RawJson<'_>) -> ToolReply {
        self.call_raw_handler(name, arguments)
    }
}

pub(crate) fn paginate_catalog(
    catalog: &Value,
    page_size: Option<usize>,
    cursor: Option<&str>,
) -> Result<crate::ToolPage, String> {
    let Value::Array(tools) = catalog else {
        return Err("tool catalog must be an array".to_owned());
    };
    let Some(page_size) = page_size else {
        if cursor.is_some() {
            return Err("invalid tools/list cursor".to_owned());
        }
        return Ok(crate::ToolPage::complete(catalog.clone()));
    };
    let offset = match cursor {
        None => 0,
        Some(cursor) => cursor
            .strip_prefix("mcport:")
            .and_then(|offset| offset.parse::<usize>().ok())
            .filter(|offset| *offset < tools.len())
            .ok_or_else(|| "invalid tools/list cursor".to_owned())?,
    };
    let end = offset.saturating_add(page_size).min(tools.len());
    let next_cursor = (end < tools.len()).then(|| format!("mcport:{end}"));
    Ok(crate::ToolPage {
        tools: Value::Array(tools[offset..end].to_vec()),
        next_cursor,
    })
}

#[cfg(test)]
mod tests {
    use super::McpServer;
    use crate::{json, serve_message, SchemaDefectKind, ToolReply, ToolServer, Value};
    use serde::Deserialize;

    #[derive(Deserialize)]
    struct Increment {
        amount: u64,
    }

    #[test]
    fn builder_registers_owned_raw_typed_and_stateful_tools() {
        let mut stateless = McpServer::new("demo", "1.0.0")
            .instructions("Demo.")
            .tool(
                "echo",
                "Echo.",
                json!({"type": "object"}),
                ToolReply::structured,
            )
            .raw_tool("raw", "Raw.", json!({"type": "object"}), |raw| {
                ToolReply::text(json!({"length": raw.len()}))
            });
        assert_eq!(stateless.catalog().as_array().map(Vec::len), Some(2));
        assert!(matches!(
            stateless.call("echo", json!({"ok": true})),
            ToolReply::Serialized { .. }
        ));

        let mut stateful = McpServer::with_state("count", "1.0.0", 0_u64).typed_tool_with_state(
            "increment",
            "Increment.",
            json!({
                "type": "object",
                "properties": {"amount": {"type": "integer"}},
                "required": ["amount"]
            }),
            |count, arguments: Increment| {
                *count += arguments.amount;
                ToolReply::structured(json!({"count": *count}))
            },
        );
        let reply = stateful.call("increment", json!({"amount": 3}));
        let ToolReply::Serialized { value, .. } = reply else {
            panic!("increment must succeed");
        };
        let value = blazingly_json::from_str::<Value>(value.get()).unwrap();
        assert_eq!(value["count"], 3);
        assert_eq!(*stateful.state(), 3);

        let mut output = Vec::new();
        serve_message(
            &mut stateful,
            r#"{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"increment","arguments":{"amount":4}}}"#,
            &mut output,
        )
        .unwrap();
        let response = blazingly_json::from_slice::<Value>(&output).unwrap();
        assert_eq!(response["result"]["structuredContent"]["count"], 7);
        assert_eq!(*stateful.state(), 7);

        output.clear();
        serve_message(
            &mut stateful,
            r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"increment","arguments":{"amount":"wrong"}}}"#,
            &mut output,
        )
        .unwrap();
        let response = blazingly_json::from_slice::<Value>(&output).unwrap();
        assert_eq!(response["result"]["isError"], true);
    }

    #[test]
    fn duplicate_registration_replaces_descriptor_and_handler() {
        let mut server = McpServer::new("demo", "1.0.0")
            .tool("echo", "Old.", json!({"type": "null"}), |_| {
                ToolReply::text(json!("old"))
            })
            .tool("echo", "New.", json!({"type": "object"}), |_| {
                ToolReply::text(json!("new"))
            });

        let catalog = server.catalog();
        assert_eq!(catalog.as_array().map(Vec::len), Some(1));
        assert_eq!(catalog[0]["description"], "New.");
        let mut output = Vec::new();
        serve_message(
            &mut server,
            r#"{"jsonrpc":"2.0","id":7,"method":"tools/list"}"#,
            &mut output,
        )
        .unwrap();
        let listed = blazingly_json::from_slice::<Value>(&output).unwrap();
        assert_eq!(listed["result"]["tools"][0]["description"], "New.");
        let ToolReply::Serialized { value, .. } = server.call("echo", json!({})) else {
            panic!("echo must succeed");
        };
        assert_eq!(value.get(), r#""new""#);
    }

    #[test]
    fn builder_paginates_tools_with_opaque_cursors() {
        let mut server = McpServer::new("test", "1")
            .tool_page_size(1)
            .tool("first", "First.", json!({"type": "object"}), |_| {
                ToolReply::text("first")
            })
            .tool("second", "Second.", json!({"type": "object"}), |_| {
                ToolReply::text("second")
            });
        let mut first = Vec::new();
        serve_message(
            &mut server,
            r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#,
            &mut first,
        )
        .unwrap();
        let first = blazingly_json::from_slice::<Value>(&first).unwrap();
        assert_eq!(first["result"]["tools"].as_array().map(Vec::len), Some(1));
        assert_eq!(first["result"]["nextCursor"], "mcport:1");

        let mut second = Vec::new();
        serve_message(
            &mut server,
            r#"{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{"cursor":"mcport:1"}}"#,
            &mut second,
        )
        .unwrap();
        let second = blazingly_json::from_slice::<Value>(&second).unwrap();
        assert_eq!(second["result"]["tools"].as_array().map(Vec::len), Some(1));
        assert!(second["result"].get("nextCursor").is_none());

        let mut invalid = Vec::new();
        serve_message(
            &mut server,
            r#"{"jsonrpc":"2.0","id":3,"method":"tools/list","params":{"cursor":"bad"}}"#,
            &mut invalid,
        )
        .unwrap();
        let invalid = blazingly_json::from_slice::<Value>(&invalid).unwrap();
        assert_eq!(invalid["error"]["code"], -32_602);
    }

    #[test]
    fn strict_schemas_refuse_to_serve_an_undescribed_catalog() {
        let mut dishonest = McpServer::new("planner", "1.0.0")
            .strict_schemas()
            .typed_tool(
                "plan",
                "Plans.",
                json!({
                    "type": "object",
                    "properties": {"budget": {"type": "object"}}
                }),
                |_: Value| ToolReply::text("planned"),
            );
        assert_eq!(dishonest.schema_defects().len(), 1);
        assert_eq!(dishonest.schema_defects()[0].tool.as_deref(), Some("plan"));
        assert_eq!(
            dishonest.schema_defects()[0].kind,
            SchemaDefectKind::UndescribedObject
        );

        let error = dishonest
            .serve_streams(&b""[..], Vec::new())
            .expect_err("strict schemas must refuse the catalog");
        assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
        assert!(error.to_string().contains("/properties/budget"));

        // Re-registering the tool honestly clears the defect it introduced.
        let mut honest = dishonest.typed_tool(
            "plan",
            "Plans.",
            json!({
                "type": "object",
                "properties": {
                    "budget": {
                        "type": "object",
                        "properties": {"ceiling_cents": {"type": "integer"}}
                    }
                }
            }),
            |_: Value| ToolReply::text("planned"),
        );
        assert!(honest.schema_defects().is_empty());
        assert!(honest.serve_streams(&b""[..], Vec::new()).is_ok());
    }

    #[test]
    fn schema_defects_are_reported_without_opting_into_strict_schemas() {
        let mut lenient = McpServer::new("planner", "1.0.0").tool(
            "plan",
            "Plans.",
            json!({"type": "object", "properties": {"tags": {"type": "array"}}}),
            ToolReply::structured,
        );
        assert_eq!(
            lenient.schema_defects()[0].kind,
            SchemaDefectKind::UndescribedArray
        );
        assert!(
            lenient.serve_streams(&b""[..], Vec::new()).is_ok(),
            "existing servers keep serving until they opt in"
        );
    }

    #[test]
    fn literal_text_does_not_add_json_quotes() {
        let mut server = McpServer::new("demo", "1.0.0").tool(
            "hello",
            "Says hello.",
            json!({"type": "object", "properties": {}, "additionalProperties": false}),
            |_| ToolReply::literal_text("hello"),
        );
        let mut output = Vec::new();
        serve_message(
            &mut server,
            r#"{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"hello","arguments":{}}}"#,
            &mut output,
        )
        .unwrap();
        let response = blazingly_json::from_slice::<Value>(&output).unwrap();
        assert_eq!(response["result"]["content"][0]["text"], "hello");

        let quoted = ToolReply::text("hello");
        let ToolReply::Serialized { value, .. } = quoted else {
            panic!("text() must keep JSON serialization");
        };
        assert_eq!(value.get(), r#""hello""#);
    }

    #[test]
    fn unknown_tools_are_protocol_errors_but_handler_failures_are_tool_results() {
        let mut server = McpServer::new("demo", "1.0.0").tool(
            "fails",
            "Fails.",
            json!({"type": "object"}),
            |_| ToolReply::error("handler failed"),
        );

        let mut output = Vec::new();
        serve_message(
            &mut server,
            r#"{"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"missing"}}"#,
            &mut output,
        )
        .unwrap();
        let unknown = blazingly_json::from_slice::<Value>(&output).unwrap();
        assert_eq!(unknown["error"]["code"], -32_602);
        assert!(unknown.get("result").is_none());

        output.clear();
        serve_message(
            &mut server,
            r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"fails"}}"#,
            &mut output,
        )
        .unwrap();
        let failed = blazingly_json::from_slice::<Value>(&output).unwrap();
        assert_eq!(failed["result"]["isError"], true);
        assert!(failed.get("error").is_none());
    }
}