mcp-tester 0.5.1

Comprehensive MCP server testing tool - library and CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use chrono::{DateTime, Utc};
use clap::ValueEnum;
use colored::*;
use prettytable::{row, Table};
use serde::{Deserialize, Serialize};
use std::time::Duration;

#[derive(Debug, Clone, Copy, PartialEq, ValueEnum, Serialize, Deserialize)]
pub enum OutputFormat {
    Pretty,
    Json,
    Minimal,
    Verbose,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum TestStatus {
    Passed,
    Failed,
    Warning,
    Skipped,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum TestCategory {
    Core,
    Protocol,
    Tools,
    Resources,
    Prompts,
    Performance,
    Compatibility,
    Apps,
    Tasks,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TestResult {
    pub name: String,
    pub category: TestCategory,
    pub status: TestStatus,
    pub duration: Duration,
    pub error: Option<String>,
    pub details: Option<String>,
}

impl TestResult {
    /// Create a passing test result.
    pub fn passed(
        name: impl Into<String>,
        category: TestCategory,
        duration: Duration,
        details: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            category,
            status: TestStatus::Passed,
            duration,
            error: None,
            details: Some(details.into()),
        }
    }

    /// Create a failing test result.
    pub fn failed(
        name: impl Into<String>,
        category: TestCategory,
        duration: Duration,
        error: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            category,
            status: TestStatus::Failed,
            duration,
            error: Some(error.into()),
            details: None,
        }
    }

    /// Create a warning test result.
    pub fn warning(
        name: impl Into<String>,
        category: TestCategory,
        duration: Duration,
        details: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            category,
            status: TestStatus::Warning,
            duration,
            error: None,
            details: Some(details.into()),
        }
    }

    /// Create a skipped test result.
    pub fn skipped(
        name: impl Into<String>,
        category: TestCategory,
        details: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            category,
            status: TestStatus::Skipped,
            duration: Duration::from_secs(0),
            error: None,
            details: Some(details.into()),
        }
    }
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TestReport {
    pub tests: Vec<TestResult>,
    pub duration: Duration,
    pub timestamp: DateTime<Utc>,
    pub summary: TestSummary,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TestSummary {
    pub total: usize,
    pub passed: usize,
    pub failed: usize,
    pub warnings: usize,
    pub skipped: usize,
}

impl Default for TestReport {
    fn default() -> Self {
        Self {
            tests: Vec::new(),
            duration: Duration::from_secs(0),
            timestamp: Utc::now(),
            summary: TestSummary {
                total: 0,
                passed: 0,
                failed: 0,
                warnings: 0,
                skipped: 0,
            },
        }
    }
}

impl TestReport {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn from_error(error: anyhow::Error) -> Self {
        let mut report = Self::new();
        report.add_test(TestResult {
            name: "Error".to_string(),
            category: TestCategory::Core,
            status: TestStatus::Failed,
            duration: Duration::from_secs(0),
            error: Some(error.to_string()),
            details: None,
        });
        report
    }

    pub fn add_test(&mut self, test: TestResult) {
        match test.status {
            TestStatus::Passed => self.summary.passed += 1,
            TestStatus::Failed => self.summary.failed += 1,
            TestStatus::Warning => self.summary.warnings += 1,
            TestStatus::Skipped => self.summary.skipped += 1,
        }
        self.summary.total += 1;
        self.tests.push(test);
    }

    pub fn has_failures(&self) -> bool {
        self.summary.failed > 0
    }

    pub fn apply_strict_mode(&mut self) {
        // In strict mode, warnings become failures
        for test in &mut self.tests {
            if test.status == TestStatus::Warning {
                test.status = TestStatus::Failed;
                self.summary.warnings -= 1;
                self.summary.failed += 1;
            }
        }
    }

    pub fn print(&self, format: OutputFormat) {
        match format {
            OutputFormat::Pretty => self.print_pretty(),
            OutputFormat::Json => self.print_json(),
            OutputFormat::Minimal => self.print_minimal(),
            OutputFormat::Verbose => self.print_verbose(),
        }
    }

    fn print_pretty(&self) {
        println!();
        println!("{}", "TEST RESULTS".cyan().bold());
        println!("{}", "".repeat(60).cyan());
        println!();

        // Group tests by category
        let mut by_category: std::collections::HashMap<String, Vec<&TestResult>> =
            std::collections::HashMap::new();

        for test in &self.tests {
            let category = format!("{:?}", test.category);
            by_category.entry(category).or_default().push(test);
        }

        // Print each category
        for (category, tests) in by_category {
            println!("{}", format!("{}:", category).yellow().bold());
            println!();

            for test in tests {
                self.print_test_result_pretty(test);
            }
            println!();
        }

        // Print summary
        self.print_summary_pretty();

        // Print recommendations if there are failures
        if self.has_failures() {
            self.print_recommendations();
        }
    }

    fn print_test_result_pretty(&self, test: &TestResult) {
        let status_symbol = match test.status {
            TestStatus::Passed => "".green().bold(),
            TestStatus::Failed => "".red().bold(),
            TestStatus::Warning => "".yellow().bold(),
            TestStatus::Skipped => "".dimmed(),
        };

        let name = if test.name.len() > 40 {
            format!("{}...", &test.name[..37])
        } else {
            test.name.clone()
        };

        print!("  {} {:<40}", status_symbol, name);

        // Print duration if significant
        if test.duration.as_millis() > 100 {
            print!(" {:>6}ms", test.duration.as_millis());
        } else {
            print!("         ");
        }

        // Print details or error
        if let Some(error) = &test.error {
            println!(" {}", error.red());
        } else if let Some(details) = &test.details {
            if test.status == TestStatus::Warning {
                println!(" {}", details.yellow());
            } else {
                println!(" {}", details.dimmed());
            }
        } else {
            println!();
        }
    }

    fn print_summary_pretty(&self) {
        println!("{}", "".repeat(60).cyan());
        println!("{}", "SUMMARY".cyan().bold());
        println!("{}", "".repeat(60).cyan());
        println!();

        let mut table = Table::new();
        table.add_row(row!["Total Tests", self.summary.total.to_string().bold()]);
        table.add_row(row![
            "Passed",
            self.summary.passed.to_string().green().bold()
        ]);

        if self.summary.failed > 0 {
            table.add_row(row!["Failed", self.summary.failed.to_string().red().bold()]);
        }

        if self.summary.warnings > 0 {
            table.add_row(row![
                "Warnings",
                self.summary.warnings.to_string().yellow().bold()
            ]);
        }

        if self.summary.skipped > 0 {
            table.add_row(row!["Skipped", self.summary.skipped.to_string().dimmed()]);
        }

        table.add_row(row![
            "Duration",
            format!("{:.2}s", self.duration.as_secs_f64())
        ]);

        table.printstd();
        println!();

        // Overall status
        let overall = if self.summary.failed > 0 {
            "FAILED".red().bold()
        } else if self.summary.warnings > 0 {
            "PASSED WITH WARNINGS".yellow().bold()
        } else {
            "PASSED".green().bold()
        };

        println!("Overall Status: {}", overall);
    }

    fn print_recommendations(&self) {
        println!();
        println!("{}", "RECOMMENDATIONS".yellow().bold());
        println!("{}", "".repeat(60).yellow());
        println!();

        let failed_tests: Vec<_> = self
            .tests
            .iter()
            .filter(|t| t.status == TestStatus::Failed)
            .collect();

        if failed_tests.is_empty() {
            return;
        }

        // Group failures by category
        let mut protocol_failures = 0;
        let mut tool_failures = 0;
        let mut core_failures = 0;
        let mut task_failures = 0;

        for test in &failed_tests {
            match test.category {
                TestCategory::Protocol => protocol_failures += 1,
                TestCategory::Tools => tool_failures += 1,
                TestCategory::Core => core_failures += 1,
                TestCategory::Tasks => task_failures += 1,
                _ => {},
            }
        }

        if core_failures > 0 {
            println!("  • Fix core connectivity issues first");
            println!("    - Verify server is running and accessible");
            println!("    - Check network configuration and firewall rules");
        }

        if protocol_failures > 0 {
            println!("  • Review MCP protocol implementation");
            println!("    - Ensure JSON-RPC 2.0 compliance");
            println!("    - Verify protocol version compatibility");
            println!("    - Check required method implementations");
        }

        if tool_failures > 0 {
            println!("  • Debug tool implementations");
            println!("    - Verify tool registration and handlers");
            println!("    - Check input validation and error handling");
            println!("    - Review tool response formats");
        }

        if task_failures > 0 {
            println!("  - Debug task implementations");
            println!("    - Verify task capability is advertised in ServerCapabilities");
            println!("    - Check task lifecycle state machine (working -> completed/failed)");
            println!("    - Ensure tasks/get and tasks/list return valid Task structures");
        }

        println!();
        println!("Run with --verbose for detailed error information");
    }

    fn print_json(&self) {
        let json = serde_json::to_string_pretty(self).unwrap();
        println!("{}", json);
    }

    fn print_minimal(&self) {
        let status = if self.summary.failed > 0 {
            "FAIL"
        } else {
            "PASS"
        };

        println!(
            "{}: {} passed, {} failed, {} warnings in {:.2}s",
            status,
            self.summary.passed,
            self.summary.failed,
            self.summary.warnings,
            self.duration.as_secs_f64()
        );
    }

    fn print_verbose(&self) {
        self.print_pretty();

        println!();
        println!("{}", "DETAILED TEST INFORMATION".cyan().bold());
        println!("{}", "".repeat(60).cyan());
        println!();

        for test in &self.tests {
            println!("Test: {}", test.name.bold());
            println!("  Category: {:?}", test.category);
            println!("  Status: {:?}", test.status);
            println!("  Duration: {:?}", test.duration);

            if let Some(error) = &test.error {
                println!("  Error: {}", error.red());
            }

            if let Some(details) = &test.details {
                println!("  Details: {}", details);
            }

            println!();
        }
    }
}