Files
pruefwerk/compliance-mcp/src/server.rs
T
sharang 1923a6c433
CI / Check (push) Skipped
CI / Detect Changes (push) Successful in 4s
CI / Deploy Docs (push) Successful in 1m13s
CI / Deploy MCP (push) Successful in 2m21s
CI / Deploy Dashboard (push) Successful in 5m9s
CI / Deploy Agent (push) Successful in 6m54s
feat(sbom): CycloneDX 1.6 export with BSI TR-03183-2 properties (#508, part A) (#509)
2026-10-06 14:23:40 +00:00

251 lines
9.5 KiB
Rust

use rmcp::{
handler::server::wrapper::Parameters, model::*, tool, tool_handler, tool_router, ServerHandler,
};
use crate::database::{Database, DatabasePool};
use crate::tools::{dast, findings, oscal, pentest, sbom, targets};
pub struct ComplianceMcpServer {
pool: DatabasePool,
/// Tenant this session serves. Bound once at session creation (the HTTP
/// factory reads the bearer-set tenant while still in the request scope;
/// stdio passes a synthetic id) — NOT a per-request `task_local`, which is
/// lost across the `tokio::spawn` that runs the Streamable-HTTP session.
tenant_id: String,
#[allow(dead_code)]
tool_router: rmcp::handler::server::router::tool::ToolRouter<Self>,
}
impl ComplianceMcpServer {
/// The per-tenant `Database` for this session.
fn tenant_db(&self) -> Result<Database, rmcp::ErrorData> {
Ok(self.pool.for_tenant_id(&self.tenant_id))
}
}
#[tool_router]
impl ComplianceMcpServer {
pub fn new(pool: DatabasePool, tenant_id: String) -> Self {
Self {
pool,
tenant_id,
tool_router: Self::tool_router(),
}
}
// ── Findings ──────────────────────────────────────────
#[tool(
description = "List security findings with optional filters for repo, severity, status, and scan type"
)]
async fn list_findings(
&self,
Parameters(params): Parameters<findings::ListFindingsParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
findings::list_findings(&db, params).await
}
#[tool(description = "Get a single finding by its ID")]
async fn get_finding(
&self,
Parameters(params): Parameters<findings::GetFindingParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
findings::get_finding(&db, params).await
}
#[tool(description = "Get a summary of findings counts grouped by severity and status")]
async fn findings_summary(
&self,
Parameters(params): Parameters<findings::FindingsSummaryParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
findings::findings_summary(&db, params).await
}
#[tool(
description = "Emit an OSCAL 1.1 assessment-results document for a repo's findings (mapped findings target their compliance controls; unmapped findings are reported as-is)"
)]
async fn oscal_assessment(
&self,
Parameters(params): Parameters<oscal::OscalAssessmentParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
oscal::oscal_assessment(&db, params).await
}
#[tool(
description = "CRA Annex I control mapping of a target as an OSCAL 1.1 assessment-results document (same as oscal_assessment; repo_id is the target id)"
)]
async fn get_control_mapping(
&self,
Parameters(params): Parameters<oscal::OscalAssessmentParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
oscal::oscal_assessment(&db, params).await
}
// ── Targets and scans ─────────────────────────────────
#[tool(
description = "List the tenant's targets (products under test) with their id, name, type and artifact kinds; the id is the repo_id/target_id the other tools take"
)]
async fn list_targets(
&self,
Parameters(params): Parameters<targets::ListTargetsParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
targets::list_targets(&db, params).await
}
#[tool(
description = "Get a target's scan runs, newest first: status, start and finish, new findings, error, and the stages that were not run with their reasons"
)]
async fn get_scan_results(
&self,
Parameters(params): Parameters<targets::GetScanResultsParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
targets::get_scan_results(&db, params).await
}
#[tool(
description = "List the CVE alerts of a target's dependencies and firmware libraries, highest CVSS first"
)]
async fn list_cves(
&self,
Parameters(params): Parameters<targets::ListCvesParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
targets::list_cves(&db, params).await
}
// ── SBOM ──────────────────────────────────────────────
#[tool(
description = "List SBOM packages with optional filters for repo, vulnerabilities, package manager, and license"
)]
async fn list_sbom_packages(
&self,
Parameters(params): Parameters<sbom::ListSbomPackagesParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
sbom::list_sbom_packages(&db, params).await
}
#[tool(
description = "Get a target's whole SBOM as one CycloneDX 1.6 (default) or SPDX 2.3 document"
)]
async fn get_sbom(
&self,
Parameters(params): Parameters<sbom::GetSbomParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
sbom::get_sbom(&db, params).await
}
#[tool(
description = "Generate a vulnerability report for a repository showing all packages with known CVEs"
)]
async fn sbom_vuln_report(
&self,
Parameters(params): Parameters<sbom::SbomVulnReportParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
sbom::sbom_vuln_report(&db, params).await
}
// ── DAST ──────────────────────────────────────────────
#[tool(
description = "List DAST findings with optional filters for target, scan run, severity, exploitability, and vulnerability type"
)]
async fn list_dast_findings(
&self,
Parameters(params): Parameters<dast::ListDastFindingsParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
dast::list_dast_findings(&db, params).await
}
#[tool(description = "Get a summary of recent DAST scan runs and finding counts")]
async fn dast_scan_summary(
&self,
Parameters(params): Parameters<dast::DastScanSummaryParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
dast::dast_scan_summary(&db, params).await
}
// ── Pentest ─────────────────────────────────────────────
#[tool(
description = "List AI pentest sessions with optional filters for target, status, and strategy"
)]
async fn list_pentest_sessions(
&self,
Parameters(params): Parameters<pentest::ListPentestSessionsParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
pentest::list_pentest_sessions(&db, params).await
}
#[tool(description = "Get a single AI pentest session by its ID")]
async fn get_pentest_session(
&self,
Parameters(params): Parameters<pentest::GetPentestSessionParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
pentest::get_pentest_session(&db, params).await
}
#[tool(
description = "Get the attack chain DAG for a pentest session showing each tool invocation, its reasoning, and results"
)]
async fn get_attack_chain(
&self,
Parameters(params): Parameters<pentest::GetAttackChainParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
pentest::get_attack_chain(&db, params).await
}
#[tool(description = "Get chat messages from a pentest session")]
async fn get_pentest_messages(
&self,
Parameters(params): Parameters<pentest::GetPentestMessagesParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
pentest::get_pentest_messages(&db, params).await
}
#[tool(
description = "Get aggregated pentest statistics including running sessions, vulnerability counts, and severity distribution"
)]
async fn pentest_stats(
&self,
Parameters(params): Parameters<pentest::PentestStatsParams>,
) -> Result<CallToolResult, rmcp::ErrorData> {
let db = self.tenant_db()?;
pentest::pentest_stats(&db, params).await
}
}
#[tool_handler]
impl ServerHandler for ComplianceMcpServer {
fn get_info(&self) -> ServerInfo {
ServerInfo {
protocol_version: ProtocolVersion::V_2024_11_05,
capabilities: ServerCapabilities::builder()
.enable_tools()
.build(),
server_info: Implementation::from_build_env(),
instructions: Some(
"Prüfwerk MCP server. Start with list_targets; each target's id is the repo_id/target_id the other tools take. Query findings, the CRA control mapping (OSCAL), SBOMs (CycloneDX, SPDX), CVE alerts, scan runs, DAST results and pentest sessions of your tenant."
.to_string(),
),
}
}
}