Skip to main content
Glama

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
NameRequiredDescriptionDefault
source_idYesID of the source memory node.
target_idYesID of the target memory node.
relationYesRelationship type between nodes.
weightNoEdge weight 0-1. Higher = stronger relationship. Default: 1.0.
metadataNoOptional key-value metadata for the edge.

Implementation Reference

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

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