mcp-think-tank
Version:
Structured thinking and knowledge management tool for Model Context Protocol
82 lines (81 loc) • 2.51 kB
JavaScript
import { contentCache } from './ContentCache.js';
import * as fs from 'fs/promises';
import * as path from 'path';
/**
* FileSystemAdapter provides cached versions of filesystem operations
* This implements Story 3-G: Execution cache for file/URL reads
*/
export class FileSystemAdapter {
/**
* Read a file with caching
* @param filePath Path to the file to read
* @returns File content as a Buffer
*/
async readFile(filePath) {
return await contentCache.readFile(filePath);
}
/**
* Read a file as text with caching
* @param filePath Path to the file to read
* @param encoding Text encoding (defaults to utf8)
* @returns File content as a string
*/
async readFileAsText(filePath, encoding = 'utf8') {
const buffer = await contentCache.readFile(filePath);
return buffer.toString(encoding);
}
/**
* Read a JSON file with caching and parsing
* @param filePath Path to the JSON file
* @returns Parsed JSON object
*/
async readJsonFile(filePath) {
return await contentCache.getJson(filePath);
}
/**
* Check if a file exists and is readable
* @param filePath Path to check
* @returns true if file exists and is readable, false otherwise
*/
async fileExists(filePath) {
try {
await fs.access(filePath, fs.constants.R_OK);
return true;
}
catch {
return false;
}
}
/**
* List files in a directory
* @param dirPath Directory path to list
* @param pattern Optional glob pattern to filter files
* @returns Array of file paths
*/
async listFiles(dirPath, pattern) {
// This operation isn't cached since directory contents can change frequently
const files = await fs.readdir(dirPath);
if (!pattern) {
return files.map(file => path.join(dirPath, file));
}
// Simple glob pattern matching (* only)
const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
return files
.filter(file => regex.test(file))
.map(file => path.join(dirPath, file));
}
/**
* Clear the content cache
*/
clearCache() {
contentCache.clear();
}
/**
* Get cache statistics
*/
getCacheStats() {
return contentCache.getStats();
}
}
// Export a singleton instance
export const fileSystemAdapter = new FileSystemAdapter();