sqlserver_mcp_catalog/tools/
search_tool.rs1use rusqlite::Connection;
4
5use crate::data::store::search_endpoints;
6use crate::services::embedding_service::embed;
7
8fn warn_if_embeddings_incomplete(conn: &Connection) {
15 let counts: rusqlite::Result<(i64, i64)> = conn.query_row(
16 "SELECT (SELECT COUNT(*) FROM endpoints), (SELECT COUNT(*) FROM semantic_endpoints)",
17 [],
18 |row| Ok((row.get(0)?, row.get(1)?)),
19 );
20 if let Ok((endpoints_count, semantic_count)) = counts
21 && semantic_count < endpoints_count
22 {
23 tracing::warn!(
24 endpoints_count,
25 semantic_count,
26 "semantic_endpoints is missing {} row(s) compared to endpoints — search results \
27 may be incomplete; re-run populate-embeddings for this store",
28 endpoints_count - semantic_count
29 );
30 }
31}
32
33pub fn search_operations(
37 conn: &Connection,
38 query: &str,
39 limit: usize,
40) -> anyhow::Result<serde_json::Value> {
41 warn_if_embeddings_incomplete(conn);
42 let query_embedding = embed(query)?;
43 let results = search_endpoints(conn, &query_embedding, limit)?;
44 Ok(serde_json::to_value(results)?)
45}
46
47#[cfg(test)]
48mod tests {
49 use super::*;
50
51 fn seeded_store() -> Connection {
60 unsafe {
61 #[allow(clippy::missing_transmute_annotations)]
62 rusqlite::ffi::sqlite3_auto_extension(Some(std::mem::transmute(
63 sqlite_vec::sqlite3_vec_init as *const (),
64 )));
65 }
66 let conn = Connection::open_in_memory().unwrap();
67 conn.execute(
68 "CREATE TABLE endpoints (
69 operation_id TEXT PRIMARY KEY,
70 path TEXT NOT NULL,
71 method TEXT NOT NULL,
72 summary TEXT,
73 description TEXT,
74 input_schema TEXT NOT NULL,
75 output_schema TEXT NOT NULL,
76 auth_scheme_ref TEXT
77 )",
78 [],
79 )
80 .unwrap();
81 conn.execute(
82 "CREATE VIRTUAL TABLE semantic_endpoints USING vec0(
83 operation_id TEXT PRIMARY KEY,
84 embedding FLOAT[768]
85 )",
86 [],
87 )
88 .unwrap();
89 conn.execute(
90 "INSERT INTO endpoints (operation_id, path, method, summary, description, input_schema, output_schema, auth_scheme_ref)
91 VALUES ('listWidgets', '/widgets', 'GET', 'List widgets', NULL, '{}', '[]', NULL)",
92 [],
93 )
94 .unwrap();
95 let embedding: Vec<u8> = std::iter::once(1.0f32)
96 .chain(std::iter::repeat_n(0.0f32, 767))
97 .flat_map(|v| v.to_le_bytes())
98 .collect();
99 conn.execute(
100 "INSERT INTO semantic_endpoints (operation_id, embedding) VALUES ('listWidgets', ?1)",
101 rusqlite::params![embedding],
102 )
103 .unwrap();
104 conn
105 }
106
107 #[test]
108 fn search_operations_finds_the_only_seeded_row() {
109 let conn = seeded_store();
110 let result = search_operations(&conn, "list all the widgets", 5).unwrap();
111 let results = result.as_array().unwrap();
112 assert_eq!(results.len(), 1);
113 assert_eq!(results[0]["operation_id"], "listWidgets");
114 }
115
116 #[test]
117 fn warn_if_embeddings_incomplete_does_not_panic_on_a_matched_store() {
118 let conn = seeded_store();
122 warn_if_embeddings_incomplete(&conn);
123 }
124
125 #[test]
126 fn warn_if_embeddings_incomplete_does_not_panic_on_a_missing_table() {
127 let conn = Connection::open_in_memory().unwrap();
131 warn_if_embeddings_incomplete(&conn);
132 }
133}