Skip to main content
Glama

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
NameRequiredDescriptionDefault
itemsYesArray of nodes to add. Each needs type, label, and content.
auto_linkNoWhether to auto-create similarity edges. Default: true.

Implementation Reference

  • 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 };
    }
  • 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");
    }

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/ForLoopCodes/contextplus'

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