spark-debugger
Native runtime state inspector and execution debugger
Brought by drakestapleton
spark-debugger / src / mcp.rs
| 1 | |
| 2 | pub fn get_tools_definition() -> Value { |
| 3 | json!([ |
| 4 | { |
| 5 | "name": "debug_scan", |
| 6 | "description": "Perform complete diagnostic scan of services, workspace compilation, and vault security invariants.", |
| 7 | "inputSchema": { |
| 8 | "type": "object", |
| 9 | "properties": {} |
| 10 | } |
| 11 | }, |
| 12 | { |
| 13 | "name": "debug_check_services", |
| 14 | "description": "Probe status and latencies of SparkOS native services (Cortex, Cockpit, Encoder, LLM Seat, Conduit).", |
| 15 | "inputSchema": { |
| 16 | "type": "object", |
| 17 | "properties": {} |
| 18 | } |
| 19 | }, |
| 20 | { |
| 21 | "name": "debug_audit_security", |
| 22 | "description": "Audit zero-disk-secrets invariant and scan for unmasked API key signatures.", |
| 23 | "inputSchema": { |
| 24 | "type": "object", |
| 25 | "properties": {} |
| 26 | } |
| 27 | } |
| 28 | ]) |
| 29 | } |
| 30 | use reqwest::Client; |
| 31 | use serde_json::{json, Value}; |
| 32 | use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader}; |
| 33 | use tracing::info; |
| 34 | |
| 35 | use crate::audit::{audit_services, audit_vault_and_unslop, run_full_audit}; |
| 36 | |
| 37 | pub async fn run_mcp_server(client: Client, workspace_dir: String) -> Result<(), Box<dyn std::error::Error>> { |
| 38 | info!("[MCP] Starting spark-debugger MCP server on stdio..."); |
| 39 | |
| 40 | let stdin = tokio::io::stdin(); |
| 41 | let mut stdout = tokio::io::stdout(); |
| 42 | let mut reader = BufReader::new(stdin).lines(); |
| 43 | |
| 44 | while let Ok(Some(line)) = reader.next_line().await { |
| 45 | let trimmed = line.trim(); |
| 46 | if trimmed.is_empty() { |
| 47 | continue; |
| 48 | } |
| 49 | |
| 50 | let parsed: Value = match serde_json::from_str(trimmed) { |
| 51 | Ok(v) => v, |
| 52 | Err(_) => continue, |
| 53 | }; |
| 54 | |
| 55 | let id = parsed.get("id").cloned().unwrap_or(Value::Null); |
| 56 | let method = parsed.get("method").and_then(|m| m.as_str()).unwrap_or(""); |
| 57 | |
| 58 | match method { |
| 59 | "initialize" => { |
| 60 | let resp = json!({ |
| 61 | "jsonrpc": "2.0", |
| 62 | "id": id, |
| 63 | "result": { |
| 64 | "protocolVersion": "2024-11-05", |
| 65 | "capabilities": { |
| 66 | "tools": {} |
| 67 | }, |
| 68 | "serverInfo": { |
| 69 | "name": "spark-debugger", |
| 70 | "version": "0.1.0" |
| 71 | } |
| 72 | } |
| 73 | }); |
| 74 | let mut out = resp.to_string(); |
| 75 | out.push_str("\n"); |
| 76 | let _ = stdout.write_all(out.as_bytes()).await; |
| 77 | let _ = stdout.flush().await; |
| 78 | } |
| 79 | "notifications/initialized" => { |
| 80 | // Client acknowledgment, no response required |
| 81 | } |
| 82 | "tools/list" => { |
| 83 | let resp = json!({ |
| 84 | "jsonrpc": "2.0", |
| 85 | "id": id, |
| 86 | "result": { |
| 87 | "tools": [ |
| 88 | { |
| 89 | "name": "debug_scan", |
| 90 | "description": "Perform complete diagnostic scan of services, workspace compilation, and vault security invariants.", |
| 91 | "inputSchema": { |
| 92 | "type": "object", |
| 93 | "properties": {} |
| 94 | } |
| 95 | }, |
| 96 | { |
| 97 | "name": "debug_check_services", |
| 98 | "description": "Probe status and latencies of SparkOS native services (Cortex, Cockpit, Encoder, LLM Seat, Conduit).", |
| 99 | "inputSchema": { |
| 100 | "type": "object", |
| 101 | "properties": {} |
| 102 | } |
| 103 | }, |
| 104 | { |
| 105 | "name": "debug_audit_invariants", |
| 106 | "description": "Audit workspaces for plaintext .env secrets, private key leaks, and unslop voice violations.", |
| 107 | "inputSchema": { |
| 108 | "type": "object", |
| 109 | "properties": {} |
| 110 | } |
| 111 | } |
| 112 | ] |
| 113 | } |
| 114 | }); |
| 115 | let mut out = resp.to_string(); |
| 116 | out.push_str("\n"); |
| 117 | let _ = stdout.write_all(out.as_bytes()).await; |
| 118 | let _ = stdout.flush().await; |
| 119 | } |
| 120 | "tools/call" => { |
| 121 | let tool_name = parsed.pointer("/params/name").and_then(|n| n.as_str()).unwrap_or(""); |
| 122 | let result_text = match tool_name { |
| 123 | "debug_scan" => { |
| 124 | let report = run_full_audit(&client, &workspace_dir).await; |
| 125 | serde_json::to_string_pretty(&report).unwrap_or_else(|_| "Failed to serialize report".to_string()) |
| 126 | } |
| 127 | "debug_check_services" => { |
| 128 | let services = audit_services(&client).await; |
| 129 | serde_json::to_string_pretty(&services).unwrap_or_else(|_| "Failed to serialize services".to_string()) |
| 130 | } |
| 131 | "debug_audit_invariants" => { |
| 132 | let inv = audit_vault_and_unslop(&[&workspace_dir, "/home/drakestapleton/spark-cockpit-rs"]); |
| 133 | serde_json::to_string_pretty(&inv).unwrap_or_else(|_| "Failed to serialize invariants".to_string()) |
| 134 | } |
| 135 | _ => format!("Unknown tool: {}", tool_name), |
| 136 | }; |
| 137 | |
| 138 | let resp = json!({ |
| 139 | "jsonrpc": "2.0", |
| 140 | "id": id, |
| 141 | "result": { |
| 142 | "content": [ |
| 143 | { |
| 144 | "type": "text", |
| 145 | "text": result_text |
| 146 | } |
| 147 | ] |
| 148 | } |
| 149 | }); |
| 150 | let mut out = resp.to_string(); |
| 151 | out.push_str("\n"); |
| 152 | let _ = stdout.write_all(out.as_bytes()).await; |
| 153 | let _ = stdout.flush().await; |
| 154 | } |
| 155 | _ => { |
| 156 | let resp = json!({ |
| 157 | "jsonrpc": "2.0", |
| 158 | "id": id, |
| 159 | "error": { |
| 160 | "code": -32601, |
| 161 | "message": "Method not found" |
| 162 | } |
| 163 | }); |
| 164 | let mut out = resp.to_string(); |
| 165 | out.push_str("\n"); |
| 166 | let _ = stdout.write_all(out.as_bytes()).await; |
| 167 | let _ = stdout.flush().await; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | Ok(()) |
| 173 | } |
| 174 | |
| 175 | |
| 176 | #[cfg(test)] |
| 177 | mod tests { |
| 178 | use super::*; |
| 179 | |
| 180 | #[test] |
| 181 | fn test_mcp_tools_definition() { |
| 182 | let tools = get_tools_definition(); |
| 183 | let list = tools.as_array().expect("array of tools"); |
| 184 | assert_eq!(list.len(), 3); |
| 185 | let names: Vec<&str> = list.iter().filter_map(|t| t.get("name").and_then(Value::as_str)).collect(); |
| 186 | assert!(names.contains(&"debug_scan")); |
| 187 | assert!(names.contains(&"debug_check_services")); |
| 188 | assert!(names.contains(&"debug_audit_security")); |
| 189 | } |
| 190 | } |