Skip to main content
Glama

update_relations

Modify multiple relationships in the knowledge graph by specifying source, target, and relationship type to ensure accurate and updated entity connections.

Instructions

Update multiple existing relations in the knowledge graph

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
relationsYesArray of relations to update

Implementation Reference

  • The execute handler for the 'update_relations' tool. Processes relations by checking entity existence, finding and deleting existing matching relations, adding new ones, tracking updated/created/failed, saving to memoryStore, and returning JSON results. Updates are implemented as delete + recreate.
    execute: async (args) => {
      const results = {
        updated: [] as Array<{from: string, to: string, relationType: string}>,
        created: [] as Array<{from: string, to: string, relationType: string}>,
        failed: [] as Array<{from: string, to: string, relationType: string, reason: string}>
      };
    
      // Check entities exist before attempting operations
      for (const relation of args.relations) {
        // First check if entities exist
        const fromExists = graph.entities.has(relation.from);
        const toExists = graph.entities.has(relation.to);
        
        if (!fromExists || !toExists) {
          let reason = "Unknown error";
          if (!fromExists) {
            reason = `Source entity '${relation.from}' doesn't exist`;
          } else if (!toExists) {
            reason = `Target entity '${relation.to}' doesn't exist`;
          }
          
          results.failed.push({...relation, reason});
          continue;
        }
        
        // Check if relationship already exists
        // Flatten the relations into a single array of Relation objects
        const allRelations: Relation[] = [];
        graph.relations.forEach(relations => {
          relations.forEach(relation => allRelations.push(relation));
        });
        
        const existingRelation = allRelations.find(rel => 
          rel.from === relation.from && 
          rel.to === relation.to && 
          rel.relationType === relation.relationType
        );
        
        // Proceed with update (which is delete + recreate)
        const deleted = existingRelation ? graph.deleteRelation(relation) : false;
        const added = graph.addRelation(relation);
        
        if (!added) {
          // If addition failed for some reason
          results.failed.push({...relation, reason: "Failed to create relation"});
        } else if (deleted) {
          // If we deleted an existing relation and added a new one, consider it updated
          results.updated.push(relation);
        } else {
          // If we just added (didn't delete first), it was a creation
          results.created.push(relation);
        }
      }
    
      // Save changes
      await memoryStore.save();
    
      // Return as string with clarified message about behavior
      return JSON.stringify({
        updated: results.updated.length > 0 ? results.updated : null,
        created: results.created.length > 0 ? results.created : null,
        failed: results.failed.length > 0 ? results.failed : null,
        message: `Updated ${results.updated.length} relations (by recreating them). Created ${results.created.length} new relations. Failed for ${results.failed.length} relations.`,
        note: "Relations are updated by removing and recreating them, rather than modifying in place."
      });
    }
  • Registration of the 'update_relations' tool using server.addTool(), specifying name, description, parameters schema (UpdateRelationsSchema), and inline execute handler.
    server.addTool({
      name: 'update_relations',
      description: 'Update multiple existing relations in the knowledge graph',
      parameters: Schemas.UpdateRelationsSchema,
      execute: async (args) => {
        const results = {
          updated: [] as Array<{from: string, to: string, relationType: string}>,
          created: [] as Array<{from: string, to: string, relationType: string}>,
          failed: [] as Array<{from: string, to: string, relationType: string, reason: string}>
        };
    
        // Check entities exist before attempting operations
        for (const relation of args.relations) {
          // First check if entities exist
          const fromExists = graph.entities.has(relation.from);
          const toExists = graph.entities.has(relation.to);
          
          if (!fromExists || !toExists) {
            let reason = "Unknown error";
            if (!fromExists) {
              reason = `Source entity '${relation.from}' doesn't exist`;
            } else if (!toExists) {
              reason = `Target entity '${relation.to}' doesn't exist`;
            }
            
            results.failed.push({...relation, reason});
            continue;
          }
          
          // Check if relationship already exists
          // Flatten the relations into a single array of Relation objects
          const allRelations: Relation[] = [];
          graph.relations.forEach(relations => {
            relations.forEach(relation => allRelations.push(relation));
          });
          
          const existingRelation = allRelations.find(rel => 
            rel.from === relation.from && 
            rel.to === relation.to && 
            rel.relationType === relation.relationType
          );
          
          // Proceed with update (which is delete + recreate)
          const deleted = existingRelation ? graph.deleteRelation(relation) : false;
          const added = graph.addRelation(relation);
          
          if (!added) {
            // If addition failed for some reason
            results.failed.push({...relation, reason: "Failed to create relation"});
          } else if (deleted) {
            // If we deleted an existing relation and added a new one, consider it updated
            results.updated.push(relation);
          } else {
            // If we just added (didn't delete first), it was a creation
            results.created.push(relation);
          }
        }
    
        // Save changes
        await memoryStore.save();
    
        // Return as string with clarified message about behavior
        return JSON.stringify({
          updated: results.updated.length > 0 ? results.updated : null,
          created: results.created.length > 0 ? results.created : null,
          failed: results.failed.length > 0 ? results.failed : null,
          message: `Updated ${results.updated.length} relations (by recreating them). Created ${results.created.length} new relations. Failed for ${results.failed.length} relations.`,
          note: "Relations are updated by removing and recreating them, rather than modifying in place."
        });
      }
    });
  • Zod input validation schema for 'update_relations' tool parameters. Expects an object with a 'relations' array, where each relation uses the shared RelationSchema (from: string, to: string, relationType: string).
    export const UpdateRelationsSchema = z.object({
      relations: z.array(RelationSchema).describe('Array of relations to update')
    }); 
Behavior2/5

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

No annotations are provided, so the description carries full burden. It states 'update' implying mutation, but lacks critical behavioral details: what permissions are required, whether changes are reversible, how errors are handled (e.g., if a relation doesn't exist), or rate limits. For a mutation tool with zero annotation coverage, this leaves significant gaps in understanding its operation and risks.

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?

The description is a single, efficient sentence that front-loads key information ('update multiple existing relations') without unnecessary words. Every part earns its place by specifying the action, scope, and resource concisely, making it easy to parse quickly.

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

Completeness2/5

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

Given the complexity (a mutation tool for a knowledge graph), lack of annotations, and no output schema, the description is incomplete. It doesn't cover behavioral aspects like error handling, permissions, or return values, leaving the agent with insufficient context to use it safely and effectively. More detail is needed to compensate for the missing structured data.

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 description coverage is 100%, with the 'relations' parameter fully documented in the schema (including nested 'from', 'to', 'relationType' fields). The description adds no additional meaning beyond implying batch updates via 'multiple', which is already clear from the array type in the schema. Baseline 3 is appropriate as the schema does the heavy lifting.

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

Purpose4/5

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

The description clearly states the action ('update') and resource ('multiple existing relations in the knowledge graph'), making the purpose immediately understandable. It distinguishes from siblings like 'create_relations' (for new relations) and 'delete_relations' (for removal), but doesn't explicitly contrast with 'upsert_entities' which might handle similar graph modifications. The specificity is good but could be slightly more comparative.

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?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention prerequisites (e.g., relations must exist), contrast with 'create_relations' for new relations or 'upsert_entities' for entity-level updates, or specify scenarios like batch updates. Without such context, an agent might misuse it or overlook better options.

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

Related 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/flight505/mcp-think-tank'

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