aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / spark-debugger / src / mcp.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | use aien_localhttp::Client; |
| 17 | use serde_json::{json, Value}; |
| 18 | use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; |
| 19 | use tracing::info; |
| 20 | |
| 21 | use crate::audit::{audit_services, audit_vault_and_unslop, run_full_audit}; |
| 22 | |
| 23 | pub async fn run_mcp_server( |
| 24 | client: Client, |
| 25 | workspace_dir: String, |
| 26 | ) -> Result<(), Box<dyn std::error::Error>> { |
| 27 | info!("[MCP] Starting spark-debugger MCP server on stdio..."); |
| 28 | |
| 29 | let stdin = tokio::io::stdin(); |
| 30 | let mut stdout = tokio::io::stdout(); |
| 31 | let mut reader = BufReader::new(stdin).lines(); |
| 32 | |
| 33 | while let Ok(Some(line)) = reader.next_line().await { |
| 34 | let trimmed = line.trim(); |
| 35 | if trimmed.is_empty() { |
| 36 | continue; |
| 37 | } |
| 38 | |
| 39 | let parsed: Value = match serde_json::from_str(trimmed) { |
| 40 | Ok(v) => v, |
| 41 | Err(_) => continue, |
| 42 | }; |
| 43 | |
| 44 | let id = parsed.get("id").cloned().unwrap_or(Value::Null); |
| 45 | let method = parsed.get("method").and_then(|m| m.as_str()).unwrap_or(""); |
| 46 | |
| 47 | match method { |
| 48 | "initialize" => { |
| 49 | let resp = json!({ |
| 50 | "jsonrpc": "2.0", |
| 51 | "id": id, |
| 52 | "result": { |
| 53 | "protocolVersion": "2024-11-05", |
| 54 | "capabilities": { |
| 55 | "tools": {} |
| 56 | }, |
| 57 | "serverInfo": { |
| 58 | "name": "spark-debugger", |
| 59 | "version": "0.1.0" |
| 60 | } |
| 61 | } |
| 62 | }); |
| 63 | let mut out = resp.to_string(); |
| 64 | out.push('\n'); |
| 65 | let _ = stdout.write_all(out.as_bytes()).await; |
| 66 | let _ = stdout.flush().await; |
| 67 | } |
| 68 | "notifications/initialized" => { |
| 69 | // Client acknowledgment, no response required |
| 70 | } |
| 71 | "tools/list" => { |
| 72 | let resp = json!({ |
| 73 | "jsonrpc": "2.0", |
| 74 | "id": id, |
| 75 | "result": { |
| 76 | "tools": [ |
| 77 | { |
| 78 | "name": "debug_scan", |
| 79 | "description": "Perform complete diagnostic scan of services, workspace compilation, and vault security invariants.", |
| 80 | "inputSchema": { |
| 81 | "type": "object", |
| 82 | "properties": {} |
| 83 | } |
| 84 | }, |
| 85 | { |
| 86 | "name": "debug_check_services", |
| 87 | "description": "Probe status and latencies of SparkOS native services (Cortex, Cockpit, Encoder, LLM Seat, Conduit).", |
| 88 | "inputSchema": { |
| 89 | "type": "object", |
| 90 | "properties": {} |
| 91 | } |
| 92 | }, |
| 93 | { |
| 94 | "name": "debug_audit_invariants", |
| 95 | "description": "Audit workspaces for plaintext .env secrets, private key leaks, and unslop voice violations.", |
| 96 | "inputSchema": { |
| 97 | "type": "object", |
| 98 | "properties": {} |
| 99 | } |
| 100 | } |
| 101 | ] |
| 102 | } |
| 103 | }); |
| 104 | let mut out = resp.to_string(); |
| 105 | out.push('\n'); |
| 106 | let _ = stdout.write_all(out.as_bytes()).await; |
| 107 | let _ = stdout.flush().await; |
| 108 | } |
| 109 | "tools/call" => { |
| 110 | let tool_name = parsed |
| 111 | .pointer("/params/name") |
| 112 | .and_then(|n| n.as_str()) |
| 113 | .unwrap_or(""); |
| 114 | let result_text = match tool_name { |
| 115 | "debug_scan" => { |
| 116 | let report = run_full_audit(&client, &workspace_dir).await; |
| 117 | serde_json::to_string_pretty(&report) |
| 118 | .unwrap_or_else(|_| "Failed to serialize report".to_string()) |
| 119 | } |
| 120 | "debug_check_services" => { |
| 121 | let services = audit_services(&client).await; |
| 122 | serde_json::to_string_pretty(&services) |
| 123 | .unwrap_or_else(|_| "Failed to serialize services".to_string()) |
| 124 | } |
| 125 | "debug_audit_invariants" => { |
| 126 | let inv = audit_vault_and_unslop(&[ |
| 127 | &workspace_dir, |
| 128 | &format!( |
| 129 | "{}/spark-cockpit-rs", |
| 130 | std::env::var("HOME").unwrap_or_default() |
| 131 | ), |
| 132 | ]); |
| 133 | serde_json::to_string_pretty(&inv) |
| 134 | .unwrap_or_else(|_| "Failed to serialize invariants".to_string()) |
| 135 | } |
| 136 | _ => format!("Unknown tool: {}", tool_name), |
| 137 | }; |
| 138 | |
| 139 | let resp = json!({ |
| 140 | "jsonrpc": "2.0", |
| 141 | "id": id, |
| 142 | "result": { |
| 143 | "content": [ |
| 144 | { |
| 145 | "type": "text", |
| 146 | "text": result_text |
| 147 | } |
| 148 | ] |
| 149 | } |
| 150 | }); |
| 151 | let mut out = resp.to_string(); |
| 152 | out.push('\n'); |
| 153 | let _ = stdout.write_all(out.as_bytes()).await; |
| 154 | let _ = stdout.flush().await; |
| 155 | } |
| 156 | _ => { |
| 157 | let resp = json!({ |
| 158 | "jsonrpc": "2.0", |
| 159 | "id": id, |
| 160 | "error": { |
| 161 | "code": -32601, |
| 162 | "message": "Method not found" |
| 163 | } |
| 164 | }); |
| 165 | let mut out = resp.to_string(); |
| 166 | out.push('\n'); |
| 167 | let _ = stdout.write_all(out.as_bytes()).await; |
| 168 | let _ = stdout.flush().await; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | Ok(()) |
| 174 | } |