mcpkit-core 0.6.0

Core types and traits for the Model Context Protocol (MCP)
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
//! JSON Schema utilities for MCP type validation.
//!
//! This module provides utilities for working with JSON Schema, which is used
//! throughout the MCP protocol for defining tool input schemas, resource
//! schemas, and elicitation schemas.
//!
//! # Features
//!
//! - Schema building with a fluent API
//! - Common schema patterns (string, number, object, array)
//! - Schema validation helpers
//!
//! # Example
//!
//! ```rust
//! use mcpkit_core::schema::{SchemaBuilder, SchemaType};
//!
//! // Build a schema for a search tool input
//! let schema = SchemaBuilder::object()
//!     .property("query", SchemaBuilder::string().description("Search query"))
//!     .property("limit", SchemaBuilder::integer().minimum(1).maximum(100).default_value(10))
//!     .required(["query"])
//!     .build();
//! ```

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// JSON Schema types as defined by the JSON Schema specification.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SchemaType {
    /// A string value.
    String,
    /// A numeric value (integer or float).
    Number,
    /// An integer value.
    Integer,
    /// A boolean value.
    Boolean,
    /// An array value.
    Array,
    /// An object value.
    Object,
    /// A null value.
    Null,
}

impl std::fmt::Display for SchemaType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::String => write!(f, "string"),
            Self::Number => write!(f, "number"),
            Self::Integer => write!(f, "integer"),
            Self::Boolean => write!(f, "boolean"),
            Self::Array => write!(f, "array"),
            Self::Object => write!(f, "object"),
            Self::Null => write!(f, "null"),
        }
    }
}

/// A JSON Schema definition.
///
/// This struct represents a JSON Schema that can be used for validating
/// tool inputs, resource contents, or elicitation responses.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Schema {
    /// The schema type.
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub schema_type: Option<SchemaType>,

    /// Human-readable description.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,

    /// Human-readable title.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// Default value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<Value>,

    /// Enumerated allowed values.
    #[serde(rename = "enum", skip_serializing_if = "Option::is_none")]
    pub enum_values: Option<Vec<Value>>,

    /// Constant value (must be exactly this value).
    #[serde(rename = "const", skip_serializing_if = "Option::is_none")]
    pub const_value: Option<Value>,

    // String constraints
    /// Minimum string length.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_length: Option<u64>,

    /// Maximum string length.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_length: Option<u64>,

    /// Regex pattern for string validation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pattern: Option<String>,

    /// Format hint (e.g., "email", "uri", "date-time").
    #[serde(skip_serializing_if = "Option::is_none")]
    pub format: Option<String>,

    // Numeric constraints
    /// Minimum numeric value (inclusive).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub minimum: Option<f64>,

    /// Maximum numeric value (inclusive).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maximum: Option<f64>,

    /// Minimum numeric value (exclusive).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclusive_minimum: Option<f64>,

    /// Maximum numeric value (exclusive).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exclusive_maximum: Option<f64>,

    /// Value must be a multiple of this number.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub multiple_of: Option<f64>,

    // Array constraints
    /// Schema for array items.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub items: Option<Box<Schema>>,

    /// Minimum number of items.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_items: Option<u64>,

    /// Maximum number of items.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_items: Option<u64>,

    /// Whether items must be unique.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unique_items: Option<bool>,

    // Object constraints
    /// Property schemas for object type.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub properties: Option<HashMap<String, Schema>>,

    /// Required property names.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<Vec<String>>,

    /// Schema for additional properties.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub additional_properties: Option<AdditionalProperties>,

    /// Minimum number of properties.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub min_properties: Option<u64>,

    /// Maximum number of properties.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_properties: Option<u64>,

    // Composition
    /// All of these schemas must match.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub all_of: Option<Vec<Schema>>,

    /// Any of these schemas must match.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub any_of: Option<Vec<Schema>>,

    /// Exactly one of these schemas must match.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub one_of: Option<Vec<Schema>>,

    /// This schema must not match.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub not: Option<Box<Schema>>,
}

/// Represents the `additionalProperties` field which can be a boolean or a schema.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AdditionalProperties {
    /// Boolean: true allows any additional properties, false forbids them.
    Boolean(bool),
    /// Schema: additional properties must match this schema.
    Schema(Box<Schema>),
}

impl Schema {
    /// Create an empty schema (matches anything).
    #[must_use]
    pub fn any() -> Self {
        Self::default()
    }

    /// Create a string schema.
    #[must_use]
    pub fn string() -> Self {
        Self {
            schema_type: Some(SchemaType::String),
            ..Default::default()
        }
    }

    /// Create a number schema.
    #[must_use]
    pub fn number() -> Self {
        Self {
            schema_type: Some(SchemaType::Number),
            ..Default::default()
        }
    }

    /// Create an integer schema.
    #[must_use]
    pub fn integer() -> Self {
        Self {
            schema_type: Some(SchemaType::Integer),
            ..Default::default()
        }
    }

    /// Create a boolean schema.
    #[must_use]
    pub fn boolean() -> Self {
        Self {
            schema_type: Some(SchemaType::Boolean),
            ..Default::default()
        }
    }

    /// Create an array schema.
    #[must_use]
    pub fn array() -> Self {
        Self {
            schema_type: Some(SchemaType::Array),
            ..Default::default()
        }
    }

    /// Create an object schema.
    #[must_use]
    pub fn object() -> Self {
        Self {
            schema_type: Some(SchemaType::Object),
            ..Default::default()
        }
    }

    /// Create a null schema.
    #[must_use]
    pub fn null() -> Self {
        Self {
            schema_type: Some(SchemaType::Null),
            ..Default::default()
        }
    }

    /// Convert this schema to a JSON value.
    #[must_use]
    pub fn to_value(&self) -> Value {
        serde_json::to_value(self).unwrap_or_else(|_| Value::Object(serde_json::Map::new()))
    }
}

/// A fluent builder for constructing JSON Schemas.
///
/// # Example
///
/// ```rust
/// use mcpkit_core::schema::SchemaBuilder;
///
/// let schema = SchemaBuilder::object()
///     .title("SearchInput")
///     .description("Input for search tool")
///     .property("query", SchemaBuilder::string().min_length(1))
///     .property("page", SchemaBuilder::integer().minimum(1).default_value(1))
///     .required(["query"])
///     .build();
/// ```
#[derive(Debug, Clone, Default)]
pub struct SchemaBuilder {
    schema: Schema,
}

impl SchemaBuilder {
    /// Create a new schema builder.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a string schema builder.
    #[must_use]
    pub fn string() -> Self {
        Self {
            schema: Schema::string(),
        }
    }

    /// Create a number schema builder.
    #[must_use]
    pub fn number() -> Self {
        Self {
            schema: Schema::number(),
        }
    }

    /// Create an integer schema builder.
    #[must_use]
    pub fn integer() -> Self {
        Self {
            schema: Schema::integer(),
        }
    }

    /// Create a boolean schema builder.
    #[must_use]
    pub fn boolean() -> Self {
        Self {
            schema: Schema::boolean(),
        }
    }

    /// Create an array schema builder.
    #[must_use]
    pub fn array() -> Self {
        Self {
            schema: Schema::array(),
        }
    }

    /// Create an object schema builder.
    #[must_use]
    pub fn object() -> Self {
        Self {
            schema: Schema::object(),
        }
    }

    /// Create a null schema builder.
    #[must_use]
    pub fn null() -> Self {
        Self {
            schema: Schema::null(),
        }
    }

    /// Set the schema title.
    #[must_use]
    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.schema.title = Some(title.into());
        self
    }

    /// Set the schema description.
    #[must_use]
    pub fn description(mut self, description: impl Into<String>) -> Self {
        self.schema.description = Some(description.into());
        self
    }

    /// Set the default value.
    #[must_use]
    pub fn default_value(mut self, default: impl Into<Value>) -> Self {
        self.schema.default = Some(default.into());
        self
    }

    /// Set enumerated allowed values.
    #[must_use]
    pub fn enum_values<I, V>(mut self, values: I) -> Self
    where
        I: IntoIterator<Item = V>,
        V: Into<Value>,
    {
        self.schema.enum_values = Some(values.into_iter().map(Into::into).collect());
        self
    }

    /// Set a constant value.
    #[must_use]
    pub fn const_value(mut self, value: impl Into<Value>) -> Self {
        self.schema.const_value = Some(value.into());
        self
    }

    // String constraints

    /// Set minimum string length.
    #[must_use]
    pub const fn min_length(mut self, min: u64) -> Self {
        self.schema.min_length = Some(min);
        self
    }

    /// Set maximum string length.
    #[must_use]
    pub const fn max_length(mut self, max: u64) -> Self {
        self.schema.max_length = Some(max);
        self
    }

    /// Set regex pattern.
    #[must_use]
    pub fn pattern(mut self, pattern: impl Into<String>) -> Self {
        self.schema.pattern = Some(pattern.into());
        self
    }

    /// Set string format hint.
    #[must_use]
    pub fn format(mut self, format: impl Into<String>) -> Self {
        self.schema.format = Some(format.into());
        self
    }

    // Numeric constraints

    /// Set minimum value (inclusive).
    #[must_use]
    pub fn minimum(mut self, min: impl Into<f64>) -> Self {
        self.schema.minimum = Some(min.into());
        self
    }

    /// Set maximum value (inclusive).
    #[must_use]
    pub fn maximum(mut self, max: impl Into<f64>) -> Self {
        self.schema.maximum = Some(max.into());
        self
    }

    /// Set exclusive minimum value.
    #[must_use]
    pub fn exclusive_minimum(mut self, min: impl Into<f64>) -> Self {
        self.schema.exclusive_minimum = Some(min.into());
        self
    }

    /// Set exclusive maximum value.
    #[must_use]
    pub fn exclusive_maximum(mut self, max: impl Into<f64>) -> Self {
        self.schema.exclusive_maximum = Some(max.into());
        self
    }

    /// Set multiple-of constraint.
    #[must_use]
    pub fn multiple_of(mut self, multiple: impl Into<f64>) -> Self {
        self.schema.multiple_of = Some(multiple.into());
        self
    }

    // Array constraints

    /// Set schema for array items.
    #[must_use]
    pub fn items(mut self, items: Self) -> Self {
        self.schema.items = Some(Box::new(items.schema));
        self
    }

    /// Set minimum number of items.
    #[must_use]
    pub const fn min_items(mut self, min: u64) -> Self {
        self.schema.min_items = Some(min);
        self
    }

    /// Set maximum number of items.
    #[must_use]
    pub const fn max_items(mut self, max: u64) -> Self {
        self.schema.max_items = Some(max);
        self
    }

    /// Require unique items.
    #[must_use]
    pub const fn unique_items(mut self, unique: bool) -> Self {
        self.schema.unique_items = Some(unique);
        self
    }

    // Object constraints

    /// Add a property to the schema.
    #[must_use]
    pub fn property(mut self, name: impl Into<String>, schema: Self) -> Self {
        let properties = self.schema.properties.get_or_insert_with(HashMap::new);
        properties.insert(name.into(), schema.schema);
        self
    }

    /// Set required properties.
    #[must_use]
    pub fn required<I, S>(mut self, required: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.schema.required = Some(required.into_iter().map(Into::into).collect());
        self
    }

    /// Set whether additional properties are allowed.
    #[must_use]
    pub fn additional_properties(mut self, allowed: bool) -> Self {
        self.schema.additional_properties = Some(AdditionalProperties::Boolean(allowed));
        self
    }

    /// Set schema for additional properties.
    #[must_use]
    pub fn additional_properties_schema(mut self, schema: Self) -> Self {
        self.schema.additional_properties =
            Some(AdditionalProperties::Schema(Box::new(schema.schema)));
        self
    }

    /// Set minimum number of properties.
    #[must_use]
    pub const fn min_properties(mut self, min: u64) -> Self {
        self.schema.min_properties = Some(min);
        self
    }

    /// Set maximum number of properties.
    #[must_use]
    pub const fn max_properties(mut self, max: u64) -> Self {
        self.schema.max_properties = Some(max);
        self
    }

    // Composition

    /// Require all of the given schemas to match.
    #[must_use]
    pub fn all_of<I>(mut self, schemas: I) -> Self
    where
        I: IntoIterator<Item = Self>,
    {
        self.schema.all_of = Some(schemas.into_iter().map(|b| b.schema).collect());
        self
    }

    /// Require any of the given schemas to match.
    #[must_use]
    pub fn any_of<I>(mut self, schemas: I) -> Self
    where
        I: IntoIterator<Item = Self>,
    {
        self.schema.any_of = Some(schemas.into_iter().map(|b| b.schema).collect());
        self
    }

    /// Require exactly one of the given schemas to match.
    #[must_use]
    pub fn one_of<I>(mut self, schemas: I) -> Self
    where
        I: IntoIterator<Item = Self>,
    {
        self.schema.one_of = Some(schemas.into_iter().map(|b| b.schema).collect());
        self
    }

    /// Require the given schema to not match.
    #[must_use]
    pub fn not(mut self, schema: Self) -> Self {
        self.schema.not = Some(Box::new(schema.schema));
        self
    }

    /// Build the schema.
    #[must_use]
    pub fn build(self) -> Schema {
        self.schema
    }

    /// Build and convert to a JSON value.
    #[must_use]
    pub fn to_value(self) -> Value {
        self.schema.to_value()
    }
}

/// Common string format hints.
pub mod formats {
    /// Email address format.
    pub const EMAIL: &str = "email";
    /// URI format.
    pub const URI: &str = "uri";
    /// URI reference format.
    pub const URI_REFERENCE: &str = "uri-reference";
    /// Date-time format (RFC 3339).
    pub const DATE_TIME: &str = "date-time";
    /// Date format.
    pub const DATE: &str = "date";
    /// Time format.
    pub const TIME: &str = "time";
    /// Duration format (ISO 8601).
    pub const DURATION: &str = "duration";
    /// UUID format.
    pub const UUID: &str = "uuid";
    /// Hostname format.
    pub const HOSTNAME: &str = "hostname";
    /// IPv4 address format.
    pub const IPV4: &str = "ipv4";
    /// IPv6 address format.
    pub const IPV6: &str = "ipv6";
    /// JSON Pointer format.
    pub const JSON_POINTER: &str = "json-pointer";
    /// Regex format.
    pub const REGEX: &str = "regex";
}

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

    #[test]
    fn test_string_schema() {
        let schema = SchemaBuilder::string()
            .description("A test string")
            .min_length(1)
            .max_length(100)
            .pattern(r"^[a-z]+$")
            .build();

        assert_eq!(schema.schema_type, Some(SchemaType::String));
        assert_eq!(schema.description.as_deref(), Some("A test string"));
        assert_eq!(schema.min_length, Some(1));
        assert_eq!(schema.max_length, Some(100));
    }

    #[test]
    fn test_number_schema() {
        let schema = SchemaBuilder::number().minimum(0).maximum(100).build();

        assert_eq!(schema.schema_type, Some(SchemaType::Number));
        assert_eq!(schema.minimum, Some(0.0));
        assert_eq!(schema.maximum, Some(100.0));
    }

    #[test]
    fn test_object_schema() {
        let schema = SchemaBuilder::object()
            .property("name", SchemaBuilder::string())
            .property("age", SchemaBuilder::integer().minimum(0))
            .required(["name"])
            .additional_properties(false)
            .build();

        assert_eq!(schema.schema_type, Some(SchemaType::Object));
        assert!(schema.properties.is_some());
        let props = schema.properties.as_ref().unwrap();
        assert!(props.contains_key("name"));
        assert!(props.contains_key("age"));
        assert_eq!(schema.required, Some(vec!["name".to_string()]));
    }

    #[test]
    fn test_array_schema() {
        let schema = SchemaBuilder::array()
            .items(SchemaBuilder::string())
            .min_items(1)
            .unique_items(true)
            .build();

        assert_eq!(schema.schema_type, Some(SchemaType::Array));
        assert!(schema.items.is_some());
        assert_eq!(schema.min_items, Some(1));
        assert_eq!(schema.unique_items, Some(true));
    }

    #[test]
    fn test_enum_schema() {
        let schema = SchemaBuilder::string()
            .enum_values(["red", "green", "blue"])
            .build();

        assert!(schema.enum_values.is_some());
        let values = schema.enum_values.as_ref().unwrap();
        assert_eq!(values.len(), 3);
    }

    #[test]
    fn test_composition() {
        let schema = SchemaBuilder::new()
            .one_of([SchemaBuilder::string(), SchemaBuilder::integer()])
            .build();

        assert!(schema.one_of.is_some());
        assert_eq!(schema.one_of.as_ref().unwrap().len(), 2);
    }

    #[test]
    fn test_to_value() -> Result<(), Box<dyn std::error::Error>> {
        let schema = SchemaBuilder::object()
            .property("query", SchemaBuilder::string())
            .required(["query"])
            .to_value();

        assert!(schema.is_object());
        let obj = schema.as_object().ok_or("Expected object")?;
        assert_eq!(obj.get("type").and_then(|v| v.as_str()), Some("object"));
        Ok(())
    }

    #[test]
    fn test_tool_input_schema() -> Result<(), Box<dyn std::error::Error>> {
        // Example: Schema for a search tool
        let schema = SchemaBuilder::object()
            .title("SearchInput")
            .description("Input parameters for the search tool")
            .property(
                "query",
                SchemaBuilder::string()
                    .description("The search query")
                    .min_length(1),
            )
            .property(
                "limit",
                SchemaBuilder::integer()
                    .description("Maximum number of results")
                    .minimum(1)
                    .maximum(100)
                    .default_value(10),
            )
            .property(
                "filters",
                SchemaBuilder::array()
                    .items(SchemaBuilder::string())
                    .description("Optional filter tags"),
            )
            .required(["query"])
            .additional_properties(false)
            .build();

        let value = schema.to_value();
        assert!(value.is_object());

        // Verify structure
        let obj = value.as_object().ok_or("Expected object")?;
        assert_eq!(obj.get("type").and_then(|v| v.as_str()), Some("object"));
        assert_eq!(
            obj.get("title").and_then(|v| v.as_str()),
            Some("SearchInput")
        );
        Ok(())
    }

    #[test]
    fn test_schema_type_display() {
        assert_eq!(SchemaType::String.to_string(), "string");
        assert_eq!(SchemaType::Number.to_string(), "number");
        assert_eq!(SchemaType::Integer.to_string(), "integer");
        assert_eq!(SchemaType::Boolean.to_string(), "boolean");
        assert_eq!(SchemaType::Array.to_string(), "array");
        assert_eq!(SchemaType::Object.to_string(), "object");
        assert_eq!(SchemaType::Null.to_string(), "null");
    }

    #[test]
    fn test_schema_any() {
        let schema = Schema::any();
        assert!(schema.schema_type.is_none());
    }

    #[test]
    fn test_schema_null() {
        let schema = Schema::null();
        assert_eq!(schema.schema_type, Some(SchemaType::Null));
    }

    #[test]
    fn test_schema_boolean() {
        let schema = SchemaBuilder::boolean().build();
        assert_eq!(schema.schema_type, Some(SchemaType::Boolean));
    }

    #[test]
    fn test_string_format() {
        let schema = SchemaBuilder::string().format(formats::EMAIL).build();
        assert_eq!(schema.format, Some("email".to_string()));
    }

    #[test]
    fn test_exclusive_bounds() {
        let schema = SchemaBuilder::number()
            .exclusive_minimum(0)
            .exclusive_maximum(100)
            .build();

        assert_eq!(schema.exclusive_minimum, Some(0.0));
        assert_eq!(schema.exclusive_maximum, Some(100.0));
    }

    #[test]
    fn test_multiple_of() {
        let schema = SchemaBuilder::integer().multiple_of(5).build();

        assert_eq!(schema.multiple_of, Some(5.0));
    }

    #[test]
    fn test_const_value() {
        let schema = SchemaBuilder::string().const_value("constant").build();

        assert_eq!(
            schema.const_value,
            Some(Value::String("constant".to_string()))
        );
    }

    #[test]
    fn test_min_max_properties() {
        let schema = SchemaBuilder::object()
            .min_properties(1)
            .max_properties(10)
            .build();

        assert_eq!(schema.min_properties, Some(1));
        assert_eq!(schema.max_properties, Some(10));
    }

    #[test]
    fn test_max_items() {
        let schema = SchemaBuilder::array()
            .items(SchemaBuilder::string())
            .max_items(5)
            .build();

        assert_eq!(schema.max_items, Some(5));
    }

    #[test]
    fn test_additional_properties_schema() {
        let schema = SchemaBuilder::object()
            .additional_properties_schema(SchemaBuilder::string())
            .build();

        assert!(schema.additional_properties.is_some());
        match schema.additional_properties {
            Some(AdditionalProperties::Schema(s)) => {
                assert_eq!(s.schema_type, Some(SchemaType::String));
            }
            _ => panic!("Expected schema for additional properties"),
        }
    }

    #[test]
    fn test_all_of_composition() {
        let schema = SchemaBuilder::new()
            .all_of([
                SchemaBuilder::object().property("name", SchemaBuilder::string()),
                SchemaBuilder::object().property("age", SchemaBuilder::integer()),
            ])
            .build();

        assert!(schema.all_of.is_some());
        assert_eq!(schema.all_of.as_ref().unwrap().len(), 2);
    }

    #[test]
    fn test_any_of_composition() {
        let schema = SchemaBuilder::new()
            .any_of([SchemaBuilder::string(), SchemaBuilder::null()])
            .build();

        assert!(schema.any_of.is_some());
        assert_eq!(schema.any_of.as_ref().unwrap().len(), 2);
    }

    #[test]
    fn test_not_composition() {
        let schema = SchemaBuilder::new().not(SchemaBuilder::null()).build();

        assert!(schema.not.is_some());
        assert_eq!(
            schema.not.as_ref().unwrap().schema_type,
            Some(SchemaType::Null)
        );
    }

    #[test]
    fn test_integer_schema() {
        let schema = SchemaBuilder::integer().minimum(0).maximum(100).build();

        assert_eq!(schema.schema_type, Some(SchemaType::Integer));
    }

    #[test]
    fn test_schema_serialization() {
        let schema = SchemaBuilder::object()
            .property("id", SchemaBuilder::integer())
            .required(["id"])
            .build();

        let json = serde_json::to_string(&schema).unwrap();
        let parsed: Schema = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed.schema_type, Some(SchemaType::Object));
        assert!(parsed.properties.is_some());
        assert!(parsed.required.is_some());
    }

    #[test]
    fn test_formats_constants() {
        assert_eq!(formats::EMAIL, "email");
        assert_eq!(formats::URI, "uri");
        assert_eq!(formats::URI_REFERENCE, "uri-reference");
        assert_eq!(formats::DATE_TIME, "date-time");
        assert_eq!(formats::DATE, "date");
        assert_eq!(formats::TIME, "time");
        assert_eq!(formats::DURATION, "duration");
        assert_eq!(formats::UUID, "uuid");
        assert_eq!(formats::HOSTNAME, "hostname");
        assert_eq!(formats::IPV4, "ipv4");
        assert_eq!(formats::IPV6, "ipv6");
        assert_eq!(formats::JSON_POINTER, "json-pointer");
        assert_eq!(formats::REGEX, "regex");
    }
}