@@ -11,14 +11,43 @@ import path from 'path';
1111import { fileURLToPath } from 'url' ;
1212
1313// Define memory file path using environment variable with fallback
14- const defaultMemoryPath = path . join ( path . dirname ( fileURLToPath ( import . meta. url ) ) , 'memory.json ' ) ;
14+ const defaultMemoryPath = path . join ( path . dirname ( fileURLToPath ( import . meta. url ) ) , 'memory.jsonl ' ) ;
1515
16- // If MEMORY_FILE_PATH is just a filename, put it in the same directory as the script
17- const MEMORY_FILE_PATH = process . env . MEMORY_FILE_PATH
18- ? path . isAbsolute ( process . env . MEMORY_FILE_PATH )
19- ? process . env . MEMORY_FILE_PATH
20- : path . join ( path . dirname ( fileURLToPath ( import . meta. url ) ) , process . env . MEMORY_FILE_PATH )
21- : defaultMemoryPath ;
16+ // Handle backward compatibility: migrate memory.json to memory.jsonl if needed
17+ async function ensureMemoryFilePath ( ) : Promise < string > {
18+ if ( process . env . MEMORY_FILE_PATH ) {
19+ // Custom path provided, use it as-is (with absolute path resolution)
20+ return path . isAbsolute ( process . env . MEMORY_FILE_PATH )
21+ ? process . env . MEMORY_FILE_PATH
22+ : path . join ( path . dirname ( fileURLToPath ( import . meta. url ) ) , process . env . MEMORY_FILE_PATH ) ;
23+ }
24+
25+ // No custom path set, check for backward compatibility migration
26+ const oldMemoryPath = path . join ( path . dirname ( fileURLToPath ( import . meta. url ) ) , 'memory.json' ) ;
27+ const newMemoryPath = defaultMemoryPath ;
28+
29+ try {
30+ // Check if old file exists and new file doesn't
31+ await fs . access ( oldMemoryPath ) ;
32+ try {
33+ await fs . access ( newMemoryPath ) ;
34+ // Both files exist, use new one (no migration needed)
35+ return newMemoryPath ;
36+ } catch {
37+ // Old file exists, new file doesn't - migrate
38+ console . error ( 'DETECTED: Found legacy memory.json file, migrating to memory.jsonl for JSONL format compatibility' ) ;
39+ await fs . rename ( oldMemoryPath , newMemoryPath ) ;
40+ console . error ( 'COMPLETED: Successfully migrated memory.json to memory.jsonl' ) ;
41+ return newMemoryPath ;
42+ }
43+ } catch {
44+ // Old file doesn't exist, use new path
45+ return newMemoryPath ;
46+ }
47+ }
48+
49+ // Initialize memory file path (will be set during startup)
50+ let MEMORY_FILE_PATH : string ;
2251
2352// We are storing our memory using entities, relations, and observations in a graph structure
2453interface Entity {
@@ -434,6 +463,9 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
434463} ) ;
435464
436465async function main ( ) {
466+ // Initialize memory file path with backward compatibility
467+ MEMORY_FILE_PATH = await ensureMemoryFilePath ( ) ;
468+
437469 const transport = new StdioServerTransport ( ) ;
438470 await server . connect ( transport ) ;
439471 console . error ( "Knowledge Graph MCP Server running on stdio" ) ;
0 commit comments