memnite-mcp 0.2.1

MCP stdio server for memnite: exposes the event-sourced memory engine as 14 tools to AI agents.
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
use std::path::PathBuf;

use memnite_cli::{
    parse_relation, parse_scope, AddSpec, AnchorSpec, App, EventCtx, RelationSpec, SearchQuery,
    UpdatePatch,
};

use crate::params::{
    AddArgs, CheckArgs, ConflictsArgs, ContextArgs, DeleteArgs, GetArgs, RelateArgs, SearchArgs,
    SessionSummaryArgs, TimelineArgs, UpdateArgs,
};

/// Per-request context (timestamp / engine / machine). Mirrors cli's EventCtx
/// but kept local so tests can inject a fixed one.
#[derive(Clone, Debug)]
pub struct Ctx {
    pub ts: String,
    pub engine: String,
    pub machine: String,
}

impl Ctx {
    fn to_event_ctx(&self) -> EventCtx {
        EventCtx {
            ts: self.ts.clone(),
            engine: self.engine.clone(),
            machine: self.machine.clone(),
        }
    }
}

type R = Result<String, memnite_cli::CliError>;

fn parse_anchor_arg(s: &str) -> Result<AnchorSpec, memnite_cli::CliError> {
    memnite_cli::parse_anchor(s)
}

/// Inline decay annotation for a recall row, or "" when fresh (or deleted — a
/// tombstone is never advised for review, matching `review_list`). `now` is stamped
/// by the caller (read-time; never enters the log).
fn decay_mark(status: &str, mem_type: &str, updated_ts: &str, now: &str) -> String {
    if status == "deleted" {
        return String::new();
    }
    memnite_core::review_marker(mem_type, updated_ts, now)
        .map(|s| format!(" [{s}]"))
        .unwrap_or_default()
}

pub fn op_add(app: &App, a: AddArgs, ctx: &Ctx) -> R {
    let scope = parse_scope(a.scope.as_deref().unwrap_or("agent"))?;
    let mut anchors = Vec::with_capacity(a.anchors.len());
    for raw in &a.anchors {
        anchors.push(parse_anchor_arg(raw)?);
    }
    let root = a
        .root
        .map(PathBuf::from)
        .unwrap_or(std::env::current_dir()?);
    let project = a.project.unwrap_or_default();
    let spec = AddSpec {
        title: a.title,
        body: a.body,
        mem_type: a.r#type.unwrap_or_else(|| "decision".to_string()),
        scope,
        project: project.clone(),
        topic_key: a.topic,
        anchors,
        tags: a.tags,
    };
    let row = app.add(spec, &root, &ctx.to_event_ctx())?;
    let mut out = format!(
        "created {} \"{}\" [{}]",
        row.memory_id, row.title, row.status
    );
    // Parity with the CLI: surface a warning when an explicit project was not canonical.
    if let Some(notice) = memnite_cli::normalization_notice(&project) {
        out.push_str(&format!("\nwarning: {notice}"));
    }
    if let Ok(cands) = app.find_candidates(&row.memory_id, 3) {
        if !cands.is_empty() {
            out.push_str(&format!(
                "\njudgment_required: {} candidate(s):",
                cands.len()
            ));
            for c in cands {
                out.push_str(&format!("\n  {}\t{}", c.memory_id, c.title));
            }
        }
    }
    Ok(out)
}

pub fn op_session_summary(app: &App, a: SessionSummaryArgs, ctx: &Ctx) -> R {
    let scope = parse_scope(a.scope.as_deref().unwrap_or("agent"))?;
    let row = app.session_summary(
        a.summary,
        a.project.unwrap_or_default(),
        scope,
        &ctx.to_event_ctx(),
    )?;
    Ok(format!(
        "saved session summary {} [{}]",
        row.memory_id, row.status
    ))
}

pub fn op_search(app: &App, a: SearchArgs) -> R {
    let scope = match a.scope.as_deref() {
        Some(s) => Some(parse_scope(s)?),
        None => None,
    };
    let q = SearchQuery {
        text: a.query,
        mem_type: a.r#type,
        project: a.project,
        scope,
        match_any: a.match_any,
    };
    let rows = app.search(q)?;
    if rows.is_empty() {
        return Ok("0 results".to_string());
    }
    let now = chrono::Utc::now().to_rfc3339();
    let body = rows
        .iter()
        .map(|m| {
            format!(
                "{}\t{}\t[{}]{}{}",
                m.memory_id,
                m.title,
                m.status,
                annotate(app, &m.memory_id),
                decay_mark(&m.status, &m.mem_type, &m.updated_ts, &now)
            )
        })
        .collect::<Vec<_>>()
        .join("\n");
    Ok(format!("{} results\n{}", rows.len(), body))
}

pub fn op_get(app: &App, a: GetArgs) -> R {
    match app.get(&a.memory_id)? {
        Some(m) => {
            let now = chrono::Utc::now().to_rfc3339();
            Ok(format!(
                "{}\t{}\t[{}]{}\n{}",
                m.memory_id,
                m.title,
                m.status,
                decay_mark(&m.status, &m.mem_type, &m.updated_ts, &now),
                m.body
            ))
        }
        None => Ok("(not found)".to_string()),
    }
}

pub fn op_stale(app: &App) -> R {
    let rows = app.list_stale()?;
    if rows.is_empty() {
        return Ok("no stale memories".to_string());
    }
    Ok(rows
        .iter()
        .map(|m| format!("{}\t{}", m.memory_id, m.title))
        .collect::<Vec<_>>()
        .join("\n"))
}

pub fn op_check(app: &App, a: CheckArgs, ctx: &Ctx) -> R {
    let root = a
        .root
        .map(PathBuf::from)
        .unwrap_or(std::env::current_dir()?);
    let s = app.check(&root, &ctx.to_event_ctx())?;
    Ok(format!(
        "checked: {} stable, {} stale, {} unchanged",
        s.stable, s.stale, s.unchanged
    ))
}

pub fn op_rebuild(app: &App) -> R {
    let n = app.rebuild()?;
    Ok(format!("rebuilt projection from {n} events"))
}

pub fn op_update(app: &App, a: UpdateArgs, ctx: &Ctx) -> R {
    let scope = match a.scope.as_deref() {
        Some(s) => Some(parse_scope(s)?),
        None => None,
    };
    let anchors = match a.anchors {
        Some(raws) => {
            let mut out = Vec::with_capacity(raws.len());
            for raw in &raws {
                out.push(parse_anchor_arg(raw)?);
            }
            Some(out)
        }
        None => None,
    };
    let patch = UpdatePatch {
        title: a.title,
        body: a.body,
        mem_type: a.r#type,
        scope,
        project: a.project,
        topic_key: a.topic,
        anchors,
        tags: a.tags,
    };
    let root = a
        .root
        .map(PathBuf::from)
        .unwrap_or(std::env::current_dir()?);
    let row = app.update(&a.memory_id, patch, &root, &ctx.to_event_ctx())?;
    Ok(format!(
        "updated {} \"{}\" [{}]",
        row.memory_id, row.title, row.status
    ))
}

pub fn op_delete(app: &App, a: DeleteArgs, ctx: &Ctx) -> R {
    app.delete(&a.memory_id, &ctx.to_event_ctx())?;
    Ok(format!("deleted {}", a.memory_id))
}

pub fn op_context(app: &App, a: ContextArgs) -> R {
    let rows = app.context(&a.project, a.limit.unwrap_or(20))?;
    if rows.is_empty() {
        return Ok("no memories".to_string());
    }
    let now = chrono::Utc::now().to_rfc3339();
    Ok(rows
        .iter()
        .map(|m| {
            format!(
                "{}\t{}\t[{}]{}",
                m.memory_id,
                m.title,
                m.status,
                decay_mark(&m.status, &m.mem_type, &m.updated_ts, &now)
            )
        })
        .collect::<Vec<_>>()
        .join("\n"))
}

pub fn op_timeline(app: &App, a: TimelineArgs) -> R {
    let rows = app.timeline(&a.memory_id)?;
    if rows.is_empty() {
        return Ok("no events".to_string());
    }
    Ok(rows
        .iter()
        .map(|e| format!("{}\t{}\t{}\t{}", e.lamport, e.kind, e.ts, e.engine))
        .collect::<Vec<_>>()
        .join("\n"))
}

pub fn op_doctor(app: &App) -> R {
    let r = app.doctor()?;
    let mut lines: Vec<String> = if r.mismatches.is_empty() {
        vec!["ok".to_string()]
    } else {
        r.mismatches.clone()
    };
    for c in &r.conflicts {
        lines.push(format!(
            "conflict {}: {}{} (L{}) overwritten by {} (L{})",
            c.memory_id,
            c.field,
            c.lost_writer,
            c.lost_lamport,
            c.winning_writer,
            c.winning_lamport
        ));
    }
    Ok(format!(
        "log={} proj={} cursor_ok={} mismatches={} conflicts={}\n{}",
        r.log_count,
        r.proj_count,
        r.cursor_ok,
        r.mismatches.len(),
        r.conflicts.len(),
        lines.join("\n")
    ))
}

pub fn op_relate(app: &App, a: RelateArgs, ctx: &Ctx) -> R {
    let relation = parse_relation(&a.relation)?;
    let spec = RelationSpec {
        relation,
        confidence: a.confidence.unwrap_or(1.0),
        reason: a.reason.unwrap_or_default(),
        judged_by: "agent".to_string(),
    };
    app.relate(&a.from_id, &a.to_id, spec, &ctx.to_event_ctx())?;
    Ok(format!("related {} -> {}", a.from_id, a.to_id))
}

pub fn op_conflicts(app: &App, a: ConflictsArgs) -> R {
    let rels = app.relations_for(&a.memory_id)?;
    if rels.is_empty() {
        return Ok("no relations".to_string());
    }
    Ok(rels
        .iter()
        .map(|r| {
            format!(
                "{}\t{}\t{}\t{:.2}\t{}",
                r.from_id, r.relation, r.to_id, r.confidence, r.judged_by
            )
        })
        .collect::<Vec<_>>()
        .join("\n"))
}

/// Compact inline relation annotation for a memory in search results.
/// `#42 supersedes #71` (this memory is the source) or `#42 superseded_by #71`
/// (this memory is the target); other relations shown symmetrically.
fn annotate(app: &App, memory_id: &str) -> String {
    let rels = match app.relations_for(memory_id) {
        Ok(r) => r,
        Err(_) => return String::new(),
    };
    let mut parts = Vec::new();
    for r in rels {
        if r.from_id == memory_id {
            parts.push(format!("{} {}", r.relation, r.to_id));
        } else if r.relation == "supersedes" {
            parts.push(format!("superseded_by {}", r.from_id));
        } else {
            parts.push(format!("{} {}", r.relation, r.from_id));
        }
    }
    if parts.is_empty() {
        String::new()
    } else {
        format!("  [{}]", parts.join("; "))
    }
}

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

    fn ctx() -> Ctx {
        Ctx {
            ts: "2026-06-27T00:00:00Z".to_string(),
            engine: "mcp".to_string(),
            machine: "test".to_string(),
        }
    }

    #[test]
    fn add_then_update_then_delete_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let app = App::open(dir.path()).unwrap();

        let added = op_add(
            &app,
            AddArgs {
                title: "t".to_string(),
                body: "b".to_string(),
                r#type: None,
                scope: None,
                project: Some("p".to_string()),
                topic: None,
                tags: vec![],
                anchors: vec![],
                root: Some(dir.path().to_string_lossy().to_string()),
            },
            &ctx(),
        )
        .unwrap();
        assert!(added.starts_with("created mem_"));

        let listed = op_context(
            &app,
            ContextArgs {
                project: "p".to_string(),
                limit: None,
            },
        )
        .unwrap();
        let id = listed.split('\t').next().unwrap().to_string();

        let updated = op_update(
            &app,
            UpdateArgs {
                memory_id: id.clone(),
                title: Some("t2".to_string()),
                body: None,
                r#type: None,
                scope: None,
                project: None,
                topic: None,
                tags: None,
                anchors: None,
                root: None,
            },
            &ctx(),
        )
        .unwrap();
        assert!(updated.contains("\"t2\""));

        let deleted = op_delete(
            &app,
            DeleteArgs {
                memory_id: id.clone(),
            },
            &ctx(),
        )
        .unwrap();
        assert_eq!(deleted, format!("deleted {id}"));
    }

    #[test]
    fn add_rejects_bogus_scope() {
        let dir = tempfile::tempdir().unwrap();
        let app = App::open(dir.path()).unwrap();
        let err = op_add(
            &app,
            AddArgs {
                title: "t".to_string(),
                body: "b".to_string(),
                r#type: None,
                scope: Some("bogus".to_string()),
                project: None,
                topic: None,
                tags: vec![],
                anchors: vec![],
                root: Some(dir.path().to_string_lossy().to_string()),
            },
            &ctx(),
        );
        assert!(err.is_err());
    }

    #[test]
    fn relate_then_conflicts_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        let app = App::open(dir.path()).unwrap();
        let root = dir.path().to_string_lossy().to_string();

        let mk = |title: &str| {
            let out = op_add(
                &app,
                AddArgs {
                    title: title.to_string(),
                    body: "b".to_string(),
                    r#type: None,
                    scope: None,
                    project: Some("p".to_string()),
                    topic: None,
                    tags: vec![],
                    anchors: vec![],
                    root: Some(root.clone()),
                },
                &ctx(),
            )
            .unwrap();
            // "created mem_XXX \"...\"" → extract the id token.
            out.split_whitespace().nth(1).unwrap().to_string()
        };
        let a = mk("payments via stripe");
        let b = mk("payments via paypal");

        let r = op_relate(
            &app,
            RelateArgs {
                from_id: a.clone(),
                to_id: b.clone(),
                relation: "conflicts_with".to_string(),
                confidence: None,
                reason: Some("different processor".to_string()),
            },
            &ctx(),
        )
        .unwrap();
        assert!(r.contains("related"));

        let listed = op_conflicts(
            &app,
            ConflictsArgs {
                memory_id: a.clone(),
            },
        )
        .unwrap();
        assert!(listed.contains("conflicts_with"));
        assert!(listed.contains(&b));
    }

    // Add a memory in project "p" and return its id.
    fn add_mem(app: &App, root: &str, title: &str) -> String {
        let out = op_add(
            app,
            AddArgs {
                title: title.to_string(),
                body: "b".to_string(),
                r#type: None,
                scope: None,
                project: Some("p".to_string()),
                topic: None,
                tags: vec![],
                anchors: vec![],
                root: Some(root.to_string()),
            },
            &ctx(),
        )
        .unwrap();
        out.split_whitespace().nth(1).unwrap().to_string()
    }

    #[test]
    fn search_annotates_supersedes_both_directions() {
        let dir = tempfile::tempdir().unwrap();
        let app = App::open(dir.path()).unwrap();
        let root = dir.path().to_string_lossy().to_string();

        let a = add_mem(&app, &root, "payments via stripe");
        let b = add_mem(&app, &root, "payments via paypal");
        op_relate(
            &app,
            RelateArgs {
                from_id: a.clone(),
                to_id: b.clone(),
                relation: "supersedes".to_string(),
                confidence: None,
                reason: None,
            },
            &ctx(),
        )
        .unwrap();

        let results = op_search(
            &app,
            SearchArgs {
                query: "payments".to_string(),
                ..Default::default()
            },
        )
        .unwrap();
        // A is the source → "supersedes <b>"; B is the target → "superseded_by <a>".
        assert!(
            results.contains("supersedes"),
            "search must annotate source"
        );
        assert!(
            results.contains("superseded_by"),
            "search must annotate target"
        );
    }

    #[test]
    fn add_surfaces_candidate_for_similar_title() {
        let dir = tempfile::tempdir().unwrap();
        let app = App::open(dir.path()).unwrap();
        let root = dir.path().to_string_lossy().to_string();

        add_mem(&app, &root, "payments via stripe");
        // Second add with an overlapping title in the same project surfaces the first.
        let out = op_add(
            &app,
            AddArgs {
                title: "payments via paypal".to_string(),
                body: "b".to_string(),
                r#type: None,
                scope: None,
                project: Some("p".to_string()),
                topic: None,
                tags: vec![],
                anchors: vec![],
                root: Some(root.clone()),
            },
            &ctx(),
        )
        .unwrap();
        assert!(
            out.contains("judgment_required"),
            "op_add must surface candidates: {out}"
        );
    }

    fn add_args(project: &str, topic: Option<&str>, root: &str) -> AddArgs {
        AddArgs {
            title: "t".to_string(),
            body: "b".to_string(),
            r#type: None,
            scope: None,
            project: Some(project.to_string()),
            topic: topic.map(str::to_string),
            tags: vec![],
            anchors: vec![],
            root: Some(root.to_string()),
        }
    }

    #[test]
    fn add_warns_when_explicit_project_not_canonical() {
        let dir = tempfile::tempdir().unwrap();
        let app = App::open(dir.path()).unwrap();
        let root = dir.path().to_string_lossy().to_string();
        let out = op_add(&app, add_args("My-Repo", None, &root), &ctx()).unwrap();
        assert!(out.contains("warning:"), "expected warning: {out}");
        assert!(
            out.contains("my-repo"),
            "warning must show canonical form: {out}"
        );
    }

    #[test]
    fn add_no_warning_when_project_already_canonical() {
        let dir = tempfile::tempdir().unwrap();
        let app = App::open(dir.path()).unwrap();
        let root = dir.path().to_string_lossy().to_string();
        let out = op_add(&app, add_args("my-repo", None, &root), &ctx()).unwrap();
        assert!(!out.contains("warning:"), "no warning expected: {out}");
    }

    #[test]
    fn add_variants_dedup_to_one_bucket_via_mcp() {
        let dir = tempfile::tempdir().unwrap();
        let app = App::open(dir.path()).unwrap();
        let root = dir.path().to_string_lossy().to_string();
        op_add(&app, add_args("My-Repo", Some("k"), &root), &ctx()).unwrap();
        op_add(&app, add_args("my-repo", Some("k"), &root), &ctx()).unwrap();
        // Both variants canonicalize to one bucket → exactly one memory under "my-repo".
        let listed = op_context(
            &app,
            ContextArgs {
                project: "my-repo".to_string(),
                limit: None,
            },
        )
        .unwrap();
        assert_eq!(listed.lines().count(), 1, "variants must dedup: {listed}");
    }
}