Skip to main content

do_memory_mcp/server/tools/embeddings/
query.rs

1//! Query semantic memory tool handler for MCP server
2//!
3//! This module provides the tool for searching episodic memory
4//! using semantic similarity with embeddings.
5
6use crate::server::MemoryMCPServer;
7use anyhow::Result;
8use serde_json::json;
9use std::sync::Arc;
10use tracing::debug;
11
12impl MemoryMCPServer {
13    /// Execute the query_semantic_memory tool
14    ///
15    /// # Arguments
16    ///
17    /// * `input` - Semantic query parameters
18    ///
19    /// # Returns
20    ///
21    /// Returns semantic search results with similarity scores
22    pub async fn execute_query_semantic_memory(
23        &self,
24        input: crate::mcp::tools::embeddings::QuerySemanticMemoryInput,
25    ) -> Result<serde_json::Value> {
26        self.track_tool_usage("query_semantic_memory").await;
27
28        // Start monitoring request
29        let request_id = format!(
30            "query_semantic_memory_{}",
31            std::time::SystemTime::now()
32                .duration_since(std::time::UNIX_EPOCH)
33                .unwrap_or_default()
34                .as_nanos()
35        );
36        self.monitoring
37            .start_request(request_id.clone(), "query_semantic_memory".to_string())
38            .await;
39
40        debug!(
41            "Semantic memory query: query='{}', limit={:?}",
42            input.query, input.limit
43        );
44
45        let tool = crate::mcp::tools::embeddings::EmbeddingTools::new(Arc::clone(&self.memory));
46
47        let result = tool.execute_query_semantic_memory(input).await;
48
49        // End monitoring request
50        self.monitoring
51            .end_request(&request_id, result.is_ok(), None)
52            .await;
53
54        let output = result?;
55
56        // Convert result to JSON
57        Ok(json!(output))
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    #[allow(clippy::manual_async_fn)]
67    fn test_query_semantic_memory_signature_compile() {
68        // This test ensures the method signature compiles correctly
69        use crate::mcp::tools::embeddings::QuerySemanticMemoryInput;
70        fn method_signature(
71            _server: &MemoryMCPServer,
72            _input: QuerySemanticMemoryInput,
73        ) -> impl std::future::Future<Output = Result<serde_json::Value>> {
74            async { Ok(json!({})) }
75        }
76        let _ = method_signature; // Use the function to avoid unused warnings
77    }
78}