assay-core 3.29.0

High-performance evaluation framework for LLM agents (Core)
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
//! `assay.mcp_server_inventory.v0` carrier (MCP09a).
//!
//! A coverage-honest projection of discovered MCP servers into a producer artifact for the OWASP MCP09
//! (shadow-server) line. Discipline, mirroring the E35 experiment that proved the shape:
//!
//! - command/args are HASHED, never emitted raw (they routinely carry secrets and paths);
//! - credential-bearing fields are flagged by key/flag NAME only, never by value;
//! - scanner coverage is declared honestly with an explicit state per source, so an incomplete scan can
//!   never be read as an absence claim.
//!
//! This carrier is the producer half only. Classification against an approved allowlist
//! (shadow/drift/duplicate findings) is a separate consumer concern.

use crate::discovery::types::{DiscoveredServer, DiscoverySource, Transport};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use sha2::{Digest, Sha256};
use std::collections::BTreeMap;

/// The carrier schema id.
pub const SCHEMA: &str = "assay.mcp_server_inventory.v0";

/// The load-bearing non-claim: not observed is not absent unless coverage is complete.
pub const ABSENCE_NON_CLAIM: &str =
    "absence from inventory is not absence from environment unless scanner coverage is complete";

/// Per-source scan coverage. Anything other than `Complete` cannot support an absence claim.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CoverageState {
    Complete,
    Partial,
    NotScanned,
    Unavailable,
    Unsupported,
}

impl CoverageState {
    /// Only a `Complete` scan of a source can support an absence claim for it. Any other state means
    /// "not observed is not absent" - in particular a heuristic scanner must never be `Complete`.
    pub fn supports_absence_claim(self) -> bool {
        matches!(self, CoverageState::Complete)
    }
}

/// What the scan actually covered, declared honestly.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScannerCoverage {
    pub config_sources: BTreeMap<String, CoverageState>,
    pub process_scan: CoverageState,
    pub network_scan: CoverageState,
}

fn digest(value: &Value) -> String {
    let bytes = serde_jcs::to_vec(value).expect("inventory carrier values are JSON-serializable");
    format!("sha256:{}", hex::encode(Sha256::digest(&bytes)))
}

fn source_id(source: &DiscoverySource) -> String {
    match source {
        DiscoverySource::ConfigFile { client, .. } => format!("{client}_mcp_config"),
        DiscoverySource::RunningProcess { .. } => "process_scan".to_string(),
        DiscoverySource::NetworkScan { .. } => "network_scan".to_string(),
    }
}

/// (transport label, command-or-url, args).
fn transport_parts(transport: &Transport) -> (&'static str, String, Vec<String>) {
    match transport {
        Transport::Stdio { command, args } => ("stdio", command.clone(), args.clone()),
        Transport::Http { url } => ("http", url.clone(), Vec::new()),
        Transport::Sse { url } => ("sse", url.clone(), Vec::new()),
        Transport::WebSocket { url } => ("websocket", url.clone(), Vec::new()),
        Transport::Unknown => ("unknown", String::new(), Vec::new()),
    }
}

fn name_is_credential(name: &str) -> bool {
    let lowered = name.to_ascii_lowercase();
    const NEEDLES: [&str; 8] = [
        "token",
        "secret",
        "password",
        "passwd",
        "credential",
        "auth",
        "apikey",
        "api_key",
    ];
    NEEDLES.iter().any(|needle| lowered.contains(needle))
        || lowered.split(['_', '-']).any(|part| part == "key")
}

/// A credential-bearing arg flag (by name), e.g. `--token=...` -> `arg:--token`. Never the value.
fn credential_arg_flag(arg: &str) -> Option<String> {
    let flag = arg.split('=').next().unwrap_or(arg);
    let normalized = flag.trim_start_matches('-').to_ascii_lowercase();
    const FLAGS: [&str; 11] = [
        "token",
        "api-key",
        "api_key",
        "apikey",
        "key",
        "secret",
        "password",
        "passwd",
        "auth",
        "authorization",
        "bearer",
    ];
    if FLAGS.contains(&normalized.as_str()) {
        Some(flag.to_string())
    } else {
        None
    }
}

fn credential_indicators(server: &DiscoveredServer, args: &[String]) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for env_key in &server.env_vars {
        if name_is_credential(env_key) {
            out.push(format!("env:{env_key}"));
        }
    }
    for arg in args {
        if let Some(flag) = credential_arg_flag(arg) {
            out.push(format!("arg:{flag}"));
        }
    }
    out.sort();
    out.dedup();
    out
}

/// Project discovered servers + scan coverage into the `assay.mcp_server_inventory.v0` carrier.
/// Deterministic: servers are sorted by (server_id, source); only digests and credential names are
/// emitted, never raw command/args or secret values.
pub fn to_inventory_carrier_v0(servers: &[DiscoveredServer], coverage: &ScannerCoverage) -> Value {
    let mut rows: Vec<(String, String, Value)> = servers
        .iter()
        .map(|server| {
            let (transport, mut command, args) = transport_parts(&server.transport);
            // A running-process row carries no transport command, but the source has the cmdline.
            // Hash that (still never raw) so a consumer can identify/compare what was observed rather
            // than seeing every process row collapse to the same empty digest.
            if command.is_empty() {
                if let DiscoverySource::RunningProcess { cmdline, .. } = &server.source {
                    command = cmdline.clone();
                }
            }
            let source = source_id(&server.source);
            let row = json!({
                "server_id": server.id,
                "source": source,
                "transport": transport,
                "command_digest": digest(&json!(command)),
                "args_digest": digest(&json!(args)),
                "credential_indicators": credential_indicators(server, &args),
                "observed_state": "observed",
            });
            (server.id.clone(), source, row)
        })
        .collect();
    rows.sort_by(|a, b| (a.0.as_str(), a.1.as_str()).cmp(&(b.0.as_str(), b.1.as_str())));
    let servers: Vec<Value> = rows.into_iter().map(|(_, _, row)| row).collect();

    json!({
        "schema": SCHEMA,
        "scanner_coverage": {
            "config_sources": coverage.config_sources,
            "process_scan": coverage.process_scan,
            "network_scan": coverage.network_scan,
        },
        "servers": servers,
        "non_claims": [ABSENCE_NON_CLAIM],
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::discovery::types::{AuthStatus, PolicyStatus, ServerStatus};
    use std::path::PathBuf;

    fn config_server(
        id: &str,
        client: &str,
        command: &str,
        args: &[&str],
        env: &[&str],
    ) -> DiscoveredServer {
        DiscoveredServer {
            id: id.to_string(),
            name: None,
            source: DiscoverySource::ConfigFile {
                path: PathBuf::from(format!("/cfg/{client}.json")),
                client: client.to_string(),
            },
            transport: Transport::Stdio {
                command: command.to_string(),
                args: args.iter().map(|a| a.to_string()).collect(),
            },
            status: ServerStatus::Configured,
            policy_status: PolicyStatus::Unmanaged,
            auth: AuthStatus::Unknown,
            env_vars: env.iter().map(|e| e.to_string()).collect(),
            risk_hints: Vec::new(),
        }
    }

    fn fixture_coverage() -> ScannerCoverage {
        let mut config_sources = BTreeMap::new();
        config_sources.insert("claude_desktop".to_string(), CoverageState::Complete);
        config_sources.insert("cursor".to_string(), CoverageState::NotScanned);
        ScannerCoverage {
            config_sources,
            process_scan: CoverageState::Unavailable,
            network_scan: CoverageState::Unsupported,
        }
    }

    fn golden_fixture_servers() -> Vec<DiscoveredServer> {
        vec![
            config_server(
                "github-tools",
                "claude_desktop",
                "npx",
                &["-y", "@modelcontextprotocol/server-github"],
                &["GITHUB_TOKEN"],
            ),
            config_server("notes", "claude_desktop", "node", &["/opt/notes.js"], &[]),
        ]
    }

    #[test]
    fn carrier_matches_golden_fixture() {
        let carrier = to_inventory_carrier_v0(&golden_fixture_servers(), &fixture_coverage());
        let path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures/mcp_server_inventory_v0.golden.json");
        if std::env::var("ASSAY_UPDATE_GOLDEN").is_ok() {
            std::fs::create_dir_all(path.parent().unwrap()).unwrap();
            std::fs::write(
                &path,
                format!("{}\n", serde_json::to_string_pretty(&carrier).unwrap()),
            )
            .unwrap();
        }
        let committed: Value =
            serde_json::from_str(&std::fs::read_to_string(&path).unwrap_or_else(|_| {
                panic!(
                    "missing {}; regenerate with ASSAY_UPDATE_GOLDEN=1",
                    path.display()
                )
            }))
            .unwrap();
        assert_eq!(
            committed, carrier,
            "mcp_server_inventory.v0 golden is stale; regenerate with ASSAY_UPDATE_GOLDEN=1"
        );
    }

    #[test]
    fn carrier_shape_is_pinned() {
        let servers = vec![
            config_server(
                "github-tools",
                "claude_desktop",
                "npx",
                &["-y", "@modelcontextprotocol/server-github"],
                &["GITHUB_TOKEN"],
            ),
            config_server("notes", "claude_desktop", "node", &["/opt/notes.js"], &[]),
        ];
        let carrier = to_inventory_carrier_v0(&servers, &fixture_coverage());

        assert_eq!(carrier["schema"], SCHEMA);
        assert_eq!(carrier["non_claims"][0], ABSENCE_NON_CLAIM);
        assert_eq!(carrier["scanner_coverage"]["process_scan"], "unavailable");
        assert_eq!(carrier["scanner_coverage"]["network_scan"], "unsupported");
        assert_eq!(
            carrier["scanner_coverage"]["config_sources"]["claude_desktop"],
            "complete"
        );

        let rows = carrier["servers"].as_array().unwrap();
        assert_eq!(rows.len(), 2);
        // sorted by server_id: github-tools before notes
        assert_eq!(rows[0]["server_id"], "github-tools");
        assert_eq!(rows[1]["server_id"], "notes");
        // credential flagged by NAME only
        assert_eq!(rows[0]["credential_indicators"][0], "env:GITHUB_TOKEN");
        assert!(rows[1]["credential_indicators"]
            .as_array()
            .unwrap()
            .is_empty());
        // command/args are digests, not raw
        assert!(rows[0]["command_digest"]
            .as_str()
            .unwrap()
            .starts_with("sha256:"));
        assert!(rows[0]["args_digest"]
            .as_str()
            .unwrap()
            .starts_with("sha256:"));
    }

    #[test]
    fn no_raw_command_or_args_or_secret_values_leak() {
        let servers = vec![config_server(
            "x",
            "claude_desktop",
            "npx",
            &["-y", "@modelcontextprotocol/server-github"],
            &["GITHUB_TOKEN"],
        )];
        let carrier = to_inventory_carrier_v0(&servers, &fixture_coverage());
        let blob = serde_json::to_string(&carrier).unwrap();
        // The row carries digests + the credential field NAME, never the raw command/args values.
        for forbidden in ["npx", "@modelcontextprotocol/server-github"] {
            assert!(
                !blob.contains(forbidden),
                "carrier must not leak `{forbidden}`"
            );
        }
        // ...and never the raw command/args KEYS (only command_digest / args_digest).
        let row = &carrier["servers"][0];
        assert!(row.get("command").is_none() && row.get("args").is_none());
    }

    #[test]
    fn http_endpoint_hashes_url_and_has_empty_args() {
        let server = DiscoveredServer {
            id: "remote".to_string(),
            name: None,
            source: DiscoverySource::ConfigFile {
                path: PathBuf::from("/cfg/cursor.json"),
                client: "cursor".to_string(),
            },
            transport: Transport::Http {
                url: "http://localhost:8080/mcp".to_string(),
            },
            status: ServerStatus::Configured,
            policy_status: PolicyStatus::Unmanaged,
            auth: AuthStatus::Unknown,
            env_vars: Vec::new(),
            risk_hints: Vec::new(),
        };
        let carrier = to_inventory_carrier_v0(&[server], &fixture_coverage());
        let row = &carrier["servers"][0];
        assert_eq!(row["transport"], "http");
        assert_eq!(row["source"], "cursor_mcp_config");
        assert!(!serde_json::to_string(&carrier)
            .unwrap()
            .contains("localhost:8080"));
    }

    fn process_server(id: &str, pid: u32, cmdline: &str) -> DiscoveredServer {
        DiscoveredServer {
            id: id.to_string(),
            name: None,
            source: DiscoverySource::RunningProcess {
                pid,
                cmdline: cmdline.to_string(),
                started_at: None,
                user: None,
            },
            transport: Transport::Unknown,
            status: ServerStatus::Running,
            policy_status: PolicyStatus::Unmanaged,
            auth: AuthStatus::Unknown,
            env_vars: Vec::new(),
            risk_hints: Vec::new(),
        }
    }

    #[test]
    fn process_rows_hash_distinct_cmdlines_without_leaking_them() {
        let servers = vec![
            process_server("proc-1", 1, "node /opt/a/mcp-server.js --port 1"),
            process_server("proc-2", 2, "node /opt/b/mcp-server.js --port 2"),
        ];
        let carrier = to_inventory_carrier_v0(&servers, &fixture_coverage());
        let rows = carrier["servers"].as_array().unwrap();
        // Distinct cmdlines -> distinct command digests (not the empty-input collision).
        assert_ne!(rows[0]["command_digest"], rows[1]["command_digest"]);
        assert!(rows[0]["command_digest"]
            .as_str()
            .unwrap()
            .starts_with("sha256:"));
        // ...and the raw cmdline never appears in the carrier.
        let blob = serde_json::to_string(&carrier).unwrap();
        assert!(!blob.contains("/opt/a/mcp-server.js") && !blob.contains("--port"));
    }

    #[test]
    fn only_complete_coverage_supports_an_absence_claim() {
        assert!(CoverageState::Complete.supports_absence_claim());
        for state in [
            CoverageState::Partial,
            CoverageState::NotScanned,
            CoverageState::Unavailable,
            CoverageState::Unsupported,
        ] {
            assert!(
                !state.supports_absence_claim(),
                "{state:?} must not support an absence claim"
            );
        }
    }
}