mcp-cli 0.3.0

Interactive CLI debugger and TUI for MCP servers
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
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
//! Test command implementation for automated MCP server testing

use crate::cli::TestArgs;
use anyhow::Result;
use indicatif::{ProgressBar, ProgressStyle};
use mcp_probe_core::{
    client::McpClient,
    messages::{
        prompts::{ListPromptsRequest, ListPromptsResponse, Prompt},
        resources::{ListResourcesRequest, ListResourcesResponse, Resource},
        tools::{ListToolsRequest, ListToolsResponse, Tool},
        Implementation,
    },
    transport::TransportConfig,
    McpResult,
};
use serde_json::Value;
use std::time::{Duration, Instant};
use tabled::{Table, Tabled};

/// Test result status
#[derive(Debug, Clone, PartialEq)]
pub enum TestStatus {
    Pass,
    Fail,
    Skip,
    Warning,
}

impl TestStatus {
    pub fn icon(&self) -> &'static str {
        match self {
            Self::Pass => "",
            Self::Fail => "",
            Self::Skip => "⏭️",
            Self::Warning => "⚠️",
        }
    }

    pub fn name(&self) -> &'static str {
        match self {
            Self::Pass => "PASS",
            Self::Fail => "FAIL",
            Self::Skip => "SKIP",
            Self::Warning => "WARN",
        }
    }
}

/// Individual test result
#[derive(Debug, Clone)]
pub struct TestResult {
    pub name: String,
    pub status: TestStatus,
    pub message: String,
    pub duration: Duration,
    pub details: Option<Value>,
}

/// Table row for displaying test results
#[derive(Tabled)]
pub struct TestTableRow {
    #[tabled(rename = "Status")]
    pub status: String,
    #[tabled(rename = "Test")]
    pub name: String,
    #[tabled(rename = "Duration")]
    pub duration: String,
    #[tabled(rename = "Message")]
    pub message: String,
}

/// Table row for displaying summary statistics
#[derive(Tabled)]
pub struct SummaryTableRow {
    #[tabled(rename = "")]
    pub icon: String,
    #[tabled(rename = "Metric")]
    pub metric: String,
    #[tabled(rename = "Value")]
    pub value: String,
}

/// Protocol information for display
#[derive(Debug, Clone)]
pub struct ProtocolInfo {
    pub version: String,
    pub spec_date: String,
    pub endpoints: Vec<String>,
    pub session_management: String,
    pub supported_methods: Vec<String>,
}

/// Table row for displaying protocol information
#[derive(Tabled)]
pub struct ProtocolTableRow {
    #[tabled(rename = "Protocol")]
    pub protocol: String,
    #[tabled(rename = "Spec Version")]
    pub spec_version: String,
    #[tabled(rename = "Endpoints")]
    pub endpoints: String,
    #[tabled(rename = "Session Type")]
    pub session_type: String,
}

/// Table row for displaying supported methods
#[derive(Tabled)]
pub struct MethodTableRow {
    #[tabled(rename = "Method")]
    pub method: String,
    #[tabled(rename = "Description")]
    pub description: String,
    #[tabled(rename = "Protocol Support")]
    pub protocol_support: String,
}

/// Table row for displaying example URLs
#[derive(Tabled)]
pub struct ExampleUrlRow {
    #[tabled(rename = "Protocol")]
    pub protocol: String,
    #[tabled(rename = "Example URL")]
    pub example_url: String,
    #[tabled(rename = "Description")]
    pub description: String,
}

/// Extension trait to add higher-level methods to McpClient
trait McpClientExt {
    async fn list_tools(&mut self) -> McpResult<Vec<Tool>>;
    async fn list_resources(&mut self) -> McpResult<Vec<Resource>>;
    async fn list_prompts(&mut self) -> McpResult<Vec<Prompt>>;
}

impl McpClientExt for McpClient {
    async fn list_tools(&mut self) -> McpResult<Vec<Tool>> {
        let request = ListToolsRequest { cursor: None };
        let response = self.send_request("tools/list", request).await?;

        if let Some(result) = response.result {
            let list_response: ListToolsResponse = serde_json::from_value(result)?;
            Ok(list_response.tools)
        } else {
            Ok(Vec::new())
        }
    }

    async fn list_resources(&mut self) -> McpResult<Vec<Resource>> {
        let request = ListResourcesRequest { cursor: None };
        let response = self.send_request("resources/list", request).await?;

        if let Some(result) = response.result {
            let list_response: ListResourcesResponse = serde_json::from_value(result)?;
            Ok(list_response.resources)
        } else {
            Ok(Vec::new())
        }
    }

    async fn list_prompts(&mut self) -> McpResult<Vec<Prompt>> {
        let request = ListPromptsRequest { cursor: None };
        let response = self.send_request("prompts/list", request).await?;

        if let Some(result) = response.result {
            let list_response: ListPromptsResponse = serde_json::from_value(result)?;
            Ok(list_response.prompts)
        } else {
            Ok(Vec::new())
        }
    }
}

/// Execute the test command
pub async fn run(args: TestArgs) -> Result<()> {
    // Handle discovery mode
    if let Some(base_url) = &args.discover {
        return run_discovery_tests(base_url, &args).await;
    }

    let start_time = Instant::now();
    let mut results = Vec::new();

    tracing::info!("Starting MCP test suite");

    let transport_config = args.transport.to_transport_config()?;
    tracing::info!("Using transport: {}", transport_config.transport_type());

    println!("🧪 MCP Test Suite");
    println!("🔌 Transport: {}", transport_config.transport_type());

    if let Some(suite) = &args.suite {
        println!("📋 Running test suite: {}", suite);
    } else {
        println!("📋 Running all tests");
    }

    if args.report {
        println!("📊 Test report generation enabled");
    }

    if args.fail_fast {
        println!("⚡ Fail-fast mode enabled");
    }

    println!();

    // Display protocol information
    display_protocol_information(&transport_config);

    // Create client info
    let client_info = Implementation {
        name: "mcp-probe".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
        metadata: std::collections::HashMap::new(),
    };

    // Phase 1: Connection - Use connection spinner
    let connection_spinner = create_connection_spinner();
    update_spinner_message(
        &connection_spinner,
        "connecting",
        "Establishing MCP connection",
    );

    let mut client = match test_connection(&transport_config, &client_info, &mut results).await {
        Ok(client) => {
            connection_spinner.finish_with_message("✅ Connection established successfully!");
            client
        }
        Err(_) => {
            connection_spinner.finish_with_message("❌ Connection failed - check server status");
            print_results(&results, start_time.elapsed());
            return Ok(());
        }
    };

    // Phase 2: Discovery - Use discovery spinner
    let discovery_spinner = create_discovery_spinner();
    update_spinner_message(
        &discovery_spinner,
        "discovering",
        "Analyzing server capabilities",
    );
    test_capability_discovery(&mut client, &mut results).await;
    discovery_spinner.finish_with_message("🔍 Server capabilities discovered");

    // Phase 3: Testing - Use testing spinner for all tests
    let testing_spinner = create_testing_spinner();

    // Test 3: Tools Listing
    update_spinner_message(
        &testing_spinner,
        "testing_tools",
        "Querying available tools",
    );
    test_tools_listing(&mut client, &mut results).await;

    // Test 4: Resources Listing
    update_spinner_message(
        &testing_spinner,
        "testing_resources",
        "Scanning resource catalog",
    );
    test_resources_listing(&mut client, &mut results).await;

    // Test 5: Prompts Listing
    update_spinner_message(
        &testing_spinner,
        "testing_prompts",
        "Loading prompt templates",
    );
    test_prompts_listing(&mut client, &mut results).await;

    // Test 6: Error Handling
    update_spinner_message(&testing_spinner, "validating", "Testing error scenarios");
    test_error_handling(&mut client, &mut results).await;

    testing_spinner.finish_with_message("🧪 All functional tests completed");

    // Phase 4: Success - Use celebration spinner
    let success_spinner = create_success_spinner();
    update_spinner_message(&success_spinner, "success", "Preparing final report");

    // Small delay to show the success animation
    tokio::time::sleep(std::time::Duration::from_millis(800)).await;
    success_spinner.finish_with_message("🎉 Test suite completed successfully!");

    // Print final results
    let total_duration = start_time.elapsed();
    print_results(&results, total_duration);

    // Generate report if requested
    if args.report {
        generate_report(
            &results,
            total_duration,
            &transport_config,
            args.output_dir.as_ref(),
        )?;
    }

    // Check fail-fast mode
    if args.fail_fast && results.iter().any(|r| r.status == TestStatus::Fail) {
        std::process::exit(1);
    }

    Ok(())
}

/// Create a connection spinner with network-themed animation
fn create_connection_spinner() -> ProgressBar {
    let spinner = ProgressBar::new_spinner();
    spinner.set_style(
        ProgressStyle::default_spinner()
            .tick_strings(&[
                "📡", "📶", "🌐", "🔗", "📡", "📶", "🌐", "🔗", "", "🌊", "📡", "🔌", "", "🌊",
                "📡", "🔌",
            ])
            .template("{spinner:.green.bold} {msg:.green}")
            .expect("Failed to create connection spinner template"),
    );
    spinner.enable_steady_tick(std::time::Duration::from_millis(150));
    spinner
}

/// Create a discovery spinner with search-themed animation
fn create_discovery_spinner() -> ProgressBar {
    let spinner = ProgressBar::new_spinner();
    spinner.set_style(
        ProgressStyle::default_spinner()
            .tick_strings(&[
                "🔍", "🔎", "🕵️", "🔬", "🔍", "🔎", "🕵️", "🔬", "💡", "🔍", "💡", "🔎", "💡", "🕵️",
                "💡", "🔬",
            ])
            .template("{spinner:.yellow.bold} {msg:.yellow}")
            .expect("Failed to create discovery spinner template"),
    );
    spinner.enable_steady_tick(std::time::Duration::from_millis(110));
    spinner
}

/// Create a testing spinner with gear-themed animation
fn create_testing_spinner() -> ProgressBar {
    let spinner = ProgressBar::new_spinner();
    spinner.set_style(
        ProgressStyle::default_spinner()
            .tick_strings(&[
                "⚙️", "🔧", "🛠️", "", "⚙️", "🔧", "🛠️", "", "🧪", "🔬", "📊", "", "🧪", "🔬",
                "📊", "",
            ])
            .template("{spinner:.blue.bold} {msg:.blue}")
            .expect("Failed to create testing spinner template"),
    );
    spinner.enable_steady_tick(std::time::Duration::from_millis(90));
    spinner
}

/// Create a success spinner with celebration animation
fn create_success_spinner() -> ProgressBar {
    let spinner = ProgressBar::new_spinner();
    spinner.set_style(
        ProgressStyle::default_spinner()
            .tick_strings(&[
                "🎉", "🎊", "", "🌟", "🎉", "🎊", "", "🌟", "🚀", "💫", "", "🔥", "🚀", "💫",
                "", "🔥",
            ])
            .template("{spinner:.green.bold} {msg:.green.bold}")
            .expect("Failed to create success spinner template"),
    );
    spinner.enable_steady_tick(std::time::Duration::from_millis(100));
    spinner
}

/// Update spinner with dynamic message and emoji
fn update_spinner_message(spinner: &ProgressBar, phase: &str, detail: &str) {
    let message = match phase {
        "connecting" => format!("🔌 Connecting to MCP server... {}", detail),
        "initializing" => format!("🤝 Initializing MCP session... {}", detail),
        "discovering" => format!("🔍 Discovering server capabilities... {}", detail),
        "testing_tools" => format!("🛠️  Testing tools endpoint... {}", detail),
        "testing_resources" => format!("📁 Testing resources endpoint... {}", detail),
        "testing_prompts" => format!("📋 Testing prompts endpoint... {}", detail),
        "validating" => format!("✅ Validating responses... {}", detail),
        "finalizing" => format!("📊 Generating report... {}", detail),
        "success" => format!("🎉 All tests completed! {}", detail),
        _ => format!("⚡ Running {} ... {}", phase, detail),
    };
    spinner.set_message(message);
}

/// Test connection and initialization
async fn test_connection(
    transport_config: &TransportConfig,
    client_info: &Implementation,
    results: &mut Vec<TestResult>,
) -> Result<McpClient> {
    let test_start = Instant::now();

    match McpClient::with_defaults(transport_config.clone()).await {
        Ok(mut client) => {
            results.push(TestResult {
                name: "Connection".to_string(),
                status: TestStatus::Pass,
                message: "Successfully created MCP client".to_string(),
                duration: test_start.elapsed(),
                details: None,
            });

            // Test initialization
            let init_start = Instant::now();
            match client.connect(client_info.clone()).await {
                Ok(server_info) => {
                    results.push(TestResult {
                        name: "Initialization".to_string(),
                        status: TestStatus::Pass,
                        message: format!(
                            "Connected to {} v{}",
                            server_info.implementation.name, server_info.implementation.version
                        ),
                        duration: init_start.elapsed(),
                        details: Some(serde_json::json!({
                            "name": server_info.implementation.name,
                            "version": server_info.implementation.version,
                            "protocol_version": server_info.protocol_version
                        })),
                    });
                    Ok(client)
                }
                Err(e) => {
                    results.push(TestResult {
                        name: "Initialization".to_string(),
                        status: TestStatus::Fail,
                        message: format!("Failed to initialize: {}", e),
                        duration: init_start.elapsed(),
                        details: None,
                    });
                    Err(e.into())
                }
            }
        }
        Err(e) => {
            results.push(TestResult {
                name: "Connection".to_string(),
                status: TestStatus::Fail,
                message: format!("Failed to create client: {}", e),
                duration: test_start.elapsed(),
                details: None,
            });
            Err(e.into())
        }
    }
}

/// Test capability discovery
async fn test_capability_discovery(_client: &mut McpClient, results: &mut Vec<TestResult>) {
    // This would test server capabilities reported during initialization
    results.push(TestResult {
        name: "Capability Discovery".to_string(),
        status: TestStatus::Pass,
        message: "Server capabilities discovered successfully".to_string(),
        duration: Duration::from_millis(1),
        details: None,
    });
}

/// Test tools listing
async fn test_tools_listing(client: &mut McpClient, results: &mut Vec<TestResult>) {
    let test_start = Instant::now();

    match client.list_tools().await {
        Ok(tools) => {
            if tools.is_empty() {
                results.push(TestResult {
                    name: "Tools Listing".to_string(),
                    status: TestStatus::Warning,
                    message: "No tools available".to_string(),
                    duration: test_start.elapsed(),
                    details: None,
                });
            } else {
                results.push(TestResult {
                    name: "Tools Listing".to_string(),
                    status: TestStatus::Pass,
                    message: format!("Successfully listed {} tools", tools.len()),
                    duration: test_start.elapsed(),
                    details: Some(serde_json::to_value(&tools).unwrap_or_default()),
                });
            }
        }
        Err(e) => {
            let status = if e.to_string().contains("Method not found") {
                TestStatus::Skip
            } else {
                TestStatus::Fail
            };

            results.push(TestResult {
                name: "Tools Listing".to_string(),
                status,
                message: format!("Tools listing: {}", e),
                duration: test_start.elapsed(),
                details: None,
            });
        }
    }
}

/// Test resources listing
async fn test_resources_listing(client: &mut McpClient, results: &mut Vec<TestResult>) {
    let test_start = Instant::now();

    match client.list_resources().await {
        Ok(resources) => {
            if resources.is_empty() {
                results.push(TestResult {
                    name: "Resources Listing".to_string(),
                    status: TestStatus::Warning,
                    message: "No resources available".to_string(),
                    duration: test_start.elapsed(),
                    details: None,
                });
            } else {
                results.push(TestResult {
                    name: "Resources Listing".to_string(),
                    status: TestStatus::Pass,
                    message: format!("Successfully listed {} resources", resources.len()),
                    duration: test_start.elapsed(),
                    details: Some(serde_json::to_value(&resources).unwrap_or_default()),
                });
            }
        }
        Err(e) => {
            let status = if e.to_string().contains("Method not found") {
                TestStatus::Skip
            } else {
                TestStatus::Fail
            };

            results.push(TestResult {
                name: "Resources Listing".to_string(),
                status,
                message: format!("Resources listing: {}", e),
                duration: test_start.elapsed(),
                details: None,
            });
        }
    }
}

/// Test prompts listing
async fn test_prompts_listing(client: &mut McpClient, results: &mut Vec<TestResult>) {
    let test_start = Instant::now();

    match client.list_prompts().await {
        Ok(prompts) => {
            if prompts.is_empty() {
                results.push(TestResult {
                    name: "Prompts Listing".to_string(),
                    status: TestStatus::Warning,
                    message: "No prompts available".to_string(),
                    duration: test_start.elapsed(),
                    details: None,
                });
            } else {
                results.push(TestResult {
                    name: "Prompts Listing".to_string(),
                    status: TestStatus::Pass,
                    message: format!("Successfully listed {} prompts", prompts.len()),
                    duration: test_start.elapsed(),
                    details: Some(serde_json::to_value(&prompts).unwrap_or_default()),
                });
            }
        }
        Err(e) => {
            let status = if e.to_string().contains("Method not found") {
                TestStatus::Skip
            } else {
                TestStatus::Fail
            };

            results.push(TestResult {
                name: "Prompts Listing".to_string(),
                status,
                message: format!("Prompts listing: {}", e),
                duration: test_start.elapsed(),
                details: None,
            });
        }
    }
}

/// Test error handling
async fn test_error_handling(client: &mut McpClient, results: &mut Vec<TestResult>) {
    let test_start = Instant::now();

    // Test invalid method
    match client
        .send_request("invalid/method", serde_json::Value::Null)
        .await
    {
        Ok(response) => {
            if let Some(error) = response.error {
                if error.code == -32601 {
                    results.push(TestResult {
                        name: "Error Handling".to_string(),
                        status: TestStatus::Pass,
                        message: "Correctly handles invalid methods".to_string(),
                        duration: test_start.elapsed(),
                        details: None,
                    });
                } else {
                    results.push(TestResult {
                        name: "Error Handling".to_string(),
                        status: TestStatus::Warning,
                        message: format!("Unexpected error code: {}", error.code),
                        duration: test_start.elapsed(),
                        details: Some(serde_json::to_value(&error).unwrap_or_default()),
                    });
                }
            } else {
                results.push(TestResult {
                    name: "Error Handling".to_string(),
                    status: TestStatus::Fail,
                    message: "Should return error for invalid methods".to_string(),
                    duration: test_start.elapsed(),
                    details: None,
                });
            }
        }
        Err(e) => {
            results.push(TestResult {
                name: "Error Handling".to_string(),
                status: TestStatus::Warning,
                message: format!("Transport error during error handling test: {}", e),
                duration: test_start.elapsed(),
                details: None,
            });
        }
    }
}

/// Print test results using a neat table
fn print_results(results: &[TestResult], total_duration: Duration) {
    println!("\n📊 MCP Test Results");
    println!("═══════════════════");

    if results.is_empty() {
        println!("⚠️  No tests were run");
        return;
    }

    // Convert results to table rows
    let table_rows: Vec<TestTableRow> = results
        .iter()
        .map(|result| TestTableRow {
            status: format!("{} {}", result.status.icon(), result.status.name()),
            name: result.name.clone(),
            duration: format!("{:.2}ms", result.duration.as_secs_f64() * 1000.0),
            message: result.message.clone(),
        })
        .collect();

    // Create and display table
    let table = Table::new(table_rows)
        .with(tabled::settings::Style::rounded())
        .with(tabled::settings::Padding::new(1, 1, 0, 0))
        .to_string();

    println!("{}", table);

    // Summary statistics
    let passed = results
        .iter()
        .filter(|r| r.status == TestStatus::Pass)
        .count();
    let failed = results
        .iter()
        .filter(|r| r.status == TestStatus::Fail)
        .count();
    let skipped = results
        .iter()
        .filter(|r| r.status == TestStatus::Skip)
        .count();
    let warnings = results
        .iter()
        .filter(|r| r.status == TestStatus::Warning)
        .count();

    println!("\n📈 Summary");
    println!("═══════════");

    // Create summary table
    let summary_rows = vec![
        SummaryTableRow {
            metric: "Total Tests".to_string(),
            value: results.len().to_string(),
            icon: "📋".to_string(),
        },
        SummaryTableRow {
            metric: "Passed".to_string(),
            value: passed.to_string(),
            icon: "".to_string(),
        },
        SummaryTableRow {
            metric: "Failed".to_string(),
            value: failed.to_string(),
            icon: "".to_string(),
        },
        SummaryTableRow {
            metric: "Skipped".to_string(),
            value: skipped.to_string(),
            icon: "⏭️".to_string(),
        },
        SummaryTableRow {
            metric: "Warnings".to_string(),
            value: warnings.to_string(),
            icon: "⚠️".to_string(),
        },
        SummaryTableRow {
            metric: "Duration".to_string(),
            value: format!("{:.2}s", total_duration.as_secs_f64()),
            icon: "⏱️".to_string(),
        },
    ];

    let summary_table = Table::new(summary_rows)
        .with(tabled::settings::Style::rounded())
        .with(tabled::settings::Padding::new(1, 1, 0, 0))
        .to_string();

    println!("{}", summary_table);

    let success_rate = (passed as f64 / results.len() as f64) * 100.0;
    println!("\n📊 Success Rate: {:.1}%", success_rate);

    if failed == 0 {
        println!("🎉 All critical tests passed!");
    } else {
        println!("❌ Some tests failed - review results above");
    }
}

/// Generate test report
fn generate_report(
    results: &[TestResult],
    duration: Duration,
    transport_config: &TransportConfig,
    output_dir: Option<&std::path::PathBuf>,
) -> Result<()> {
    use crate::paths::get_mcp_probe_paths;
    use std::fs;
    use std::io::Write;

    let output_path = if let Some(dir) = output_dir {
        // If user specifies output dir, use it but still add date prefix
        let date = chrono::Utc::now().format("%Y%m%d");
        let timestamp = chrono::Utc::now().format("%H%M%S");
        dir.join(format!("{}-test-report-{}.json", date, timestamp))
    } else {
        // Use centralized path management with date prefix
        let paths = get_mcp_probe_paths()?;
        paths.report_file("test-report", "json")
    };

    let current_protocol = detect_protocol_info(transport_config);
    let all_protocols = get_all_protocol_versions();

    let report = serde_json::json!({
        "metadata": {
            "generated_at": chrono::Utc::now().to_rfc3339(),
            "transport_type": transport_config.transport_type(),
            "total_duration_ms": duration.as_millis(),
            "mcp_probe_version": env!("CARGO_PKG_VERSION")
        },
        "protocol_info": {
            "detected_protocol": {
                "version": current_protocol.version,
                "spec_date": current_protocol.spec_date,
                "endpoints": current_protocol.endpoints,
                "session_management": current_protocol.session_management,
                "supported_methods": current_protocol.supported_methods
            },
            "available_protocols": all_protocols.iter().map(|p| serde_json::json!({
                "version": p.version,
                "spec_date": p.spec_date,
                "endpoints": p.endpoints,
                "session_management": p.session_management,
                "supported_methods": p.supported_methods
            })).collect::<Vec<_>>()
        },
        "summary": {
            "total_tests": results.len(),
            "passed": results.iter().filter(|r| r.status == TestStatus::Pass).count(),
            "failed": results.iter().filter(|r| r.status == TestStatus::Fail).count(),
            "skipped": results.iter().filter(|r| r.status == TestStatus::Skip).count(),
            "warnings": results.iter().filter(|r| r.status == TestStatus::Warning).count(),
        },
        "results": results.iter().map(|r| serde_json::json!({
            "name": r.name,
            "status": r.status.name(),
            "message": r.message,
            "duration_ms": r.duration.as_millis(),
            "details": r.details
        })).collect::<Vec<_>>()
    });

    if let Some(parent) = output_path.parent() {
        fs::create_dir_all(parent)?;
    }

    let mut file = fs::File::create(&output_path)?;
    file.write_all(serde_json::to_string_pretty(&report)?.as_bytes())?;

    println!("📄 Test report written to: {}", output_path.display());

    Ok(())
}

/// Display protocol information and supported methods
fn display_protocol_information(transport_config: &TransportConfig) {
    println!("🔗 Available MCP Protocol Versions");
    println!("═══════════════════════════════════");

    // Show ALL available protocols, not just the detected one
    let all_protocols = get_all_protocol_versions();
    let current_protocol = detect_protocol_info(transport_config);

    let protocol_rows: Vec<ProtocolTableRow> = all_protocols
        .iter()
        .map(|protocol| {
            let is_current = protocol.version == current_protocol.version;
            let protocol_name = if is_current {
                format!("{} (DETECTED)", protocol.version)
            } else {
                protocol.version.clone()
            };

            ProtocolTableRow {
                protocol: protocol_name,
                spec_version: protocol.spec_date.clone(),
                endpoints: protocol.endpoints.join(", "),
                session_type: protocol.session_management.clone(),
            }
        })
        .collect();

    let protocol_table = Table::new(protocol_rows)
        .with(tabled::settings::Style::rounded())
        .with(tabled::settings::Padding::new(1, 1, 0, 0))
        .to_string();

    println!("{}", protocol_table);

    // Display example URLs for each protocol
    println!("\n📡 Example URLs by Protocol");
    println!("═══════════════════════════");

    let example_rows = vec![
        ExampleUrlRow {
            protocol: "Modern Streamable HTTP".to_string(),
            example_url: "http://localhost:8931/mcp".to_string(),
            description: "Single endpoint, header-based sessions".to_string(),
        },
        ExampleUrlRow {
            protocol: "Legacy HTTP+SSE".to_string(),
            example_url: "http://localhost:8931/sse".to_string(),
            description: "Dual endpoints, query parameter sessions".to_string(),
        },
        ExampleUrlRow {
            protocol: "Standard Transport".to_string(),
            example_url: "--stdio your-mcp-server".to_string(),
            description: "Process-based communication".to_string(),
        },
    ];

    let examples_table = Table::new(example_rows)
        .with(tabled::settings::Style::rounded())
        .with(tabled::settings::Padding::new(1, 1, 0, 0))
        .to_string();

    println!("{}", examples_table);

    // Display supported methods for current protocol
    println!(
        "\n📋 Methods for Current Protocol: {}",
        current_protocol.version
    );
    println!("═════════════════════════════════════════════════════");

    let method_rows = get_supported_methods(&current_protocol);
    let methods_table = Table::new(method_rows)
        .with(tabled::settings::Style::rounded())
        .with(tabled::settings::Padding::new(1, 1, 0, 0))
        .to_string();

    println!("{}", methods_table);
    println!();
}

/// Get all available protocol versions
fn get_all_protocol_versions() -> Vec<ProtocolInfo> {
    vec![
        ProtocolInfo {
            version: "Modern Streamable HTTP".to_string(),
            spec_date: "2025-03-26".to_string(),
            endpoints: vec!["/mcp".to_string()],
            session_management: "Mcp-Session-Id header".to_string(),
            supported_methods: vec![
                "initialize".to_string(),
                "initialized".to_string(),
                "tools/list".to_string(),
                "tools/call".to_string(),
                "resources/list".to_string(),
                "resources/read".to_string(),
                "prompts/list".to_string(),
                "prompts/get".to_string(),
                "logging/setLevel".to_string(),
                "notifications/*".to_string(),
            ],
        },
        ProtocolInfo {
            version: "Legacy HTTP+SSE".to_string(),
            spec_date: "2024-11-05".to_string(),
            endpoints: vec!["/sse".to_string(), "/events".to_string()],
            session_management: "sessionId query parameter".to_string(),
            supported_methods: vec![
                "initialize".to_string(),
                "initialized".to_string(),
                "tools/list".to_string(),
                "tools/call".to_string(),
                "resources/list".to_string(),
                "resources/read".to_string(),
                "prompts/list".to_string(),
                "prompts/get".to_string(),
                "logging/setLevel".to_string(),
            ],
        },
        ProtocolInfo {
            version: "Standard Transport".to_string(),
            spec_date: "2025-03-26".to_string(),
            endpoints: vec!["stdio".to_string()],
            session_management: "N/A (stdio)".to_string(),
            supported_methods: vec![
                "initialize".to_string(),
                "initialized".to_string(),
                "tools/list".to_string(),
                "tools/call".to_string(),
                "resources/list".to_string(),
                "resources/read".to_string(),
                "prompts/list".to_string(),
                "prompts/get".to_string(),
                "logging/setLevel".to_string(),
                "notifications/*".to_string(),
            ],
        },
    ]
}

/// Detect protocol information based on transport configuration
fn detect_protocol_info(transport_config: &TransportConfig) -> ProtocolInfo {
    if let TransportConfig::HttpSse(config) = transport_config {
        let endpoint_path = config.base_url.path();

        match endpoint_path {
            "/mcp" => ProtocolInfo {
                version: "Modern Streamable HTTP".to_string(),
                spec_date: "2025-03-26".to_string(),
                endpoints: vec!["/mcp".to_string()],
                session_management: "Mcp-Session-Id header".to_string(),
                supported_methods: vec![
                    "initialize".to_string(),
                    "initialized".to_string(),
                    "tools/list".to_string(),
                    "tools/call".to_string(),
                    "resources/list".to_string(),
                    "resources/read".to_string(),
                    "prompts/list".to_string(),
                    "prompts/get".to_string(),
                    "logging/setLevel".to_string(),
                    "notifications/*".to_string(),
                ],
            },
            "/sse" => ProtocolInfo {
                version: "Legacy HTTP+SSE".to_string(),
                spec_date: "2024-11-05".to_string(),
                endpoints: vec!["/sse".to_string(), "/events".to_string()],
                session_management: "sessionId query parameter".to_string(),
                supported_methods: vec![
                    "initialize".to_string(),
                    "initialized".to_string(),
                    "tools/list".to_string(),
                    "tools/call".to_string(),
                    "resources/list".to_string(),
                    "resources/read".to_string(),
                    "prompts/list".to_string(),
                    "prompts/get".to_string(),
                    "logging/setLevel".to_string(),
                ],
            },
            _ => ProtocolInfo {
                version: "Auto-detected".to_string(),
                spec_date: "Unknown".to_string(),
                endpoints: vec![endpoint_path.to_string()],
                session_management: "Auto-detected".to_string(),
                supported_methods: vec![
                    "initialize".to_string(),
                    "tools/list".to_string(),
                    "resources/list".to_string(),
                    "prompts/list".to_string(),
                ],
            },
        }
    } else {
        ProtocolInfo {
            version: "Standard Transport".to_string(),
            spec_date: "2025-03-26".to_string(),
            endpoints: vec!["stdio".to_string()],
            session_management: "N/A (stdio)".to_string(),
            supported_methods: vec![
                "initialize".to_string(),
                "initialized".to_string(),
                "tools/list".to_string(),
                "tools/call".to_string(),
                "resources/list".to_string(),
                "resources/read".to_string(),
                "prompts/list".to_string(),
                "prompts/get".to_string(),
                "logging/setLevel".to_string(),
                "notifications/*".to_string(),
            ],
        }
    }
}

/// Get supported methods with descriptions
fn get_supported_methods(protocol_info: &ProtocolInfo) -> Vec<MethodTableRow> {
    let method_descriptions = [
        (
            "initialize",
            "Initialize MCP connection with server capabilities",
            "All",
        ),
        ("initialized", "Confirm successful initialization", "All"),
        ("tools/list", "List all available tools", "All"),
        (
            "tools/call",
            "Execute a specific tool with parameters",
            "All",
        ),
        ("resources/list", "List all available resources", "All"),
        (
            "resources/read",
            "Read content from a specific resource",
            "All",
        ),
        ("prompts/list", "List all available prompt templates", "All"),
        (
            "prompts/get",
            "Get a specific prompt template with arguments",
            "All",
        ),
        (
            "logging/setLevel",
            "Set the logging level for the session",
            "All",
        ),
        (
            "notifications/*",
            "Server-to-client notifications",
            "Modern/stdio",
        ),
    ];

    method_descriptions
        .iter()
        .filter(|(method, _, _)| {
            protocol_info.supported_methods.iter().any(|supported| {
                supported == method
                    || (supported.ends_with("/*")
                        && method.starts_with(&supported[..supported.len() - 1]))
            })
        })
        .map(|(method, description, support)| MethodTableRow {
            method: method.to_string(),
            description: description.to_string(),
            protocol_support: support.to_string(),
        })
        .collect()
}

/// Comprehensive discovery and testing of all MCP endpoints
async fn run_discovery_tests(base_url: &str, args: &TestArgs) -> Result<()> {
    let start_time = Instant::now();

    println!("🔍 MCP Endpoint Discovery & Testing");
    println!("🌐 Base URL: {}", base_url);
    println!("═════════════════════════════════════");

    // Define MCP-compliant endpoints to test
    let endpoints_to_test = vec![
        EndpointTest {
            name: "Modern Streamable HTTP".to_string(),
            url: format!("{}/mcp", base_url.trim_end_matches('/')),
            description: "Single endpoint with header-based sessions".to_string(),
            expected_protocol: "Modern Streamable HTTP".to_string(),
        },
        EndpointTest {
            name: "Legacy HTTP+SSE".to_string(),
            url: format!("{}/sse", base_url.trim_end_matches('/')),
            description: "Dual endpoints with query parameter sessions".to_string(),
            expected_protocol: "Legacy HTTP+SSE".to_string(),
        },
    ];

    let mut discovery_results = Vec::new();
    let mut all_test_results = Vec::new();

    for endpoint in endpoints_to_test {
        println!("\n🔗 Testing Endpoint: {}", endpoint.name);
        println!("🌐 URL: {}", endpoint.url);
        println!("📝 Description: {}", endpoint.description);
        println!("─────────────────────────────────────");

        let endpoint_start = Instant::now();

        // Create endpoint-specific spinner
        let endpoint_spinner = create_discovery_spinner();
        update_spinner_message(
            &endpoint_spinner,
            "discovering",
            &format!("Testing {}", endpoint.name),
        );

        // Try to create transport config for this endpoint
        let transport_result = create_transport_config(&endpoint.url);

        match transport_result {
            Ok(transport_config) => {
                let mut endpoint_tests = Vec::new();

                // Update spinner for connection phase
                update_spinner_message(
                    &endpoint_spinner,
                    "connecting",
                    &format!("Connecting to {}", endpoint.url),
                );

                // Test this specific endpoint
                let test_result =
                    test_single_endpoint(&transport_config, &mut endpoint_tests, args).await;

                let (status, error_msg, spinner_msg) = match &test_result {
                    Ok(_) => {
                        let tools = count_tools(&endpoint_tests);
                        (
                            DiscoveryStatus::Available,
                            None,
                            format!(
                                "{} available - {} tools discovered",
                                endpoint.name, tools
                            ),
                        )
                    }
                    Err(e) => (
                        DiscoveryStatus::Failed,
                        Some(e.to_string()),
                        format!("{} failed to connect", endpoint.name),
                    ),
                };

                let endpoint_result = DiscoveryResult {
                    endpoint: endpoint.clone(),
                    status,
                    error: error_msg,
                    test_results: endpoint_tests.clone(),
                    duration: endpoint_start.elapsed(),
                    tools_count: count_tools(&endpoint_tests),
                    resources_count: count_resources(&endpoint_tests),
                    prompts_count: count_prompts(&endpoint_tests),
                };

                endpoint_spinner.finish_with_message(spinner_msg);

                discovery_results.push(endpoint_result);
                all_test_results.extend(endpoint_tests);
            }
            Err(e) => {
                endpoint_spinner.finish_with_message(format!("🚫 {} invalid URL", endpoint.name));

                let endpoint_result = DiscoveryResult {
                    endpoint: endpoint.clone(),
                    status: DiscoveryStatus::InvalidUrl,
                    error: Some(e.to_string()),
                    test_results: Vec::new(),
                    duration: endpoint_start.elapsed(),
                    tools_count: 0,
                    resources_count: 0,
                    prompts_count: 0,
                };

                discovery_results.push(endpoint_result);
            }
        }
    }

    // Display comprehensive discovery results
    print_discovery_results(&discovery_results, start_time.elapsed());

    // Generate discovery report if requested
    if args.report {
        generate_discovery_report(
            &discovery_results,
            start_time.elapsed(),
            args.output_dir.as_ref(),
        )?;
    }

    Ok(())
}

/// Endpoint to test during discovery
#[derive(Debug, Clone)]
struct EndpointTest {
    pub name: String,
    pub url: String,
    pub description: String,
    pub expected_protocol: String,
}

/// Discovery result for a single endpoint
#[derive(Debug)]
struct DiscoveryResult {
    pub endpoint: EndpointTest,
    pub status: DiscoveryStatus,
    pub error: Option<String>,
    pub test_results: Vec<TestResult>,
    pub duration: Duration,
    pub tools_count: usize,
    pub resources_count: usize,
    pub prompts_count: usize,
}

/// Discovery status for endpoints
#[derive(Debug, PartialEq)]
enum DiscoveryStatus {
    Available,
    Failed,
    InvalidUrl,
}

impl DiscoveryStatus {
    fn icon(&self) -> &'static str {
        match self {
            DiscoveryStatus::Available => "",
            DiscoveryStatus::Failed => "",
            DiscoveryStatus::InvalidUrl => "🚫",
        }
    }

    fn name(&self) -> &'static str {
        match self {
            DiscoveryStatus::Available => "Available",
            DiscoveryStatus::Failed => "Failed",
            DiscoveryStatus::InvalidUrl => "Invalid URL",
        }
    }
}

/// Create transport config from URL string
fn create_transport_config(url: &str) -> Result<TransportConfig> {
    if url.starts_with("http://") || url.starts_with("https://") {
        Ok(TransportConfig::http_sse(url)?)
    } else {
        anyhow::bail!("Unsupported URL scheme: {}", url)
    }
}

/// Test a single endpoint comprehensively
async fn test_single_endpoint(
    transport_config: &TransportConfig,
    results: &mut Vec<TestResult>,
    args: &TestArgs,
) -> Result<()> {
    let client_info = Implementation {
        name: "mcp-probe".to_string(),
        version: env!("CARGO_PKG_VERSION").to_string(),
        metadata: std::collections::HashMap::new(),
    };

    // Test connection with timeout
    let connection_result = tokio::time::timeout(
        Duration::from_secs(args.timeout),
        test_connection(transport_config, &client_info, results),
    )
    .await;

    match connection_result {
        Ok(Ok(mut client)) => {
            // Run all tests for this endpoint
            test_capability_discovery(&mut client, results).await;
            test_tools_listing(&mut client, results).await;
            test_resources_listing(&mut client, results).await;
            test_prompts_listing(&mut client, results).await;
            test_error_handling(&mut client, results).await;
            Ok(())
        }
        Ok(Err(e)) => Err(e),
        Err(_) => {
            results.push(TestResult {
                name: "Connection".to_string(),
                status: TestStatus::Fail,
                message: "Connection timeout".to_string(),
                duration: Duration::from_secs(args.timeout),
                details: None,
            });
            anyhow::bail!("Connection timeout")
        }
    }
}

/// Count tools from test results
fn count_tools(results: &[TestResult]) -> usize {
    results
        .iter()
        .find(|r| r.name == "Tools Listing" && r.status == TestStatus::Pass)
        .and_then(|r| r.details.as_ref())
        .and_then(|d| d.as_array())
        .map(|arr| arr.len())
        .unwrap_or(0)
}

/// Count resources from test results
fn count_resources(results: &[TestResult]) -> usize {
    results
        .iter()
        .find(|r| r.name == "Resources Listing" && r.status == TestStatus::Pass)
        .and_then(|r| r.details.as_ref())
        .and_then(|d| d.as_array())
        .map(|arr| arr.len())
        .unwrap_or(0)
}

/// Count prompts from test results
fn count_prompts(results: &[TestResult]) -> usize {
    results
        .iter()
        .find(|r| r.name == "Prompts Listing" && r.status == TestStatus::Pass)
        .and_then(|r| r.details.as_ref())
        .and_then(|d| d.as_array())
        .map(|arr| arr.len())
        .unwrap_or(0)
}

/// Print comprehensive discovery results
fn print_discovery_results(results: &[DiscoveryResult], total_duration: Duration) {
    println!("\n🔍 MCP Endpoint Discovery Results");
    println!("═════════════════════════════════");

    // Discovery overview table
    let discovery_rows: Vec<DiscoveryTableRow> = results
        .iter()
        .map(|result| DiscoveryTableRow {
            status: format!("{} {}", result.status.icon(), result.status.name()),
            endpoint: result.endpoint.name.clone(),
            url: result.endpoint.url.clone(),
            tools: result.tools_count.to_string(),
            resources: result.resources_count.to_string(),
            prompts: result.prompts_count.to_string(),
            duration: format!("{:.2}ms", result.duration.as_secs_f64() * 1000.0),
        })
        .collect();

    let discovery_table = Table::new(discovery_rows)
        .with(tabled::settings::Style::rounded())
        .with(tabled::settings::Padding::new(1, 1, 0, 0))
        .to_string();

    println!("{}", discovery_table);

    // Available endpoints summary
    let available_endpoints: Vec<&DiscoveryResult> = results
        .iter()
        .filter(|r| r.status == DiscoveryStatus::Available)
        .collect();

    if !available_endpoints.is_empty() {
        println!("\n🚀 Available MCP Endpoints");
        println!("═════════════════════════");

        for endpoint in available_endpoints {
            println!(
                "\n📡 {} - {}",
                endpoint.endpoint.name, endpoint.endpoint.url
            );
            println!("   📝 {}", endpoint.endpoint.description);
            println!(
                "   🛠️  Tools: {}, 📁 Resources: {}, 📋 Prompts: {}",
                endpoint.tools_count, endpoint.resources_count, endpoint.prompts_count
            );

            // Show how to use this endpoint
            if endpoint.endpoint.url.contains("/mcp") || endpoint.endpoint.url.contains("/sse") {
                println!("   🔧 Usage: --http-sse {}", endpoint.endpoint.url);
            }
        }
    }

    // Overall summary
    let total_available = results
        .iter()
        .filter(|r| r.status == DiscoveryStatus::Available)
        .count();
    let total_failed = results
        .iter()
        .filter(|r| r.status == DiscoveryStatus::Failed)
        .count();
    let total_invalid = results
        .iter()
        .filter(|r| r.status == DiscoveryStatus::InvalidUrl)
        .count();

    println!("\n📊 Discovery Summary");
    println!("═══════════════════");

    let summary_rows = vec![
        SummaryTableRow {
            metric: "Total Endpoints".to_string(),
            value: results.len().to_string(),
            icon: "📡".to_string(),
        },
        SummaryTableRow {
            metric: "Available".to_string(),
            value: total_available.to_string(),
            icon: "".to_string(),
        },
        SummaryTableRow {
            metric: "Failed".to_string(),
            value: total_failed.to_string(),
            icon: "".to_string(),
        },
        SummaryTableRow {
            metric: "Invalid URLs".to_string(),
            value: total_invalid.to_string(),
            icon: "🚫".to_string(),
        },
        SummaryTableRow {
            metric: "Total Duration".to_string(),
            value: format!("{:.2}s", total_duration.as_secs_f64()),
            icon: "⏱️".to_string(),
        },
    ];

    let summary_table = Table::new(summary_rows)
        .with(tabled::settings::Style::rounded())
        .with(tabled::settings::Padding::new(1, 1, 0, 0))
        .to_string();

    println!("{}", summary_table);

    if total_available > 0 {
        println!("\n🎉 {} MCP endpoint(s) are available!", total_available);
        println!("💡 Use the URLs above with --http-sse to test individual endpoints");
    } else {
        println!("\n❌ No MCP endpoints are available at the provided base URL");
        println!("💡 Please check that your MCP server is running and accessible");
    }
}

/// Table row for discovery results
#[derive(Tabled)]
struct DiscoveryTableRow {
    #[tabled(rename = "Status")]
    pub status: String,
    #[tabled(rename = "Endpoint")]
    pub endpoint: String,
    #[tabled(rename = "URL")]
    pub url: String,
    #[tabled(rename = "Tools")]
    pub tools: String,
    #[tabled(rename = "Resources")]
    pub resources: String,
    #[tabled(rename = "Prompts")]
    pub prompts: String,
    #[tabled(rename = "Duration")]
    pub duration: String,
}

/// Generate discovery report
fn generate_discovery_report(
    results: &[DiscoveryResult],
    total_duration: Duration,
    output_dir: Option<&std::path::PathBuf>,
) -> Result<()> {
    use crate::paths::get_mcp_probe_paths;
    use std::fs;
    use std::io::Write;

    let output_path = if let Some(dir) = output_dir {
        // If user specifies output dir, use it but still add date prefix
        let date = chrono::Utc::now().format("%Y%m%d");
        let timestamp = chrono::Utc::now().format("%H%M%S");
        dir.join(format!("{}-discovery-report-{}.json", date, timestamp))
    } else {
        // Use centralized path management with date prefix
        let paths = get_mcp_probe_paths()?;
        paths.report_file("discovery-report", "json")
    };

    let report = serde_json::json!({
        "metadata": {
            "generated_at": chrono::Utc::now().to_rfc3339(),
            "total_duration_ms": total_duration.as_millis(),
            "mcp_probe_version": env!("CARGO_PKG_VERSION"),
            "discovery_mode": true
        },
        "endpoints": results.iter().map(|r| serde_json::json!({
            "name": r.endpoint.name,
            "url": r.endpoint.url,
            "description": r.endpoint.description,
            "expected_protocol": r.endpoint.expected_protocol,
            "status": r.status.name(),
            "error": r.error,
            "duration_ms": r.duration.as_millis(),
            "capabilities": {
                "tools_count": r.tools_count,
                "resources_count": r.resources_count,
                "prompts_count": r.prompts_count
            },
            "test_results": r.test_results.iter().map(|t| serde_json::json!({
                "name": t.name,
                "status": t.status.name(),
                "message": t.message,
                "duration_ms": t.duration.as_millis(),
                "details": t.details
            })).collect::<Vec<_>>()
        })).collect::<Vec<_>>(),
        "summary": {
            "total_endpoints": results.len(),
            "available_endpoints": results.iter().filter(|r| r.status == DiscoveryStatus::Available).count(),
            "failed_endpoints": results.iter().filter(|r| r.status == DiscoveryStatus::Failed).count(),
            "invalid_endpoints": results.iter().filter(|r| r.status == DiscoveryStatus::InvalidUrl).count(),
            "total_tools": results.iter().map(|r| r.tools_count).sum::<usize>(),
            "total_resources": results.iter().map(|r| r.resources_count).sum::<usize>(),
            "total_prompts": results.iter().map(|r| r.prompts_count).sum::<usize>(),
        }
    });

    let json_content = serde_json::to_string_pretty(&report)?;

    if let Some(parent) = output_path.parent() {
        fs::create_dir_all(parent)?;
    }

    let mut file = fs::File::create(&output_path)?;
    file.write_all(json_content.as_bytes())?;

    println!("📄 Discovery report written to: {}", output_path.display());

    Ok(())
}