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

NameRequiredDescriptionDefault
relationsYesArray of relations to update

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "relations": { "description": "Array of relations to update", "items": { "additionalProperties": false, "properties": { "from": { "description": "Source entity name", "minLength": 1, "type": "string" }, "relationType": { "description": "Type of relationship (in active voice)", "minLength": 1, "type": "string" }, "to": { "description": "Target entity name", "minLength": 1, "type": "string" } }, "required": [ "from", "to", "relationType" ], "type": "object" }, "type": "array" } }, "required": [ "relations" ], "type": "object" }

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') });

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