init-memory-bank
Set up a memory-bank directory and generate core files for structured project context tracking. Integrates existing project briefs and provides guidance for next steps. Uses a root directory path to initialize or overwrite files as needed.
Instructions
Initialize memory-bank directory and core files. This tool will:
Create memory-bank directory
Generate initial templates for 5 core files
Read and integrate projectBrief.md if it exists
Provide next steps guidance
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| force | No | Force re-initialization (will overwrite existing files) | |
| rootPath | Yes | Project root directory path Windows example: "C:/Users/name/project" macOS/Linux example: "/home/name/project" |
Implementation Reference
- index.ts:575-687 (handler)The main execution logic of the init-memory-bank tool. Handles directory creation, template generation and file writing for the memory bank system.async ({ rootPath, force = false }) => { const normalizedPath = normalizePath(rootPath); const memoryBankPath = path.join(normalizedPath, "memory-bank"); try { // Check if directory exists if (existsSync(memoryBankPath) && !force) { const files = await fs.readdir(memoryBankPath); if (files.length > 0) { return { content: [ { type: "text", text: `[MEMORY BANK: EXISTS] memory-bank directory already exists and contains files. To re-initialize, use force: true parameter. Existing files: ${files.map((f) => `- ${f}`).join("\n")} Suggestions: - Use get-memory-bank-info to read existing content - If you really need to re-initialize, set force: true`, }, ], }; } } // Create directory await fs.mkdir(memoryBankPath, { recursive: true }); // Check if projectBrief.md exists let projectBriefContent = ""; const projectBriefPath = path.join(normalizedPath, "projectBrief.md"); if (existsSync(projectBriefPath)) { try { projectBriefContent = await fs.readFile(projectBriefPath, "utf-8"); } catch (err) { console.error("Failed to read projectBrief.md:", err); } } // Get templates const templates = getMemoryBankTemplates(); // If projectBrief exists, update productContext.md template if (projectBriefContent) { templates["productContext.md"] = templates["productContext.md"].replace( "## Project Goal\n\n* ", `## Project Goal\n\n*Based on projectBrief.md content:*\n\n${projectBriefContent}\n\n*Extract and define project goals from the above content:*\n\n* ` ); } // Create all files const createdFiles: string[] = []; for (const [filename, content] of Object.entries(templates)) { const filePath = path.join(memoryBankPath, filename); await fs.writeFile(filePath, content, "utf-8"); createdFiles.push(filename); } return { content: [ { type: "text", text: `[MEMORY BANK: INITIALIZED] Memory Bank has been successfully initialized! Created files: ${createdFiles.map((f) => `- ${f}`).join("\n")} ${ projectBriefContent ? "✓ Read projectBrief.md and integrated into productContext.md\n\n" : "" } [ATTENTION] Next steps to execute: 1. Read and update each memory-bank/*.md file 2. Fill in relevant content following the guidance in each file 3. Do not use get-memory-bank-info before completing initial edits 4. After completing edits, you can start using Memory Bank Important file descriptions: - productContext.md: Define project goals, features, and architecture - activeContext.md: Track current work status and focus - progress.md: Manage task progress - decisionLog.md: Record important decisions - systemPatterns.md: Document code patterns and standards Maintenance Tips: - Keep each file under 300 lines for optimal performance - Archive old content daily/weekly to memory-bank/archive/ - Use update-memory-bank tool for detailed maintenance guidance - Check file sizes after each work session`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error initializing Memory Bank: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- index.ts:564-574 (schema)Input schema validation using Zod for the tool parameters: rootPath (required string) and force (optional boolean).{ rootPath: z.string().describe( `Project root directory path Windows example: "C:/Users/name/project" macOS/Linux example: "/home/name/project"` ), force: z .boolean() .optional() .describe("Force re-initialization (will overwrite existing files)"), },
- index.ts:556-688 (registration)The server.tool call that registers the 'init-memory-bank' tool with its name, description, input schema, and handler function.server.tool( "init-memory-bank", `Initialize memory-bank directory and core files. This tool will: - Create memory-bank directory - Generate initial templates for 5 core files - Read and integrate projectBrief.md if it exists - Provide next steps guidance`, { rootPath: z.string().describe( `Project root directory path Windows example: "C:/Users/name/project" macOS/Linux example: "/home/name/project"` ), force: z .boolean() .optional() .describe("Force re-initialization (will overwrite existing files)"), }, async ({ rootPath, force = false }) => { const normalizedPath = normalizePath(rootPath); const memoryBankPath = path.join(normalizedPath, "memory-bank"); try { // Check if directory exists if (existsSync(memoryBankPath) && !force) { const files = await fs.readdir(memoryBankPath); if (files.length > 0) { return { content: [ { type: "text", text: `[MEMORY BANK: EXISTS] memory-bank directory already exists and contains files. To re-initialize, use force: true parameter. Existing files: ${files.map((f) => `- ${f}`).join("\n")} Suggestions: - Use get-memory-bank-info to read existing content - If you really need to re-initialize, set force: true`, }, ], }; } } // Create directory await fs.mkdir(memoryBankPath, { recursive: true }); // Check if projectBrief.md exists let projectBriefContent = ""; const projectBriefPath = path.join(normalizedPath, "projectBrief.md"); if (existsSync(projectBriefPath)) { try { projectBriefContent = await fs.readFile(projectBriefPath, "utf-8"); } catch (err) { console.error("Failed to read projectBrief.md:", err); } } // Get templates const templates = getMemoryBankTemplates(); // If projectBrief exists, update productContext.md template if (projectBriefContent) { templates["productContext.md"] = templates["productContext.md"].replace( "## Project Goal\n\n* ", `## Project Goal\n\n*Based on projectBrief.md content:*\n\n${projectBriefContent}\n\n*Extract and define project goals from the above content:*\n\n* ` ); } // Create all files const createdFiles: string[] = []; for (const [filename, content] of Object.entries(templates)) { const filePath = path.join(memoryBankPath, filename); await fs.writeFile(filePath, content, "utf-8"); createdFiles.push(filename); } return { content: [ { type: "text", text: `[MEMORY BANK: INITIALIZED] Memory Bank has been successfully initialized! Created files: ${createdFiles.map((f) => `- ${f}`).join("\n")} ${ projectBriefContent ? "✓ Read projectBrief.md and integrated into productContext.md\n\n" : "" } [ATTENTION] Next steps to execute: 1. Read and update each memory-bank/*.md file 2. Fill in relevant content following the guidance in each file 3. Do not use get-memory-bank-info before completing initial edits 4. After completing edits, you can start using Memory Bank Important file descriptions: - productContext.md: Define project goals, features, and architecture - activeContext.md: Track current work status and focus - progress.md: Manage task progress - decisionLog.md: Record important decisions - systemPatterns.md: Document code patterns and standards Maintenance Tips: - Keep each file under 300 lines for optimal performance - Archive old content daily/weekly to memory-bank/archive/ - Use update-memory-bank tool for detailed maintenance guidance - Check file sizes after each work session`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error initializing Memory Bank: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } } );
- index.ts:51-152 (helper)Supporting function that generates and returns the initial template strings for all five core memory-bank markdown files, including timestamps.// Memory Bank file template generation function function getMemoryBankTemplates(): Record<string, string> { const timestamp = formatTimestamp(); return { "productContext.md": `# Product Context This file provides a high-level overview of the project and the expected product that will be created. Initially it is based upon projectBrief.md (if provided) and all other available project-related information in the working directory. This file is intended to be updated as the project evolves, and should be used to inform all other modes of the project's goals and context. ${timestamp} - Log of updates made will be appended as footnotes to the end of this file. * ## Project Goal * ## Key Features * ## Overall Architecture * `, "activeContext.md": `# Active Context This file tracks the project's current status, including recent changes, current goals, and open questions. ${timestamp} - Log of updates made. * ## Current Focus * ## Recent Changes * ## Open Questions/Issues * `, "progress.md": `# Progress This file tracks the project's progress using a task list format. ${timestamp} - Log of updates made. * ## Completed Tasks * ## Current Tasks * ## Next Steps * `, "decisionLog.md": `# Decision Log This file records architectural and implementation decisions using a list format. ${timestamp} - Log of updates made. * ## Decision * ## Rationale * ## Implementation Details * `, "systemPatterns.md": `# System Patterns *Optional* This file documents recurring patterns and standards used in the project. It is optional, but recommended to be updated as the project evolves. ${timestamp} - Log of updates made. * ## Coding Patterns * ## Architectural Patterns * ## Testing Patterns * `, }; }