Skip to main content

ironcontext_core/
lib.rs

1//! IronContext — security & optimization engine for the Model Context Protocol.
2//!
3//! `ironcontext-core` is the pure-CPU, offline-by-default library that powers the
4//! `sentinel` CLI, the Python wrapper and the GitHub Action.  It exposes four
5//! cohesive subsystems:
6//!
7//! * [`manifest`] — strict deserializer for MCP `initialize` / `tools/list` payloads.
8//! * [`rules`]    — May 2026 CVE pattern detectors (SEN-001 … SEN-010).
9//! * [`ris`]      — the Reasoning-Impact Score (0..100 hallucination grade).
10//! * [`optimizer`] — heuristic description pruner + pluggable LLM trait.
11//!
12//! Everything is glued together by [`report::Report`] / [`scan`].
13
14pub mod manifest;
15pub mod optimizer;
16pub mod report;
17pub mod ris;
18pub mod rules;
19pub mod sarif;
20
21use thiserror::Error;
22
23pub use manifest::{Manifest, Tool};
24pub use optimizer::{DescriptionOptimizer, HeuristicOptimizer, OptimizationOutcome};
25pub use report::{Report, ToolReport};
26pub use ris::{RisBreakdown, RisScore};
27pub use rules::{Finding, RuleId, Severity};
28
29/// Errors surfaced by the library.
30#[derive(Debug, Error)]
31pub enum SentinelError {
32    #[error("invalid MCP manifest: {0}")]
33    InvalidManifest(String),
34    #[error("io error: {0}")]
35    Io(#[from] std::io::Error),
36    #[error("json error: {0}")]
37    Json(#[from] serde_json::Error),
38}
39
40/// Convenience: parse an MCP manifest from raw JSON bytes and produce a full report.
41pub fn scan(bytes: &[u8]) -> Result<Report, SentinelError> {
42    let manifest = manifest::Manifest::from_slice(bytes)?;
43    Ok(Report::build(&manifest))
44}
45
46/// Convenience: parse + run only the optimizer pass.
47pub fn optimize(bytes: &[u8]) -> Result<Vec<OptimizationOutcome>, SentinelError> {
48    let manifest = manifest::Manifest::from_slice(bytes)?;
49    let opt = HeuristicOptimizer::default();
50    Ok(manifest
51        .tools
52        .iter()
53        .map(|t| opt.rewrite(t))
54        .collect::<Vec<_>>())
55}