codemem-mcp 0.4.0

MCP server for Codemem (JSON-RPC 2.0 over stdio)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
//! Advanced recall & namespace tools: recall_with_expansion, list_namespaces,
//! namespace_stats, delete_namespace, export_memories, import_memories.

use crate::scoring::{compute_score, truncate_str};
use crate::types::ToolResult;
use crate::McpServer;
use codemem_core::{
    CodememError, GraphBackend, GraphNode, MemoryNode, MemoryType, NodeKind, SearchResult,
    VectorBackend,
};
use codemem_storage::Storage;
use serde_json::{json, Value};
use std::collections::{HashMap, HashSet};

impl McpServer {
    /// MCP tool: recall_with_expansion -- vector search + graph expansion.
    pub(crate) fn tool_recall_with_expansion(&self, args: &Value) -> ToolResult {
        let query = match args.get("query").and_then(|v| v.as_str()) {
            Some(q) if !q.is_empty() => q,
            _ => return ToolResult::tool_error("Missing or empty 'query' parameter"),
        };

        let k = args.get("k").and_then(|v| v.as_u64()).unwrap_or(5) as usize;
        let expansion_depth = args
            .get("expansion_depth")
            .and_then(|v| v.as_u64())
            .unwrap_or(1) as usize;
        let namespace_filter: Option<&str> = args.get("namespace").and_then(|v| v.as_str());

        let query_lower = query.to_lowercase();
        let query_tokens: Vec<&str> = query_lower.split_whitespace().collect();

        // Step 1: Run normal vector search (or text fallback)
        let vector_results: Vec<(String, f32)> = if let Some(emb_guard) =
            match self.lock_embeddings() {
                Ok(g) => g,
                Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
            } {
            match emb_guard.embed(query) {
                Ok(query_embedding) => {
                    drop(emb_guard);
                    let vec = match self.lock_vector() {
                        Ok(v) => v,
                        Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
                    };
                    vec.search(&query_embedding, k * 2).unwrap_or_default()
                }
                Err(e) => {
                    tracing::warn!("Query embedding failed: {e}");
                    vec![]
                }
            }
        } else {
            vec![]
        };

        let graph = match self.lock_graph() {
            Ok(g) => g,
            Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
        };
        let bm25 = match self.lock_bm25() {
            Ok(b) => b,
            Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
        };

        // Collect initial seed memories with their vector similarity
        struct ScoredMemory {
            memory: MemoryNode,
            vector_sim: f64,
            expansion_path: String,
        }

        let mut all_memories: Vec<ScoredMemory> = Vec::new();
        let mut seen_ids: HashSet<String> = HashSet::new();

        if vector_results.is_empty() {
            // Fallback: text search over all memories
            let ids = match self.storage.list_memory_ids() {
                Ok(ids) => ids,
                Err(e) => return ToolResult::tool_error(format!("Storage error: {e}")),
            };

            for id in &ids {
                if let Ok(Some(memory)) = self.storage.get_memory(id) {
                    if let Some(ns) = namespace_filter {
                        if memory.namespace.as_deref() != Some(ns) {
                            continue;
                        }
                    }
                    let breakdown =
                        compute_score(&memory, query, &query_tokens, 0.0, &graph, &bm25);
                    let weights = match self.scoring_weights() {
                        Ok(w) => w,
                        Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
                    };
                    let score = breakdown.total_with_weights(&weights);
                    drop(weights);
                    if score > 0.01 {
                        seen_ids.insert(memory.id.clone());
                        all_memories.push(ScoredMemory {
                            memory,
                            vector_sim: 0.0,
                            expansion_path: "direct".to_string(),
                        });
                    }
                }
            }
        } else {
            // Vector search path
            for (id, distance) in &vector_results {
                if let Ok(Some(memory)) = self.storage.get_memory(id) {
                    if let Some(ns) = namespace_filter {
                        if memory.namespace.as_deref() != Some(ns) {
                            continue;
                        }
                    }
                    seen_ids.insert(memory.id.clone());
                    let similarity = 1.0 - (*distance as f64);
                    all_memories.push(ScoredMemory {
                        memory,
                        vector_sim: similarity,
                        expansion_path: "direct".to_string(),
                    });
                }
            }
        }

        // Step 2-4: Graph expansion from each direct result
        // Collect the IDs of direct results for expansion
        let direct_ids: Vec<String> = all_memories.iter().map(|m| m.memory.id.clone()).collect();

        for direct_id in &direct_ids {
            // Use BFS expansion from this memory's graph node
            if let Ok(expanded_nodes) = graph.bfs(direct_id, expansion_depth) {
                for expanded_node in &expanded_nodes {
                    // Skip the start node itself (already in results)
                    if expanded_node.id == *direct_id {
                        continue;
                    }

                    // Only consider memory nodes
                    if expanded_node.kind != NodeKind::Memory {
                        continue;
                    }

                    // Get the memory_id from the graph node
                    let memory_id = expanded_node
                        .memory_id
                        .as_deref()
                        .unwrap_or(&expanded_node.id);

                    // Skip if already seen
                    if seen_ids.contains(memory_id) {
                        continue;
                    }

                    // Fetch the memory
                    if let Ok(Some(memory)) = self.storage.get_memory(memory_id) {
                        if let Some(ns) = namespace_filter {
                            if memory.namespace.as_deref() != Some(ns) {
                                continue;
                            }
                        }

                        // Build expansion path description
                        let expansion_path = if let Ok(edges) = graph.get_edges(direct_id) {
                            edges
                                .iter()
                                .find(|e| e.dst == expanded_node.id || e.src == expanded_node.id)
                                .map(|e| format!("via {} from {}", e.relationship, direct_id))
                                .unwrap_or_else(|| format!("via graph from {direct_id}"))
                        } else {
                            format!("via graph from {direct_id}")
                        };

                        seen_ids.insert(memory_id.to_string());
                        all_memories.push(ScoredMemory {
                            memory,
                            vector_sim: 0.0,
                            expansion_path,
                        });
                    }
                }
            }
        }

        // Step 5-6: Score all memories and sort
        let weights = match self.scoring_weights() {
            Ok(w) => w,
            Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
        };
        let mut scored_results: Vec<(SearchResult, String)> = all_memories
            .into_iter()
            .map(|sm| {
                let breakdown = compute_score(
                    &sm.memory,
                    query,
                    &query_tokens,
                    sm.vector_sim,
                    &graph,
                    &bm25,
                );
                let score = breakdown.total_with_weights(&weights);
                (
                    SearchResult {
                        memory: sm.memory,
                        score,
                        score_breakdown: breakdown,
                    },
                    sm.expansion_path,
                )
            })
            .collect();
        drop(weights);

        scored_results.sort_by(|a, b| {
            b.0.score
                .partial_cmp(&a.0.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        scored_results.truncate(k);

        // Step 7: Format results with expansion_path
        if scored_results.is_empty() {
            return ToolResult::text("No matching memories found.");
        }

        let output: Vec<Value> = scored_results
            .iter()
            .map(|(r, path)| {
                json!({
                    "id": r.memory.id,
                    "content": r.memory.content,
                    "memory_type": r.memory.memory_type.to_string(),
                    "score": format!("{:.4}", r.score),
                    "importance": r.memory.importance,
                    "tags": r.memory.tags,
                    "access_count": r.memory.access_count,
                    "expansion_path": path,
                })
            })
            .collect();

        ToolResult::text(
            serde_json::to_string_pretty(&output).expect("JSON serialization of literal"),
        )
    }

    /// MCP tool: list_namespaces -- list all namespaces with memory counts.
    pub(crate) fn tool_list_namespaces(&self) -> ToolResult {
        let namespaces = match self.storage.list_namespaces() {
            Ok(ns) => ns,
            Err(e) => return ToolResult::tool_error(format!("Storage error: {e}")),
        };

        let mut ns_list: Vec<Value> = Vec::new();
        for ns in &namespaces {
            let count = match self.storage.list_memory_ids_for_namespace(ns) {
                Ok(ids) => ids.len(),
                Err(_) => 0,
            };
            ns_list.push(json!({
                "name": ns,
                "memory_count": count,
            }));
        }

        let response = json!({ "namespaces": ns_list });
        ToolResult::text(
            serde_json::to_string_pretty(&response).expect("JSON serialization of literal"),
        )
    }

    /// MCP tool: namespace_stats -- detailed stats for a single namespace.
    pub(crate) fn tool_namespace_stats(&self, args: &Value) -> ToolResult {
        let namespace = match args.get("namespace").and_then(|v| v.as_str()) {
            Some(ns) if !ns.is_empty() => ns,
            _ => return ToolResult::tool_error("Missing or empty 'namespace' parameter"),
        };

        let ids = match self.storage.list_memory_ids_for_namespace(namespace) {
            Ok(ids) => ids,
            Err(e) => return ToolResult::tool_error(format!("Storage error: {e}")),
        };

        if ids.is_empty() {
            return ToolResult::text(
                serde_json::to_string_pretty(&json!({
                    "namespace": namespace,
                    "count": 0,
                    "message": "No memories found in this namespace"
                }))
                .expect("JSON serialization of literal"),
            );
        }

        let mut total_importance = 0.0;
        let mut total_confidence = 0.0;
        let mut type_distribution: HashMap<String, usize> = HashMap::new();
        let mut tag_frequency: HashMap<String, usize> = HashMap::new();
        let mut oldest: Option<chrono::DateTime<chrono::Utc>> = None;
        let mut newest: Option<chrono::DateTime<chrono::Utc>> = None;
        let mut count = 0usize;

        for id in &ids {
            if let Ok(Some(memory)) = self.storage.get_memory(id) {
                count += 1;
                total_importance += memory.importance;
                total_confidence += memory.confidence;

                *type_distribution
                    .entry(memory.memory_type.to_string())
                    .or_insert(0) += 1;

                for tag in &memory.tags {
                    *tag_frequency.entry(tag.clone()).or_insert(0) += 1;
                }

                match oldest {
                    None => oldest = Some(memory.created_at),
                    Some(ref o) if memory.created_at < *o => oldest = Some(memory.created_at),
                    _ => {}
                }
                match newest {
                    None => newest = Some(memory.created_at),
                    Some(ref n) if memory.created_at > *n => newest = Some(memory.created_at),
                    _ => {}
                }
            }
        }

        let avg_importance = if count > 0 {
            total_importance / count as f64
        } else {
            0.0
        };
        let avg_confidence = if count > 0 {
            total_confidence / count as f64
        } else {
            0.0
        };

        let response = json!({
            "namespace": namespace,
            "count": count,
            "avg_importance": format!("{:.4}", avg_importance),
            "avg_confidence": format!("{:.4}", avg_confidence),
            "type_distribution": type_distribution,
            "tag_frequency": tag_frequency,
            "oldest": oldest.map(|d| d.to_rfc3339()),
            "newest": newest.map(|d| d.to_rfc3339()),
        });

        ToolResult::text(
            serde_json::to_string_pretty(&response).expect("JSON serialization of literal"),
        )
    }

    /// MCP tool: delete_namespace -- delete all memories in a namespace.
    pub(crate) fn tool_delete_namespace(&self, args: &Value) -> ToolResult {
        let namespace = match args.get("namespace").and_then(|v| v.as_str()) {
            Some(ns) if !ns.is_empty() => ns,
            _ => return ToolResult::tool_error("Missing or empty 'namespace' parameter"),
        };

        let confirm = args
            .get("confirm")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        if !confirm {
            return ToolResult::tool_error(
                "Destructive operation requires 'confirm': true parameter",
            );
        }

        let ids = match self.storage.list_memory_ids_for_namespace(namespace) {
            Ok(ids) => ids,
            Err(e) => return ToolResult::tool_error(format!("Storage error: {e}")),
        };

        let mut deleted = 0usize;
        let mut graph = match self.lock_graph() {
            Ok(g) => g,
            Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
        };
        let mut vector = match self.lock_vector() {
            Ok(v) => v,
            Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
        };
        let mut bm25 = match self.lock_bm25() {
            Ok(b) => b,
            Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
        };

        for id in &ids {
            // Delete memory from storage
            if let Ok(true) = self.storage.delete_memory(id) {
                deleted += 1;

                // Remove from vector index
                let _ = vector.remove(id);

                // Remove from in-memory graph
                let _ = graph.remove_node(id);

                // Remove graph node and edges from SQLite
                let _ = self.storage.delete_graph_edges_for_node(id);
                let _ = self.storage.delete_graph_node(id);

                // Remove embedding from SQLite
                let _ = self.storage.delete_embedding(id);

                // Remove from BM25 index
                bm25.remove_document(id);
            }
        }

        // Drop locks before calling save_index (which acquires vector lock)
        drop(graph);
        drop(vector);
        drop(bm25);

        // Persist vector index to disk
        self.save_index();

        let response = json!({
            "deleted": deleted,
            "namespace": namespace,
        });

        ToolResult::text(
            serde_json::to_string_pretty(&response).expect("JSON serialization of literal"),
        )
    }

    // ── Export/Import Tools ─────────────────────────────────────────────────

    /// MCP tool: export_memories -- export memories as a JSON array.
    pub(crate) fn tool_export_memories(&self, args: &Value) -> ToolResult {
        let namespace_filter: Option<&str> = args.get("namespace").and_then(|v| v.as_str());
        let memory_type_filter: Option<MemoryType> = args
            .get("memory_type")
            .and_then(|v| v.as_str())
            .and_then(|s| s.parse().ok());
        let limit = args.get("limit").and_then(|v| v.as_u64()).unwrap_or(100) as usize;

        let ids = match namespace_filter {
            Some(ns) => match self.storage.list_memory_ids_for_namespace(ns) {
                Ok(ids) => ids,
                Err(e) => return ToolResult::tool_error(format!("Storage error: {e}")),
            },
            None => match self.storage.list_memory_ids() {
                Ok(ids) => ids,
                Err(e) => return ToolResult::tool_error(format!("Storage error: {e}")),
            },
        };

        let mut exported: Vec<Value> = Vec::new();

        for id in &ids {
            if exported.len() >= limit {
                break;
            }
            if let Ok(Some(memory)) = self.storage.get_memory(id) {
                // Apply memory_type filter
                if let Some(ref filter_type) = memory_type_filter {
                    if memory.memory_type != *filter_type {
                        continue;
                    }
                }

                // Get edges for this memory
                let edges: Vec<Value> = self
                    .storage
                    .get_edges_for_node(id)
                    .unwrap_or_default()
                    .iter()
                    .map(|e| {
                        json!({
                            "id": e.id,
                            "src": e.src,
                            "dst": e.dst,
                            "relationship": e.relationship.to_string(),
                            "weight": e.weight,
                        })
                    })
                    .collect();

                exported.push(json!({
                    "id": memory.id,
                    "content": memory.content,
                    "memory_type": memory.memory_type.to_string(),
                    "importance": memory.importance,
                    "confidence": memory.confidence,
                    "tags": memory.tags,
                    "namespace": memory.namespace,
                    "metadata": memory.metadata,
                    "created_at": memory.created_at.to_rfc3339(),
                    "updated_at": memory.updated_at.to_rfc3339(),
                    "edges": edges,
                }));
            }
        }

        ToolResult::text(
            serde_json::to_string_pretty(&exported).expect("JSON serialization of literal"),
        )
    }

    /// MCP tool: import_memories -- import memories from a JSON array.
    pub(crate) fn tool_import_memories(&self, args: &Value) -> ToolResult {
        let memories_arr = match args.get("memories").and_then(|v| v.as_array()) {
            Some(arr) => arr,
            None => return ToolResult::tool_error("Missing 'memories' parameter (expected array)"),
        };

        let mut imported = 0usize;
        let mut skipped = 0usize;
        let mut ids: Vec<String> = Vec::new();

        for mem_val in memories_arr {
            let content = match mem_val.get("content").and_then(|v| v.as_str()) {
                Some(c) if !c.is_empty() => c,
                _ => {
                    skipped += 1;
                    continue;
                }
            };

            let memory_type: MemoryType = mem_val
                .get("memory_type")
                .and_then(|v| v.as_str())
                .and_then(|s| s.parse().ok())
                .unwrap_or(MemoryType::Context);

            let importance = mem_val
                .get("importance")
                .and_then(|v| v.as_f64())
                .unwrap_or(0.5);

            let confidence = mem_val
                .get("confidence")
                .and_then(|v| v.as_f64())
                .unwrap_or(1.0);

            let tags: Vec<String> = mem_val
                .get("tags")
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_str().map(String::from))
                        .collect()
                })
                .unwrap_or_default();

            let namespace = mem_val
                .get("namespace")
                .and_then(|v| v.as_str())
                .map(String::from);

            let metadata: HashMap<String, serde_json::Value> = mem_val
                .get("metadata")
                .and_then(|v| serde_json::from_value(v.clone()).ok())
                .unwrap_or_default();

            let now = chrono::Utc::now();
            let id = uuid::Uuid::new_v4().to_string();
            let hash = Storage::content_hash(content);

            let memory = MemoryNode {
                id: id.clone(),
                content: content.to_string(),
                memory_type,
                importance,
                confidence,
                access_count: 0,
                content_hash: hash,
                tags,
                metadata,
                namespace,
                created_at: now,
                updated_at: now,
                last_accessed_at: now,
            };

            match self.storage.insert_memory(&memory) {
                Ok(()) => {
                    // Update BM25 index
                    match self.lock_bm25() {
                        Ok(mut bm25) => bm25.add_document(&id, content),
                        Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
                    }

                    // Create graph node first (so enrichment can reference it)
                    let graph_node = GraphNode {
                        id: id.clone(),
                        kind: NodeKind::Memory,
                        label: truncate_str(content, 80),
                        payload: HashMap::new(),
                        centrality: 0.0,
                        memory_id: Some(id.clone()),
                        namespace: None,
                    };
                    let _ = self.storage.insert_graph_node(&graph_node);
                    match self.lock_graph() {
                        Ok(mut graph) => {
                            let _ = graph.add_node(graph_node);
                        }
                        Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
                    }

                    // Generate contextual embedding and insert into vector index
                    if let Some(emb_guard) = match self.lock_embeddings() {
                        Ok(g) => g,
                        Err(e) => return ToolResult::tool_error(format!("Lock error: {e}")),
                    } {
                        let enriched = self.enrich_memory_text(
                            content,
                            memory_type,
                            &memory.tags,
                            memory.namespace.as_deref(),
                            Some(&id),
                        );
                        let emb_result = emb_guard.embed(&enriched);
                        drop(emb_guard);
                        if let Ok(embedding) = emb_result {
                            let _ = self.storage.store_embedding(&id, &embedding);
                            match self.lock_vector() {
                                Ok(mut vec) => {
                                    let _ = vec.insert(&id, &embedding);
                                }
                                Err(e) => {
                                    return ToolResult::tool_error(format!("Lock error: {e}"))
                                }
                            }
                        }
                    }

                    ids.push(id);
                    imported += 1;
                }
                Err(CodememError::Duplicate(_)) => {
                    skipped += 1;
                }
                Err(_) => {
                    skipped += 1;
                }
            }
        }

        // Persist vector index to disk
        self.save_index();

        ToolResult::text(
            serde_json::to_string_pretty(&json!({
                "imported": imported,
                "skipped": skipped,
                "ids": ids,
            }))
            .expect("JSON serialization of literal"),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_helpers::*;

    /// Helper: call a tool and return the result Value.
    fn call_tool(server: &McpServer, tool_name: &str, arguments: Value) -> Value {
        let params = json!({"name": tool_name, "arguments": arguments});
        let resp = server.handle_request("tools/call", Some(&params), json!("req"));
        assert!(
            resp.error.is_none(),
            "Unexpected error calling {tool_name}: {:?}",
            resp.error
        );
        resp.result.unwrap()
    }

    /// Helper: call a tool and parse the text content as JSON.
    fn call_tool_parse(server: &McpServer, tool_name: &str, arguments: Value) -> Value {
        let result = call_tool(server, tool_name, arguments);
        let text = result["content"][0]["text"].as_str().unwrap();
        serde_json::from_str(text).unwrap_or_else(|_| Value::String(text.to_string()))
    }

    /// Helper: store a memory with namespace.
    fn store_ns(
        server: &McpServer,
        content: &str,
        namespace: &str,
        memory_type: &str,
        tags: &[&str],
    ) -> Value {
        call_tool_parse(
            server,
            "store_memory",
            json!({
                "content": content,
                "memory_type": memory_type,
                "tags": tags,
                "namespace": namespace,
            }),
        )
    }

    #[test]
    fn recall_with_expansion_no_embeddings() {
        let server = test_server();

        // Store two memories and link them
        let mem_a = store_ns(
            &server,
            "graph expansion base memory about architecture",
            "test-ns",
            "insight",
            &["arch"],
        );
        let id_a = mem_a["id"].as_str().unwrap();

        let mem_b = store_ns(
            &server,
            "related memory about design patterns",
            "test-ns",
            "pattern",
            &["design"],
        );
        let id_b = mem_b["id"].as_str().unwrap();

        // Associate them
        call_tool(
            &server,
            "associate_memories",
            json!({
                "source_id": id_a,
                "target_id": id_b,
                "relationship": "RELATES_TO",
            }),
        );

        // Recall with expansion (no embeddings = text fallback)
        let result = call_tool(
            &server,
            "recall_with_expansion",
            json!({
                "query": "architecture",
                "k": 5,
                "expansion_depth": 1,
            }),
        );
        let text = result["content"][0]["text"].as_str().unwrap();
        // Should find at least the base memory via token overlap
        assert!(text.contains("architecture") || text.contains("design"));
    }

    #[test]
    fn list_namespaces_empty() {
        let server = test_server();

        let result = call_tool_parse(&server, "list_namespaces", json!({}));
        let namespaces = result["namespaces"].as_array().unwrap();
        assert_eq!(namespaces.len(), 0);
    }

    #[test]
    fn list_namespaces_with_data() {
        let server = test_server();

        // Store memories in two namespaces
        store_ns(
            &server,
            "memory alpha one about rust",
            "ns-alpha",
            "insight",
            &["rust"],
        );
        store_ns(
            &server,
            "memory alpha two about safety",
            "ns-alpha",
            "pattern",
            &["safety"],
        );
        store_ns(
            &server,
            "memory beta one about python",
            "ns-beta",
            "context",
            &["python"],
        );

        let result = call_tool_parse(&server, "list_namespaces", json!({}));
        let namespaces = result["namespaces"].as_array().unwrap();
        assert_eq!(namespaces.len(), 2);

        // Verify names and counts
        let ns_names: Vec<&str> = namespaces
            .iter()
            .filter_map(|n| n["name"].as_str())
            .collect();
        assert!(ns_names.contains(&"ns-alpha"));
        assert!(ns_names.contains(&"ns-beta"));

        for ns in namespaces {
            if ns["name"].as_str().unwrap() == "ns-alpha" {
                assert_eq!(ns["memory_count"], 2);
            } else if ns["name"].as_str().unwrap() == "ns-beta" {
                assert_eq!(ns["memory_count"], 1);
            }
        }
    }

    #[test]
    fn namespace_stats_basic() {
        let server = test_server();

        store_ns(
            &server,
            "insight about architecture patterns",
            "stats-ns",
            "insight",
            &["arch", "patterns"],
        );
        store_ns(
            &server,
            "pattern for error handling in rust",
            "stats-ns",
            "pattern",
            &["rust", "errors"],
        );

        let result = call_tool_parse(&server, "namespace_stats", json!({"namespace": "stats-ns"}));
        assert_eq!(result["namespace"], "stats-ns");
        assert_eq!(result["count"], 2);

        // Check type distribution
        let types = &result["type_distribution"];
        assert_eq!(types["insight"], 1);
        assert_eq!(types["pattern"], 1);

        // Check tag frequency
        let tags = &result["tag_frequency"];
        assert_eq!(tags["arch"], 1);
        assert_eq!(tags["patterns"], 1);
        assert_eq!(tags["rust"], 1);
        assert_eq!(tags["errors"], 1);

        // Dates should be present
        assert!(result["oldest"].is_string());
        assert!(result["newest"].is_string());
    }

    #[test]
    fn delete_namespace_requires_confirm() {
        let server = test_server();

        store_ns(
            &server,
            "memory to be protected",
            "protected-ns",
            "context",
            &[],
        );

        // Try to delete without confirm
        let result = call_tool(
            &server,
            "delete_namespace",
            json!({
                "namespace": "protected-ns",
                "confirm": false,
            }),
        );
        let text = result["content"][0]["text"].as_str().unwrap();
        assert_eq!(result["isError"], true);
        assert!(text.contains("confirm"));

        // Memory should still exist
        let stats = call_tool_parse(
            &server,
            "namespace_stats",
            json!({"namespace": "protected-ns"}),
        );
        assert_eq!(stats["count"], 1);
    }

    #[test]
    fn delete_namespace_with_confirm() {
        let server = test_server();

        store_ns(
            &server,
            "memory to delete alpha",
            "delete-ns",
            "insight",
            &["test"],
        );
        store_ns(
            &server,
            "memory to delete beta",
            "delete-ns",
            "pattern",
            &["test"],
        );

        // Verify they exist
        let stats = call_tool_parse(
            &server,
            "namespace_stats",
            json!({"namespace": "delete-ns"}),
        );
        assert_eq!(stats["count"], 2);

        // Delete with confirm
        let result = call_tool_parse(
            &server,
            "delete_namespace",
            json!({
                "namespace": "delete-ns",
                "confirm": true,
            }),
        );
        assert_eq!(result["deleted"], 2);
        assert_eq!(result["namespace"], "delete-ns");

        // Verify they are gone
        let stats_after = call_tool_parse(
            &server,
            "namespace_stats",
            json!({"namespace": "delete-ns"}),
        );
        assert_eq!(stats_after["count"], 0);
    }

    // ── Export/Import Tests ─────────────────────────────────────────────

    #[test]
    fn export_memories_empty() {
        let server = test_server();
        let params = json!({"name": "export_memories", "arguments": {}});
        let resp = server.handle_request("tools/call", Some(&params), json!(400));
        let result = resp.result.unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        let exported: Vec<Value> = serde_json::from_str(text).unwrap();
        assert!(exported.is_empty());
    }

    #[test]
    fn import_and_export_roundtrip() {
        let server = test_server();

        // Import 2 memories
        let import_params = json!({
            "name": "import_memories",
            "arguments": {
                "memories": [
                    {
                        "content": "roundtrip memory one about rust",
                        "memory_type": "insight",
                        "importance": 0.8,
                        "tags": ["rust", "test"]
                    },
                    {
                        "content": "roundtrip memory two about python",
                        "memory_type": "pattern",
                        "tags": ["python"]
                    }
                ]
            }
        });
        let resp = server.handle_request("tools/call", Some(&import_params), json!(401));
        let result = resp.result.unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        let import_result: Value = serde_json::from_str(text).unwrap();
        assert_eq!(import_result["imported"], 2);
        assert_eq!(import_result["skipped"], 0);
        assert_eq!(import_result["ids"].as_array().unwrap().len(), 2);

        // Export all memories
        let export_params = json!({"name": "export_memories", "arguments": {}});
        let resp = server.handle_request("tools/call", Some(&export_params), json!(402));
        let result = resp.result.unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        let exported: Vec<Value> = serde_json::from_str(text).unwrap();
        assert_eq!(exported.len(), 2);

        // Verify content matches
        let contents: Vec<&str> = exported
            .iter()
            .filter_map(|e| e["content"].as_str())
            .collect();
        assert!(contents.contains(&"roundtrip memory one about rust"));
        assert!(contents.contains(&"roundtrip memory two about python"));

        // Verify memory types
        let types: Vec<&str> = exported
            .iter()
            .filter_map(|e| e["memory_type"].as_str())
            .collect();
        assert!(types.contains(&"insight"));
        assert!(types.contains(&"pattern"));
    }

    #[test]
    fn export_with_namespace_filter() {
        let server = test_server();

        // Import memories with different namespaces
        let import_params = json!({
            "name": "import_memories",
            "arguments": {
                "memories": [
                    {
                        "content": "project-a memory about architecture",
                        "memory_type": "decision",
                        "namespace": "/projects/a"
                    },
                    {
                        "content": "project-b memory about testing",
                        "memory_type": "insight",
                        "namespace": "/projects/b"
                    },
                    {
                        "content": "project-a memory about patterns",
                        "memory_type": "pattern",
                        "namespace": "/projects/a"
                    }
                ]
            }
        });
        let resp = server.handle_request("tools/call", Some(&import_params), json!(403));
        let result = resp.result.unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        let import_result: Value = serde_json::from_str(text).unwrap();
        assert_eq!(import_result["imported"], 3);

        // Export only namespace /projects/a
        let export_params = json!({
            "name": "export_memories",
            "arguments": {"namespace": "/projects/a"}
        });
        let resp = server.handle_request("tools/call", Some(&export_params), json!(404));
        let result = resp.result.unwrap();
        let text = result["content"][0]["text"].as_str().unwrap();
        let exported: Vec<Value> = serde_json::from_str(text).unwrap();
        assert_eq!(exported.len(), 2);

        // All exported should be from /projects/a
        for mem in &exported {
            assert_eq!(mem["namespace"].as_str().unwrap(), "/projects/a");
        }
    }
}