initialize_memory_bank
Set up persistent project memory by initializing a Memory Bank in a specified directory, enabling AI assistants to retain context, decisions, and progress across sessions.
Instructions
Initialize a Memory Bank in the specified directory
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Path where the Memory Bank will be initialized |
Implementation Reference
- src/server/tools/CoreTools.ts:575-678 (handler)Handler function for the initialize_memory_bank tool. Calls memoryBankManager.setCustomPath(), then memoryBankManager.initialize(true) to create core template files, and initializes the mode manager.
export async function handleInitializeMemoryBank( memoryBankManager: MemoryBankManager, dirPath: string ) { try { // If dirPath is not provided, use the project path const basePath = dirPath || memoryBankManager.getProjectPath(); // Ensure the path is absolute const absolutePath = path.isAbsolute(basePath) ? basePath : path.resolve(process.cwd(), basePath); console.error('Using absolute path:', absolutePath); try { // Set the custom path first await memoryBankManager.setCustomPath(absolutePath); // Initialize the Memory Bank with createIfNotExists = true await memoryBankManager.initialize(true); // Initialize mode manager (creates missing .mcprules files) await memoryBankManager.initializeModeManager(); // Get the Memory Bank directory const memoryBankDir = memoryBankManager.getMemoryBankDir(); return { content: [ { type: 'text', text: `Memory Bank initialized at ${memoryBankDir}`, }, ], }; } catch (initError) { // Check if the error is related to .mcprules files const errorMessage = String(initError); if (errorMessage.includes('.mcprules')) { console.warn('Warning: Error related to .mcprules files:', initError); console.warn('Continuing with Memory Bank initialization despite .mcprules issues.'); // Use the provided path directly as the memory bank directory const memoryBankDir = absolutePath; try { await FileUtils.ensureDirectory(memoryBankDir); memoryBankManager.setMemoryBankDir(memoryBankDir); return { content: [ { type: 'text', text: `Memory Bank initialized at ${memoryBankDir} (with warnings about .mcprules files)`, }, ], }; } catch (dirError) { console.error('Failed to create memory-bank directory:', dirError); // Try to use an existing memory-bank directory if it exists if (await FileUtils.fileExists(memoryBankDir) && await FileUtils.isDirectory(memoryBankDir)) { memoryBankManager.setMemoryBankDir(memoryBankDir); return { content: [ { type: 'text', text: `Memory Bank initialized at ${memoryBankDir} (with warnings)`, }, ], }; } // If we can't create or find a memory-bank directory, return an error return { content: [ { type: 'text', text: `Failed to initialize Memory Bank: ${dirError}`, }, ], }; } } // For other errors, return the error message return { content: [ { type: 'text', text: `Failed to initialize Memory Bank: ${initError}`, }, ], }; } } catch (error) { return { content: [ { type: 'text', text: `Error initializing Memory Bank: ${error}`, }, ], }; } } - src/server/tools/CoreTools.ts:28-40 (schema)Tool schema definition for initialize_memory_bank, defining the 'path' string parameter as required.
name: 'initialize_memory_bank', description: 'Initialize a Memory Bank in the specified directory', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path where the Memory Bank will be initialized', }, }, required: ['path'], }, }, - src/server/tools/index.ts:94-101 (registration)Registration of the tool name 'initialize_memory_bank' in the switch/case dispatch inside setupToolHandlers. Extracts the 'path' argument and delegates to the handler.
case 'initialize_memory_bank': { const { path: dirPath } = request.params.arguments as { path: string }; if (!dirPath) { throw new McpError(ErrorCode.InvalidParams, 'Path not specified'); } console.error('Initializing Memory Bank at path:', dirPath); return handleInitializeMemoryBank(memoryBankManager, dirPath); } - Core helper method on MemoryBankManager that sets the custom path and calls initialize(true) to create the memory-bank directory and template files.
async initializeMemoryBank(dirPath: string): Promise<string> { try { // Set the custom path await this.setCustomPath(dirPath); // Initialize the Memory Bank return await this.initialize(true); } catch (error) { logger.error('MemoryBankManager', `Failed to initialize Memory Bank: ${error}`); throw new Error(`Failed to initialize Memory Bank: ${error instanceof Error ? error.message : String(error)}`); } }