read_multiple_files
Read contents of multiple files simultaneously for analysis or comparison, returning each file's content with its path. Processes allowed directories only, allowing individual read failures without halting the operation.
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. Only works within allowed directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| encoding | No | File encoding | utf-8 |
| paths | Yes | List of file paths to read |
Implementation Reference
- src/utils/tools.ts:86-117 (handler)Core handler function for the 'read_multiple_files' tool. Reads specified files concurrently using Promise.all, validates each path and file size, handles individual file errors without failing the entire operation, and returns a record mapping each path to its content or an Error.export async function readMultipleFiles( args: z.infer<typeof ReadMultipleFilesArgsSchema>, config: Config ): Promise<Record<string, string | Error>> { const endMetric = metrics.startOperation('read_multiple_files') const results: Record<string, string | Error> = {} await Promise.all( args.paths.map(async (filePath: string) => { try { const validPath = await validatePath(filePath, config) // Validate file size if (config.security.maxFileSize > 0) { await validateFileSize(validPath, config.security.maxFileSize) } const content = await fs.readFile(validPath, args.encoding) results[filePath] = content } catch (error) { if (error instanceof Error) { results[filePath] = error } else { results[filePath] = new Error(String(error)) } } }) ) endMetric() return results }
- src/utils/tools.ts:68-77 (schema)Zod schema defining the input arguments for the read_multiple_files tool: an array of file paths and an optional encoding parameter.* Schema for read_multiple_files arguments */ export const ReadMultipleFilesArgsSchema = z.object({ paths: z.array(z.string()).describe('List of file paths to read'), encoding: z .enum(['utf-8', 'utf8', 'base64']) .optional() .default('utf-8') .describe('File encoding'), })
- src/index.ts:244-253 (registration)Registration of the 'read_multiple_files' tool in the list_tools handler, including name, description, and JSON schema derived from the Zod schema.{ name: 'read_multiple_files', description: '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. Only works within allowed directories.', inputSchema: zodToJsonSchema(ReadMultipleFilesArgsSchema) as ToolInput, },
- src/index.ts:416-438 (handler)Tool call handler in the main switch statement that parses arguments using the schema, invokes the core readMultipleFiles function, formats the results for MCP response, and handles validation errors.case 'read_multiple_files': { const parsed = ReadMultipleFilesArgsSchema.safeParse(a) if (!parsed.success) { throw new FileSystemError(`Invalid arguments for ${name}`, 'INVALID_ARGS', undefined, { errors: parsed.error.format(), }) } const results = await readMultipleFiles(parsed.data, config) const formattedResults = Object.entries(results) .map(([filePath, content]) => { if (content instanceof Error) { return `${filePath}: Error - ${content.message}` } return `${filePath}:\n${content}\n` }) .join('\n---\n') endMetric() return { content: [{ type: 'text', text: formattedResults }], } }