Skip to main content

agentic_memory_mcp/tools/
memory_stats.rs

1//! Tool: memory_stats — Get statistics about the memory graph.
2
3use std::sync::Arc;
4use tokio::sync::Mutex;
5
6use serde_json::{json, Value};
7
8use agentic_memory::EventType;
9
10use crate::session::SessionManager;
11use crate::types::{McpResult, ToolCallResult, ToolDefinition};
12
13/// Return the tool definition for memory_stats.
14pub fn definition() -> ToolDefinition {
15    ToolDefinition {
16        name: "memory_stats".to_string(),
17        description: Some("Get statistics about the memory graph".to_string()),
18        input_schema: json!({
19            "type": "object",
20            "properties": {}
21        }),
22    }
23}
24
25/// Execute the memory_stats tool.
26pub async fn execute(
27    _args: Value,
28    session: &Arc<Mutex<SessionManager>>,
29) -> McpResult<ToolCallResult> {
30    let session = session.lock().await;
31    let graph = session.graph();
32
33    let type_index = graph.type_index();
34    let session_index = graph.session_index();
35
36    let type_counts = json!({
37        "fact": type_index.count(EventType::Fact),
38        "decision": type_index.count(EventType::Decision),
39        "inference": type_index.count(EventType::Inference),
40        "correction": type_index.count(EventType::Correction),
41        "skill": type_index.count(EventType::Skill),
42        "episode": type_index.count(EventType::Episode),
43    });
44
45    let file_size = std::fs::metadata(session.file_path())
46        .map(|m| m.len())
47        .unwrap_or(0);
48
49    Ok(ToolCallResult::json(&json!({
50        "node_count": graph.node_count(),
51        "edge_count": graph.edge_count(),
52        "dimension": graph.dimension(),
53        "session_count": session_index.session_count(),
54        "current_session": session.current_session_id(),
55        "type_counts": type_counts,
56        "file_size_bytes": file_size,
57        "file_path": session.file_path().display().to_string(),
58    })))
59}