create_relation
Establish typed connections between memory nodes to build semantic relationships within codebases, supporting various relation types with weighted edges that update automatically.
Instructions
Create a typed edge between two memory nodes. Supports relation types: relates_to, depends_on, implements, references, similar_to, contains. Edges have weights (0-1) that decay over time via e^(-λt). Duplicate edges update weight instead of creating new ones.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| source_id | Yes | ID of the source memory node. | |
| target_id | Yes | ID of the target memory node. | |
| relation | Yes | Relationship type between nodes. | |
| weight | No | Edge weight 0-1. Higher = stronger relationship. Default: 1.0. | |
| metadata | No | Optional key-value metadata for the edge. |
Implementation Reference
- src/core/memory-graph.ts:155-181 (handler)The core function `createRelation` that creates or updates a relationship edge between two memory nodes in the graph storage.
export async function createRelation(rootDir: string, sourceId: string, targetId: string, relation: RelationType, weight?: number, metadata?: Record<string, string>): Promise<MemoryEdge | null> { const graph = await loadGraph(rootDir); if (!graph.nodes[sourceId] || !graph.nodes[targetId]) return null; const duplicate = Object.values(graph.edges).find(e => e.source === sourceId && e.target === targetId && e.relation === relation ); if (duplicate) { duplicate.weight = weight ?? duplicate.weight; if (metadata) Object.assign(duplicate.metadata, metadata); scheduleSave(rootDir); return duplicate; } const edge: MemoryEdge = { id: generateId("me"), source: sourceId, target: targetId, relation, weight: weight ?? 1.0, createdAt: Date.now(), metadata: metadata ?? {}, }; graph.edges[edge.id] = edge; scheduleSave(rootDir); return edge; } - src/tools/memory-tools.ts:71-82 (handler)The tool wrapper `toolCreateRelation` which exposes `createRelation` as an MCP tool, handling request mapping and providing user-friendly feedback.
export async function toolCreateRelation(options: CreateRelationOptions): Promise<string> { const edge = await createRelation(options.rootDir, options.sourceId, options.targetId, options.relation, options.weight, options.metadata); if (!edge) return `❌ Failed: one or both node IDs not found (source: ${options.sourceId}, target: ${options.targetId})`; const stats = await getGraphStats(options.rootDir); return [ `✅ Relation created: ${options.sourceId} --[${edge.relation}]--> ${options.targetId}`, ` Edge ID: ${edge.id}`, ` Weight: ${edge.weight}`, `\nGraph: ${stats.nodes} nodes, ${stats.edges} edges`, ].join("\n"); }