knowledgegraph-mcp
Version:
MCP server for enabling persistent knowledge storage for Claude through a knowledge graph with multiple storage backends
199 lines ⢠9.77 kB
JavaScript
import { KnowledgeGraphManager } from '../index.js';
import { StorageType } from '../storage/types.js';
import path from 'path';
const __dirname = path.join(process.cwd(), 'examples');
// Sample entities with tags for demonstration
const sampleEntities = [
{
name: 'React',
entityType: 'JavaScript Library',
observations: [
'Component-based architecture',
'Virtual DOM for performance',
'Developed by Meta (Facebook)',
'Declarative programming model'
],
tags: ['frontend', 'javascript', 'ui', 'popular', 'meta']
},
{
name: 'Vue.js',
entityType: 'JavaScript Framework',
observations: [
'Progressive framework',
'Easy learning curve',
'Excellent documentation',
'Template-based syntax'
],
tags: ['frontend', 'javascript', 'ui', 'progressive', 'beginner-friendly']
},
{
name: 'Django',
entityType: 'Python Framework',
observations: [
'Batteries-included philosophy',
'ORM for database operations',
'Admin interface out of the box',
'MTV (Model-Template-View) pattern'
],
tags: ['backend', 'python', 'web', 'orm', 'admin']
},
{
name: 'Express.js',
entityType: 'Node.js Framework',
observations: [
'Minimal and flexible',
'Middleware-based architecture',
'Fast and unopinionated',
'Large ecosystem'
],
tags: ['backend', 'javascript', 'nodejs', 'minimal', 'middleware']
},
{
name: 'PostgreSQL',
entityType: 'Database',
observations: [
'ACID compliant',
'Advanced SQL features',
'Extensible with custom functions',
'Strong consistency guarantees'
],
tags: ['database', 'sql', 'relational', 'acid', 'enterprise']
},
{
name: 'MongoDB',
entityType: 'Database',
observations: [
'Document-oriented storage',
'Flexible schema',
'Horizontal scaling',
'JSON-like documents'
],
tags: ['database', 'nosql', 'document', 'scalable', 'json']
}
];
const sampleRelations = [
{ from: 'React', to: 'Vue.js', relationType: 'competes_with' },
{ from: 'Express.js', to: 'Django', relationType: 'alternative_to' },
{ from: 'React', to: 'Express.js', relationType: 'often_used_with' },
{ from: 'Django', to: 'PostgreSQL', relationType: 'commonly_uses' },
{ from: 'Express.js', to: 'MongoDB', relationType: 'frequently_paired_with' }
];
async function demonstrateTagSystem() {
console.log('š·ļø Knowledge Graph MCP Server - Tag System Demonstration');
console.log('========================================================\n');
const config = {
type: StorageType.POSTGRESQL,
connectionString: 'postgresql://postgres:password@localhost:5432/knowledgegraph_tag_demo'
};
const manager = new KnowledgeGraphManager(config);
try {
// 1. Create entities with tags
console.log('1ļøā£ Creating entities with tags...');
await manager.createEntities(sampleEntities, 'tag_demo');
await manager.createRelations(sampleRelations, 'tag_demo');
console.log(`ā
Created ${sampleEntities.length} entities and ${sampleRelations.length} relations\n`);
// 2. Demonstrate tag search - exact matching
console.log('2ļøā£ Searching by tags (exact matching)...');
// Search for frontend technologies
const frontendResults = await manager.searchNodes('', { exactTags: ['frontend'], tagMatchMode: 'any' }, 'tag_demo');
console.log(`š Frontend technologies (${frontendResults.entities.length} found):`);
frontendResults.entities.forEach(entity => {
console.log(` ⢠${entity.name} (${entity.entityType}) - Tags: [${entity.tags?.join(', ') || 'none'}]`);
});
console.log();
// Search for JavaScript technologies
const jsResults = await manager.searchNodes('', { exactTags: ['javascript'], tagMatchMode: 'any' }, 'tag_demo');
console.log(`š JavaScript technologies (${jsResults.entities.length} found):`);
jsResults.entities.forEach(entity => {
console.log(` ⢠${entity.name} (${entity.entityType}) - Tags: [${entity.tags?.join(', ') || 'none'}]`);
});
console.log();
// Search for databases
const dbResults = await manager.searchNodes('', { exactTags: ['database'], tagMatchMode: 'any' }, 'tag_demo');
console.log(`š Database technologies (${dbResults.entities.length} found):`);
dbResults.entities.forEach(entity => {
console.log(` ⢠${entity.name} (${entity.entityType}) - Tags: [${entity.tags?.join(', ') || 'none'}]`);
});
console.log();
// 3. Demonstrate "all" mode search
console.log('3ļøā£ Searching with "all" mode (must have ALL specified tags)...');
const frontendJsResults = await manager.searchNodes('', { exactTags: ['frontend', 'javascript'], tagMatchMode: 'all' }, 'tag_demo');
console.log(`š Frontend + JavaScript technologies (${frontendJsResults.entities.length} found):`);
frontendJsResults.entities.forEach(entity => {
console.log(` ⢠${entity.name} - Tags: [${entity.tags?.join(', ') || 'none'}]`);
});
console.log();
// 4. Demonstrate adding tags
console.log('4ļøā£ Adding new tags to existing entities...');
const addTagsResult = await manager.addTags([
{ entityName: 'React', tags: ['hooks', 'jsx'] },
{ entityName: 'Vue.js', tags: ['composition-api', 'reactive'] },
{ entityName: 'PostgreSQL', tags: ['open-source'] }
], 'tag_demo');
addTagsResult.forEach(result => {
console.log(`ā
Added tags to ${result.entityName}: [${result.addedTags.join(', ')}]`);
});
console.log();
// 5. Show updated entities
console.log('5ļøā£ Updated entities after adding tags...');
const updatedGraph = await manager.readGraph('tag_demo');
const reactEntity = updatedGraph.entities.find(e => e.name === 'React');
const vueEntity = updatedGraph.entities.find(e => e.name === 'Vue.js');
const pgEntity = updatedGraph.entities.find(e => e.name === 'PostgreSQL');
console.log(`š React tags: [${reactEntity?.tags?.join(', ') || 'none'}]`);
console.log(`š Vue.js tags: [${vueEntity?.tags?.join(', ') || 'none'}]`);
console.log(`š PostgreSQL tags: [${pgEntity?.tags?.join(', ') || 'none'}]`);
console.log();
// 6. Demonstrate removing tags
console.log('6ļøā£ Removing tags from entities...');
const removeTagsResult = await manager.removeTags([
{ entityName: 'React', tags: ['meta'] },
{ entityName: 'MongoDB', tags: ['json'] }
], 'tag_demo');
removeTagsResult.forEach(result => {
console.log(`šļø Removed tags from ${result.entityName}: [${result.removedTags.join(', ')}]`);
});
console.log();
// 7. Demonstrate enhanced search (includes tags in general search)
console.log('7ļøā£ Enhanced search (includes tags in general text search)...');
const enhancedResults = await manager.searchNodes('nosql', 'tag_demo');
console.log(`š Search for "nosql" (${enhancedResults.entities.length} found):`);
enhancedResults.entities.forEach(entity => {
console.log(` ⢠${entity.name} - Found via tags: [${entity.tags?.join(', ') || 'none'}]`);
});
console.log();
// 8. Demonstrate case sensitivity
console.log('8ļøā£ Demonstrating case sensitivity in tag search...');
const caseSensitiveResults = await manager.searchNodes('', { exactTags: ['Frontend'], tagMatchMode: 'any' }, 'tag_demo'); // Capital F
console.log(`š Search for "Frontend" (capital F): ${caseSensitiveResults.entities.length} found`);
console.log(' (Tags are case-sensitive for exact matching)\n');
// 9. Show final state
console.log('9ļøā£ Final knowledge graph state...');
const finalGraph = await manager.readGraph('tag_demo');
console.log(`š Total entities: ${finalGraph.entities.length}`);
console.log(`š Total relations: ${finalGraph.relations.length}`);
console.log(`š Total unique tags: ${[...new Set(finalGraph.entities.flatMap(e => e.tags))].length}`);
const allTags = [...new Set(finalGraph.entities.flatMap(e => e.tags))].sort();
console.log(`š·ļø All tags: [${allTags.join(', ')}]`);
}
catch (error) {
console.error('ā Demo failed:', error);
process.exit(1);
}
finally {
await manager.close();
}
console.log('\nš Tag system demonstration completed successfully!');
console.log('\nKey Features Demonstrated:');
console.log('⢠ā
Exact-match tag searching (case-sensitive)');
console.log('⢠ā
"Any" and "All" search modes');
console.log('⢠ā
Adding and removing tags from existing entities');
console.log('⢠ā
Enhanced general search that includes tags');
console.log('⢠ā
Backward compatibility with entities without tags');
console.log('⢠ā
Duplicate tag prevention');
console.log('⢠ā
Relation preservation in filtered results');
}
// Run the demonstration
demonstrateTagSystem().catch(console.error);
//# sourceMappingURL=tag-system-demo.js.map