Skip to main content

trusty_memory/tools/
mod.rs

1//! MCP tool surface for trusty-memory.
2//!
3//! Why: Concentrates the public tool contract in one file so changes are
4//! auditable and the MCP schema stays in sync with the implementation.
5//! What: Defines `MemoryMcpServer`, `tool_definitions()` (the MCP
6//! `tools/list` payload), and the in-process tool dispatcher wired to the
7//! real `PalaceRegistry` + retrieval / KG APIs.
8//! Test: `cargo test -p trusty-memory-mcp` validates the schema and dispatch.
9//!
10//! Tools exposed:
11//! - `memory_remember(palace, text, room?, tags?)` -> drawer_id
12//! - `memory_recall(palace, query, top_k?)`        -> Vec<Drawer> (L0+L1+L2)
13//! - `memory_recall_deep(palace, query, top_k?)`   -> Vec<Drawer> (L3 deep)
14//! - `memory_list(palace, room?, tag?, limit?)`    -> Vec<Drawer>
15//! - `memory_forget(palace, drawer_id)`            -> status: deleted|not_found
16//! - `palace_create(name, description?)`           -> PalaceId
17//! - `palace_list()`                                -> Vec<PalaceId>
18//! - `palace_info(palace)`                          -> palace metadata + stats
19//! - `room_list(palace)`                            -> Vec<RoomSummary>
20//! - `room_create(palace, label, description?)`     -> room_id (idempotent)
21//! - `room_rename(palace, room, new_label)`         -> renamed room
22//! - `kg_assert(palace, subject, predicate, object, confidence?, provenance?)` -> ()
23//! - `kg_retract_triple(palace, subject, predicate, object)` -> closed count
24//! - `kg_query(palace, subject)`                    -> Vec<Triple>
25//! - `kg_list_subjects(palace, limit?, with_counts?)` -> subjects (#4776)
26//! - `wing_list(palace)`                            -> Vec<WingSummary>
27//! - `wing_create(palace, label)`                   -> wing_id (idempotent)
28//! - `wing_rename(palace, wing, new_label)`         -> WingSummary
29
30pub mod bm25;
31pub mod chat_definitions;
32pub mod chat_ops;
33pub mod definitions;
34pub mod dream_ops;
35pub mod helpers;
36pub mod kg_ops;
37pub mod memory_ops;
38pub mod palace_ops;
39pub mod room_definitions;
40pub mod room_ops;
41pub mod task_definitions;
42pub mod task_ops;
43// ADR-0027 T9 (#4809): the wing surface ships WITH the wing entity — a level
44// nobody reads is the defect the ADR exists to correct.
45pub mod wing_definitions;
46pub mod wing_ops;
47
48// Re-export the public + cross-module surface so external call sites
49// (`crate::tools::X`) and the `super::*` glob in `tools::tests` keep
50// resolving exactly as they did against the former monolithic module.
51pub use bm25::{spawn_bm25_index_worker, Bm25IndexRequest, BM25_INDEX_QUEUE_CAPACITY};
52pub use definitions::{tool_definitions, tool_definitions_with, MemoryMcpServer};
53pub(crate) use helpers::{auto_extract_and_assert, room_label};
54
55// Re-exports used only by the in-crate test module (`super::*`).
56#[cfg(test)]
57pub(crate) use bm25::{bm25_hits_to_recall_results, bm25_index_enqueue};
58#[cfg(test)]
59pub(crate) use helpers::{blocklist_gate, content_gate, dedup_gate, open_palace_handle};
60
61use crate::AppState;
62use anyhow::Result;
63use serde_json::Value;
64
65use chat_ops::{
66    handle_chat_session_add_turn, handle_chat_session_create, handle_chat_session_delete,
67    handle_chat_session_get, handle_chat_session_list, handle_chat_session_recall,
68    handle_chat_turn_append,
69};
70use dream_ops::{handle_dream_consolidate_room, handle_palace_dream};
71use kg_ops::{
72    handle_add_alias, handle_discover_aliases, handle_get_prompt_context, handle_kg_assert,
73    handle_kg_bootstrap, handle_kg_gaps, handle_kg_list_subjects, handle_kg_query,
74    handle_kg_retract_triple, handle_list_prompt_facts, handle_remove_prompt_fact,
75    handle_upgrade_tool,
76};
77use memory_ops::{
78    handle_memory_forget, handle_memory_list, handle_memory_note, handle_memory_recall,
79    handle_memory_recall_all, handle_memory_recall_deep, handle_memory_remember,
80    handle_memory_send_message,
81};
82use palace_ops::{
83    handle_palace_compact, handle_palace_create, handle_palace_delete, handle_palace_info,
84    handle_palace_list, handle_palace_reembed, handle_palace_unalias, handle_palace_update,
85};
86use room_ops::{handle_room_create, handle_room_list, handle_room_rename};
87use task_ops::{handle_task_add, handle_task_complete, handle_task_list};
88use wing_ops::{handle_wing_create, handle_wing_list, handle_wing_rename};
89
90/// Dispatch a tool call by name to its real handler.
91///
92/// Why: Centralises the name → handler mapping; every handler now performs a
93/// real read/write against the live `PalaceRegistry` instead of returning a
94/// stub. After issue #227 the body is a thin router — every tool's logic
95/// lives in its own `handle_*` function above so the dispatcher itself is
96/// auditable at a glance.
97/// What: Returns `Ok(Value)` on success, `Err` on unknown tool / bad args /
98/// underlying failure.
99/// Test: `dispatch_palace_create_persists`, `dispatch_remember_then_recall`,
100/// `dispatch_kg_assert_then_query`, `dispatch_unknown_tool_errors`.
101pub async fn dispatch_tool(state: &AppState, name: &str, args: Value) -> Result<Value> {
102    match name {
103        "memory_remember" => handle_memory_remember(state, args).await,
104        "memory_note" => handle_memory_note(state, args).await,
105        "memory_recall" => handle_memory_recall(state, args).await,
106        "memory_recall_deep" => handle_memory_recall_deep(state, args).await,
107        "palace_create" => handle_palace_create(state, args).await,
108        "palace_list" => handle_palace_list(state, args).await,
109        "palace_delete" => handle_palace_delete(state, args).await,
110        "palace_update" => handle_palace_update(state, args).await,
111        "kg_assert" => handle_kg_assert(state, args).await,
112        // The inverse of `kg_assert`: closes one (subject, predicate, object)
113        // and leaves the pair's other objects live.
114        "kg_retract_triple" => handle_kg_retract_triple(state, args).await,
115        "add_alias" => handle_add_alias(state, args).await,
116        "list_prompt_facts" => handle_list_prompt_facts(state, args).await,
117        "remove_prompt_fact" => handle_remove_prompt_fact(state, args).await,
118        "kg_query" => handle_kg_query(state, args).await,
119        // #4776: subject discovery — the read that makes `kg_query` usable
120        // without already knowing a subject.
121        "kg_list_subjects" => handle_kg_list_subjects(state, args).await,
122        "memory_list" => handle_memory_list(state, args).await,
123        "memory_forget" => handle_memory_forget(state, args).await,
124        "palace_info" => handle_palace_info(state, args).await,
125        "palace_compact" => handle_palace_compact(state, args).await,
126        // #4906: report / repair drawers that have no vector.
127        "palace_reembed" => handle_palace_reembed(state, args).await,
128        // #5005: free drawers destroyed by a vector-id collision.
129        "palace_unalias" => handle_palace_unalias(state, args).await,
130        "kg_gaps" => handle_kg_gaps(state, args).await,
131        "memory_recall_all" => handle_memory_recall_all(state, args).await,
132        "get_prompt_context" => handle_get_prompt_context(state, args).await,
133        "discover_aliases" => handle_discover_aliases(state, args).await,
134        "kg_bootstrap" => handle_kg_bootstrap(state, args).await,
135        "memory_send_message" => handle_memory_send_message(state, args).await,
136        "upgrade" => handle_upgrade_tool(state, args).await,
137        "console_metrics" => crate::console_metrics::handle_console_metrics(state, args).await,
138        "chat_session_create" => handle_chat_session_create(state, args).await,
139        "chat_session_add_turn" => handle_chat_session_add_turn(state, args).await,
140        "chat_session_get" => handle_chat_session_get(state, args).await,
141        "chat_session_recall" => handle_chat_session_recall(state, args).await,
142        "chat_session_list" => handle_chat_session_list(state, args).await,
143        "chat_session_delete" => handle_chat_session_delete(state, args).await,
144        "chat_turn_append" => handle_chat_turn_append(state, args).await,
145        "dream_consolidate_room" => handle_dream_consolidate_room(state, args).await,
146        "palace_dream" => handle_palace_dream(state, args).await,
147        "task_add" => handle_task_add(state, args).await,
148        "task_list" => handle_task_list(state, args).await,
149        "task_complete" => handle_task_complete(state, args).await,
150        // ADR-0027 T6 (#4805): the room surface — discovery, idempotent
151        // creation, and the rename that repairs an `unresolved-*` label.
152        "room_list" => handle_room_list(state, args).await,
153        "room_create" => handle_room_create(state, args).await,
154        "room_rename" => handle_room_rename(state, args).await,
155        // ADR-0027 T9 (#4809): the wing surface — the scope axis over rooms.
156        "wing_list" => handle_wing_list(state, args).await,
157        "wing_create" => handle_wing_create(state, args).await,
158        "wing_rename" => handle_wing_rename(state, args).await,
159        other => anyhow::bail!("unknown tool: {other}"),
160    }
161}
162
163#[cfg(test)]
164mod tests;