protocol-droid 0.3.0

A Model Context Protocol toolkit that speaks every published spec revision, with one canonical IR behind many per-revision codecs.
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
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
//! Cross-protocol mapping rules: A2A v1.0 ↔ MCP (any negotiated revision).
//!
//! # What this module is
//!
//! Pure functions that translate between an A2A concept (skill, message) and
//! the corresponding MCP concept (tool, content block). The functions are
//! total where the mapping is 1:1 and partial where it is not — when an
//! A2A concept has no MCP shape, the translation records the loss in a
//! [`CrossProtocolReport`] rather than dropping the value silently.
//!
//! # What this module is *not*
//!
//! It is not the bridge. The bridge (in `application/bridge/`) is the loop
//! that drives these translations against a live peer on each side. This
//! module is the pure half: the *what* of the translation, kept separate
//! from the *when* so the rules can be unit-tested without a transport and
//! the transport can be exercised against a test fixture of rules.
//!
//! # Why the report accumulates rather than summarising
//!
//! A count of "3 rules fired" loses the *which*. Two translations that both
//! dropped a content block can be perfectly fine and a serious bug, and the
//! difference is whether it was a file part the caller agreed to lose or an
//! audio block nobody mentioned. [`CrossProtocolReport`] keeps the steps in
//! the order they were applied and lets the reviewer read what each one was.
//!
//! # Conventions
//!
//! A function whose name is `*_to_*` translates the first protocol's
//! concept into the second's. A function whose name is `*_from_*` is the
//! inverse. A pair of directions is exposed together so the consumer can
//! read both shapes without hunting.

use std::collections::BTreeMap;

use serde_json::Value;

use crate::domain::a2a::agent_card::AgentSkill;
use crate::domain::a2a::message::{DataPart, FilePart, Message, Part, TextPart};
use crate::domain::ir::{
    AudioContent, Content, EmbeddedResource, ExtensionValue, Extensions, ImageContent, JsonObject,
    JsonObjectError, ResourceLinkContent, TextContent,
};
use crate::domain::registry::primitive::{Tool, ToolAnnotations};

/// The two protocols the mapping crosses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Protocol {
    /// A2A v1.0. The `major.minor` shape is a private concern of the A2A
    /// module; this enum is a tag, not a value object.
    A2a,
    /// MCP at the revision a peer negotiated.
    Mcp,
}

impl Protocol {
    /// The other side of the crossing.
    #[must_use]
    pub const fn other(self) -> Self {
        match self {
            Self::A2a => Self::Mcp,
            Self::Mcp => Self::A2a,
        }
    }
}

/// The two directions a translation can take.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Direction {
    /// A2A → MCP.
    ToMcp,
    /// MCP → A2A.
    ToA2a,
}

/// The reason a step fired.
///
/// Each variant is a statement a reviewer can act on. The trivial case
/// (`PassThrough`) is recorded when the report needs to show *what* was
/// carried so the absence of any other step is meaningful; a translation
/// that did not need to record anything reports an empty
/// [`CrossProtocolReport`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StepKind {
    /// A field round-tripped unchanged.
    PassThrough,
    /// A field was mapped onto a different field that carries the same
    /// information. The report names which side lost its native shape.
    Renamed {
        /// The A2A field the value came from, when the rule is A2A-side.
        from: Option<&'static str>,
        /// The MCP field the value went to, when the rule is MCP-side.
        to: Option<&'static str>,
    },
    /// A field has no peer-side shape and the value was dropped. The
    /// caller will see the step and decide whether the drop is acceptable.
    Dropped {
        /// The A2A field that was dropped, when the rule is A2A-side.
        from: Option<&'static str>,
    },
    /// A field was filled with a placeholder because the source had no
    /// shape to map. The placeholder is documented on the function that
    /// produced it.
    Synthesised {
        /// The MCP field that was filled, when the rule is MCP-side.
        to: Option<&'static str>,
    },
}

/// One step the translation took, and what it cost.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Step {
    /// Which direction the step went.
    pub direction: Direction,
    /// The A2A concept the step touched, when the step's rule is
    /// A2A-specific. `None` for a step whose rule is protocol-neutral.
    pub source: Option<Protocol>,
    /// What was done, named for the operator reading the report.
    pub note: StepKind,
}

/// What one translation changed.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CrossProtocolReport {
    steps: Vec<Step>,
}

impl CrossProtocolReport {
    /// A report with nothing in it yet.
    #[must_use]
    pub const fn new() -> Self {
        Self { steps: Vec::new() }
    }

    /// Records a step.
    pub fn record(&mut self, direction: Direction, source: Option<Protocol>, note: StepKind) {
        self.steps.push(Step {
            direction,
            source,
            note,
        });
    }

    /// Whether anything was recorded. An empty report is a lossless
    /// translation; a non-empty report names the changes.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        self.steps.is_empty()
    }

    /// The number of recorded steps.
    #[must_use]
    pub const fn len(&self) -> usize {
        self.steps.len()
    }

    /// The steps, in the order they were applied.
    #[must_use]
    pub fn steps(&self) -> &[Step] {
        &self.steps
    }

    /// Merges `other` into this one. Used by the bridge to combine the
    /// report from a forward translation with the report from the reply.
    pub fn merge(&mut self, other: Self) {
        self.steps.extend(other.steps);
    }
}

impl core::fmt::Display for CrossProtocolReport {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        if self.steps.is_empty() {
            return formatter.write_str("cross-protocol translation: no changes");
        }
        for (index, step) in self.steps.iter().enumerate() {
            if index > 0 {
                formatter.write_str("; ")?;
            }
            core::fmt::Display::fmt(step, formatter)?;
        }
        Ok(())
    }
}

impl core::fmt::Display for Step {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let direction = match self.direction {
            Direction::ToMcp => "a2a→mcp",
            Direction::ToA2a => "mcp→a2a",
        };
        let source = self
            .source
            .map_or_else(|| "neutral".to_owned(), |p| format!("{p:?}").to_lowercase());
        write!(formatter, "{direction}@{source}: {}", self.note)
    }
}

impl core::fmt::Display for StepKind {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::PassThrough => formatter.write_str("pass-through"),
            Self::Renamed { from, to } => {
                let from = from.unwrap_or("?");
                let to = to.unwrap_or("?");
                write!(formatter, "renamed `{from}` → `{to}`")
            }
            Self::Dropped { from } => {
                let from = from.unwrap_or("?");
                write!(formatter, "dropped `{from}`")
            }
            Self::Synthesised { to } => {
                let to = to.unwrap_or("?");
                write!(formatter, "synthesised `{to}`")
            }
        }
    }
}

/// A2A `AgentSkill` → MCP `Tool`.
///
/// # Mapping
///
/// - `id` → `Tool.name`
/// - `name` → `Tool.title` (an empty name is recorded as a dropped step)
/// - `description` → `Tool.description` (an empty description is recorded)
/// - `tags` → `extensions["tags"]` (MCP has no first-class tag field)
/// - `inputSchema` is **synthesised** as `{"type":"object"}` because A2A
///   skills carry no schema and MCP requires one at every revision. The
///   synthesis is a recorded step.
///
/// # Errors
///
/// [`Tool::new`] parses the synthesised schema and refuses a value that is
/// not a JSON Schema object. The empty-object schema is always parseable, so
/// the only way this function errors is if [`JsonObject::new`] itself fails,
/// which it does not for `{"type":"object"}`; the signature keeps the
/// fallibility explicit so a future synthesised schema is honest about the
/// cost of being parseable.
pub fn a2a_skill_to_mcp_tool(
    skill: &AgentSkill,
    report: &mut CrossProtocolReport,
) -> Result<Tool, JsonObjectError> {
    JsonObject::new(r#"{"type":"object"}"#)?;
    report.record(
        Direction::ToMcp,
        Some(Protocol::A2a),
        StepKind::Synthesised {
            to: Some("inputSchema"),
        },
    );

    let mut extensions = Extensions::empty();
    if !skill.tags.is_empty() {
        let tags_value = Value::Array(
            skill
                .tags
                .iter()
                .map(|tag| Value::String(tag.to_string()))
                .collect(),
        );
        extensions.insert("tags".to_owned(), ExtensionValue::from(tags_value));
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Renamed {
                from: Some("tags"),
                to: Some("extensions[\"tags\"]"),
            },
        );
    }

    report.record(
        Direction::ToMcp,
        Some(Protocol::A2a),
        StepKind::Renamed {
            from: Some("id"),
            to: Some("name"),
        },
    );

    let title = if skill.name.as_ref().is_empty() {
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Dropped { from: Some("name") },
        );
        None
    } else {
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Renamed {
                from: Some("name"),
                to: Some("title"),
            },
        );
        Some(skill.name.to_string())
    };

    let description = if skill.description.as_ref().is_empty() {
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Dropped {
                from: Some("description"),
            },
        );
        None
    } else {
        report.record(Direction::ToMcp, Some(Protocol::A2a), StepKind::PassThrough);
        Some(skill.description.to_string())
    };

    let mut tool = Tool::new(skill.id.as_ref(), r#"{"type":"object"}"#)?;
    tool.title = title;
    tool.description = description;
    tool.annotations = Some(ToolAnnotations::default());
    tool.extensions = extensions;
    Ok(tool)
}

/// A2A `Message` → a list of MCP `Content` blocks.
///
/// The mapping is one-to-one for the common case (`TextPart` → `Text`); the
/// `FilePart` and `DataPart` cases each get a best-effort mapping with a
/// recorded step. A `FilePart` whose MIME type is an image becomes an
/// `ImageContent` block, an audio MIME type becomes `AudioContent`, and a
/// URI becomes a `ResourceLink`. A `FilePart` whose MIME type is neither
/// and whose content is a URI is treated as a `ResourceLink`; a `FilePart`
/// with no URI and a non-image/audio MIME type is dropped with a recorded
/// step. A `DataPart` becomes an `EmbeddedResource` with a JSON MIME type.
pub fn a2a_message_to_mcp_content(
    message: &Message,
    report: &mut CrossProtocolReport,
) -> Vec<Content> {
    let mut out = Vec::with_capacity(message.parts.len());
    for part in &message.parts {
        match part {
            Part::Text(text) => {
                out.push(Content::Text(TextContent::new(text.text.clone())));
                report.record(Direction::ToMcp, Some(Protocol::A2a), StepKind::PassThrough);
            }
            Part::File(file) => {
                if let Some(block) = a2a_file_to_mcp_content(file, report) {
                    out.push(block);
                }
            }
            Part::Data(data) => {
                let value = data.data.clone();
                let block = EmbeddedResource {
                    text: Some(value.to_string()),
                    blob: None,
                    uri: format!("data:application/json,{value}"),
                    mime_type: Some("application/json".to_owned()),
                    annotations: None,
                    extensions: Extensions::empty(),
                };
                out.push(Content::EmbeddedResource(block));
                report.record(
                    Direction::ToMcp,
                    Some(Protocol::A2a),
                    StepKind::Renamed {
                        from: Some("DataPart"),
                        to: Some("EmbeddedResource"),
                    },
                );
            }
        }
    }
    out
}

fn a2a_file_to_mcp_content(file: &FilePart, report: &mut CrossProtocolReport) -> Option<Content> {
    if let Some(uri) = &file.file.uri {
        let mut link = ResourceLinkContent::new(uri.clone(), uri.clone());
        link.mime_type.clone_from(&file.file.mime_type);
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Renamed {
                from: Some("FilePart.uri"),
                to: Some("ResourceLink"),
            },
        );
        Some(Content::ResourceLink(link))
    } else if let Some(bytes) = &file.file.bytes {
        let mime = file
            .file
            .mime_type
            .as_deref()
            .unwrap_or("application/octet-stream");
        if mime.starts_with("image/") {
            report.record(
                Direction::ToMcp,
                Some(Protocol::A2a),
                StepKind::Renamed {
                    from: Some("FilePart.bytes(image)"),
                    to: Some("ImageContent"),
                },
            );
            Some(Content::Image(ImageContent::new(
                bytes.clone(),
                mime.to_owned(),
            )))
        } else if mime.starts_with("audio/") {
            report.record(
                Direction::ToMcp,
                Some(Protocol::A2a),
                StepKind::Renamed {
                    from: Some("FilePart.bytes(audio)"),
                    to: Some("AudioContent"),
                },
            );
            Some(Content::Audio(AudioContent::new(
                bytes.clone(),
                mime.to_owned(),
            )))
        } else {
            report.record(
                Direction::ToMcp,
                Some(Protocol::A2a),
                StepKind::Dropped {
                    from: Some("FilePart.bytes"),
                },
            );
            None
        }
    } else {
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Dropped {
                from: Some("FilePart (empty)"),
            },
        );
        None
    }
}

/// A2A `Message` → a single text payload, the join of every `TextPart`.
///
/// A2A messages are multimodal — the `Message` carries any number of
/// `Part` variants — but an MCP `tools/call` arguments payload is a single
/// JSON object. The conservative translation is to join every `TextPart`
/// into one block and drop the rest, recording the loss in `report`. A
/// caller that wants the full translation uses
/// [`a2a_message_to_mcp_content`] and a tool whose `inputSchema` accepts
/// an array of content blocks.
///
/// Returns `None` when the message has no text parts; the bridge treats
/// `None` as a refusal and surfaces a `-32602 Invalid params` to the peer.
pub fn a2a_message_to_mcp_text(
    message: &Message,
    report: &mut CrossProtocolReport,
) -> Option<TextContent> {
    let mut joined = String::new();
    let mut saw_non_text = false;
    for part in &message.parts {
        match part {
            Part::Text(text) => {
                if !joined.is_empty() {
                    joined.push('\n');
                }
                joined.push_str(&text.text);
            }
            Part::File(_) | Part::Data(_) => saw_non_text = true,
        }
    }
    if saw_non_text {
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Dropped {
                from: Some("non-text parts"),
            },
        );
    }
    if joined.is_empty() {
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Dropped {
                from: Some("text parts"),
            },
        );
        return None;
    }
    Some(TextContent::new(joined))
}

/// MCP `Content` → A2A `Part`.
///
/// The inverse of [`a2a_message_to_mcp_content`]. The mapping is
/// best-effort: every content block has a candidate A2A shape, but the
/// round-trip is lossy when the original A2A `Part` carried information
/// MCP has no slot for (the per-part `name` / `metadata` in a `FilePart`,
/// for example). The steps recorded in `report` are the loss summary.
pub fn mcp_content_to_a2a_part(
    content: &Content,
    report: &mut CrossProtocolReport,
) -> Option<Part> {
    match content {
        Content::Text(text) => {
            report.record(Direction::ToA2a, Some(Protocol::Mcp), StepKind::PassThrough);
            Some(Part::Text(TextPart {
                text: text.text.clone(),
                metadata: BTreeMap::new(),
            }))
        }
        Content::Image(image) => {
            report.record(
                Direction::ToA2a,
                Some(Protocol::Mcp),
                StepKind::Renamed {
                    from: Some("ImageContent"),
                    to: Some("FilePart(bytes, image/*)"),
                },
            );
            Some(Part::File(FilePart {
                file: a2a_bytes_file(image.mime_type.clone(), image.data.clone()),
                metadata: BTreeMap::new(),
            }))
        }
        Content::Audio(audio) => {
            report.record(
                Direction::ToA2a,
                Some(Protocol::Mcp),
                StepKind::Renamed {
                    from: Some("AudioContent"),
                    to: Some("FilePart(bytes, audio/*)"),
                },
            );
            Some(Part::File(FilePart {
                file: a2a_bytes_file(audio.mime_type.clone(), audio.data.clone()),
                metadata: BTreeMap::new(),
            }))
        }
        Content::ResourceLink(link) => {
            report.record(
                Direction::ToA2a,
                Some(Protocol::Mcp),
                StepKind::Renamed {
                    from: Some("ResourceLink"),
                    to: Some("FilePart(uri)"),
                },
            );
            Some(Part::File(FilePart {
                file: a2a_uri_file(link.uri.clone(), link.mime_type.clone()),
                metadata: BTreeMap::new(),
            }))
        }
        Content::EmbeddedResource(resource) => {
            let Ok(value) = serde_json::from_str::<Value>(&resource.uri) else {
                report.record(
                    Direction::ToA2a,
                    Some(Protocol::Mcp),
                    StepKind::Dropped {
                        from: Some("EmbeddedResource.uri"),
                    },
                );
                return None;
            };
            let Value::Object(map) = value else {
                report.record(
                    Direction::ToA2a,
                    Some(Protocol::Mcp),
                    StepKind::Dropped {
                        from: Some("EmbeddedResource.uri (not an object)"),
                    },
                );
                return None;
            };
            report.record(
                Direction::ToA2a,
                Some(Protocol::Mcp),
                StepKind::Renamed {
                    from: Some("EmbeddedResource"),
                    to: Some("DataPart"),
                },
            );
            Some(Part::Data(DataPart {
                data: Value::Object(map),
                metadata: BTreeMap::new(),
            }))
        }
        Content::Unknown(_) => {
            report.record(
                Direction::ToA2a,
                Some(Protocol::Mcp),
                StepKind::Dropped {
                    from: Some("Unknown"),
                },
            );
            None
        }
    }
}

const fn a2a_bytes_file(mime: String, bytes: String) -> crate::domain::a2a::message::FileContent {
    crate::domain::a2a::message::FileContent {
        name: None,
        mime_type: Some(mime),
        bytes: Some(bytes),
        uri: None,
    }
}

const fn a2a_uri_file(
    uri: String,
    mime: Option<String>,
) -> crate::domain::a2a::message::FileContent {
    crate::domain::a2a::message::FileContent {
        name: None,
        mime_type: mime,
        bytes: None,
        uri: Some(uri),
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::*;
    use crate::domain::a2a::message::{FileContent, FilePart, MessageRole};

    /// All the error types the tests below can return. A single enum keeps
    /// `From` impls narrow and the test signatures short.
    enum Failure {
        Skill(crate::domain::ir::JsonObjectError),
        Mismatch(String),
    }

    impl core::fmt::Display for Failure {
        fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            match self {
                Self::Skill(err) => core::fmt::Display::fmt(err, formatter),
                Self::Mismatch(message) => formatter.write_str(message),
            }
        }
    }

    impl core::fmt::Debug for Failure {
        fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
            core::fmt::Debug::fmt(&self.mismatch_or_skill(), formatter)
        }
    }

    impl Failure {
        fn mismatch_or_skill(&self) -> String {
            match self {
                Self::Skill(err) => format!("{err}"),
                Self::Mismatch(message) => message.clone(),
            }
        }
    }

    impl From<crate::domain::ir::JsonObjectError> for Failure {
        fn from(err: crate::domain::ir::JsonObjectError) -> Self {
            Self::Skill(err)
        }
    }

    fn skill(id: &str, name: &str, description: &str, tags: &[&str]) -> AgentSkill {
        AgentSkill {
            id: Arc::from(id),
            name: Arc::from(name),
            description: Arc::from(description),
            tags: tags.iter().map(|t| Arc::from(*t)).collect(),
        }
    }

    #[test]
    fn skill_to_tool_maps_required_fields_and_records_steps() -> Result<(), Failure> {
        let s = skill("lookup", "Lookup", "Look up a record", &["search", "read"]);
        let mut report = CrossProtocolReport::new();
        let tool = a2a_skill_to_mcp_tool(&s, &mut report)?;

        if tool.name.as_str() != "lookup" {
            return Err(Failure::Mismatch(format!(
                "expected tool.name 'lookup', got {:?}",
                tool.name
            )));
        }
        if tool.title.as_deref() != Some("Lookup") {
            return Err(Failure::Mismatch(format!(
                "expected tool.title 'Lookup', got {:?}",
                tool.title
            )));
        }
        if tool.description.as_deref() != Some("Look up a record") {
            return Err(Failure::Mismatch(format!(
                "expected tool.description, got {:?}",
                tool.description
            )));
        }
        if report.is_empty() {
            return Err(Failure::Mismatch(
                "expected the steps to be recorded, got an empty report".to_owned(),
            ));
        }
        if tool.extensions.get("tags").is_none() {
            return Err(Failure::Mismatch(
                "expected tags to be recorded in extensions".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn skill_to_tool_with_empty_name_drops_title() -> Result<(), Failure> {
        let s = skill("lookup", "", "Look up a record", &[]);
        let mut report = CrossProtocolReport::new();
        let tool = a2a_skill_to_mcp_tool(&s, &mut report)?;
        if tool.title.is_some() {
            return Err(Failure::Mismatch("expected title to be None".to_owned()));
        }
        if !report
            .steps()
            .iter()
            .any(|s| matches!(&s.note, StepKind::Dropped { from: Some("name") }))
        {
            return Err(Failure::Mismatch(
                "expected a Dropped(name) step".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn skill_to_tool_with_empty_description_drops_description() -> Result<(), Failure> {
        let s = skill("lookup", "Lookup", "", &[]);
        let mut report = CrossProtocolReport::new();
        let tool = a2a_skill_to_mcp_tool(&s, &mut report)?;
        if tool.description.is_some() {
            return Err(Failure::Mismatch(
                "expected description to be None".to_owned(),
            ));
        }
        if !report.steps().iter().any(|s| {
            matches!(
                &s.note,
                StepKind::Dropped {
                    from: Some("description")
                }
            )
        }) {
            return Err(Failure::Mismatch(
                "expected a Dropped(description) step".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn message_to_content_text_round_trips() -> Result<(), Failure> {
        let m = Message {
            role: MessageRole::User,
            parts: vec![Part::Text(TextPart {
                text: "hello".to_owned(),
                metadata: BTreeMap::new(),
            })],
            metadata: BTreeMap::new(),
        };
        let mut report = CrossProtocolReport::new();
        let blocks = a2a_message_to_mcp_content(&m, &mut report);
        if blocks.len() != 1 {
            return Err(Failure::Mismatch(format!(
                "expected 1 block, got {}",
                blocks.len()
            )));
        }
        let Some(first) = blocks.first() else {
            return Err(Failure::Mismatch(
                "expected at least one content block".to_owned(),
            ));
        };
        let Content::Text(t) = first else {
            return Err(Failure::Mismatch(format!("expected Text, got {first:?}")));
        };
        if t.text != "hello" {
            return Err(Failure::Mismatch(format!(
                "expected 'hello', got {:?}",
                t.text
            )));
        }
        if report.is_empty() {
            return Err(Failure::Mismatch("expected a pass-through step".to_owned()));
        }
        Ok(())
    }

    #[test]
    fn message_to_content_image_file_becomes_image_content() -> Result<(), Failure> {
        let file = FilePart {
            file: FileContent {
                name: None,
                mime_type: Some("image/png".to_owned()),
                bytes: Some("AAAA".to_owned()),
                uri: None,
            },
            metadata: BTreeMap::new(),
        };
        let m = Message {
            role: MessageRole::User,
            parts: vec![Part::File(file)],
            metadata: BTreeMap::new(),
        };
        let mut report = CrossProtocolReport::new();
        let blocks = a2a_message_to_mcp_content(&m, &mut report);
        if !blocks.iter().any(|b| matches!(b, Content::Image(_))) {
            return Err(Failure::Mismatch(
                "expected at least one ImageContent block".to_owned(),
            ));
        }
        if !report.steps().iter().any(|s| {
            matches!(
                &s.note,
                StepKind::Renamed {
                    from: Some(_),
                    to: Some("ImageContent")
                }
            )
        }) {
            return Err(Failure::Mismatch(
                "expected a Renamed → ImageContent step".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn message_to_content_uri_file_becomes_resource_link() -> Result<(), Failure> {
        let file = FilePart {
            file: FileContent {
                name: None,
                mime_type: Some("application/pdf".to_owned()),
                bytes: None,
                uri: Some("https://example.com/x".to_owned()),
            },
            metadata: BTreeMap::new(),
        };
        let m = Message {
            role: MessageRole::User,
            parts: vec![Part::File(file)],
            metadata: BTreeMap::new(),
        };
        let mut report = CrossProtocolReport::new();
        let blocks = a2a_message_to_mcp_content(&m, &mut report);
        if !blocks.iter().any(|b| matches!(b, Content::ResourceLink(_))) {
            return Err(Failure::Mismatch(
                "expected at least one ResourceLink block".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn message_to_content_unknown_file_type_is_dropped() -> Result<(), Failure> {
        let file = FilePart {
            file: FileContent {
                name: None,
                mime_type: Some("application/x-binary".to_owned()),
                bytes: Some("AAAA".to_owned()),
                uri: None,
            },
            metadata: BTreeMap::new(),
        };
        let m = Message {
            role: MessageRole::User,
            parts: vec![Part::File(file)],
            metadata: BTreeMap::new(),
        };
        let mut report = CrossProtocolReport::new();
        let blocks = a2a_message_to_mcp_content(&m, &mut report);
        if !blocks.is_empty() {
            return Err(Failure::Mismatch(format!(
                "expected no blocks, got {}",
                blocks.len()
            )));
        }
        if !report.steps().iter().any(|s| {
            matches!(
                &s.note,
                StepKind::Dropped {
                    from: Some("FilePart.bytes")
                }
            )
        }) {
            return Err(Failure::Mismatch(
                "expected a Dropped(FilePart.bytes) step".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn message_to_content_data_becomes_embedded_resource() -> Result<(), Failure> {
        let mut map = serde_json::Map::new();
        map.insert("k".to_owned(), Value::String("v".to_owned()));
        let m = Message {
            role: MessageRole::User,
            parts: vec![Part::Data(DataPart {
                data: Value::Object(map),
                metadata: BTreeMap::new(),
            })],
            metadata: BTreeMap::new(),
        };
        let mut report = CrossProtocolReport::new();
        let blocks = a2a_message_to_mcp_content(&m, &mut report);
        if !blocks
            .iter()
            .any(|b| matches!(b, Content::EmbeddedResource(_)))
        {
            return Err(Failure::Mismatch(
                "expected an EmbeddedResource block".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn message_to_text_joins_text_parts_and_drops_others() -> Result<(), Failure> {
        let m = Message {
            role: MessageRole::User,
            parts: vec![
                Part::Text(TextPart {
                    text: "first".to_owned(),
                    metadata: BTreeMap::new(),
                }),
                Part::Text(TextPart {
                    text: "second".to_owned(),
                    metadata: BTreeMap::new(),
                }),
                Part::File(FilePart {
                    file: FileContent {
                        name: None,
                        mime_type: None,
                        bytes: None,
                        uri: Some("https://example.com/x".to_owned()),
                    },
                    metadata: BTreeMap::new(),
                }),
            ],
            metadata: BTreeMap::new(),
        };
        let mut report = CrossProtocolReport::new();
        let joined = a2a_message_to_mcp_text(&m, &mut report);
        let Some(joined) = joined else {
            return Err(Failure::Mismatch("expected Some(text)".to_owned()));
        };
        if joined.text != "first\nsecond" {
            return Err(Failure::Mismatch(format!(
                "expected 'first\\nsecond', got {:?}",
                joined.text
            )));
        }
        if !report.steps().iter().any(|s| {
            matches!(
                &s.note,
                StepKind::Dropped {
                    from: Some("non-text parts")
                }
            )
        }) {
            return Err(Failure::Mismatch(
                "expected a Dropped(non-text parts) step".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn message_to_text_with_only_non_text_returns_none() -> Result<(), Failure> {
        let m = Message {
            role: MessageRole::User,
            parts: vec![Part::File(FilePart {
                file: FileContent {
                    name: None,
                    mime_type: None,
                    bytes: None,
                    uri: Some("https://example.com/x".to_owned()),
                },
                metadata: BTreeMap::new(),
            })],
            metadata: BTreeMap::new(),
        };
        let mut report = CrossProtocolReport::new();
        let joined = a2a_message_to_mcp_text(&m, &mut report);
        if joined.is_some() {
            return Err(Failure::Mismatch("expected None".to_owned()));
        }
        Ok(())
    }

    #[test]
    fn content_to_part_text_round_trips() -> Result<(), Failure> {
        let block = Content::Text(TextContent::new("hi"));
        let mut report = CrossProtocolReport::new();
        let part = mcp_content_to_a2a_part(&block, &mut report);
        let Some(Part::Text(text)) = part else {
            return Err(Failure::Mismatch(format!(
                "expected TextPart, got {part:?}"
            )));
        };
        if text.text != "hi" {
            return Err(Failure::Mismatch(format!(
                "expected 'hi', got {:?}",
                text.text
            )));
        }
        Ok(())
    }

    #[test]
    fn content_to_part_image_becomes_file_part() -> Result<(), Failure> {
        let block = Content::Image(ImageContent::new("AAAA".to_owned(), "image/png".to_owned()));
        let mut report = CrossProtocolReport::new();
        let part = mcp_content_to_a2a_part(&block, &mut report);
        let Some(Part::File(f)) = part else {
            return Err(Failure::Mismatch(format!(
                "expected FilePart, got {part:?}"
            )));
        };
        if f.file.mime_type.as_deref() != Some("image/png") {
            return Err(Failure::Mismatch(format!(
                "expected image/png, got {:?}",
                f.file.mime_type
            )));
        }
        if f.file.bytes.is_none() {
            return Err(Failure::Mismatch("expected bytes".to_owned()));
        }
        if f.file.uri.is_some() {
            return Err(Failure::Mismatch("expected no uri".to_owned()));
        }
        Ok(())
    }

    #[test]
    fn content_to_part_resource_link_becomes_uri_file() -> Result<(), Failure> {
        let mut link = ResourceLinkContent::new("https://example.com/x".to_owned(), "x".to_owned());
        link.mime_type = Some("application/pdf".to_owned());
        let block = Content::ResourceLink(link);
        let mut report = CrossProtocolReport::new();
        let part = mcp_content_to_a2a_part(&block, &mut report);
        let Some(Part::File(f)) = part else {
            return Err(Failure::Mismatch(format!(
                "expected FilePart, got {part:?}"
            )));
        };
        if f.file.uri.as_deref() != Some("https://example.com/x") {
            return Err(Failure::Mismatch(format!(
                "expected uri, got {:?}",
                f.file.uri
            )));
        }
        if f.file.bytes.is_some() {
            return Err(Failure::Mismatch("expected no bytes".to_owned()));
        }
        Ok(())
    }

    #[test]
    fn report_merge_combines_steps() -> Result<(), Failure> {
        let mut a = CrossProtocolReport::new();
        a.record(Direction::ToMcp, Some(Protocol::A2a), StepKind::PassThrough);
        let mut b = CrossProtocolReport::new();
        b.record(
            Direction::ToA2a,
            Some(Protocol::Mcp),
            StepKind::Dropped { from: Some("x") },
        );
        a.merge(b);
        if a.len() != 2 {
            return Err(Failure::Mismatch(format!(
                "expected 2 steps after merge, got {}",
                a.len()
            )));
        }
        Ok(())
    }

    #[test]
    fn report_display_shows_steps() -> Result<(), Failure> {
        let mut report = CrossProtocolReport::new();
        report.record(
            Direction::ToMcp,
            Some(Protocol::A2a),
            StepKind::Dropped { from: Some("name") },
        );
        let s = report.to_string();
        if !s.contains("a2a→mcp") {
            return Err(Failure::Mismatch(
                "expected 'a2a→mcp' in the report".to_owned(),
            ));
        }
        if !s.contains("dropped") {
            return Err(Failure::Mismatch(
                "expected 'dropped' in the report".to_owned(),
            ));
        }
        Ok(())
    }

    #[test]
    fn protocol_other_swaps() -> Result<(), Failure> {
        if Protocol::A2a.other() != Protocol::Mcp {
            return Err(Failure::Mismatch("A2a.other() should be Mcp".to_owned()));
        }
        if Protocol::Mcp.other() != Protocol::A2a {
            return Err(Failure::Mismatch("Mcp.other() should be A2a".to_owned()));
        }
        Ok(())
    }
}