UNPKG

knowledgegraph-mcp

Version:

MCP server for enabling persistent knowledge storage for Claude through a knowledge graph with multiple storage backends

185 lines • 8.23 kB
#!/usr/bin/env node /** * File Attachments Demo * * This example demonstrates the file attachment functionality of the knowledge graph system. * It shows how to: * 1. Create entities * 2. Attach files to entities * 3. List attachments * 4. Retrieve file information * 5. Detach files */ import { KnowledgeGraphManager, StorageType } from '../index.js'; async function fileAttachmentsDemo() { console.log('šŸ“Ž File Attachments Demo'); console.log('========================\n'); // Create knowledge graph manager with SQLite storage (file-based for file attachments) const manager = new KnowledgeGraphManager({ type: StorageType.SQLITE, connectionString: 'sqlite://./demo-file-attachments.db', // File-based database for file storage fuzzySearch: { useDatabaseSearch: false, // SQLite uses client-side search threshold: 0.3, clientSideFallback: true }, fileStorage: { maxFileSize: 10 * 1024 * 1024, // 10MB maxFilesPerEntity: 50, allowedMimeTypes: [ 'text/plain', 'text/markdown', 'application/json', 'application/pdf', 'image/png', 'image/jpeg' ] } }); try { // Wait for initialization await new Promise(resolve => setTimeout(resolve, 500)); console.log('1. Creating test entities...'); await manager.createEntities([ { name: 'Project_Alpha', entityType: 'project', observations: ['A new software project', 'Uses TypeScript and Node.js'], tags: ['active', 'development'] }, { name: 'John_Smith', entityType: 'person', observations: ['Lead developer', 'Expert in TypeScript'], tags: ['team', 'developer'] } ], 'file_demo'); console.log('āœ… Entities created successfully\n'); console.log('2. Attaching files to entities...'); // Create sample file content const textFileContent = 'This is a sample text file for the demo.\nIt contains project documentation.'; const jsonFileContent = JSON.stringify({ project: 'Alpha', version: '1.0.0', dependencies: ['typescript', 'node'] }, null, 2); // Attach text file to project const textFileUpload = { buffer: Buffer.from(textFileContent, 'utf8'), originalname: 'project-readme.txt', mimetype: 'text/plain', size: Buffer.from(textFileContent, 'utf8').length }; const textResult = await manager.attachFile('Project_Alpha', textFileUpload, 'file_demo'); if (textResult.success) { console.log(`āœ… Text file attached: ${textResult.fileId}`); } else { console.log(`āŒ Failed to attach text file: ${textResult.error}`); } // Attach JSON file to project const jsonFileUpload = { buffer: Buffer.from(jsonFileContent, 'utf8'), originalname: 'package-info.json', mimetype: 'application/json', size: Buffer.from(jsonFileContent, 'utf8').length }; const jsonResult = await manager.attachFile('Project_Alpha', jsonFileUpload, 'file_demo'); if (jsonResult.success) { console.log(`āœ… JSON file attached: ${jsonResult.fileId}`); } else { console.log(`āŒ Failed to attach JSON file: ${jsonResult.error}`); } // Attach a file to person const resumeContent = 'John Smith - Senior Developer\n\nExperience:\n- 5 years TypeScript\n- 3 years Node.js'; const resumeFileUpload = { buffer: Buffer.from(resumeContent, 'utf8'), originalname: 'john-smith-resume.txt', mimetype: 'text/plain', size: Buffer.from(resumeContent, 'utf8').length }; const resumeResult = await manager.attachFile('John_Smith', resumeFileUpload, 'file_demo'); if (resumeResult.success) { console.log(`āœ… Resume file attached: ${resumeResult.fileId}`); } else { console.log(`āŒ Failed to attach resume file: ${resumeResult.error}`); } console.log('\n3. Listing attachments...'); // List attachments for Project_Alpha const projectAttachments = await manager.listAttachments('Project_Alpha', 'file_demo'); console.log(`\nšŸ“ Project_Alpha has ${projectAttachments.length} attachments:`); projectAttachments.forEach((att, index) => { console.log(` ${index + 1}. ${att.originalFilename} (${att.mimeType}, ${att.fileSize} bytes)`); console.log(` File ID: ${att.fileId}`); console.log(` Uploaded: ${att.uploadDate.toISOString()}`); }); // List attachments for John_Smith const personAttachments = await manager.listAttachments('John_Smith', 'file_demo'); console.log(`\nšŸ‘¤ John_Smith has ${personAttachments.length} attachments:`); personAttachments.forEach((att, index) => { console.log(` ${index + 1}. ${att.originalFilename} (${att.mimeType}, ${att.fileSize} bytes)`); console.log(` File ID: ${att.fileId}`); console.log(` Uploaded: ${att.uploadDate.toISOString()}`); }); console.log('\n4. Getting file information...'); if (textResult.success) { const fileInfo = await manager.getAttachmentInfo(textResult.fileId, 'file_demo'); if (fileInfo) { console.log(`\nšŸ“„ File Info for ${fileInfo.originalFilename}:`); console.log(` MIME Type: ${fileInfo.mimeType}`); console.log(` Size: ${fileInfo.fileSize} bytes`); console.log(` Hash: ${fileInfo.fileHash}`); console.log(` Upload Date: ${fileInfo.uploadDate.toISOString()}`); } } console.log('\n5. Retrieving file content...'); if (textResult.success) { try { const fileContent = await manager.retrieveFile(textResult.fileId); console.log(`\nšŸ“„ Content of project-readme.txt:`); console.log(`"${fileContent.toString('utf8')}"`); } catch (error) { console.log(`āŒ Failed to retrieve file: ${error}`); } } console.log('\n6. Testing file detachment...'); if (jsonResult.success) { const detachResult = await manager.detachFile('Project_Alpha', jsonResult.fileId, 'file_demo'); if (detachResult.success) { console.log('āœ… JSON file detached successfully'); } else { console.log(`āŒ Failed to detach file: ${detachResult.error}`); } // Verify file was removed const updatedAttachments = await manager.listAttachments('Project_Alpha', 'file_demo'); console.log(`šŸ“ Project_Alpha now has ${updatedAttachments.length} attachments`); } console.log('\n7. Reading final graph state...'); const graph = await manager.readGraph('file_demo'); console.log(`\nšŸ“Š Final graph contains:`); console.log(` - ${graph.entities.length} entities`); console.log(` - ${graph.relations.length} relations`); // Show entities with attachment counts graph.entities.forEach(entity => { const attachmentCount = entity.attachments?.length || 0; console.log(` - ${entity.name}: ${attachmentCount} attachments`); }); console.log('\nāœ… File attachments demo completed successfully!'); } catch (error) { console.error('āŒ Demo failed:', error); } finally { await manager.close(); } } // Run the demo if (import.meta.url === `file://${process.argv[1]}`) { fileAttachmentsDemo().catch(console.error); } export { fileAttachmentsDemo }; //# sourceMappingURL=file-attachments-demo.js.map