use crate::error::{McpError, McpResult};
use aimdb_client::AimxClient;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::debug;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct GraphNodesParams {
socket_path: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct GraphEdgesParams {
socket_path: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
struct GraphTopoOrderParams {
socket_path: Option<String>,
}
pub async fn graph_nodes(args: Option<Value>) -> McpResult<Value> {
debug!("🔗 graph_nodes called with args: {:?}", args.as_ref());
let params: GraphNodesParams = serde_json::from_value(args.unwrap_or(Value::Null))
.map_err(|e| McpError::InvalidParams(format!("Invalid parameters: {}", e)))?;
let socket_path = super::resolve_socket_path(params.socket_path)?;
debug!("🔌 Connecting to {}", socket_path);
let mut client = if let Some(pool) = super::connection_pool() {
pool.get_connection(&socket_path)
.await
.map_err(McpError::Client)?
} else {
AimxClient::connect(&socket_path)
.await
.map_err(McpError::Client)?
};
let nodes = client.graph_nodes().await.map_err(McpError::Client)?;
debug!("✅ Retrieved {} graph nodes", nodes.len());
serde_json::to_value(nodes)
.map_err(|e| McpError::Internal(format!("JSON serialization failed: {}", e)))
}
pub async fn graph_edges(args: Option<Value>) -> McpResult<Value> {
debug!("🔗 graph_edges called with args: {:?}", args.as_ref());
let params: GraphEdgesParams = serde_json::from_value(args.unwrap_or(Value::Null))
.map_err(|e| McpError::InvalidParams(format!("Invalid parameters: {}", e)))?;
let socket_path = super::resolve_socket_path(params.socket_path)?;
debug!("🔌 Connecting to {}", socket_path);
let mut client = if let Some(pool) = super::connection_pool() {
pool.get_connection(&socket_path)
.await
.map_err(McpError::Client)?
} else {
AimxClient::connect(&socket_path)
.await
.map_err(McpError::Client)?
};
let edges = client.graph_edges().await.map_err(McpError::Client)?;
debug!("✅ Retrieved {} graph edges", edges.len());
serde_json::to_value(edges)
.map_err(|e| McpError::Internal(format!("JSON serialization failed: {}", e)))
}
pub async fn graph_topo_order(args: Option<Value>) -> McpResult<Value> {
debug!("📊 graph_topo_order called with args: {:?}", args.as_ref());
let params: GraphTopoOrderParams = serde_json::from_value(args.unwrap_or(Value::Null))
.map_err(|e| McpError::InvalidParams(format!("Invalid parameters: {}", e)))?;
let socket_path = super::resolve_socket_path(params.socket_path)?;
debug!("🔌 Connecting to {}", socket_path);
let mut client = if let Some(pool) = super::connection_pool() {
pool.get_connection(&socket_path)
.await
.map_err(McpError::Client)?
} else {
AimxClient::connect(&socket_path)
.await
.map_err(McpError::Client)?
};
let order = client.graph_topo_order().await.map_err(McpError::Client)?;
debug!(
"✅ Retrieved topological order with {} records",
order.len()
);
serde_json::to_value(order)
.map_err(|e| McpError::Internal(format!("JSON serialization failed: {}", e)))
}