mcplint 0.4.0

MCP Server Testing, Fuzzing, and Security Scanning Platform
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
//! Rug-Pull Detection - Detect changes in tool definitions
//!
//! Monitors tool definitions across scans to detect unexpected
//! changes that could indicate malicious behavior ("rug pulls").

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};

use crate::protocol::mcp::Tool;

/// Record of tool hashes for a server
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolHashRecord {
    /// Server identifier
    pub server_id: String,
    /// When this record was created
    pub created_at: DateTime<Utc>,
    /// When this record was last updated
    pub updated_at: DateTime<Utc>,
    /// Hash of the overall tool configuration
    pub config_hash: String,
    /// Individual tool hashes for comparison
    pub tool_hashes: HashMap<String, ToolHash>,
    /// Number of times this configuration has been seen
    pub seen_count: u64,
}

/// Hash information for a single tool
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ToolHash {
    /// Tool name
    pub name: String,
    /// Hash of tool description
    pub description_hash: String,
    /// Hash of input schema
    pub schema_hash: String,
    /// Combined hash
    pub combined_hash: String,
}

impl ToolHash {
    /// Create a hash record from a tool
    pub fn from_tool(tool: &Tool) -> Self {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut desc_hasher = DefaultHasher::new();
        tool.description.hash(&mut desc_hasher);
        let description_hash = format!("{:016x}", desc_hasher.finish());

        let mut schema_hasher = DefaultHasher::new();
        tool.input_schema.to_string().hash(&mut schema_hasher);
        let schema_hash = format!("{:016x}", schema_hasher.finish());

        let mut combined_hasher = DefaultHasher::new();
        tool.name.hash(&mut combined_hasher);
        tool.description.hash(&mut combined_hasher);
        tool.input_schema.to_string().hash(&mut combined_hasher);
        let combined_hash = format!("{:016x}", combined_hasher.finish());

        Self {
            name: tool.name.clone(),
            description_hash,
            schema_hash,
            combined_hash,
        }
    }
}

impl ToolHashRecord {
    /// Create a new record from a list of tools
    pub fn from_tools(server_id: &str, tools: &[Tool]) -> Self {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let now = Utc::now();
        let mut tool_hashes = HashMap::new();

        // Create individual tool hashes
        for tool in tools {
            let hash = ToolHash::from_tool(tool);
            tool_hashes.insert(tool.name.clone(), hash);
        }

        // Create overall config hash
        let mut config_hasher = DefaultHasher::new();
        let mut sorted_names: Vec<_> = tool_hashes.keys().collect();
        sorted_names.sort();
        for name in sorted_names {
            name.hash(&mut config_hasher);
            tool_hashes[name].combined_hash.hash(&mut config_hasher);
        }
        let config_hash = format!("{:016x}", config_hasher.finish());

        Self {
            server_id: server_id.to_string(),
            created_at: now,
            updated_at: now,
            config_hash,
            tool_hashes,
            seen_count: 1,
        }
    }

    /// Update the seen count and timestamp
    pub fn mark_seen(&mut self) {
        self.seen_count += 1;
        self.updated_at = Utc::now();
    }
}

/// Result of rug-pull detection comparison
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RugPullDetection {
    /// Server identifier
    pub server_id: String,
    /// Tools that were added
    pub added: Vec<String>,
    /// Tools that were removed
    pub removed: Vec<String>,
    /// Tools with modified definitions
    pub modified: Vec<ToolModification>,
    /// Severity assessment
    pub severity: RugPullSeverity,
    /// Human-readable summary
    pub summary: String,
    /// Previous config hash
    pub previous_hash: String,
    /// Current config hash
    pub current_hash: String,
    /// When the previous record was created
    pub previous_seen: DateTime<Utc>,
}

/// Details of a modified tool
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolModification {
    /// Tool name
    pub name: String,
    /// Whether description changed
    pub description_changed: bool,
    /// Whether schema changed
    pub schema_changed: bool,
    /// Previous hash
    pub previous_hash: String,
    /// Current hash
    pub current_hash: String,
}

/// Severity of detected changes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RugPullSeverity {
    /// Informational changes (metadata only)
    Info,
    /// Low severity (minor additions)
    Low,
    /// Medium severity (significant changes)
    Medium,
    /// High severity (suspicious changes)
    High,
    /// Critical severity (likely malicious)
    Critical,
}

impl std::fmt::Display for RugPullSeverity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RugPullSeverity::Info => write!(f, "info"),
            RugPullSeverity::Low => write!(f, "low"),
            RugPullSeverity::Medium => write!(f, "medium"),
            RugPullSeverity::High => write!(f, "high"),
            RugPullSeverity::Critical => write!(f, "critical"),
        }
    }
}

/// Compare tool configurations and detect changes
pub fn detect_rug_pull(
    server_id: &str,
    previous: &ToolHashRecord,
    current_tools: &[Tool],
) -> Option<RugPullDetection> {
    let current = ToolHashRecord::from_tools(server_id, current_tools);

    // Quick check - if hashes match, no changes
    if previous.config_hash == current.config_hash {
        return None;
    }

    let previous_names: HashSet<_> = previous.tool_hashes.keys().collect();
    let current_names: HashSet<_> = current.tool_hashes.keys().collect();

    // Find added tools
    let added: Vec<String> = current_names
        .difference(&previous_names)
        .map(|s| (*s).clone())
        .collect();

    // Find removed tools
    let removed: Vec<String> = previous_names
        .difference(&current_names)
        .map(|s| (*s).clone())
        .collect();

    // Find modified tools
    let mut modified = Vec::new();
    for name in previous_names.intersection(&current_names) {
        let prev_hash = &previous.tool_hashes[*name];
        let curr_hash = &current.tool_hashes[*name];

        if prev_hash.combined_hash != curr_hash.combined_hash {
            modified.push(ToolModification {
                name: (*name).clone(),
                description_changed: prev_hash.description_hash != curr_hash.description_hash,
                schema_changed: prev_hash.schema_hash != curr_hash.schema_hash,
                previous_hash: prev_hash.combined_hash.clone(),
                current_hash: curr_hash.combined_hash.clone(),
            });
        }
    }

    // No changes detected
    if added.is_empty() && removed.is_empty() && modified.is_empty() {
        return None;
    }

    // Assess severity
    let severity = assess_severity(&added, &removed, &modified);

    // Generate summary
    let summary = generate_summary(&added, &removed, &modified);

    Some(RugPullDetection {
        server_id: server_id.to_string(),
        added,
        removed,
        modified,
        severity,
        summary,
        previous_hash: previous.config_hash.clone(),
        current_hash: current.config_hash,
        previous_seen: previous.updated_at,
    })
}

/// Assess the severity of detected changes
fn assess_severity(
    added: &[String],
    removed: &[String],
    modified: &[ToolModification],
) -> RugPullSeverity {
    // Critical: Tools removed or schemas changed significantly
    if !removed.is_empty() {
        return RugPullSeverity::High;
    }

    // Check for suspicious tool names in added tools
    let suspicious_names = [
        "exec", "shell", "system", "eval", "run", "execute", "command", "admin", "sudo", "root",
    ];

    for tool_name in added {
        let lower = tool_name.to_lowercase();
        for suspicious in &suspicious_names {
            if lower.contains(suspicious) {
                return RugPullSeverity::Critical;
            }
        }
    }

    // Schema changes are more concerning than description changes
    let schema_changes = modified.iter().filter(|m| m.schema_changed).count();

    if schema_changes > 0 {
        if schema_changes >= 3 {
            return RugPullSeverity::High;
        }
        return RugPullSeverity::Medium;
    }

    // Description-only changes
    if !modified.is_empty() {
        return RugPullSeverity::Low;
    }

    // Only additions
    if !added.is_empty() {
        if added.len() >= 5 {
            return RugPullSeverity::Medium;
        }
        return RugPullSeverity::Low;
    }

    RugPullSeverity::Info
}

/// Generate a human-readable summary
fn generate_summary(added: &[String], removed: &[String], modified: &[ToolModification]) -> String {
    let mut parts = Vec::new();

    if !added.is_empty() {
        parts.push(format!("{} tools added", added.len()));
    }

    if !removed.is_empty() {
        parts.push(format!("{} tools removed", removed.len()));
    }

    if !modified.is_empty() {
        let schema_changes = modified.iter().filter(|m| m.schema_changed).count();
        let desc_changes = modified.len() - schema_changes;

        if schema_changes > 0 {
            parts.push(format!("{} schema changes", schema_changes));
        }
        if desc_changes > 0 {
            parts.push(format!("{} description changes", desc_changes));
        }
    }

    if parts.is_empty() {
        "No changes detected".to_string()
    } else {
        format!("Tool changes detected: {}", parts.join(", "))
    }
}

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

    fn make_tool(name: &str, description: &str) -> Tool {
        Tool {
            name: name.to_string(),
            description: Some(description.to_string()),
            input_schema: json!({"type": "object"}),
        }
    }

    fn make_tool_with_schema(name: &str, description: &str, schema: serde_json::Value) -> Tool {
        Tool {
            name: name.to_string(),
            description: Some(description.to_string()),
            input_schema: schema,
        }
    }

    #[test]
    fn tool_hash_creation() {
        let tool = make_tool("test_tool", "A test tool");
        let hash = ToolHash::from_tool(&tool);

        assert_eq!(hash.name, "test_tool");
        assert!(!hash.description_hash.is_empty());
        assert!(!hash.schema_hash.is_empty());
        assert!(!hash.combined_hash.is_empty());
    }

    #[test]
    fn tool_hash_record_creation() {
        let tools = vec![
            make_tool("tool1", "First tool"),
            make_tool("tool2", "Second tool"),
        ];

        let record = ToolHashRecord::from_tools("test-server", &tools);

        assert_eq!(record.server_id, "test-server");
        assert_eq!(record.tool_hashes.len(), 2);
        assert!(record.tool_hashes.contains_key("tool1"));
        assert!(record.tool_hashes.contains_key("tool2"));
    }

    #[test]
    fn no_changes_detected() {
        let tools = vec![make_tool("tool1", "First tool")];
        let record = ToolHashRecord::from_tools("server", &tools);

        let detection = detect_rug_pull("server", &record, &tools);
        assert!(detection.is_none());
    }

    #[test]
    fn detect_added_tools() {
        let tools_v1 = vec![make_tool("tool1", "First tool")];
        let tools_v2 = vec![
            make_tool("tool1", "First tool"),
            make_tool("tool2", "New tool"),
        ];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2);

        assert!(detection.is_some());
        let d = detection.unwrap();
        assert_eq!(d.added, vec!["tool2".to_string()]);
        assert!(d.removed.is_empty());
    }

    #[test]
    fn detect_removed_tools() {
        let tools_v1 = vec![
            make_tool("tool1", "First tool"),
            make_tool("tool2", "Second tool"),
        ];
        let tools_v2 = vec![make_tool("tool1", "First tool")];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2);

        assert!(detection.is_some());
        let d = detection.unwrap();
        assert!(d.added.is_empty());
        assert_eq!(d.removed, vec!["tool2".to_string()]);
        assert_eq!(d.severity, RugPullSeverity::High);
    }

    #[test]
    fn detect_modified_tools() {
        let tools_v1 = vec![make_tool("tool1", "Original description")];
        let tools_v2 = vec![make_tool("tool1", "Changed description")];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2);

        assert!(detection.is_some());
        let d = detection.unwrap();
        assert_eq!(d.modified.len(), 1);
        assert!(d.modified[0].description_changed);
    }

    #[test]
    fn suspicious_tool_name_detection() {
        let tools_v1 = vec![make_tool("tool1", "Safe tool")];
        let tools_v2 = vec![
            make_tool("tool1", "Safe tool"),
            make_tool("execute_shell", "New suspicious tool"),
        ];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2);

        assert!(detection.is_some());
        let d = detection.unwrap();
        assert_eq!(d.severity, RugPullSeverity::Critical);
    }

    // New comprehensive tests

    #[test]
    fn tool_hash_consistency() {
        let tool = make_tool("test", "description");
        let hash1 = ToolHash::from_tool(&tool);
        let hash2 = ToolHash::from_tool(&tool);

        assert_eq!(hash1, hash2);
    }

    #[test]
    fn tool_hash_different_description() {
        let tool1 = make_tool("test", "desc1");
        let tool2 = make_tool("test", "desc2");

        let hash1 = ToolHash::from_tool(&tool1);
        let hash2 = ToolHash::from_tool(&tool2);

        assert_ne!(hash1.description_hash, hash2.description_hash);
        assert_eq!(hash1.schema_hash, hash2.schema_hash);
        assert_ne!(hash1.combined_hash, hash2.combined_hash);
    }

    #[test]
    fn tool_hash_different_schema() {
        let tool1 = make_tool_with_schema("test", "desc", json!({"type": "object"}));
        let tool2 = make_tool_with_schema("test", "desc", json!({"type": "string"}));

        let hash1 = ToolHash::from_tool(&tool1);
        let hash2 = ToolHash::from_tool(&tool2);

        assert_eq!(hash1.description_hash, hash2.description_hash);
        assert_ne!(hash1.schema_hash, hash2.schema_hash);
        assert_ne!(hash1.combined_hash, hash2.combined_hash);
    }

    #[test]
    fn tool_hash_record_seen_count() {
        let tools = vec![make_tool("tool1", "First tool")];
        let record = ToolHashRecord::from_tools("server", &tools);

        assert_eq!(record.seen_count, 1);
    }

    #[test]
    fn tool_hash_record_mark_seen() {
        let tools = vec![make_tool("tool1", "First tool")];
        let mut record = ToolHashRecord::from_tools("server", &tools);

        let original_count = record.seen_count;
        let original_updated = record.updated_at;

        std::thread::sleep(std::time::Duration::from_millis(10));
        record.mark_seen();

        assert_eq!(record.seen_count, original_count + 1);
        assert!(record.updated_at > original_updated);
    }

    #[test]
    fn tool_hash_record_config_hash_consistency() {
        let tools = vec![make_tool("tool1", "First"), make_tool("tool2", "Second")];
        let record1 = ToolHashRecord::from_tools("server", &tools);
        let record2 = ToolHashRecord::from_tools("server", &tools);

        assert_eq!(record1.config_hash, record2.config_hash);
    }

    #[test]
    fn detect_schema_changes() {
        let tools_v1 = vec![make_tool_with_schema(
            "tool1",
            "desc",
            json!({"type": "object"}),
        )];
        let tools_v2 = vec![make_tool_with_schema(
            "tool1",
            "desc",
            json!({"type": "string"}),
        )];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2);

        assert!(detection.is_some());
        let d = detection.unwrap();
        assert_eq!(d.modified.len(), 1);
        assert!(d.modified[0].schema_changed);
        assert!(!d.modified[0].description_changed);
    }

    #[test]
    fn detect_both_description_and_schema_changes() {
        let tools_v1 = vec![make_tool_with_schema(
            "tool1",
            "old",
            json!({"type": "object"}),
        )];
        let tools_v2 = vec![make_tool_with_schema(
            "tool1",
            "new",
            json!({"type": "string"}),
        )];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2);

        assert!(detection.is_some());
        let d = detection.unwrap();
        assert_eq!(d.modified.len(), 1);
        assert!(d.modified[0].schema_changed);
        assert!(d.modified[0].description_changed);
    }

    #[test]
    fn severity_info_for_metadata_only() {
        // Info severity is the default fallback
        let severity = assess_severity(&[], &[], &[]);
        assert_eq!(severity, RugPullSeverity::Info);
    }

    #[test]
    fn severity_low_for_few_additions() {
        let added = vec!["tool1".to_string(), "tool2".to_string()];
        let severity = assess_severity(&added, &[], &[]);
        assert_eq!(severity, RugPullSeverity::Low);
    }

    #[test]
    fn severity_medium_for_many_additions() {
        let added = vec![
            "tool1".to_string(),
            "tool2".to_string(),
            "tool3".to_string(),
            "tool4".to_string(),
            "tool5".to_string(),
        ];
        let severity = assess_severity(&added, &[], &[]);
        assert_eq!(severity, RugPullSeverity::Medium);
    }

    #[test]
    fn severity_high_for_removals() {
        let removed = vec!["tool1".to_string()];
        let severity = assess_severity(&[], &removed, &[]);
        assert_eq!(severity, RugPullSeverity::High);
    }

    #[test]
    fn severity_low_for_description_only_changes() {
        let modified = vec![ToolModification {
            name: "tool1".to_string(),
            description_changed: true,
            schema_changed: false,
            previous_hash: "old".to_string(),
            current_hash: "new".to_string(),
        }];
        let severity = assess_severity(&[], &[], &modified);
        assert_eq!(severity, RugPullSeverity::Low);
    }

    #[test]
    fn severity_medium_for_single_schema_change() {
        let modified = vec![ToolModification {
            name: "tool1".to_string(),
            description_changed: false,
            schema_changed: true,
            previous_hash: "old".to_string(),
            current_hash: "new".to_string(),
        }];
        let severity = assess_severity(&[], &[], &modified);
        assert_eq!(severity, RugPullSeverity::Medium);
    }

    #[test]
    fn severity_high_for_multiple_schema_changes() {
        let modified = vec![
            ToolModification {
                name: "tool1".to_string(),
                description_changed: false,
                schema_changed: true,
                previous_hash: "old1".to_string(),
                current_hash: "new1".to_string(),
            },
            ToolModification {
                name: "tool2".to_string(),
                description_changed: false,
                schema_changed: true,
                previous_hash: "old2".to_string(),
                current_hash: "new2".to_string(),
            },
            ToolModification {
                name: "tool3".to_string(),
                description_changed: false,
                schema_changed: true,
                previous_hash: "old3".to_string(),
                current_hash: "new3".to_string(),
            },
        ];
        let severity = assess_severity(&[], &[], &modified);
        assert_eq!(severity, RugPullSeverity::High);
    }

    #[test]
    fn severity_critical_for_suspicious_names() {
        let suspicious_names = vec![
            "exec", "shell", "system", "eval", "run", "execute", "command", "admin", "sudo", "root",
        ];

        for name in suspicious_names {
            let added = vec![format!("tool_{}", name)];
            let severity = assess_severity(&added, &[], &[]);
            assert_eq!(
                severity,
                RugPullSeverity::Critical,
                "Expected critical for tool with name containing '{}'",
                name
            );
        }
    }

    #[test]
    fn summary_no_changes() {
        let summary = generate_summary(&[], &[], &[]);
        assert_eq!(summary, "No changes detected");
    }

    #[test]
    fn summary_additions_only() {
        let added = vec!["tool1".to_string(), "tool2".to_string()];
        let summary = generate_summary(&added, &[], &[]);
        assert!(summary.contains("2 tools added"));
    }

    #[test]
    fn summary_removals_only() {
        let removed = vec!["tool1".to_string()];
        let summary = generate_summary(&[], &removed, &[]);
        assert!(summary.contains("1 tools removed"));
    }

    #[test]
    fn summary_modifications_only() {
        let modified = vec![
            ToolModification {
                name: "tool1".to_string(),
                description_changed: true,
                schema_changed: false,
                previous_hash: "old".to_string(),
                current_hash: "new".to_string(),
            },
            ToolModification {
                name: "tool2".to_string(),
                description_changed: false,
                schema_changed: true,
                previous_hash: "old".to_string(),
                current_hash: "new".to_string(),
            },
        ];
        let summary = generate_summary(&[], &[], &modified);
        assert!(summary.contains("1 schema changes"));
        assert!(summary.contains("1 description changes"));
    }

    #[test]
    fn summary_combined_changes() {
        let added = vec!["tool3".to_string()];
        let removed = vec!["tool1".to_string()];
        let modified = vec![ToolModification {
            name: "tool2".to_string(),
            description_changed: true,
            schema_changed: true,
            previous_hash: "old".to_string(),
            current_hash: "new".to_string(),
        }];
        let summary = generate_summary(&added, &removed, &modified);

        assert!(summary.contains("1 tools added"));
        assert!(summary.contains("1 tools removed"));
        assert!(summary.contains("1 schema changes"));
    }

    #[test]
    fn rug_pull_severity_display() {
        assert_eq!(RugPullSeverity::Info.to_string(), "info");
        assert_eq!(RugPullSeverity::Low.to_string(), "low");
        assert_eq!(RugPullSeverity::Medium.to_string(), "medium");
        assert_eq!(RugPullSeverity::High.to_string(), "high");
        assert_eq!(RugPullSeverity::Critical.to_string(), "critical");
    }

    #[test]
    fn rug_pull_detection_includes_timestamps() {
        let tools_v1 = vec![make_tool("tool1", "First tool")];
        let tools_v2 = vec![
            make_tool("tool1", "First tool"),
            make_tool("tool2", "New tool"),
        ];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2).unwrap();

        assert_eq!(detection.previous_seen, record.updated_at);
        assert_ne!(detection.previous_hash, detection.current_hash);
    }

    #[test]
    fn rug_pull_detection_includes_server_id() {
        let tools_v1 = vec![make_tool("tool1", "First tool")];
        let tools_v2 = vec![make_tool("tool1", "Changed tool")];

        let record = ToolHashRecord::from_tools("my-server", &tools_v1);
        let detection = detect_rug_pull("my-server", &record, &tools_v2).unwrap();

        assert_eq!(detection.server_id, "my-server");
    }

    #[test]
    fn detect_multiple_tools_added_and_removed() {
        let tools_v1 = vec![
            make_tool("tool1", "First"),
            make_tool("tool2", "Second"),
            make_tool("tool3", "Third"),
        ];
        let tools_v2 = vec![
            make_tool("tool1", "First"),
            make_tool("tool4", "Fourth"),
            make_tool("tool5", "Fifth"),
        ];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2).unwrap();

        assert_eq!(detection.added.len(), 2);
        assert_eq!(detection.removed.len(), 2);
        assert!(detection.added.contains(&"tool4".to_string()));
        assert!(detection.added.contains(&"tool5".to_string()));
        assert!(detection.removed.contains(&"tool2".to_string()));
        assert!(detection.removed.contains(&"tool3".to_string()));
    }

    #[test]
    fn tool_modification_tracks_hash_changes() {
        let tools_v1 = vec![make_tool("tool1", "Old description")];
        let tools_v2 = vec![make_tool("tool1", "New description")];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2).unwrap();

        assert_eq!(detection.modified.len(), 1);
        assert_ne!(
            detection.modified[0].previous_hash,
            detection.modified[0].current_hash
        );
    }

    #[test]
    fn empty_tools_list_detected() {
        let tools_v1 = vec![make_tool("tool1", "First")];
        let tools_v2 = vec![];

        let record = ToolHashRecord::from_tools("server", &tools_v1);
        let detection = detect_rug_pull("server", &record, &tools_v2).unwrap();

        assert_eq!(detection.removed.len(), 1);
        assert_eq!(detection.severity, RugPullSeverity::High);
    }

    #[test]
    fn case_sensitive_suspicious_names() {
        let added = vec!["EXECUTE_COMMAND".to_string()];
        let severity = assess_severity(&added, &[], &[]);
        assert_eq!(severity, RugPullSeverity::Critical);
    }
}