Skip to main content

synapse_core/
mcp.rs

1use crate::server::MySemanticEngine;
2use serde::Deserialize;
3use std::sync::Arc;
4
5/// Request payload for the `query_knowledge_graph` tool.
6#[derive(Debug, Deserialize)]
7pub struct QueryGraphParams {
8    pub query: String,
9    pub limit: Option<u32>,
10}
11
12/// Request payload for the `add_observation` tool.
13#[derive(Debug, Deserialize)]
14pub struct AddObservationParams {
15    pub text: String,
16    pub source: Option<String>,
17}
18
19/// The MCP Server adapter.
20/// Wraps the Semantic Engine and exposes it via standard MCP tool interfaces.
21pub struct McpServer {
22    _engine: Arc<MySemanticEngine>,
23}
24
25impl McpServer {
26    pub fn new(engine: Arc<MySemanticEngine>) -> Self {
27        Self { _engine: engine }
28    }
29
30    /// Lists the tools available in this MCP server.
31    pub fn list_tools(&self) -> Vec<String> {
32        vec![
33            "query_knowledge_graph".to_string(),
34            "add_observation".to_string(),
35            "validate_hypothesis".to_string(),
36        ]
37    }
38
39    /// Handles a tool call.
40    /// In a real implementation, this would parse JSON-RPC requests.
41    pub async fn call_tool(
42        &self,
43        tool_name: &str,
44        arguments: serde_json::Value,
45    ) -> Result<serde_json::Value, String> {
46        match tool_name {
47            "query_knowledge_graph" => {
48                let params: QueryGraphParams = serde_json::from_value(arguments)
49                    .map_err(|e| format!("Invalid arguments: {}", e))?;
50                self.query_knowledge_graph(params).await
51            }
52            "add_observation" => {
53                let params: AddObservationParams = serde_json::from_value(arguments)
54                    .map_err(|e| format!("Invalid arguments: {}", e))?;
55                self.add_observation(params).await
56            }
57            _ => Err(format!("Tool not found: {}", tool_name)),
58        }
59    }
60
61    async fn query_knowledge_graph(
62        &self,
63        _params: QueryGraphParams,
64    ) -> Result<serde_json::Value, String> {
65        // Bridge to the internal engine
66        // For now, just returning a mock response
67        Ok(serde_json::json!({
68            "results": [
69                { "node_id": 1, "content": "Mock result for query", "score": 0.9 }
70            ]
71        }))
72    }
73
74    async fn add_observation(
75        &self,
76        _params: AddObservationParams,
77    ) -> Result<serde_json::Value, String> {
78        // Bridge to internal engine ingestion
79        Ok(serde_json::json!({
80            "status": "success",
81            "nodes_added": 1 // Placeholder
82        }))
83    }
84}