add_interlinked_context
Add multiple memory nodes with automatic similarity linking. Computes embeddings and creates connections between related concepts, files, or notes for semantic intelligence.
Instructions
Bulk-add multiple memory nodes with automatic similarity linking. Computes embeddings for all items, then creates similarity edges between any pair (new-to-new and new-to-existing) with cosine similarity ≥ 0.72. Ideal for importing related concepts, files, or notes at once.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| items | Yes | Array of nodes to add. Each needs type, label, and content. | |
| auto_link | No | Whether to auto-create similarity edges. Default: true. |
Implementation Reference
- src/core/memory-graph.ts:273-310 (handler)The core implementation of the logic to add nodes and interlink them based on similarity.
export async function addInterlinkedContext(rootDir: string, items: Array<{ type: NodeType; label: string; content: string; metadata?: Record<string, string> }>, autoLink: boolean = true): Promise<{ nodes: MemoryNode[]; edges: MemoryEdge[] }> { const createdNodes: MemoryNode[] = []; for (const item of items) { createdNodes.push(await upsertNode(rootDir, item.type, item.label, item.content, item.metadata)); } const createdEdges: MemoryEdge[] = []; if (autoLink && createdNodes.length > 1) { for (let i = 0; i < createdNodes.length; i++) { for (let j = i + 1; j < createdNodes.length; j++) { const similarity = cosine(createdNodes[i].embedding, createdNodes[j].embedding); if (similarity >= SIMILARITY_THRESHOLD) { const edge = await createRelation(rootDir, createdNodes[i].id, createdNodes[j].id, "similar_to", similarity); if (edge) createdEdges.push(edge); } } } } const graph = await loadGraph(rootDir); const existingNodes = Object.values(graph.nodes) .filter(n => !createdNodes.find(cn => cn.id === n.id)) .slice(0, 200); if (autoLink) { for (const newNode of createdNodes) { for (const existing of existingNodes) { const similarity = cosine(newNode.embedding, existing.embedding); if (similarity >= SIMILARITY_THRESHOLD) { const edge = await createRelation(rootDir, newNode.id, existing.id, "similar_to", similarity); if (edge) createdEdges.push(edge); } } } } return { nodes: createdNodes, edges: createdEdges }; } - src/tools/memory-tools.ts:110-132 (helper)The MCP tool handler that wraps the core logic and formats the output string.
export async function toolAddInterlinkedContext(options: AddInterlinkedContextOptions): Promise<string> { const result = await addInterlinkedContext(options.rootDir, options.items, options.autoLink); const sections = [ `✅ Added ${result.nodes.length} interlinked nodes`, result.edges.length > 0 ? ` Auto-linked: ${result.edges.length} similarity edges (threshold ≥ 0.72)` : " No auto-links above threshold", "\nNodes:", ]; for (const node of result.nodes) { sections.push(` [${node.type}] ${node.label} → ${node.id}`); } if (result.edges.length > 0) { sections.push("\nEdges:"); for (const edge of result.edges) { sections.push(` ${edge.source} --[${edge.relation} w:${Math.round(edge.weight * 100) / 100}]--> ${edge.target}`); } } const stats = await getGraphStats(options.rootDir); sections.push(`\nGraph total: ${stats.nodes} nodes, ${stats.edges} edges`); return sections.join("\n"); }