UNPKG

repomix

Version:

A tool to pack repository contents to single file for AI consumption

78 lines (77 loc) 3.56 kB
import fs from 'node:fs/promises'; import path from 'node:path'; import { z } from 'zod'; import { createSecretLintConfig, runSecretLint } from '../../core/security/workers/securityCheckWorker.js'; import { logger } from '../../shared/logger.js'; import { buildMcpToolErrorResponse, buildMcpToolSuccessResponse } from './mcpToolRuntime.js'; const fileSystemReadFileInputSchema = z.object({ path: z.string().describe('Absolute path to the file to read'), }); const fileSystemReadFileOutputSchema = z.object({ path: z.string().describe('The file path that was read'), content: z.string().describe('The file content'), size: z.number().describe('File size in bytes'), encoding: z.string().describe('Text encoding used to read the file'), lines: z.number().describe('Number of lines in the file'), }); export const registerFileSystemReadFileTool = (mcpServer) => { mcpServer.registerTool('file_system_read_file', { title: 'Read File', description: 'Read a file from the local file system using an absolute path. Includes built-in security validation to detect and prevent access to files containing sensitive information (API keys, passwords, secrets).', inputSchema: fileSystemReadFileInputSchema, outputSchema: fileSystemReadFileOutputSchema, annotations: { readOnlyHint: true, destructiveHint: false, idempotentHint: true, openWorldHint: false, }, }, async ({ path: filePath }) => { try { logger.trace(`Reading file at absolute path: ${filePath}`); if (!path.isAbsolute(filePath)) { return buildMcpToolErrorResponse({ errorMessage: `Error: Path must be absolute. Received: ${filePath}`, }); } try { await fs.access(filePath); } catch { return buildMcpToolErrorResponse({ errorMessage: `Error: File not found at path: ${filePath}`, }); } const stats = await fs.stat(filePath); if (stats.isDirectory()) { return buildMcpToolErrorResponse({ errorMessage: `Error: The specified path is a directory, not a file: ${filePath}. Use file_system_read_directory for directories.`, }); } const fileStats = await fs.stat(filePath); const fileContent = await fs.readFile(filePath, 'utf8'); const config = createSecretLintConfig(); const securityCheckResult = await runSecretLint(filePath, fileContent, 'file', config); if (securityCheckResult !== null) { return buildMcpToolErrorResponse({ errorMessage: `Error: Security check failed. The file at ${filePath} may contain sensitive information.`, }); } const lines = fileContent.split('\n').length; const size = fileStats.size; return buildMcpToolSuccessResponse({ path: filePath, content: fileContent, size, encoding: 'utf8', lines, }); } catch (error) { logger.error(`Error in file_system_read_file tool: ${error}`); return buildMcpToolErrorResponse({ errorMessage: `Error reading file: ${error instanceof Error ? error.message : String(error)}`, }); } }); };