Skip to main content

agentic_memory_mcp/tools/
memory_workspace_add.rs

1//! Tool: memory_workspace_add — Add an .amem file to a workspace.
2
3use std::sync::Arc;
4use tokio::sync::Mutex;
5
6use serde::Deserialize;
7use serde_json::{json, Value};
8
9use crate::session::workspace::ContextRole;
10use crate::session::SessionManager;
11use crate::types::{McpError, McpResult, ToolCallResult, ToolDefinition};
12
13#[derive(Debug, Deserialize)]
14struct AddParams {
15    workspace_id: String,
16    path: String,
17    #[serde(default = "default_role")]
18    role: String,
19    label: Option<String>,
20}
21
22fn default_role() -> String {
23    "primary".to_string()
24}
25
26/// Return the tool definition for memory_workspace_add.
27pub fn definition() -> ToolDefinition {
28    ToolDefinition {
29        name: "memory_workspace_add".to_string(),
30        description: Some(
31            "Add an .amem memory file to a workspace. Each file becomes a context \
32             that can be queried alongside other loaded memories."
33                .to_string(),
34        ),
35        input_schema: json!({
36            "type": "object",
37            "required": ["workspace_id", "path"],
38            "properties": {
39                "workspace_id": {
40                    "type": "string",
41                    "description": "ID of the workspace to add to"
42                },
43                "path": {
44                    "type": "string",
45                    "description": "Path to the .amem file"
46                },
47                "role": {
48                    "type": "string",
49                    "enum": ["primary", "secondary", "reference", "archive"],
50                    "default": "primary",
51                    "description": "Role of this context in the workspace"
52                },
53                "label": {
54                    "type": "string",
55                    "description": "Optional human-readable label for this context"
56                }
57            }
58        }),
59    }
60}
61
62/// Execute the memory_workspace_add tool.
63pub async fn execute(
64    args: Value,
65    session: &Arc<Mutex<SessionManager>>,
66) -> McpResult<ToolCallResult> {
67    let params: AddParams =
68        serde_json::from_value(args).map_err(|e| McpError::InvalidParams(e.to_string()))?;
69
70    let role = ContextRole::parse_str(&params.role).ok_or_else(|| {
71        McpError::InvalidParams(format!(
72            "Invalid role: {}. Use primary/secondary/reference/archive",
73            params.role
74        ))
75    })?;
76
77    let mut session = session.lock().await;
78    let ctx_id = session.workspace_manager_mut().add_context(
79        &params.workspace_id,
80        &params.path,
81        role,
82        params.label,
83    )?;
84
85    Ok(ToolCallResult::json(&json!({
86        "context_id": ctx_id,
87        "workspace_id": params.workspace_id,
88        "path": params.path,
89        "role": role.label(),
90        "status": "added"
91    })))
92}