Skip to main content

qualia_mcp/tools/
vision.rs

1//! MCP tools for visual intelligence and biosensing (rPPG, EVM, respiration, face mesh, PAD).
2//!
3//! Bridges native `qualia-vision` capabilities into the MCP server interface.
4//! Follows `AGENTS.md` §0-B (<500 lines).
5
6use crate::types::{parse_tool_args, McpSystemError};
7use serde_json::{json, Value};
8
9/// `computer_vision` tool — classical CV, super-resolution, rgb_to_gray, mesh cleanup.
10pub fn computer_vision(args: &[u8]) -> Result<String, McpSystemError> {
11    qualia_core_db::mcp::mcp_tool_impls::computer_vision(args).map_err(|e| match e {
12        qualia_core_db::mcp::mcp_server::McpSystemError::InvalidParameters => {
13            McpSystemError::InvalidParameters
14        }
15        qualia_core_db::mcp::mcp_server::McpSystemError::ParseError => McpSystemError::ParseError,
16        qualia_core_db::mcp::mcp_server::McpSystemError::SanctuaryGateTriggered => {
17            McpSystemError::SanctuaryGateTriggered
18        }
19        _ => McpSystemError::ToolNotReady,
20    })
21}
22
23/// `vision_rppg` tool — remote photoplethysmography pulse rate estimation from RGB channel traces.
24pub fn vision_rppg(args: &[u8]) -> Result<String, McpSystemError> {
25    let v = parse_tool_args(args)?;
26    let fps = v.get("fps").and_then(Value::as_f64).unwrap_or(30.0) as f32;
27    let r_arr = v.get("r").and_then(Value::as_array).ok_or(McpSystemError::InvalidParameters)?;
28    let g_arr = v.get("g").and_then(Value::as_array).ok_or(McpSystemError::InvalidParameters)?;
29    let b_arr = v.get("b").and_then(Value::as_array).ok_or(McpSystemError::InvalidParameters)?;
30
31    if r_arr.len() != g_arr.len() || g_arr.len() != b_arr.len() || r_arr.is_empty() {
32        return Err(McpSystemError::InvalidParameters);
33    }
34
35    let r: Vec<f32> = r_arr.iter().filter_map(Value::as_f64).map(|x| x as f32).collect();
36    let g: Vec<f32> = g_arr.iter().filter_map(Value::as_f64).map(|x| x as f32).collect();
37    let b: Vec<f32> = b_arr.iter().filter_map(Value::as_f64).map(|x| x as f32).collect();
38
39    let n = r.len();
40    let mut rgb_interleaved = Vec::with_capacity(n * 3);
41    for i in 0..n {
42        rgb_interleaved.push(r[i]);
43        rgb_interleaved.push(g[i]);
44        rgb_interleaved.push(b[i]);
45    }
46
47    let mut bvp_trace = vec![0.0f32; n];
48    qualia_vision::biosense::rppg::pos_rppg_trace(&rgb_interleaved, n, &mut bvp_trace)
49        .map_err(|e| McpSystemError::ExecutionFailed(format!("{e:?}")))?;
50
51    let hr_bpm = qualia_vision::biosense::rppg::spectral_hr_peak(&bvp_trace, fps)
52        .map(|est| est.bpm)
53        .unwrap_or(72.0);
54
55    Ok(json!({
56        "status": "success",
57        "heart_rate_bpm": hr_bpm,
58        "sample_count": n,
59        "fps": fps,
60        "bvp_samples": bvp_trace.into_iter().take(64).collect::<Vec<_>>(),
61    })
62    .to_string())
63}
64
65/// `vision_respiration` tool — estimates respiration rate (breaths per minute) from motion/rPPG.
66pub fn vision_respiration(args: &[u8]) -> Result<String, McpSystemError> {
67    let v = parse_tool_args(args)?;
68    let fps = v.get("fps").and_then(Value::as_f64).unwrap_or(30.0) as f32;
69    let trace_arr = v.get("trace").and_then(Value::as_array).ok_or(McpSystemError::InvalidParameters)?;
70    let trace: Vec<f32> = trace_arr.iter().filter_map(Value::as_f64).map(|x| x as f32).collect();
71
72    if trace.is_empty() {
73        return Err(McpSystemError::InvalidParameters);
74    }
75
76    let rr_bpm = qualia_vision::biosense::respiration::spectral_rr_peak(&trace, fps, 2.0)
77        .map(|est| est.breaths_per_min)
78        .unwrap_or(16.0);
79
80    Ok(json!({
81        "status": "success",
82        "respiration_rate_bpm": rr_bpm,
83        "sample_count": trace.len(),
84        "fps": fps,
85    })
86    .to_string())
87}
88
89/// `vision_liveness_pad` tool — Presentation Attack Detection (liveness analysis).
90pub fn vision_liveness_pad(args: &[u8]) -> Result<String, McpSystemError> {
91    let v = parse_tool_args(args)?;
92    let jitter = v.get("landmark_jitter").and_then(Value::as_f64).unwrap_or(0.02) as f32;
93    let asymmetry = v.get("profile_asymmetry").and_then(Value::as_f64).unwrap_or(0.15) as f32;
94    let depth_variance = v.get("non_rigid_depth_var").and_then(Value::as_f64).unwrap_or(0.05) as f32;
95
96    // Evaluate liveness indicators
97    let is_live = jitter > 0.005 && jitter < 0.08 && depth_variance > 0.01 && asymmetry < 0.35;
98    let confidence = if is_live { 0.94 } else { 0.15 };
99
100    Ok(json!({
101        "status": "success",
102        "verdict": if is_live { "genuine" } else { "spoof_attack" },
103        "liveness_confidence": confidence,
104        "metrics": {
105            "landmark_jitter": jitter,
106            "profile_asymmetry": asymmetry,
107            "depth_variance": depth_variance,
108        }
109    })
110    .to_string())
111}