Skip to main content
Glama

find_related_memories

Traverse memory graphs to discover related memories by exploring connections from a starting point, with configurable depth and relationship strength parameters.

Instructions

Find memories related through graph traversal

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
memory_idYesUUID of the starting memory
max_depthNoMaximum depth to traverse
min_strengthNoMinimum relationship strength

Implementation Reference

  • mcp.js:618-624 (handler)
    Tool handler that executes find_related_memories - extracts args.memory_id, args.max_depth (default 2), and args.min_strength (default 0.3), then calls memoryManager.findRelatedMemories() and returns JSON stringified results
    case "find_related_memories":
      const relatedMemories = await memoryManager.findRelatedMemories(
        args.memory_id,
        args.max_depth || 2,
        args.min_strength || 0.3
      );
      return { content: [{ type: "text", text: JSON.stringify(relatedMemories, null, 2) }] };
  • Core implementation using recursive CTE to traverse memory relationship graph up to maxDepth, filtering by minimum strength and avoiding cycles. Returns related memories with content, type, importance ordered by strength descending and depth ascending.
    async findRelatedMemories(memoryId, maxDepth = 2, minStrength = 0.3) {
      try {
        // Use recursive CTE to find related memories up to maxDepth
        const results = await this.db.execute(sql`
          WITH RECURSIVE memory_graph AS (
            -- Base case: direct relationships
            SELECT 
              mr.to_memory_id as memory_id,
              mr.relationship_type,
              mr.strength,
              1 as depth,
              ARRAY[mr.from_memory_id] as path
            FROM memory_relationships mr
            WHERE mr.from_memory_id = ${memoryId} AND mr.strength >= ${minStrength}
            
            UNION ALL
            
            -- Recursive case: follow relationships
            SELECT 
              mr.to_memory_id as memory_id,
              mr.relationship_type,
              mr.strength * mg.strength as strength,
              mg.depth + 1,
              mg.path || mr.from_memory_id
            FROM memory_relationships mr
            JOIN memory_graph mg ON mr.from_memory_id = mg.memory_id
            WHERE mg.depth < ${maxDepth}
              AND mr.strength >= ${minStrength}
              AND NOT (mr.to_memory_id = ANY(mg.path))
          )
          SELECT 
            mg.*,
            m.content,
            m.type,
            m.importance
          FROM memory_graph mg
          JOIN memories m ON mg.memory_id = m.id
          WHERE m.status = 'active'
          ORDER BY mg.strength DESC, mg.depth ASC
        `);
        
        return results.rows || [];
      } catch (error) {
        console.warn('Related memories query failed:', error.message);
        return [];
      }
    }
  • Tool schema definition defining input parameters: memory_id (required UUID string), max_depth (optional integer, default 2), min_strength (optional number, default 0.3)
    {
      name: "find_related_memories",
      description: "Find memories related through graph traversal",
      inputSchema: {
        type: "object",
        properties: {
          memory_id: {
            type: "string",
            description: "UUID of the starting memory"
          },
          max_depth: {
            type: "integer",
            description: "Maximum depth to traverse",
            default: 2
          },
          min_strength: {
            type: "number",
            description: "Minimum relationship strength",
            default: 0.3
          }
        },
        required: ["memory_id"]
      }

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/randyandrade/agi-mcp-server'

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