add_long_term_memory
Store persistent memories with custom JavaScript triggers that activate based on conversation context, keywords, or specific conditions to provide relevant information when needed.
Instructions
Add a new long-term memory with a trigger condition. The trigger is JavaScript code that determines when this memory should be activated. Available context: context.messages (array), context.conversation_id (string), context.participants (object). Available functions: match_keys(messages, keywords, scope, depth), match_keys_all(messages, keywords, scope, depth).
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| name | Yes | ||
| prompt | Yes | ||
| trigger | Yes | ||
| createdContext | No | ||
| recentMessages | No |
Implementation Reference
- src/tools/long-term-tools.js:32-66 (handler)The async handler function that implements the core logic of the add_long_term_memory tool. It auto-generates createdContext if recentMessages are provided, adds the memory using memoryManager.addMemory, persists changes via storageManager.saveLongTermMemories, and returns a success or error response.handler: async (args) => { try { // Auto-generate context if messages provided let createdContext = args.createdContext || ''; if (!createdContext && args.recentMessages) { createdContext = createContextSnapshot(args.recentMessages, 4); } const result = await memoryManager.addMemory({ name: args.name, prompt: args.prompt, trigger: args.trigger, createdContext }); if (result.success) { await storageManager.saveLongTermMemories(memoryManager.getMemories()); return { success: true, message: `Memory "${args.name}" added successfully`, totalMemories: memoryManager.getMemories().length }; } else { return { success: false, error: result.error }; } } catch (error) { return { success: false, error: error.message }; } }
- src/tools/long-term-tools.js:21-31 (schema)Zod inputSchema defining the parameters for the add_long_term_memory tool: name, prompt, trigger, optional conversation_id, createdContext, and recentMessages.inputSchema: z.object({ name: z.string().describe('Unique name for the memory'), prompt: z.string().describe('The memory content to be recalled when triggered'), trigger: z.string().describe('JavaScript code that returns true/false to determine if memory should activate. Example: "match_keys(context.messages, [\'birthday\'], \'any\') || new Date().getMonth() === 6"'), conversation_id: z.string().optional().describe('Conversation ID that owns this memory (defaults to "default")'), createdContext: z.string().optional().describe('Optional context about when/why this memory was created'), recentMessages: z.array(z.object({ role: z.enum(['user', 'assistant', 'system']), content: z.string() })).optional().describe('Optional recent messages to auto-generate createdContext') }),
- src/index.js:156-158 (registration)Initial registration of all long-term tools, including add_long_term_memory, to the MCP server's tool registry using default managers.// 注册所有长期记忆工具 const longTermTools = createLongTermTools(defaultLongTermManager, defaultStorageManager); longTermTools.forEach(tool => registerTool(tool, 'long-term'));
- src/index.js:291-295 (registration)Dynamic recreation of long-term tools with conversation-specific managers and invocation of the add_long_term_memory handler during tool calls.manager = await getLongTermManager(conversationId); storage = getStorageManager(conversationId); const tools = createLongTermTools(manager, storage); const tool = tools.find(t => t.name === toolName); result = await withTimeout(tool.handler(validatedArgs), timeout, `Tool ${toolName} timeout`);
- src/index.js:24-24 (helper)Import of createLongTermTools function used to define and register the long-term memory tools.import { createLongTermTools } from './tools/long-term-tools.js';