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
//! Fuzz plan: generates payloads for each tool and reports the resulting
//! findings.
use std::time::Duration;
use anyhow::{Context, Result};
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use serde::Serialize;
use serde_json::Value;
use crate::{
client::CallOutcome,
corpus::Corpus,
finding::{Finding, FindingKind, ReproInfo},
fuzz_corpus::{response_fingerprint, CorpusTrigger, FuzzCorpus, FuzzCorpusEntry},
mutate::{corpus_mutator, try_generate_payload, GenMode},
seed::{derive_seed, derive_seed_canonical},
target::SeverityConfig,
};
use super::{
destructive::DestructiveDetector,
exec::McpExec,
glob,
reporter::{Reporter, RunInfo},
};
/// Tools whose schema could not be exercised. Reasons are surfaced from
/// [`crate::mutate::SkipReason`] formatted as a string.
#[derive(Debug, Clone, Serialize)]
pub struct SkippedTool {
/// Tool name.
pub tool: String,
/// Why we gave up (e.g. unresolved `$ref`).
pub reason: String,
}
/// Outcome of a fuzz run.
///
/// Phase E4: findings are streamed to the corpus and to the reporter
/// during the run; this report carries only counts and the diagnostic
/// lists (skipped, blocked) needed for exit-code logic and post-run
/// summaries. Front-ends that need the findings themselves accumulate
/// them via [`Reporter::on_finding`].
#[derive(Debug, Default, Serialize)]
pub struct FuzzReport {
/// Number of findings produced during the run.
pub findings_count: usize,
/// Tools we could not generate inputs for.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub skipped: Vec<SkippedTool>,
/// Tools that were filtered out as destructive without an allowlist
/// match. Surfaced for visibility, not as findings.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub blocked: Vec<String>,
}
/// Returned when a plan runs without errors. Distinct from `FuzzReport`
/// only because dry-run mode does not produce a report.
#[derive(Debug)]
pub enum FuzzOutcome {
/// Tools that would be fuzzed; produced by [`FuzzPlan::dry_run`].
DryRun(Vec<String>),
/// Real fuzz results; produced by [`FuzzPlan::execute`].
Completed(FuzzReport),
}
/// A reproducible fuzz plan.
#[derive(Debug)]
pub struct FuzzPlan {
/// Number of payloads generated per tool.
pub iterations: u64,
/// Generation mode (Conform / Adversarial / Mixed).
pub mode: GenMode,
/// Master seed used to derive per-iteration seeds. The same seed
/// reproduces the same sequence of payloads.
pub master_seed: u64,
/// Glob patterns: empty = match every tool name.
pub include: Vec<String>,
/// Glob patterns excluded from the fuzz set. Always honored.
pub exclude: Vec<String>,
/// Cap on the number of tools after filtering. `None` = unlimited.
pub max_tools: Option<usize>,
/// Timeout applied to each `call_tool`.
pub timeout: Duration,
/// Transport label persisted in the [`ReproInfo`]. Plans don't open
/// the transport themselves, so we receive a stable name (`stdio` /
/// `http`) from the caller.
pub transport_name: String,
/// Compiled destructive-tool detector built from
/// `[destructive]` + `[allow_destructive]` config.
pub detector: DestructiveDetector,
/// `[severity]` overrides from `wallfacer.toml`. Applied to every
/// produced finding before it lands on disk.
pub severity: SeverityConfig,
/// Phase R — optional persistent fuzz corpus. When set, the
/// loop pulls inputs that triggered findings or new response
/// fingerprints from prior runs and mutates from them
/// `mutate_ratio` fraction of the time. Pure schema-driven
/// generation handles the remainder so the fuzzer keeps
/// exploring beyond the corpus's basin.
pub fuzz_corpus: Option<FuzzCorpus>,
/// Phase R — fraction of iterations that mutate from the
/// corpus instead of generating fresh schema-driven payloads.
/// Default `0.9` matches AFL/libFuzzer convention. Ignored
/// when [`Self::fuzz_corpus`] is `None` or the corpus is
/// empty.
pub mutate_ratio: f64,
}
impl FuzzPlan {
/// Returns the tool names that would be fuzzed, for `--dry-run`.
pub async fn dry_run<C: McpExec + ?Sized>(&self, client: &C) -> Result<Vec<String>> {
let (tools, _blocked) = self.select_tools(client).await?;
Ok(tools
.into_iter()
.map(|tool| tool.name.to_string())
.collect())
}
/// Drives the full fuzz loop, persisting findings to `corpus` and
/// notifying `reporter` along the way.
pub async fn execute<C: McpExec + ?Sized>(
self,
client: &mut C,
corpus: &Corpus,
reporter: &mut dyn Reporter,
) -> Result<FuzzReport> {
let (tools, blocked) = self.select_tools(client).await?;
let total = tools.len() as u64 * self.iterations;
reporter.on_run_start(&RunInfo {
kind: "fuzz",
total_iterations: total,
tools: tools.iter().map(|tool| tool.name.to_string()).collect(),
blocked: blocked.clone(),
master_seed: Some(self.master_seed),
});
let mut report = FuzzReport {
findings_count: 0,
skipped: Vec::new(),
blocked,
};
// Phase R — preload the corpus (if enabled) and the
// fingerprint set so we can dedup novel responses against
// prior runs.
let mut seen_fingerprints: std::collections::BTreeSet<String> =
std::collections::BTreeSet::new();
if let Some(corpus_ref) = self.fuzz_corpus.as_ref() {
for tool in &tools {
let tool_name = tool.name.to_string();
if let Ok(entries) = corpus_ref.list(&tool_name) {
for e in entries {
seen_fingerprints.insert(e.fingerprint);
}
}
}
}
for tool in tools {
let tool_name = tool.name.to_string();
let input_schema = Value::Object((*tool.input_schema).clone());
// Cache prior corpus entries for THIS tool so the
// 90/10 split doesn't re-list every iteration.
let prior_corpus: Vec<FuzzCorpusEntry> = self
.fuzz_corpus
.as_ref()
.map(|c| c.list(&tool_name).unwrap_or_default())
.unwrap_or_default();
for iteration in 0..self.iterations {
reporter.on_iteration_start(&tool_name, iteration);
let seed = derive_seed(self.master_seed, &tool_name, iteration);
let canonical = derive_seed_canonical(self.master_seed, &tool_name, iteration);
let mut rng = ChaCha20Rng::from_seed(canonical);
// 90/10 mutate-vs-random when the corpus has at
// least one prior entry for this tool. Without a
// corpus or with an empty per-tool sub-corpus we
// fall back to pure schema-driven generation.
use rand::Rng;
let use_mutation = !prior_corpus.is_empty()
&& self.fuzz_corpus.is_some()
&& rng.gen_bool(self.mutate_ratio.clamp(0.0, 1.0));
let (payload_value, payload_trail): (Value, Vec<String>) = if use_mutation {
let pick = &prior_corpus[rng.gen_range(0..prior_corpus.len())];
let mutated = corpus_mutator::mutate(&pick.input, &mut rng);
(mutated, vec![format!("mutated from corpus seed")])
} else {
match try_generate_payload(&input_schema, &mut rng, self.mode) {
Ok(payload) => (payload.value, payload.trail),
Err(reason) => {
let skip = SkippedTool {
tool: tool_name.clone(),
reason: reason.to_string(),
};
reporter.on_skipped(&skip.tool, &skip.reason);
report.skipped.push(skip);
// Bump remaining iterations on the reporter so the
// progress bar accounts for the skipped tail.
for i in (iteration + 1)..self.iterations {
reporter.on_iteration_end(&tool_name, i);
}
break;
}
}
};
let outcome = client
.call_tool(&tool_name, payload_value.clone(), self.timeout)
.await;
// Phase R — capture the response fingerprint
// *before* we destructure outcome (the match below
// moves Hang/Crash/ProtocolError out of the enum).
let response_value: Value = match &outcome {
CallOutcome::Ok(result) => serde_json::to_value(result).unwrap_or(Value::Null),
_ => Value::Null,
};
let fingerprint = response_fingerprint(&response_value);
let kind_message_details: Option<(FindingKind, &str, String)> = match outcome {
CallOutcome::Ok(_) => None,
CallOutcome::Hang(duration) => Some((
FindingKind::Hang {
ms: duration.as_millis() as u64,
},
"tool call timed out",
format!("timeout exceeded after {duration:?}"),
)),
CallOutcome::Crash(reason) => Some((
FindingKind::Crash,
"server crashed during tool call",
reason,
)),
CallOutcome::ProtocolError(message) => Some((
FindingKind::ProtocolError,
"protocol error during tool call",
message,
)),
};
if let Some((kind, message, details)) = kind_message_details {
let mut finding = Finding::new(
kind,
&tool_name,
message,
details,
ReproInfo {
seed,
tool_call: payload_value.clone(),
transport: self.transport_name.clone(),
composition_trail: payload_trail,
},
);
if let Some(override_sev) = self.severity.resolve(finding.kind.keyword()) {
finding = finding.with_severity(override_sev);
}
corpus
.write_finding(&finding)
.with_context(|| format!("failed to persist finding for `{tool_name}`"))?;
reporter.on_finding(&finding);
report.findings_count += 1;
// Phase R — input that triggered a finding is
// the highest-value corpus entry. Save it
// before the reconnect (the reconnect is best-
// effort).
if let Some(corpus_ref) = self.fuzz_corpus.as_ref() {
let _ = corpus_ref.save(&FuzzCorpusEntry {
tool: tool_name.clone(),
input: payload_value.clone(),
trigger: CorpusTrigger::Finding {
kind: finding.kind.keyword().to_string(),
},
fingerprint: fingerprint.clone(),
timestamp: chrono::Utc::now(),
});
}
client.reconnect().await.with_context(|| {
format!("failed to reconnect after fault on `{tool_name}`")
})?;
reporter.on_iteration_end(&tool_name, iteration);
break;
}
// Phase R — non-finding outcome. Save the input
// when the response fingerprint is novel (helps
// the next run explore further from this point).
if let Some(corpus_ref) = self.fuzz_corpus.as_ref() {
if seen_fingerprints.insert(fingerprint.clone()) {
let _ = corpus_ref.save(&FuzzCorpusEntry {
tool: tool_name.clone(),
input: payload_value,
trigger: CorpusTrigger::NewFingerprint,
fingerprint,
timestamp: chrono::Utc::now(),
});
}
}
reporter.on_iteration_end(&tool_name, iteration);
}
}
reporter.on_run_end();
Ok(report)
}
async fn select_tools<C: McpExec + ?Sized>(
&self,
client: &C,
) -> Result<(Vec<rmcp::model::Tool>, Vec<String>)> {
let all_tools = client
.list_tools()
.await
.context("failed to list tools from MCP server")?;
let mut blocked = Vec::new();
let mut tools: Vec<rmcp::model::Tool> = all_tools
.into_iter()
.filter(|tool| glob::matches_filters(tool.name.as_ref(), &self.include, &self.exclude))
.filter(|tool| {
let classification = self.detector.classify(tool);
if classification.is_runnable() {
true
} else {
blocked.push(tool.name.to_string());
false
}
})
.collect();
if let Some(max_tools) = self.max_tools {
tools.truncate(max_tools);
}
Ok((tools, blocked))
}
}
#[cfg(test)]
#[allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
mod tests {
use super::*;
use crate::run::exec::MockClient;
use crate::run::reporter::NoopReporter;
use crate::target::{AllowDestructiveConfig, DestructiveConfig};
use rmcp::model::Tool;
use serde_json::json;
use std::sync::Arc;
fn make_tool(name: &str, schema: Value) -> Tool {
let map = schema.as_object().cloned().unwrap_or_default();
Tool::new(name.to_string(), "test tool".to_string(), Arc::new(map))
}
fn detector() -> DestructiveDetector {
DestructiveDetector::from_config(
&DestructiveConfig::default(),
&AllowDestructiveConfig::default(),
)
.unwrap()
}
fn plan(detector: DestructiveDetector) -> FuzzPlan {
FuzzPlan {
iterations: 4,
mode: GenMode::Conform,
master_seed: 42,
include: Vec::new(),
exclude: Vec::new(),
max_tools: None,
timeout: Duration::from_secs(1),
transport_name: "mock".to_string(),
detector,
severity: SeverityConfig::default(),
fuzz_corpus: None,
mutate_ratio: 0.0,
}
}
#[tokio::test]
async fn fuzz_records_protocol_error_finding_and_reconnects() {
let tool = make_tool(
"echo",
json!({"type": "object", "properties": {"msg": {"type": "string"}}}),
);
let mut client = MockClient::new().register(tool, |_args| {
CallOutcome::ProtocolError("synthetic failure".to_string())
});
let tmp = tempfile::tempdir().unwrap();
let corpus = Corpus::new(tmp.path().join("corpus"));
let mut reporter = NoopReporter;
let report = plan(detector())
.execute(&mut client, &corpus, &mut reporter)
.await
.unwrap();
assert_eq!(report.findings_count, 1);
assert_eq!(client.reconnect_count(), 1);
assert!(report.skipped.is_empty());
}
#[tokio::test]
async fn fuzz_skips_tools_with_unresolvable_refs() {
let tool = make_tool(
"broken",
json!({"$ref": "https://external.example/schema.json"}),
);
let mut client = MockClient::new().register(tool, |_args| {
CallOutcome::Ok(rmcp::model::CallToolResult::success(vec![]))
});
let tmp = tempfile::tempdir().unwrap();
let corpus = Corpus::new(tmp.path().join("corpus"));
let mut reporter = NoopReporter;
let report = plan(detector())
.execute(&mut client, &corpus, &mut reporter)
.await
.unwrap();
assert_eq!(report.findings_count, 0);
assert_eq!(report.skipped.len(), 1);
assert!(report.skipped[0].reason.contains("external"));
}
#[tokio::test]
async fn fuzz_blocks_destructive_tools_unless_allowlisted() {
let destructive_tool = make_tool(
"delete_user",
json!({"type": "object", "properties": {"id": {"type": "string"}}}),
);
let safe_tool = make_tool(
"read_user",
json!({"type": "object", "properties": {"id": {"type": "string"}}}),
);
let mut client = MockClient::new()
.register(destructive_tool, |_| {
CallOutcome::Ok(rmcp::model::CallToolResult::success(vec![]))
})
.register(safe_tool, |_| {
CallOutcome::Ok(rmcp::model::CallToolResult::success(vec![]))
});
let tmp = tempfile::tempdir().unwrap();
let corpus = Corpus::new(tmp.path().join("corpus"));
let mut reporter = NoopReporter;
let report = plan(detector())
.execute(&mut client, &corpus, &mut reporter)
.await
.unwrap();
assert_eq!(report.blocked, vec!["delete_user".to_string()]);
}
}