turbomcp-protocol 3.1.0

Complete MCP protocol implementation with types, traits, context management, and message handling
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
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
//! User input elicitation types
//!
//! This module contains types for server-initiated user input requests:
//! - **Form Mode** (MCP 2025-11-25): In-band structured data collection
//! - **URL Mode** (MCP 2025-11-25 draft, SEP-1036): Out-of-band sensitive data collection
//!
//! ## Form Mode vs URL Mode
//!
//! | Aspect | Form Mode | URL Mode |
//! |--------|-----------|----------|
//! | **Data Flow** | In-band (through MCP) | Out-of-band (external URL) |
//! | **Use Case** | Non-sensitive structured data | Sensitive data, OAuth, credentials |
//! | **Security** | Data visible to MCP client | Data **NOT** visible to MCP client |

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

/// Elicitation action taken by user
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum ElicitationAction {
    /// User submitted the form/confirmed the action
    Accept,
    /// User explicitly declined the action
    Decline,
    /// User dismissed without making an explicit choice
    Cancel,
}

/// Schema for elicitation input validation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElicitationSchema {
    /// Schema type (must be "object", required by MCP spec)
    #[serde(rename = "type")]
    pub schema_type: String,
    /// Schema properties (required by MCP spec)
    pub properties: HashMap<String, PrimitiveSchemaDefinition>,
    /// Required properties
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<Vec<String>>,
    /// Additional properties allowed
    #[serde(
        rename = "additionalProperties",
        skip_serializing_if = "Option::is_none"
    )]
    pub additional_properties: Option<bool>,
}

impl ElicitationSchema {
    /// Create a new elicitation schema
    pub fn new() -> Self {
        Self {
            schema_type: "object".to_string(),
            properties: HashMap::new(),
            required: Some(Vec::new()),
            additional_properties: Some(false),
        }
    }

    /// Add a string property to the schema
    pub fn add_string_property(
        mut self,
        name: String,
        required: bool,
        description: Option<String>,
    ) -> Self {
        let property = PrimitiveSchemaDefinition::String {
            title: None,
            description,
            format: None,
            min_length: None,
            max_length: None,
            enum_values: None,
            enum_names: None,
        };

        self.properties.insert(name.clone(), property);

        if required && let Some(ref mut required_fields) = self.required {
            required_fields.push(name);
        }

        self
    }

    /// Add a number property to the schema
    pub fn add_number_property(
        mut self,
        name: String,
        required: bool,
        description: Option<String>,
        minimum: Option<f64>,
        maximum: Option<f64>,
    ) -> Self {
        let property = PrimitiveSchemaDefinition::Number {
            title: None,
            description,
            minimum,
            maximum,
        };

        self.properties.insert(name.clone(), property);

        if required && let Some(ref mut required_fields) = self.required {
            required_fields.push(name);
        }

        self
    }

    /// Add a boolean property to the schema
    pub fn add_boolean_property(
        mut self,
        name: String,
        required: bool,
        description: Option<String>,
        default: Option<bool>,
    ) -> Self {
        let property = PrimitiveSchemaDefinition::Boolean {
            title: None,
            description,
            default,
        };

        self.properties.insert(name.clone(), property);

        if required && let Some(ref mut required_fields) = self.required {
            required_fields.push(name);
        }

        self
    }
}

impl Default for ElicitationSchema {
    fn default() -> Self {
        Self::new()
    }
}

/// Primitive schema definition for elicitation fields
///
/// ## Version Support
/// - MCP 2025-11-25: String (with legacy enumNames), Number, Integer, Boolean
/// - MCP 2025-11-25 draft (SEP-1330): Use `EnumSchema` for standards-compliant enum handling
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum PrimitiveSchemaDefinition {
    /// String field schema definition
    ///
    /// **Note**: For enum fields, prefer using `EnumSchema` (MCP 2025-11-25 draft)
    /// over the legacy `enum_values`/`enum_names` pattern for standards compliance.
    #[serde(rename = "string")]
    String {
        /// Field title
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Field description
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        /// String format (email, uri, date, date-time, etc.)
        #[serde(skip_serializing_if = "Option::is_none")]
        format: Option<String>,
        /// Minimum string length
        #[serde(skip_serializing_if = "Option::is_none")]
        #[serde(rename = "minLength")]
        min_length: Option<u32>,
        /// Maximum string length
        #[serde(skip_serializing_if = "Option::is_none")]
        #[serde(rename = "maxLength")]
        max_length: Option<u32>,
        /// Allowed enum values
        ///
        /// **Legacy**: Use `EnumSchema::UntitledSingleSelect` instead (MCP 2025-11-25)
        #[serde(skip_serializing_if = "Option::is_none")]
        #[serde(rename = "enum")]
        enum_values: Option<Vec<String>>,
        /// Display names for enum values
        ///
        /// **Deprecated**: This is non-standard JSON Schema. Use `EnumSchema::TitledSingleSelect`
        /// with `oneOf` pattern instead (MCP 2025-11-25 draft, SEP-1330).
        #[serde(skip_serializing_if = "Option::is_none")]
        #[serde(rename = "enumNames")]
        enum_names: Option<Vec<String>>,
    },
    /// Number field schema definition
    #[serde(rename = "number")]
    Number {
        /// Field title
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Field description
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        /// Minimum value
        #[serde(skip_serializing_if = "Option::is_none")]
        minimum: Option<f64>,
        /// Maximum value
        #[serde(skip_serializing_if = "Option::is_none")]
        maximum: Option<f64>,
    },
    /// Integer field schema definition
    #[serde(rename = "integer")]
    Integer {
        /// Field title
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Field description
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        /// Minimum value
        #[serde(skip_serializing_if = "Option::is_none")]
        minimum: Option<i64>,
        /// Maximum value
        #[serde(skip_serializing_if = "Option::is_none")]
        maximum: Option<i64>,
    },
    /// Boolean field schema definition
    #[serde(rename = "boolean")]
    Boolean {
        /// Field title
        #[serde(skip_serializing_if = "Option::is_none")]
        title: Option<String>,
        /// Field description
        #[serde(skip_serializing_if = "Option::is_none")]
        description: Option<String>,
        /// Default value
        #[serde(skip_serializing_if = "Option::is_none")]
        default: Option<bool>,
    },
}

// ========== MCP 2025-11-25 Draft: Standards-Based Enum Schemas (SEP-1330) ==========

/// Enum option with value and display title (JSON Schema 2020-12 compliant)
///
/// Used with `oneOf` (single-select) or `anyOf` (multi-select) patterns
/// to provide human-readable labels for enum values.
///
/// ## Example
/// ```json
/// { "const": "#FF0000", "title": "Red" }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct EnumOption {
    /// The enum value (must match one of the allowed values)
    #[serde(rename = "const")]
    pub const_value: String,
    /// Human-readable display name for this option
    pub title: String,
}

/// Single-select enum schema with display titles (JSON Schema 2020-12 compliant)
///
/// Replaces the legacy `enum + enumNames` pattern with standards-compliant `oneOf` + `const`.
///
/// ## Example
/// ```json
/// {
///   "type": "string",
///   "title": "Color Selection",
///   "oneOf": [
///     { "const": "#FF0000", "title": "Red" },
///     { "const": "#00FF00", "title": "Green" }
///   ],
///   "default": "#FF0000"
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TitledSingleSelectEnumSchema {
    /// Schema type (must be "string")
    #[serde(rename = "type")]
    pub schema_type: String,
    /// Array of enum options with const/title pairs (JSON Schema 2020-12)
    #[serde(rename = "oneOf")]
    pub one_of: Vec<EnumOption>,
    /// Optional schema title
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Optional schema description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional default value (must match one of the const values)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<String>,
}

/// Single-select enum schema without display titles (standard JSON Schema)
///
/// Simple enum pattern without custom titles - uses enum values as labels.
///
/// ## Example
/// ```json
/// {
///   "type": "string",
///   "enum": ["red", "green", "blue"],
///   "default": "red"
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UntitledSingleSelectEnumSchema {
    /// Schema type (must be "string")
    #[serde(rename = "type")]
    pub schema_type: String,
    /// Array of allowed string values (standard JSON Schema)
    #[serde(rename = "enum")]
    pub enum_values: Vec<String>,
    /// Optional schema title
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Optional schema description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional default value (must match one of the enum values)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<String>,
}

/// Multi-select enum schema with display titles (JSON Schema 2020-12 compliant)
///
/// Allows selecting multiple values from an enumeration with human-readable labels.
/// Uses `anyOf` pattern for array items.
///
/// ## Example
/// ```json
/// {
///   "type": "array",
///   "title": "Color Selection",
///   "minItems": 1,
///   "maxItems": 2,
///   "items": {
///     "anyOf": [
///       { "const": "#FF0000", "title": "Red" },
///       { "const": "#00FF00", "title": "Green" }
///     ]
///   },
///   "default": ["#FF0000"]
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TitledMultiSelectEnumSchema {
    /// Schema type (must be "array")
    #[serde(rename = "type")]
    pub schema_type: String,
    /// Minimum number of selections required
    #[serde(rename = "minItems", skip_serializing_if = "Option::is_none")]
    pub min_items: Option<u32>,
    /// Maximum number of selections allowed
    #[serde(rename = "maxItems", skip_serializing_if = "Option::is_none")]
    pub max_items: Option<u32>,
    /// Array item schema with anyOf enum options
    pub items: MultiSelectItems,
    /// Optional schema title
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Optional schema description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional default value (array of selected values)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<Vec<String>>,
}

/// Multi-select enum schema without display titles (standard JSON Schema)
///
/// Simple multi-select pattern using enum array.
///
/// ## Example
/// ```json
/// {
///   "type": "array",
///   "items": {
///     "type": "string",
///     "enum": ["red", "green", "blue"]
///   },
///   "default": ["red", "green"]
/// }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UntitledMultiSelectEnumSchema {
    /// Schema type (must be "array")
    #[serde(rename = "type")]
    pub schema_type: String,
    /// Minimum number of selections required
    #[serde(rename = "minItems", skip_serializing_if = "Option::is_none")]
    pub min_items: Option<u32>,
    /// Maximum number of selections allowed
    #[serde(rename = "maxItems", skip_serializing_if = "Option::is_none")]
    pub max_items: Option<u32>,
    /// Array item schema with simple enum
    pub items: UntitledMultiSelectItems,
    /// Optional schema title
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// Optional schema description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional default value (array of selected values)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default: Option<Vec<String>>,
}

/// Array items schema for titled multi-select (using anyOf)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiSelectItems {
    /// Array of enum options with const/title pairs (JSON Schema 2020-12)
    #[serde(rename = "anyOf")]
    pub any_of: Vec<EnumOption>,
}

/// Array items schema for untitled multi-select (using simple enum)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UntitledMultiSelectItems {
    /// Item type (must be "string")
    #[serde(rename = "type")]
    pub schema_type: String,
    /// Array of allowed string values
    #[serde(rename = "enum")]
    pub enum_values: Vec<String>,
}

/// Standards-based enum schema (MCP 2025-11-25, SEP-1330)
///
/// JSON Schema 2020-12 compliant enum patterns using `oneOf`/`const` (single-select)
/// and `anyOf` (multi-select) patterns.
///
/// ## Variants
///
/// - **TitledSingleSelect**: Single-select with display titles (oneOf + const)
/// - **UntitledSingleSelect**: Single-select without titles (simple enum)
/// - **TitledMultiSelect**: Multi-select with display titles (array + anyOf)
/// - **UntitledMultiSelect**: Multi-select without titles (array + enum)
///
/// ## Standards Compliance
///
/// All patterns use standard JSON Schema 2020-12 keywords:
/// - `oneOf` with `const` and `title` for discriminated unions
/// - `anyOf` for array items with multiple allowed types
/// - `enum` for simple value restrictions
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum EnumSchema {
    /// Single-select enum with display titles (oneOf pattern)
    TitledSingleSelect(TitledSingleSelectEnumSchema),
    /// Single-select enum without titles (simple enum)
    UntitledSingleSelect(UntitledSingleSelectEnumSchema),
    /// Multi-select enum with display titles (array + anyOf)
    TitledMultiSelect(TitledMultiSelectEnumSchema),
    /// Multi-select enum without titles (array + enum)
    UntitledMultiSelect(UntitledMultiSelectEnumSchema),
}

/// Elicit request mode (MCP 2025-11-25 draft, SEP-1036)
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum ElicitMode {
    /// Form mode: In-band structured data collection (MCP 2025-11-25)
    Form,
    /// URL mode: Out-of-band sensitive data collection (MCP 2025-11-25 draft)
    Url,
}

/// URL mode elicitation parameters (MCP 2025-11-25 draft, SEP-1036)
///
/// Used for out-of-band interactions where sensitive information must not
/// pass through the MCP client (e.g., OAuth flows, API keys, credentials).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct URLElicitRequestParams {
    /// Elicitation mode (must be "url")
    pub mode: ElicitMode,

    /// Unique identifier for this elicitation
    #[serde(rename = "elicitationId")]
    pub elicitation_id: String,

    /// Human-readable message explaining why the interaction is needed
    pub message: String,

    /// URL user should navigate to (MUST NOT contain sensitive information)
    #[serde(with = "url_serde")]
    pub url: url::Url,
}

// Custom serde for url::Url to serialize as string
mod url_serde {
    use serde::{Deserialize, Deserializer, Serializer};
    use url::Url;

    pub(super) fn serialize<S>(url: &Url, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(url.as_str())
    }

    pub(super) fn deserialize<'de, D>(deserializer: D) -> Result<Url, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Url::parse(&s).map_err(serde::de::Error::custom)
    }
}

/// Form mode elicitation parameters (MCP 2025-11-25)
///
/// Used for in-band structured data collection with JSON schema validation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormElicitRequestParams {
    /// Human-readable message for the user
    pub message: String,

    /// Schema for input validation (per MCP specification)
    #[serde(rename = "requestedSchema")]
    pub schema: ElicitationSchema,

    /// Optional timeout in milliseconds
    #[serde(rename = "timeoutMs", skip_serializing_if = "Option::is_none")]
    pub timeout_ms: Option<u32>,

    /// Whether the request can be cancelled
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cancellable: Option<bool>,
}

/// Elicit request parameters (supports both form and URL modes)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ElicitRequestParams {
    /// Form mode: In-band structured data collection
    Form(FormElicitRequestParams),

    /// URL mode: Out-of-band sensitive data collection (MCP 2025-11-25 draft)
    Url(URLElicitRequestParams),
}

impl ElicitRequestParams {
    /// Create a new form mode elicitation request
    pub fn form(
        message: String,
        schema: ElicitationSchema,
        timeout_ms: Option<u32>,
        cancellable: Option<bool>,
    ) -> Self {
        ElicitRequestParams::Form(FormElicitRequestParams {
            message,
            schema,
            timeout_ms,
            cancellable,
        })
    }

    /// Create a new URL mode elicitation request
    pub fn url(elicitation_id: String, message: String, url: url::Url) -> Self {
        ElicitRequestParams::Url(URLElicitRequestParams {
            mode: ElicitMode::Url,
            elicitation_id,
            message,
            url,
        })
    }

    /// Get the message for this elicitation
    pub fn message(&self) -> &str {
        match self {
            ElicitRequestParams::Form(form) => &form.message,
            ElicitRequestParams::Url(url_params) => &url_params.message,
        }
    }
}

/// Elicit request wrapper
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElicitRequest {
    /// Elicitation parameters
    #[serde(flatten)]
    pub params: ElicitRequestParams,
    /// Task metadata for task-augmented elicitation (MCP 2025-11-25 draft, SEP-1686)
    ///
    /// When present, indicates the server should execute this elicitation request as a long-running
    /// task and return a CreateTaskResult instead of the immediate ElicitResult.
    /// The actual result can be retrieved later via tasks/result.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub task: Option<crate::types::tasks::TaskMetadata>,
    /// Optional metadata per MCP 2025-11-25 specification
    #[serde(skip_serializing_if = "Option::is_none")]
    pub _meta: Option<serde_json::Value>,
}

impl Default for ElicitRequest {
    fn default() -> Self {
        Self {
            params: ElicitRequestParams::form(String::new(), ElicitationSchema::new(), None, None),
            task: None,
            _meta: None,
        }
    }
}

/// Elicit result
///
/// ## Version Support
/// - MCP 2025-11-25: action, content (form mode), _meta
/// - MCP 2025-11-25 draft (SEP-1330): Clarified content field behavior
///
/// ## Content Field Behavior (SEP-1330 Clarification)
///
/// The `content` field is **only present** when:
/// 1. `action` is `"accept"` (user submitted the form), AND
/// 2. Mode was `"form"` (in-band structured data collection)
///
/// The `content` field is **omitted** when:
/// - Action is `"decline"` or `"cancel"`
/// - Mode was `"url"` (out-of-band, data doesn't transit through MCP)
///
/// ## Example
///
/// **Form mode with accept:**
/// ```json
/// {
///   "action": "accept",
///   "content": {
///     "name": "Alice",
///     "email": "alice@example.com"
///   }
/// }
/// ```
///
/// **URL mode with accept:**
/// ```json
/// {
///   "action": "accept"
/// }
/// ```
/// Note: No `content` field - data was collected out-of-band
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElicitResult {
    /// The user action in response to the elicitation
    ///
    /// - `accept`: User submitted the form/confirmed the action
    /// - `decline`: User explicitly declined the action
    /// - `cancel`: User dismissed without making an explicit choice
    pub action: ElicitationAction,

    /// The submitted form data, only present when action is "accept"
    /// and mode was "form".
    ///
    /// Contains values matching the requested schema. Omitted for:
    /// - `action` is `"decline"` or `"cancel"`
    /// - URL mode responses (out-of-band data collection)
    ///
    /// Per MCP 2025-11-25 draft (SEP-1330), this clarification ensures
    /// clients understand when to expect form data vs. out-of-band completion.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<std::collections::HashMap<String, serde_json::Value>>,

    /// Optional metadata per MCP 2025-11-25 specification
    #[serde(skip_serializing_if = "Option::is_none")]
    pub _meta: Option<serde_json::Value>,
}

/// Elicitation completion notification parameters (MCP 2025-11-25 draft, SEP-1036)
///
/// Sent by the server to indicate that an out-of-band elicitation has been completed.
/// This allows the client to know when to retry the original request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ElicitationCompleteParams {
    /// The ID of the completed elicitation
    #[serde(rename = "elicitationId")]
    pub elicitation_id: String,
}

/// Error returned when a server requires URL-mode elicitation but the client
/// requested form-mode (or vice versa). Carries the URL the client should open
/// out-of-band to satisfy the elicitation, per MCP 2025-11-25 / SEP-1036.
///
/// Servers should map this to a JSON-RPC error response with `data` populated:
/// ```json
/// { "code": -32001, "message": "URL elicitation required", "data": { "url": "https://..." } }
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct URLElicitationRequiredError {
    /// The URL the client must open out-of-band to satisfy the elicitation.
    pub url: String,
    /// Optional human-readable description of what the user is being asked for.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Optional `elicitationId` the client should reference when polling/awaiting completion.
    #[serde(rename = "elicitationId", skip_serializing_if = "Option::is_none")]
    pub elicitation_id: Option<String>,
}

impl URLElicitationRequiredError {
    /// Create a new URL-elicitation-required error.
    pub fn new(url: impl Into<String>) -> Self {
        Self {
            url: url.into(),
            description: None,
            elicitation_id: None,
        }
    }

    /// Attach a human-readable description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Attach an elicitation ID for completion correlation.
    pub fn with_elicitation_id(mut self, id: impl Into<String>) -> Self {
        self.elicitation_id = Some(id.into());
        self
    }

    /// Suggested JSON-RPC error code for this condition (server-error range).
    pub const ERROR_CODE: i32 = -32001;
}

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

    #[test]
    fn test_elicitation_action_serialization() {
        assert_eq!(
            serde_json::to_string(&ElicitationAction::Accept).unwrap(),
            "\"accept\""
        );
        assert_eq!(
            serde_json::to_string(&ElicitationAction::Decline).unwrap(),
            "\"decline\""
        );
        assert_eq!(
            serde_json::to_string(&ElicitationAction::Cancel).unwrap(),
            "\"cancel\""
        );
    }

    #[test]
    fn test_form_elicit_params() {
        let schema = ElicitationSchema::new().add_string_property(
            "name".to_string(),
            true,
            Some("User name".to_string()),
        );

        let params = ElicitRequestParams::form(
            "Please provide your name".to_string(),
            schema,
            Some(30000),
            Some(true),
        );

        assert_eq!(params.message(), "Please provide your name");

        // Test serialization
        let json = serde_json::to_string(&params).unwrap();
        assert!(json.contains("Please provide your name"));
        assert!(json.contains("requestedSchema"));
    }

    #[test]
    fn test_url_elicit_params() {
        use url::Url;

        let url = Url::parse("https://example.com/oauth/authorize").unwrap();
        let params = ElicitRequestParams::url(
            "test-id-123".to_string(),
            "Please authorize the connection".to_string(),
            url,
        );

        assert_eq!(params.message(), "Please authorize the connection");

        // Test serialization
        let json = serde_json::to_string(&params).unwrap();
        assert!(json.contains("test-id-123"));
        assert!(json.contains("https://example.com/oauth/authorize"));
        assert!(json.contains("\"mode\":\"url\""));
    }

    #[test]
    fn test_elicit_mode_serialization() {
        assert_eq!(
            serde_json::to_string(&ElicitMode::Form).unwrap(),
            "\"form\""
        );
        assert_eq!(serde_json::to_string(&ElicitMode::Url).unwrap(), "\"url\"");
    }

    #[test]
    fn test_completion_notification() {
        let params = ElicitationCompleteParams {
            elicitation_id: "550e8400-e29b-41d4-a716-446655440000".to_string(),
        };

        let json = serde_json::to_string(&params).unwrap();
        assert!(json.contains("550e8400-e29b-41d4-a716-446655440000"));
        assert!(json.contains("elicitationId"));
    }

    #[test]
    fn test_elicit_result_form_mode() {
        let mut content = std::collections::HashMap::new();
        content.insert("name".to_string(), serde_json::json!("Alice"));

        let result = ElicitResult {
            action: ElicitationAction::Accept,
            content: Some(content),
            _meta: None,
        };

        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("\"action\":\"accept\""));
        assert!(json.contains("\"name\":\"Alice\""));
    }

    #[test]
    fn test_elicit_result_url_mode() {
        // URL mode: no content field
        let result = ElicitResult {
            action: ElicitationAction::Accept,
            content: None,
            _meta: None,
        };

        let json = serde_json::to_string(&result).unwrap();
        assert!(json.contains("\"action\":\"accept\""));
        assert!(!json.contains("content"));
    }

    // ========== SEP-1330 Enum Schema Tests ==========

    #[test]
    fn test_titled_single_select_enum_schema() {
        use super::{EnumOption, TitledSingleSelectEnumSchema};

        let schema = TitledSingleSelectEnumSchema {
            schema_type: "string".to_string(),
            one_of: vec![
                EnumOption {
                    const_value: "#FF0000".to_string(),
                    title: "Red".to_string(),
                },
                EnumOption {
                    const_value: "#00FF00".to_string(),
                    title: "Green".to_string(),
                },
                EnumOption {
                    const_value: "#0000FF".to_string(),
                    title: "Blue".to_string(),
                },
            ],
            title: Some("Color Selection".to_string()),
            description: Some("Choose your favorite color".to_string()),
            default: Some("#FF0000".to_string()),
        };

        let json = serde_json::to_string(&schema).unwrap();
        assert!(json.contains("\"type\":\"string\""));
        assert!(json.contains("\"oneOf\""));
        assert!(json.contains("\"const\":\"#FF0000\""));
        assert!(json.contains("\"title\":\"Red\""));
        assert!(json.contains("\"default\":\"#FF0000\""));

        // Verify deserialization
        let deserialized: TitledSingleSelectEnumSchema = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.one_of.len(), 3);
        assert_eq!(deserialized.one_of[0].const_value, "#FF0000");
        assert_eq!(deserialized.one_of[0].title, "Red");
    }

    #[test]
    fn test_untitled_single_select_enum_schema() {
        use super::UntitledSingleSelectEnumSchema;

        let schema = UntitledSingleSelectEnumSchema {
            schema_type: "string".to_string(),
            enum_values: vec!["red".to_string(), "green".to_string(), "blue".to_string()],
            title: Some("Color Selection".to_string()),
            description: Some("Choose a color".to_string()),
            default: Some("red".to_string()),
        };

        let json = serde_json::to_string(&schema).unwrap();
        assert!(json.contains("\"type\":\"string\""));
        assert!(json.contains("\"enum\""));
        assert!(json.contains("\"red\""));
        assert!(json.contains("\"green\""));
        assert!(json.contains("\"blue\""));
        assert!(!json.contains("oneOf"));

        // Verify deserialization
        let deserialized: UntitledSingleSelectEnumSchema = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.enum_values.len(), 3);
        assert_eq!(deserialized.enum_values[0], "red");
    }

    #[test]
    fn test_titled_multi_select_enum_schema() {
        use super::{EnumOption, MultiSelectItems, TitledMultiSelectEnumSchema};

        let schema = TitledMultiSelectEnumSchema {
            schema_type: "array".to_string(),
            min_items: Some(1),
            max_items: Some(2),
            items: MultiSelectItems {
                any_of: vec![
                    EnumOption {
                        const_value: "#FF0000".to_string(),
                        title: "Red".to_string(),
                    },
                    EnumOption {
                        const_value: "#00FF00".to_string(),
                        title: "Green".to_string(),
                    },
                ],
            },
            title: Some("Color Selection".to_string()),
            description: Some("Choose up to 2 colors".to_string()),
            default: Some(vec!["#FF0000".to_string()]),
        };

        let json = serde_json::to_string(&schema).unwrap();
        assert!(json.contains("\"type\":\"array\""));
        assert!(json.contains("\"minItems\":1"));
        assert!(json.contains("\"maxItems\":2"));
        assert!(json.contains("\"anyOf\""));
        assert!(json.contains("\"const\":\"#FF0000\""));

        // Verify deserialization
        let deserialized: TitledMultiSelectEnumSchema = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.items.any_of.len(), 2);
        assert_eq!(deserialized.min_items, Some(1));
        assert_eq!(deserialized.max_items, Some(2));
    }

    #[test]
    fn test_untitled_multi_select_enum_schema() {
        use super::{UntitledMultiSelectEnumSchema, UntitledMultiSelectItems};

        let schema = UntitledMultiSelectEnumSchema {
            schema_type: "array".to_string(),
            min_items: Some(1),
            max_items: None,
            items: UntitledMultiSelectItems {
                schema_type: "string".to_string(),
                enum_values: vec!["red".to_string(), "green".to_string(), "blue".to_string()],
            },
            title: Some("Color Selection".to_string()),
            description: Some("Choose colors".to_string()),
            default: Some(vec!["red".to_string(), "green".to_string()]),
        };

        let json = serde_json::to_string(&schema).unwrap();
        assert!(json.contains("\"type\":\"array\""));
        assert!(json.contains("\"minItems\":1"));
        assert!(json.contains("\"items\""));
        assert!(json.contains("\"enum\""));
        assert!(!json.contains("anyOf"));

        // Verify deserialization
        let deserialized: UntitledMultiSelectEnumSchema = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.items.enum_values.len(), 3);
        assert_eq!(deserialized.default.as_ref().unwrap().len(), 2);
    }

    #[test]
    fn test_enum_schema_union_type() {
        use super::EnumSchema;

        // Test that EnumSchema can deserialize different variants
        let titled_json = r#"{
            "type": "string",
            "oneOf": [
                {"const": "red", "title": "Red"},
                {"const": "green", "title": "Green"}
            ]
        }"#;

        let schema: EnumSchema = serde_json::from_str(titled_json).unwrap();
        match schema {
            EnumSchema::TitledSingleSelect(s) => {
                assert_eq!(s.one_of.len(), 2);
                assert_eq!(s.one_of[0].const_value, "red");
            }
            _ => panic!("Expected TitledSingleSelect variant"),
        }

        // Test untitled variant
        let untitled_json = r#"{
            "type": "string",
            "enum": ["red", "green", "blue"]
        }"#;

        let schema: EnumSchema = serde_json::from_str(untitled_json).unwrap();
        match schema {
            EnumSchema::UntitledSingleSelect(s) => {
                assert_eq!(s.enum_values.len(), 3);
            }
            _ => panic!("Expected UntitledSingleSelect variant"),
        }
    }

    #[test]
    fn test_enum_option_serialization() {
        use super::EnumOption;

        let option = EnumOption {
            const_value: "#FF0000".to_string(),
            title: "Red".to_string(),
        };

        let json = serde_json::to_string(&option).unwrap();
        assert!(json.contains("\"const\":\"#FF0000\""));
        assert!(json.contains("\"title\":\"Red\""));
        assert!(!json.contains("const_value")); // Verifies camelCase rename

        // Verify deserialization
        let deserialized: EnumOption = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.const_value, "#FF0000");
        assert_eq!(deserialized.title, "Red");
    }
}