@knowall-ai/mcp-neo4j-agent-memory
Version:
Graph-based memory system for AI agents. Store people, places, organizations as nodes. Build semantic relationships (KNOWS, WORKS_AT, CREATED). Word-tokenized search finds partial matches. Filter by date, traverse relationships, maintain temporal context.
69 lines (68 loc) • 2.29 kB
JavaScript
// Enhanced remember function with deduplication
async function enhancedRemember(args) {
const { type, content, details } = args;
// First, check if this entity already exists
const label = type.charAt(0).toUpperCase() + type.slice(1);
const checkQuery = `
MATCH (n:${label} {content: $content})
RETURN n, id(n) as id
LIMIT 1
`;
const existing = await this.neo4j.executeQuery(checkQuery, { content });
if (existing.length > 0) {
// Entity exists - update it with new details
const updateQuery = `
MATCH (n:${label} {content: $content})
SET n += $properties
SET n.updated_at = datetime()
RETURN n
`;
const properties = parseDetails(details);
const result = await this.neo4j.executeQuery(updateQuery, {
content,
properties
});
return {
action: 'updated',
node: result[0].n,
message: `Updated existing ${type}: ${content}`
};
}
else {
// Create new node
const properties = parseDetails(details);
properties.content = content;
properties.type = type;
properties.created_at = new Date().toISOString();
const result = await this.neo4j.createNode(label, properties);
return {
action: 'created',
node: result,
message: `Created new ${type}: ${content}`
};
}
}
// Alternative: Create a merge operation
async function mergeMemory(args) {
const { type, content, details } = args;
const label = type.charAt(0).toUpperCase() + type.slice(1);
const properties = parseDetails(details);
// Use MERGE to create or match existing
const mergeQuery = `
MERGE (n:${label} {content: $content})
ON CREATE SET n += $createProps, n.created_at = datetime()
ON MATCH SET n += $updateProps, n.updated_at = datetime()
RETURN n,
CASE WHEN n.created_at = n.updated_at THEN 'created' ELSE 'updated' END as action
`;
const result = await this.neo4j.executeQuery(mergeQuery, {
content,
createProps: { ...properties, type },
updateProps: properties
});
return {
action: result[0].action,
node: result[0].n
};
}
export {};