read_multiple_files
Read and process multiple files at once, returning content with file paths for analysis or comparison. Individual read failures do not halt the operation. Requires specified max bytes per file and operates within authorized directories.
Instructions
Read the contents of multiple files simultaneously. This is more efficient than reading files one by one when you need to analyze or compare multiple files. Each file's content is returned with its path as a reference. Failed reads for individual files won't stop the entire operation. Requires maxBytesPerFile parameter. Only works within allowed directories.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| maxBytesPerFile | Yes | Maximum bytes to read per file. Must be a positive integer. Handler default: 10KB. | |
| paths | Yes |
Implementation Reference
- src/handlers/file-handlers.ts:48-79 (handler)The core handler function for the 'read_multiple_files' tool. Parses arguments, validates paths, checks sizes, reads multiple files concurrently, handles per-file errors, returns combined content.export async function handleReadMultipleFiles( args: unknown, allowedDirectories: string[], symlinksMap: Map<string, string>, noFollowSymlinks: boolean ) { const { paths, maxBytesPerFile } = parseArgs(ReadMultipleFilesArgsSchema, args, 'read_multiple_files'); const effectiveMaxBytes = maxBytesPerFile ?? (10 * 1024); // Default 10KB per file const results = await Promise.all( paths.map(async (filePath: string) => { try { const validPath = await validatePath(filePath, allowedDirectories, symlinksMap, noFollowSymlinks); // Check file size before reading const stats = await fs.stat(validPath); if (stats.size > effectiveMaxBytes) { return `${filePath}: Error - File size (${stats.size} bytes) exceeds the maximum allowed size (${effectiveMaxBytes} bytes).`; } const content = await fs.readFile(validPath, "utf-8"); return `${filePath}:\n${content}\n`; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return `${filePath}: Error - ${errorMessage}`; } }), ); return { content: [{ type: "text", text: results.join("\n---\n") }], }; }
- src/schemas/file-operations.ts:14-21 (schema)Defines the input schema using TypeBox for 'read_multiple_files': array of file paths and optional max bytes per file.export const ReadMultipleFilesArgsSchema = Type.Object({ paths: Type.Array(Type.String()), maxBytesPerFile: Type.Integer({ minimum: 1, description: 'Maximum bytes to read per file. Must be a positive integer. Handler default: 10KB.' }) }); export type ReadMultipleFilesArgs = Static<typeof ReadMultipleFilesArgsSchema>;
- index.ts:169-175 (registration)Binds the handleReadMultipleFiles function to the 'read_multiple_files' tool name in the toolHandlers object, injecting server context parameters.read_multiple_files: (a: unknown) => handleReadMultipleFiles( a, allowedDirectories, symlinksMap, noFollowSymlinks, ),
- src/schemas/index.ts:72-72 (schema)Maps the ReadMultipleFilesArgsSchema to 'read_multiple_files' key in the central toolSchemas export used by the MCP server for parameter validation.read_multiple_files: ReadMultipleFilesArgsSchema,
- index.ts:306-306 (registration)Declares the 'read_multiple_files' tool metadata (name and description) in the allTools list, which determines available tools based on permissions.{ name: "read_multiple_files", description: "Read multiple files" },