UNPKG

@allpepper/memory-bank-mcp

Version:

MCP server for remote management of project memory banks

79 lines (78 loc) 2.81 kB
import fs from "fs-extra"; import path from "path"; /** * Filesystem implementation of the FileRepository protocol */ export class FsFileRepository { rootDir; /** * Creates a new FsFileRepository * @param rootDir The root directory where all projects are stored */ constructor(rootDir) { this.rootDir = rootDir; } /** * Lists all files in a project * @param projectName The name of the project * @returns An array of file names */ async listFiles(projectName) { const projectPath = path.join(this.rootDir, projectName); const projectExists = await fs.pathExists(projectPath); if (!projectExists) { return []; } const entries = await fs.readdir(projectPath, { withFileTypes: true }); return entries.filter((entry) => entry.isFile()).map((entry) => entry.name); } /** * Loads the content of a file * @param projectName The name of the project * @param fileName The name of the file * @returns The content of the file or null if the file doesn't exist */ async loadFile(projectName, fileName) { const filePath = path.join(this.rootDir, projectName, fileName); const fileExists = await fs.pathExists(filePath); if (!fileExists) { return null; } const content = await fs.readFile(filePath, "utf-8"); return content; } /** * Writes a new file * @param projectName The name of the project * @param fileName The name of the file * @param content The content to write * @returns The content of the file after writing, or null if the file already exists */ async writeFile(projectName, fileName, content) { const projectPath = path.join(this.rootDir, projectName); await fs.ensureDir(projectPath); const filePath = path.join(projectPath, fileName); const fileExists = await fs.pathExists(filePath); if (fileExists) { return null; } await fs.writeFile(filePath, content, "utf-8"); return await this.loadFile(projectName, fileName); } /** * Updates an existing file * @param projectName The name of the project * @param fileName The name of the file * @param content The new content * @returns The content of the file after updating, or null if the file doesn't exist */ async updateFile(projectName, fileName, content) { const filePath = path.join(this.rootDir, projectName, fileName); const fileExists = await fs.pathExists(filePath); if (!fileExists) { return null; } await fs.writeFile(filePath, content, "utf-8"); return await this.loadFile(projectName, fileName); } }