Skip to main content
Glama

add_long_term_memory

Store persistent memories that automatically activate when specific conversation conditions are met, using JavaScript triggers to determine relevance.

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
NameRequiredDescriptionDefault
nameYesUnique name for the memory
promptYesThe memory content to be recalled when triggered
triggerYesJavaScript code that returns true/false to determine if memory should activate. Example: "match_keys(context.messages, ['birthday'], 'any') || new Date().getMonth() === 6"
conversation_idNoConversation ID that owns this memory (defaults to "default")
createdContextNoOptional context about when/why this memory was created
recentMessagesNoOptional recent messages to auto-generate createdContext

Implementation Reference

  • The main handler function for the 'add_long_term_memory' tool. It processes input arguments, auto-generates context if recentMessages provided, adds the memory via memoryManager.addMemory, saves to storage, and returns success/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
        };
      }
    }
  • Zod inputSchema defining parameters for the tool: name, prompt, trigger, conversation_id (opt), createdContext (opt), recentMessages (opt).
    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)
    Static registration of default long-term tools (including add_long_term_memory) to the toolRegistry for list_tools response.
    // 注册所有长期记忆工具
    const longTermTools = createLongTermTools(defaultLongTermManager, defaultStorageManager);
    longTermTools.forEach(tool => registerTool(tool, 'long-term'));
  • src/index.js:291-295 (registration)
    Dynamic registration/execution: recreates long-term tools with conversation-specific managers and calls the handler for 'add_long_term_memory' during tool invocation.
    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`);

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/win10ogod/memory-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server