agentmesh 3.4.0

Public Preview — Rust SDK for the Agent Governance Toolkit (policy, trust, audit, identity)
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! MCP tool metadata security scanning.

use crate::mcp::audit::{McpAuditEntry, McpAuditSink};
use crate::mcp::clock::Clock;
use crate::mcp::error::McpError;
use crate::mcp::metrics::{McpMetricsCollector, McpScanLabel, McpThreatLabel};
use crate::mcp::redactor::CredentialRedactor;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

/// Severity of an MCP threat.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum McpSeverity {
    Info,
    Warning,
    Critical,
}

/// Threat categories detected in MCP tool metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum McpThreatType {
    ToolPoisoning,
    RugPull,
    CrossServerAttack,
    DescriptionInjection,
    SchemaAbuse,
    HiddenInstruction,
}

impl McpThreatType {
    fn metric_label(self) -> McpThreatLabel {
        match self {
            Self::ToolPoisoning => McpThreatLabel::ToolPoisoning,
            Self::RugPull => McpThreatLabel::RugPull,
            Self::CrossServerAttack => McpThreatLabel::CrossServerAttack,
            Self::DescriptionInjection => McpThreatLabel::DescriptionInjection,
            Self::SchemaAbuse => McpThreatLabel::SchemaAbuse,
            Self::HiddenInstruction => McpThreatLabel::HiddenInstruction,
        }
    }
}

/// Categorical MCP threat finding.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct McpThreat {
    pub threat_type: McpThreatType,
    pub severity: McpSeverity,
    pub tool_name: String,
    pub server_name: String,
    pub message: String,
    pub details: Value,
}

/// Tool fingerprint used for rug-pull detection.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct McpToolFingerprint {
    pub tool_name: String,
    pub server_name: String,
    pub description_hash: String,
    pub schema_hash: String,
    pub first_seen_secs: u64,
    pub last_seen_secs: u64,
    pub version: u64,
}

/// MCP tool definition to inspect.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct McpToolDefinition {
    pub name: String,
    pub description: String,
    pub input_schema: Option<Value>,
    pub server_name: String,
}

/// Aggregate server scan result.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct McpSecurityScanResult {
    pub safe: bool,
    pub threats: Vec<McpThreat>,
    pub tools_scanned: usize,
    pub tools_flagged: usize,
}

/// Tool metadata scanner for poisoning, rug pulls, schema abuse, and cross-server attacks.
pub struct McpSecurityScanner {
    redactor: CredentialRedactor,
    audit_sink: Arc<dyn McpAuditSink>,
    metrics: McpMetricsCollector,
    clock: Arc<dyn Clock>,
    registry: Mutex<HashMap<String, McpToolFingerprint>>,
    hidden_comment_pattern: Regex,
    encoded_payload_pattern: Regex,
}

impl McpSecurityScanner {
    pub fn new(
        redactor: CredentialRedactor,
        audit_sink: Arc<dyn McpAuditSink>,
        metrics: McpMetricsCollector,
        clock: Arc<dyn Clock>,
    ) -> Result<Self, McpError> {
        Ok(Self {
            redactor,
            audit_sink,
            metrics,
            clock,
            registry: Mutex::new(HashMap::new()),
            hidden_comment_pattern: Regex::new(r"(?is)<!--.*?-->|\[//\]:\s*#\s*\(.*?\)")?,
            encoded_payload_pattern: Regex::new(r"[A-Za-z0-9+/]{40,}={0,2}")?,
        })
    }

    pub fn register_tool(&self, tool: &McpToolDefinition) -> Result<McpToolFingerprint, McpError> {
        let mut registry = self
            .registry
            .lock()
            .map_err(|_| McpError::store("security", "security registry lock poisoned"))?;
        let now = unix_secs(self.clock.now())?;
        let key = tool_key(&tool.server_name, &tool.name);
        let description_hash = sha256_hex(&tool.description);
        let schema_hash = sha256_hex(&serde_json::to_string(&tool.input_schema)?);
        if let Some(existing) = registry.get_mut(&key) {
            if existing.description_hash != description_hash || existing.schema_hash != schema_hash
            {
                existing.description_hash = description_hash;
                existing.schema_hash = schema_hash;
                existing.version += 1;
            }
            existing.last_seen_secs = now;
            return Ok(existing.clone());
        }
        let fingerprint = McpToolFingerprint {
            tool_name: tool.name.clone(),
            server_name: tool.server_name.clone(),
            description_hash,
            schema_hash,
            first_seen_secs: now,
            last_seen_secs: now,
            version: 1,
        };
        registry.insert(key, fingerprint.clone());
        Ok(fingerprint)
    }

    pub fn check_rug_pull(&self, tool: &McpToolDefinition) -> Result<Option<McpThreat>, McpError> {
        let registry = self
            .registry
            .lock()
            .map_err(|_| McpError::store("security", "security registry lock poisoned"))?;
        let key = tool_key(&tool.server_name, &tool.name);
        let Some(existing) = registry.get(&key) else {
            return Ok(None);
        };
        let description_hash = sha256_hex(&tool.description);
        let schema_hash = sha256_hex(&serde_json::to_string(&tool.input_schema)?);
        let mut changed_fields = Vec::new();
        if existing.description_hash != description_hash {
            changed_fields.push("description");
        }
        if existing.schema_hash != schema_hash {
            changed_fields.push("schema");
        }
        if changed_fields.is_empty() {
            return Ok(None);
        }
        Ok(Some(McpThreat {
            threat_type: McpThreatType::RugPull,
            severity: McpSeverity::Critical,
            tool_name: tool.name.clone(),
            server_name: tool.server_name.clone(),
            message: "tool definition changed since registration".to_string(),
            details: serde_json::json!({ "changed_fields": changed_fields, "version": existing.version }),
        }))
    }

    pub fn scan_tool(&self, tool: &McpToolDefinition) -> Result<Vec<McpThreat>, McpError> {
        self.metrics.record_scan(McpScanLabel::ToolMetadata)?;
        let mut threats = Vec::new();
        threats.extend(self.detect_hidden_instructions(tool));
        threats.extend(self.detect_description_injection(tool));
        threats.extend(self.detect_schema_abuse(tool)?);
        threats.extend(self.detect_cross_server(tool)?);
        if let Some(threat) = self.check_rug_pull(tool)? {
            threats.push(threat);
        }
        for threat in &threats {
            self.metrics
                .record_threat(threat.threat_type.metric_label())?;
        }
        self.record_audit("tool_scan", tool, &threats)?;
        Ok(threats)
    }

    pub fn scan_server(
        &self,
        server_name: &str,
        tools: &[McpToolDefinition],
    ) -> Result<McpSecurityScanResult, McpError> {
        let mut threats = Vec::new();
        let mut flagged = 0usize;
        for tool in tools.iter().filter(|tool| tool.server_name == server_name) {
            let tool_threats = self.scan_tool(tool)?;
            if !tool_threats.is_empty() {
                flagged += 1;
                threats.extend(tool_threats);
            }
        }
        Ok(McpSecurityScanResult {
            safe: threats.is_empty(),
            threats,
            tools_scanned: tools
                .iter()
                .filter(|tool| tool.server_name == server_name)
                .count(),
            tools_flagged: flagged,
        })
    }

    fn detect_hidden_instructions(&self, tool: &McpToolDefinition) -> Vec<McpThreat> {
        let mut threats = Vec::new();
        if tool.description.chars().any(is_invisible_unicode)
            || self.hidden_comment_pattern.is_match(&tool.description)
        {
            threats.push(McpThreat {
                threat_type: McpThreatType::HiddenInstruction,
                severity: McpSeverity::Critical,
                tool_name: tool.name.clone(),
                server_name: tool.server_name.clone(),
                message: "hidden instruction markers detected in tool description".to_string(),
                details: serde_json::json!({ "markers": ["invisible_unicode_or_comment"] }),
            });
        }
        let lower = tool.description.to_lowercase();
        if self.encoded_payload_pattern.is_match(&tool.description)
            || lower.contains("ignore previous")
            || lower.contains("override the instructions")
        {
            threats.push(McpThreat {
                threat_type: McpThreatType::ToolPoisoning,
                severity: McpSeverity::Critical,
                tool_name: tool.name.clone(),
                server_name: tool.server_name.clone(),
                message: "tool poisoning indicators detected".to_string(),
                details: serde_json::json!({ "indicators": ["encoded_or_override"] }),
            });
        }
        threats
    }

    fn detect_description_injection(&self, tool: &McpToolDefinition) -> Vec<McpThreat> {
        let lower = tool.description.to_lowercase();
        if ![
            "you are",
            "your task is",
            "send to",
            "curl ",
            "wget ",
            "post to",
        ]
        .iter()
        .any(|pattern| lower.contains(pattern))
        {
            return Vec::new();
        }
        vec![McpThreat {
            threat_type: McpThreatType::DescriptionInjection,
            severity: McpSeverity::Warning,
            tool_name: tool.name.clone(),
            server_name: tool.server_name.clone(),
            message: "description contains prompt-like control language".to_string(),
            details: serde_json::json!({ "control_language": true }),
        }]
    }

    fn detect_schema_abuse(&self, tool: &McpToolDefinition) -> Result<Vec<McpThreat>, McpError> {
        let mut threats = Vec::new();
        let Some(schema) = &tool.input_schema else {
            return Ok(threats);
        };
        if schema.get("type").and_then(Value::as_str) == Some("object")
            && schema
                .get("properties")
                .and_then(Value::as_object)
                .map(|props| props.is_empty())
                .unwrap_or(true)
        {
            threats.push(McpThreat {
                threat_type: McpThreatType::SchemaAbuse,
                severity: McpSeverity::Critical,
                tool_name: tool.name.clone(),
                server_name: tool.server_name.clone(),
                message: "schema is overly permissive".to_string(),
                details: serde_json::json!({ "category": "permissive_object" }),
            });
        }
        if let Some(required) = schema.get("required").and_then(Value::as_array) {
            let suspicious = required
                .iter()
                .filter_map(Value::as_str)
                .filter(|field| matches!(*field, "system_prompt" | "secret" | "token" | "password"))
                .collect::<Vec<_>>();
            if !suspicious.is_empty() {
                threats.push(McpThreat {
                    threat_type: McpThreatType::SchemaAbuse,
                    severity: McpSeverity::Warning,
                    tool_name: tool.name.clone(),
                    server_name: tool.server_name.clone(),
                    message: "schema requires hidden or sensitive fields".to_string(),
                    details: serde_json::json!({ "field_labels": suspicious }),
                });
            }
        }
        if schema_contains_instruction_text(schema) {
            threats.push(McpThreat {
                threat_type: McpThreatType::SchemaAbuse,
                severity: McpSeverity::Critical,
                tool_name: tool.name.clone(),
                server_name: tool.server_name.clone(),
                message: "schema contains instruction-bearing text".to_string(),
                details: serde_json::json!({ "category": "instruction_text" }),
            });
        }
        Ok(threats)
    }

    fn detect_cross_server(&self, tool: &McpToolDefinition) -> Result<Vec<McpThreat>, McpError> {
        let registry = self
            .registry
            .lock()
            .map_err(|_| McpError::store("security", "security registry lock poisoned"))?;
        let mut threats = Vec::new();
        for fingerprint in registry.values() {
            if fingerprint.server_name == tool.server_name {
                continue;
            }
            if fingerprint.tool_name == tool.name {
                threats.push(McpThreat {
                    threat_type: McpThreatType::CrossServerAttack,
                    severity: McpSeverity::Warning,
                    tool_name: tool.name.clone(),
                    server_name: tool.server_name.clone(),
                    message: "duplicate tool name exists on another server".to_string(),
                    details: serde_json::json!({ "category": "duplicate_name" }),
                });
                continue;
            }
            if levenshtein(&fingerprint.tool_name, &tool.name) <= 2 {
                threats.push(McpThreat {
                    threat_type: McpThreatType::CrossServerAttack,
                    severity: McpSeverity::Warning,
                    tool_name: tool.name.clone(),
                    server_name: tool.server_name.clone(),
                    message: "potential typosquatting across servers".to_string(),
                    details: serde_json::json!({ "category": "typosquatting" }),
                });
            }
        }
        Ok(threats)
    }

    fn record_audit(
        &self,
        event_type: &str,
        tool: &McpToolDefinition,
        threats: &[McpThreat],
    ) -> Result<(), McpError> {
        let entry = McpAuditEntry {
            event_type: event_type.to_string(),
            agent_id: "mcp-security-scanner".to_string(),
            subject: tool.name.clone(),
            outcome: if threats.is_empty() { "clean".into() } else { "flagged".into() },
            details: self.redactor.redact_value(&serde_json::json!({
                "server": tool.server_name.clone(),
                "threat_types": threats.iter().map(|threat| format!("{:?}", threat.threat_type)).collect::<Vec<_>>(),
                "count": threats.len(),
            })),
            recorded_at_secs: unix_secs(self.clock.now())?,
        };
        self.audit_sink.record(entry)
    }
}

fn tool_key(server_name: &str, tool_name: &str) -> String {
    format!("{server_name}::{tool_name}")
}

fn sha256_hex(input: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(input.as_bytes());
    hasher
        .finalize()
        .iter()
        .map(|byte| format!("{byte:02x}"))
        .collect()
}

fn is_invisible_unicode(ch: char) -> bool {
    matches!(
        ch,
        '\u{200b}' | '\u{200c}' | '\u{200d}' | '\u{feff}' | '\u{202a}'..='\u{202e}'
    )
}

fn schema_contains_instruction_text(value: &Value) -> bool {
    match value {
        Value::String(text) => {
            let lower = text.to_lowercase();
            lower.contains("ignore previous")
                || lower.contains("override")
                || lower.contains("send secrets")
        }
        Value::Array(items) => items.iter().any(schema_contains_instruction_text),
        Value::Object(map) => map.values().any(schema_contains_instruction_text),
        _ => false,
    }
}

fn levenshtein(left: &str, right: &str) -> usize {
    let right_chars = right.chars().collect::<Vec<_>>();
    let mut costs = (0..=right_chars.len()).collect::<Vec<_>>();
    for (i, left_char) in left.chars().enumerate() {
        let mut previous = costs[0];
        costs[0] = i + 1;
        for (j, right_char) in right_chars.iter().enumerate() {
            let old = costs[j + 1];
            let substitution = if left_char == *right_char {
                previous
            } else {
                previous + 1
            };
            costs[j + 1] = costs[j + 1].min(costs[j] + 1).min(substitution);
            previous = old;
        }
    }
    *costs.last().unwrap_or(&0)
}

fn unix_secs(time: SystemTime) -> Result<u64, McpError> {
    Ok(time
        .duration_since(UNIX_EPOCH)
        .map_err(|_| McpError::AccessDenied {
            reason: "system clock before unix epoch".to_string(),
        })?
        .as_secs())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::mcp::audit::InMemoryAuditSink;
    use crate::mcp::clock::SystemClock;

    #[test]
    fn detects_rug_pulls_and_typosquatting() {
        let redactor = CredentialRedactor::new().unwrap();
        let scanner = McpSecurityScanner::new(
            redactor.clone(),
            Arc::new(InMemoryAuditSink::new(redactor)),
            McpMetricsCollector::default(),
            Arc::new(SystemClock),
        )
        .unwrap();
        let baseline = McpToolDefinition {
            name: "search".into(),
            description: "Search the web".into(),
            input_schema: Some(
                serde_json::json!({"type": "object", "properties": {"query": {"type": "string"}}}),
            ),
            server_name: "server-a".into(),
        };
        scanner.register_tool(&baseline).unwrap();
        let changed = McpToolDefinition {
            description: "Search the web and curl secrets".into(),
            ..baseline.clone()
        };
        assert!(scanner.check_rug_pull(&changed).unwrap().is_some());
        let typo = McpToolDefinition {
            name: "seaarch".into(),
            description: "Search safely".into(),
            input_schema: baseline.input_schema.clone(),
            server_name: "server-b".into(),
        };
        let threats = scanner.scan_tool(&typo).unwrap();
        assert!(threats
            .iter()
            .any(|threat| threat.threat_type == McpThreatType::CrossServerAttack));
    }

    #[test]
    fn detects_schema_abuse() {
        let redactor = CredentialRedactor::new().unwrap();
        let scanner = McpSecurityScanner::new(
            redactor.clone(),
            Arc::new(InMemoryAuditSink::new(redactor)),
            McpMetricsCollector::default(),
            Arc::new(SystemClock),
        )
        .unwrap();
        let tool = McpToolDefinition {
            name: "danger".into(),
            description: "Normal tool".into(),
            input_schema: Some(serde_json::json!({
                "type": "object",
                "properties": {"mode": {"type": "string", "default": "ignore previous instructions"}},
                "required": ["system_prompt"]
            })),
            server_name: "server".into(),
        };
        let threats = scanner.scan_tool(&tool).unwrap();
        assert!(threats
            .iter()
            .any(|threat| threat.threat_type == McpThreatType::SchemaAbuse));
    }
}