use serde_json::{json, Value};
use crate::error::Result;
use crate::model::{AuditReport, Severity};
use crate::rules::all_rules;
impl AuditReport {
pub fn to_sarif_json(&self) -> Result<String> {
let rules: Vec<Value> = all_rules()
.into_iter()
.map(|r| {
json!({
"id": r.code,
"name": r.slug,
"shortDescription": { "text": r.summary },
"help": { "text": r.remediation },
"defaultConfiguration": {
"level": sarif_level(r.default_severity)
},
"properties": {
"dimension": r.dimension.as_str(),
"tags": ["mcp", "ai-agent", r.dimension.as_str()]
}
})
})
.collect();
let results: Vec<Value> = self
.findings
.iter()
.map(|f| {
let mut location = json!({
"logicalLocations": [{
"name": f.tool,
"fullyQualifiedName": f.json_pointer,
"kind": "mcp-tool"
}]
});
if let Some(loc) = &f.source_location {
location["physicalLocation"] = json!({
"artifactLocation": { "uri": loc.file },
"region": {
"startLine": loc.line.max(1),
"startColumn": loc.column.max(1)
}
});
}
json!({
"ruleId": f.code,
"level": sarif_level(f.severity),
"message": {
"text": format!("{} Fix hint: {}", f.message, f.fix_hint)
},
"locations": [location],
"properties": {
"slug": f.slug,
"dimension": f.dimension.as_str(),
"related_tool": f.related_tool,
"source_server": f.source_server,
"fix_hint": f.fix_hint,
"evidence": f.evidence
}
})
})
.collect();
let sarif = json!({
"$schema": "https://json.schemastore.org/sarif-2.1.0.json",
"version": "2.1.0",
"runs": [{
"tool": {
"driver": {
"name": "mcp-vol",
"version": env!("CARGO_PKG_VERSION"),
"informationUri": "https://github.com/utkuvolkanuzunhan/MCP-vol",
"rules": rules
}
},
"results": results
}]
});
Ok(serde_json::to_string_pretty(&sarif)?)
}
}
fn sarif_level(severity: Severity) -> &'static str {
match severity {
Severity::Error => "error",
Severity::Warn => "warning",
Severity::Info => "note",
}
}