@@ -48,7 +48,7 @@ pub async fn refresh_all_datasources<R: Runtime>(_app_handle: &AppHandle<R>) ->
4848 // dbg!("fetch datasources for server: {}", &server.id);
4949
5050 // Attempt to get datasources by server, and continue even if it fails
51- let connectors = match get_datasources_by_server ( server. id . as_str ( ) , None ) . await {
51+ let connectors = match datasource_search ( server. id . as_str ( ) , None ) . await {
5252 Ok ( connectors) => {
5353 // Process connectors only after fetching them
5454 let connectors_map: HashMap < String , DataSource > = connectors
@@ -90,7 +90,7 @@ pub async fn refresh_all_datasources<R: Runtime>(_app_handle: &AppHandle<R>) ->
9090}
9191
9292#[ tauri:: command]
93- pub async fn get_datasources_by_server (
93+ pub async fn datasource_search (
9494 id : & str ,
9595 options : Option < GetDatasourcesByServerOptions > ,
9696) -> Result < Vec < DataSource > , String > {
@@ -144,3 +144,59 @@ pub async fn get_datasources_by_server(
144144
145145 Ok ( datasources)
146146}
147+
148+ #[ tauri:: command]
149+ pub async fn mcp_server_search (
150+ id : & str ,
151+ options : Option < GetDatasourcesByServerOptions > ,
152+ ) -> Result < Vec < DataSource > , String > {
153+ let from = options. as_ref ( ) . and_then ( |opt| opt. from ) . unwrap_or ( 0 ) ;
154+ let size = options. as_ref ( ) . and_then ( |opt| opt. size ) . unwrap_or ( 10000 ) ;
155+ let query = options
156+ . and_then ( |opt| opt. query )
157+ . unwrap_or ( String :: default ( ) ) ;
158+
159+ let mut body = serde_json:: json!( {
160+ "from" : from,
161+ "size" : size,
162+ } ) ;
163+
164+ if !query. is_empty ( ) {
165+ body[ "query" ] = serde_json:: json!( {
166+ "bool" : {
167+ "must" : [ {
168+ "query_string" : {
169+ "fields" : [ "combined_fulltext" ] ,
170+ "query" : query,
171+ "fuzziness" : "AUTO" ,
172+ "fuzzy_prefix_length" : 2 ,
173+ "fuzzy_max_expansions" : 10 ,
174+ "fuzzy_transpositions" : true ,
175+ "allow_leading_wildcard" : false
176+ }
177+ } ]
178+ }
179+ } ) ;
180+ }
181+
182+ // Perform the async HTTP request outside the cache lock
183+ let resp = HttpClient :: post (
184+ id,
185+ "/mcp_server/_search" ,
186+ None ,
187+ Some ( reqwest:: Body :: from ( body. to_string ( ) ) ) ,
188+ )
189+ . await
190+ . map_err ( |e| format ! ( "Error fetching datasource: {}" , e) ) ?;
191+
192+ // Parse the search results from the response
193+ let mcp_server: Vec < DataSource > = parse_search_results ( resp) . await . map_err ( |e| {
194+ dbg ! ( "Error parsing search results: {}" , & e) ;
195+ e. to_string ( )
196+ } ) ?;
197+
198+ // Save the updated mcp_server to cache
199+ // save_datasource_to_cache(&id, mcp_server.clone());
200+
201+ Ok ( mcp_server)
202+ }
0 commit comments