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)`            -> ()
16//! - `palace_create(name, description?)`           -> PalaceId
17//! - `palace_list()`                                -> Vec<PalaceId>
18//! - `palace_info(palace)`                          -> palace metadata + stats
19//! - `kg_assert(palace, subject, predicate, object, confidence?, provenance?)` -> ()
20//! - `kg_query(palace, subject)`                    -> Vec<Triple>
21
22pub mod bm25;
23pub mod chat_definitions;
24pub mod chat_ops;
25pub mod definitions;
26pub mod dream_ops;
27pub mod helpers;
28pub mod kg_ops;
29pub mod memory_ops;
30pub mod palace_ops;
31pub mod task_definitions;
32pub mod task_ops;
33
34// Re-export the public + cross-module surface so external call sites
35// (`crate::tools::X`) and the `super::*` glob in `tools::tests` keep
36// resolving exactly as they did against the former monolithic module.
37pub use bm25::{spawn_bm25_index_worker, Bm25IndexRequest, BM25_INDEX_QUEUE_CAPACITY};
38pub use definitions::{tool_definitions, tool_definitions_with, MemoryMcpServer};
39pub(crate) use helpers::{auto_extract_and_assert, room_label};
40
41// Re-exports used only by the in-crate test module (`super::*`).
42#[cfg(test)]
43pub(crate) use bm25::{bm25_hits_to_recall_results, bm25_index_enqueue};
44#[cfg(test)]
45pub(crate) use helpers::{blocklist_gate, content_gate, dedup_gate, open_palace_handle};
46
47use crate::AppState;
48use anyhow::Result;
49use serde_json::Value;
50
51use chat_ops::{
52    handle_chat_session_add_turn, handle_chat_session_create, handle_chat_session_delete,
53    handle_chat_session_get, handle_chat_session_list, handle_chat_session_recall,
54    handle_chat_turn_append,
55};
56use dream_ops::{handle_dream_consolidate_room, handle_palace_dream};
57use kg_ops::{
58    handle_add_alias, handle_discover_aliases, handle_get_prompt_context, handle_kg_assert,
59    handle_kg_bootstrap, handle_kg_gaps, handle_kg_query, handle_list_prompt_facts,
60    handle_remove_prompt_fact, handle_upgrade_tool,
61};
62use memory_ops::{
63    handle_memory_forget, handle_memory_list, handle_memory_note, handle_memory_recall,
64    handle_memory_recall_all, handle_memory_recall_deep, handle_memory_remember,
65    handle_memory_send_message,
66};
67use palace_ops::{
68    handle_palace_compact, handle_palace_create, handle_palace_delete, handle_palace_info,
69    handle_palace_list, handle_palace_update,
70};
71use task_ops::{handle_task_add, handle_task_complete, handle_task_list};
72
73/// Dispatch a tool call by name to its real handler.
74///
75/// Why: Centralises the name → handler mapping; every handler now performs a
76/// real read/write against the live `PalaceRegistry` instead of returning a
77/// stub. After issue #227 the body is a thin router — every tool's logic
78/// lives in its own `handle_*` function above so the dispatcher itself is
79/// auditable at a glance.
80/// What: Returns `Ok(Value)` on success, `Err` on unknown tool / bad args /
81/// underlying failure.
82/// Test: `dispatch_palace_create_persists`, `dispatch_remember_then_recall`,
83/// `dispatch_kg_assert_then_query`, `dispatch_unknown_tool_errors`.
84pub async fn dispatch_tool(state: &AppState, name: &str, args: Value) -> Result<Value> {
85    match name {
86        "memory_remember" => handle_memory_remember(state, args).await,
87        "memory_note" => handle_memory_note(state, args).await,
88        "memory_recall" => handle_memory_recall(state, args).await,
89        "memory_recall_deep" => handle_memory_recall_deep(state, args).await,
90        "palace_create" => handle_palace_create(state, args).await,
91        "palace_list" => handle_palace_list(state, args).await,
92        "palace_delete" => handle_palace_delete(state, args).await,
93        "palace_update" => handle_palace_update(state, args).await,
94        "kg_assert" => handle_kg_assert(state, args).await,
95        "add_alias" => handle_add_alias(state, args).await,
96        "list_prompt_facts" => handle_list_prompt_facts(state, args).await,
97        "remove_prompt_fact" => handle_remove_prompt_fact(state, args).await,
98        "kg_query" => handle_kg_query(state, args).await,
99        "memory_list" => handle_memory_list(state, args).await,
100        "memory_forget" => handle_memory_forget(state, args).await,
101        "palace_info" => handle_palace_info(state, args).await,
102        "palace_compact" => handle_palace_compact(state, args).await,
103        "kg_gaps" => handle_kg_gaps(state, args).await,
104        "memory_recall_all" => handle_memory_recall_all(state, args).await,
105        "get_prompt_context" => handle_get_prompt_context(state, args).await,
106        "discover_aliases" => handle_discover_aliases(state, args).await,
107        "kg_bootstrap" => handle_kg_bootstrap(state, args).await,
108        "memory_send_message" => handle_memory_send_message(state, args).await,
109        "upgrade" => handle_upgrade_tool(state, args).await,
110        "console_metrics" => crate::console_metrics::handle_console_metrics(state, args).await,
111        "chat_session_create" => handle_chat_session_create(state, args).await,
112        "chat_session_add_turn" => handle_chat_session_add_turn(state, args).await,
113        "chat_session_get" => handle_chat_session_get(state, args).await,
114        "chat_session_recall" => handle_chat_session_recall(state, args).await,
115        "chat_session_list" => handle_chat_session_list(state, args).await,
116        "chat_session_delete" => handle_chat_session_delete(state, args).await,
117        "chat_turn_append" => handle_chat_turn_append(state, args).await,
118        "dream_consolidate_room" => handle_dream_consolidate_room(state, args).await,
119        "palace_dream" => handle_palace_dream(state, args).await,
120        "task_add" => handle_task_add(state, args).await,
121        "task_list" => handle_task_list(state, args).await,
122        "task_complete" => handle_task_complete(state, args).await,
123        other => anyhow::bail!("unknown tool: {other}"),
124    }
125}
126
127#[cfg(test)]
128mod tests;