Skip to main content
Glama

memory_relate

Link two entities with a typed relationship (e.g., builds, works at). Stores direction but allows bidirectional querying, with optional strength and context.

Instructions

Create a relationship between two entities (e.g., "Amin" builds "ModQuote"). Relationships are bidirectional for querying but stored with a direction.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
from_entity_idYesSource entity ID
to_entity_idYesTarget entity ID
relationship_typeYesType of relationship (e.g., "builds", "uses", "works_at", "prefers")
strengthNo
contextNoWhat established this relationship

Implementation Reference

  • The MCP tool handler for 'memory_relate'. Defines the schema with Zod (from_entity_id, to_entity_id, relationship_type, optional strength/context) and calls memoryEngine.relate() with the parameters.
    import { z } from 'zod'
    import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js'
    import type { MemoryEngine } from '../engine/memory-engine.js'
    
    export function registerMemoryRelate(server: McpServer, memoryEngine: MemoryEngine, agentId: string): void {
      server.tool(
        'memory_relate',
        'Create a relationship between two entities (e.g., "Amin" builds "ModQuote"). Relationships are bidirectional for querying but stored with a direction.',
        {
          from_entity_id: z.string().describe('Source entity ID'),
          to_entity_id: z.string().describe('Target entity ID'),
          relationship_type: z.string().describe('Type of relationship (e.g., "builds", "uses", "works_at", "prefers")'),
          strength: z.enum(['certain', 'high', 'medium', 'low']).optional(),
          context: z.string().optional().describe('What established this relationship'),
        },
        async (params) => {
          const result = await memoryEngine.relate({
            fromEntityId: params.from_entity_id,
            toEntityId: params.to_entity_id,
            relationshipType: params.relationship_type,
            agentId,
            strength: params.strength,
            context: params.context,
          })
    
          return {
            content: [{
              type: 'text' as const,
              text: JSON.stringify({
                relationship: result.relationship,
                receipt_id: result.receipt.receipt_id,
              }, null, 2),
            }],
          }
        },
      )
    }
  • Zod input schema for the memory_relate tool: from_entity_id, to_entity_id, relationship_type (required), strength (optional enum), context (optional string).
    {
      from_entity_id: z.string().describe('Source entity ID'),
      to_entity_id: z.string().describe('Target entity ID'),
      relationship_type: z.string().describe('Type of relationship (e.g., "builds", "uses", "works_at", "prefers")'),
      strength: z.enum(['certain', 'high', 'medium', 'low']).optional(),
      context: z.string().optional().describe('What established this relationship'),
    },
  • RelateParams interface used by the engine's relate() method. Contains fromEntityId, toEntityId, relationshipType, agentId, optional strength and context.
    export interface RelateParams {
      fromEntityId: string
      toEntityId: string
      relationshipType: string
      agentId: string
      strength?: ConfidenceLevel
      context?: string
    }
  • MemoryEngine.relate() method - the core logic. Creates a receipt via receiptEngine, then calls memoryStore.addRelationship() to persist the relationship, returning both the relationship and receipt.
    async relate(params: RelateParams): Promise<{ relationship: Relationship; receipt: ActionReceipt }> {
      const receipt = await this.receiptEngine.create({
        action: 'memory.relate',
        receipt_type: 'memory',
        input_hash: hashData({ from: params.fromEntityId, to: params.toEntityId, type: params.relationshipType }),
        status: 'completed',
        metadata: {
          memory: {
            memory_operation: 'observe',
            entity_id: params.fromEntityId,
            observation_id: null,
            relationship_id: null,
            scope: 'agent',
            query: null,
            results_count: null,
            confidence: params.strength ?? 'medium',
          },
        },
      })
    
      const relationship = this.memoryStore.addRelationship({
        relationship_id: `rel_${nanoid(12)}`,
        from_entity_id: params.fromEntityId,
        to_entity_id: params.toEntityId,
        relationship_type: params.relationshipType,
        strength: params.strength ?? 'medium',
        source_receipt_id: receipt.receipt_id,
        created_at: new Date().toISOString(),
        forgotten_at: null,
        metadata: params.context ? { context: params.context } : {},
      })
    
      return { relationship, receipt }
    }
  • Import of registerMemoryRelate from the memory-relate module.
    import { registerMemoryRelate } from './memory-relate.js'
  • Registration call for memory_relate tool within registerAllTools, only when memoryEngine, memoryStore, and agentId are all available.
    registerMemoryRelate(server, memoryEngine, agentId)
Behavior3/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Discloses bidirectional querying but directional storage, which is useful. No annotations provided, so description bears full burden; however, it omits side effects, permissions, or implications like idempotency or overwrites.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

Two sentences, concise and front-loaded with purpose. No redundant information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Lacks description of return value (no output schema). For a creation tool with 5 parameters, details on what is returned upon success or failure are missing. Adequate for basic understanding but not fully self-contained.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema covers 80% of parameters with descriptions. The description adds minimal parameter insight beyond the schema; the example is helpful but does not clarify optional parameters like 'strength' or 'context' beyond their schema definitions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

Clearly states the tool creates a relationship between two entities with an example ("Amin builds ModQuote"). Differentiates from siblings like memory_observe and memory_entities by focusing on relational creation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No guidance on when to use this tool versus alternatives (e.g., memory_observe for observations, memory_entities for entity creation). Lacks context about prerequisites or when not to use.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/webaesbyamin/agent-receipts'

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