get_memory_relationships
Retrieve connections between stored memories to analyze how data points relate within the AGI MCP Server's persistent memory system.
Instructions
Get relationships for a specific memory
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| memory_id | Yes | UUID of the memory | |
| direction | No | Direction of relationships to retrieve | both |
| relationship_type | No | Filter by relationship type (optional) |
Implementation Reference
- src/memory-manager.js:628-675 (handler)Main handler implementation that executes the get_memory_relationships tool logic. Queries the memory_relationships table with optional filters for direction (incoming/outgoing/both) and relationship_type. Returns results sorted by strength and creation date.
async getMemoryRelationships(memoryId, direction = 'both', relationshipType = null) { try { let query = this.db .select({ id: schema.memoryRelationships.id, fromMemoryId: schema.memoryRelationships.fromMemoryId, toMemoryId: schema.memoryRelationships.toMemoryId, relationshipType: schema.memoryRelationships.relationshipType, strength: schema.memoryRelationships.strength, properties: schema.memoryRelationships.properties, createdAt: schema.memoryRelationships.createdAt, direction: sql`CASE WHEN ${schema.memoryRelationships.fromMemoryId} = ${memoryId} THEN 'outgoing' ELSE 'incoming' END`.as('direction'), relatedMemoryId: sql`CASE WHEN ${schema.memoryRelationships.fromMemoryId} = ${memoryId} THEN ${schema.memoryRelationships.toMemoryId} ELSE ${schema.memoryRelationships.fromMemoryId} END`.as('related_memory_id') }) .from(schema.memoryRelationships); if (direction === 'outgoing') { query = query.where(eq(schema.memoryRelationships.fromMemoryId, memoryId)); } else if (direction === 'incoming') { query = query.where(eq(schema.memoryRelationships.toMemoryId, memoryId)); } else { query = query.where( or( eq(schema.memoryRelationships.fromMemoryId, memoryId), eq(schema.memoryRelationships.toMemoryId, memoryId) ) ); } if (relationshipType) { query = query.where(eq(schema.memoryRelationships.relationshipType, relationshipType)); } const results = await query .orderBy(desc(schema.memoryRelationships.strength), desc(schema.memoryRelationships.createdAt)); return results; } catch (error) { console.warn('Memory relationships query failed:', error.message); return []; } } - mcp.js:610-616 (registration)Tool handler invocation in the MCP server's request router that calls the memoryManager.getMemoryRelationships method with arguments from the MCP request.
case "get_memory_relationships": const relationships = await memoryManager.getMemoryRelationships( args.memory_id, args.direction || 'both', args.relationship_type || null ); return { content: [{ type: "text", text: JSON.stringify(relationships, null, 2) }] }; - mcp.js:248-270 (registration)Tool registration in the MCP server defining the input schema for get_memory_relationships including memory_id (required), direction (enum: incoming/outgoing/both, default: both), and relationship_type (optional).
{ name: "get_memory_relationships", description: "Get relationships for a specific memory", inputSchema: { type: "object", properties: { memory_id: { type: "string", description: "UUID of the memory" }, direction: { type: "string", enum: ["incoming", "outgoing", "both"], description: "Direction of relationships to retrieve", default: "both" }, relationship_type: { type: "string", description: "Filter by relationship type (optional)" } }, required: ["memory_id"] } - src/db/schema.js:274-296 (schema)Database schema definition for memory_relationships table that stores relationships between memories, including id, fromMemoryId, toMemoryId, relationshipType, strength, properties, and createdAt fields with indexes and foreign key constraints.
export const memoryRelationships = pgTable("memory_relationships", { id: uuid().defaultRandom().primaryKey().notNull(), fromMemoryId: uuid("from_memory_id").notNull(), toMemoryId: uuid("to_memory_id").notNull(), relationshipType: text("relationship_type").notNull(), strength: doublePrecision().default(0.5), properties: jsonb(), createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).default(sql`CURRENT_TIMESTAMP`), }, (table) => [ index("memory_relationships_from_memory_id_idx").using("btree", table.fromMemoryId.asc().nullsLast().op("uuid_ops")), index("memory_relationships_to_memory_id_idx").using("btree", table.toMemoryId.asc().nullsLast().op("uuid_ops")), index("memory_relationships_type_strength_idx").using("btree", table.relationshipType.asc().nullsLast().op("text_ops"), table.strength.desc().nullsFirst().op("float8_ops")), foreignKey({ columns: [table.fromMemoryId], foreignColumns: [memories.id], name: "memory_relationships_from_memory_id_fkey" }).onDelete("cascade"), foreignKey({ columns: [table.toMemoryId], foreignColumns: [memories.id], name: "memory_relationships_to_memory_id_fkey" }).onDelete("cascade"), ]);