create_entities
Add multiple entities to a knowledge graph by specifying names, types, and associated observations for enhanced data organization and retrieval.
Instructions
Create multiple new entities in the knowledge graph
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| entities | Yes | An array of entities to create |
Implementation Reference
- src/manager.ts:197-254 (handler)Core handler function in KnowledgeGraphManager that creates new entities and their observations in the DuckDB database, avoiding duplicates and updating search index.async createEntities(entities: Entity[]): Promise<Entity[]> { const createdEntities: Entity[] = []; using conn = await this.getConn(); try { // Begin transaction await conn.execute("BEGIN TRANSACTION"); // Get existing entity names const existingEntitiesReader = await conn.executeAndReadAll( "SELECT name FROM entities" ); const existingEntitiesData = existingEntitiesReader.getRows(); const nameColumnIndex = 0; // name column is the first column const existingNames = new Set( existingEntitiesData.map((row: any) => row[nameColumnIndex] as string) ); // Filter new entities const newEntities = entities.filter( (entity) => !existingNames.has(entity.name) ); // Insert new entities for (const entity of newEntities) { // Insert entity await conn.execute( "INSERT INTO entities (name, entityType) VALUES (?, ?)", [entity.name, entity.entityType] ); // Insert observations for (const observation of entity.observations) { await conn.execute( "INSERT INTO observations (entityName, content) VALUES (?, ?)", [entity.name, observation] ); } createdEntities.push(entity); } // Commit transaction await conn.execute("COMMIT"); // Update Fuse.js index const allEntities = await this.getAllEntities(); this.fuse.setCollection(allEntities); return createdEntities; } catch (error: unknown) { // Rollback in case of error await conn.execute("ROLLBACK"); this.logger.error("Error creating entities", extractError(error)); throw error; } }
- src/server.ts:19-40 (registration)MCP tool registration and thin handler wrapper that validates input with Zod schema and delegates to KnowledgeGraphManager.createEntities, returning JSON response.// Create entities tool server.tool( "create_entities", "Create multiple new entities in the knowledge graph", { entities: z .array(EntityObject) .describe("An array of entities to create"), }, async ({ entities }) => ({ content: [ { type: "text", text: JSON.stringify( await knowledgeGraphManager.createEntities(entities), null, 2 ), }, ], }) );
- src/types.ts:6-13 (schema)Zod schema definition for Entity type used in create_entities tool input.export const EntityObject = z.object({ name: z.string().describe("The name of the entity"), entityType: z.string().describe("The type of the entity"), observations: z .array(z.string()) .describe("An array of observation contents associated with the entity"), }); export type Entity = z.infer<typeof EntityObject>;
- src/types.ts:55-56 (schema)TypeScript interface defining the createEntities method signature.export type KnowledgeGraphManagerInterface = { createEntities(entities: Entity[]): Promise<Entity[]>;