aien-sovereign-core
Autonomous sovereign intelligence engine and runtime core for NVIDIA DGX Spark
Brought by drakestapleton
aien-sovereign-core / crates / spark-debugger / src / mcp.rs
| 1 | use reqwest::Client; |
| 2 | use serde_json::{json, Value}; |
| 3 | use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; |
| 4 | use tracing::info; |
| 5 | |
| 6 | use crate::audit::{audit_services, audit_vault_and_unslop, run_full_audit}; |
| 7 | |
| 8 | pub async fn run_mcp_server(client: Client, workspace_dir: String) -> Result<(), Box<dyn std::error::Error>> { |
| 9 | info!("[MCP] Starting spark-debugger MCP server on stdio..."); |
| 10 | |
| 11 | let stdin = tokio::io::stdin(); |
| 12 | let mut stdout = tokio::io::stdout(); |
| 13 | let mut reader = BufReader::new(stdin).lines(); |
| 14 | |
| 15 | while let Ok(Some(line)) = reader.next_line().await { |
| 16 | let trimmed = line.trim(); |
| 17 | if trimmed.is_empty() { |
| 18 | continue; |
| 19 | } |
| 20 | |
| 21 | let parsed: Value = match serde_json::from_str(trimmed) { |
| 22 | Ok(v) => v, |
| 23 | Err(_) => continue, |
| 24 | }; |
| 25 | |
| 26 | let id = parsed.get("id").cloned().unwrap_or(Value::Null); |
| 27 | let method = parsed.get("method").and_then(|m| m.as_str()).unwrap_or(""); |
| 28 | |
| 29 | match method { |
| 30 | "initialize" => { |
| 31 | let resp = json!({ |
| 32 | "jsonrpc": "2.0", |
| 33 | "id": id, |
| 34 | "result": { |
| 35 | "protocolVersion": "2024-11-05", |
| 36 | "capabilities": { |
| 37 | "tools": {} |
| 38 | }, |
| 39 | "serverInfo": { |
| 40 | "name": "spark-debugger", |
| 41 | "version": "0.1.0" |
| 42 | } |
| 43 | } |
| 44 | }); |
| 45 | let mut out = resp.to_string(); |
| 46 | out.push_str("\n"); |
| 47 | let _ = stdout.write_all(out.as_bytes()).await; |
| 48 | let _ = stdout.flush().await; |
| 49 | } |
| 50 | "notifications/initialized" => { |
| 51 | // Client acknowledgment, no response required |
| 52 | } |
| 53 | "tools/list" => { |
| 54 | let resp = json!({ |
| 55 | "jsonrpc": "2.0", |
| 56 | "id": id, |
| 57 | "result": { |
| 58 | "tools": [ |
| 59 | { |
| 60 | "name": "debug_scan", |
| 61 | "description": "Perform complete diagnostic scan of services, workspace compilation, and vault security invariants.", |
| 62 | "inputSchema": { |
| 63 | "type": "object", |
| 64 | "properties": {} |
| 65 | } |
| 66 | }, |
| 67 | { |
| 68 | "name": "debug_check_services", |
| 69 | "description": "Probe status and latencies of SparkOS native services (Cortex, Cockpit, Encoder, LLM Seat, Conduit).", |
| 70 | "inputSchema": { |
| 71 | "type": "object", |
| 72 | "properties": {} |
| 73 | } |
| 74 | }, |
| 75 | { |
| 76 | "name": "debug_audit_invariants", |
| 77 | "description": "Audit workspaces for plaintext .env secrets, private key leaks, and unslop voice violations.", |
| 78 | "inputSchema": { |
| 79 | "type": "object", |
| 80 | "properties": {} |
| 81 | } |
| 82 | } |
| 83 | ] |
| 84 | } |
| 85 | }); |
| 86 | let mut out = resp.to_string(); |
| 87 | out.push_str("\n"); |
| 88 | let _ = stdout.write_all(out.as_bytes()).await; |
| 89 | let _ = stdout.flush().await; |
| 90 | } |
| 91 | "tools/call" => { |
| 92 | let tool_name = parsed.pointer("/params/name").and_then(|n| n.as_str()).unwrap_or(""); |
| 93 | let result_text = match tool_name { |
| 94 | "debug_scan" => { |
| 95 | let report = run_full_audit(&client, &workspace_dir).await; |
| 96 | serde_json::to_string_pretty(&report).unwrap_or_else(|_| "Failed to serialize report".to_string()) |
| 97 | } |
| 98 | "debug_check_services" => { |
| 99 | let services = audit_services(&client).await; |
| 100 | serde_json::to_string_pretty(&services).unwrap_or_else(|_| "Failed to serialize services".to_string()) |
| 101 | } |
| 102 | "debug_audit_invariants" => { |
| 103 | let inv = audit_vault_and_unslop(&[&workspace_dir, "/home/drakestapleton/spark-cockpit-rs"]); |
| 104 | serde_json::to_string_pretty(&inv).unwrap_or_else(|_| "Failed to serialize invariants".to_string()) |
| 105 | } |
| 106 | _ => format!("Unknown tool: {}", tool_name), |
| 107 | }; |
| 108 | |
| 109 | let resp = json!({ |
| 110 | "jsonrpc": "2.0", |
| 111 | "id": id, |
| 112 | "result": { |
| 113 | "content": [ |
| 114 | { |
| 115 | "type": "text", |
| 116 | "text": result_text |
| 117 | } |
| 118 | ] |
| 119 | } |
| 120 | }); |
| 121 | let mut out = resp.to_string(); |
| 122 | out.push_str("\n"); |
| 123 | let _ = stdout.write_all(out.as_bytes()).await; |
| 124 | let _ = stdout.flush().await; |
| 125 | } |
| 126 | _ => { |
| 127 | let resp = json!({ |
| 128 | "jsonrpc": "2.0", |
| 129 | "id": id, |
| 130 | "error": { |
| 131 | "code": -32601, |
| 132 | "message": "Method not found" |
| 133 | } |
| 134 | }); |
| 135 | let mut out = resp.to_string(); |
| 136 | out.push_str("\n"); |
| 137 | let _ = stdout.write_all(out.as_bytes()).await; |
| 138 | let _ = stdout.flush().await; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | Ok(()) |
| 144 | } |