read_multiple_files
Read contents from multiple files at once, returning text data and rendering images as viewable content. Handles PNG, JPEG, GIF, and WebP files while continuing operation even if individual file reads fail.
Instructions
Read the contents of multiple files simultaneously.
Each file's content is returned with its path as a reference.
Handles text files normally and renders images as viewable content.
Recognized image types: PNG, JPEG, GIF, WebP.
Failed reads for individual files won't stop the entire operation.
Only works within allowed directories.
IMPORTANT: Always use absolute paths for reliability. Paths are automatically normalized regardless of slash direction. Relative paths may fail as they depend on the current working directory. Tilde paths (~/...) might not work in all contexts. Unless the user explicitly asks for relative paths, use absolute paths.
This command can be referenced as "DC: ..." or "use Desktop Commander to ..." in your instructions.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| paths | Yes |
Implementation Reference
- The handler function that executes the read_multiple_files tool: parses args with schema, calls readMultipleFiles helper, processes results (PDFs/images/text/errors), builds summary and content items for MCP response./** * Handle read_multiple_files command */ export async function handleReadMultipleFiles(args: unknown): Promise<ServerResult> { const parsed = ReadMultipleFilesArgsSchema.parse(args); const fileResults = await readMultipleFiles(parsed.paths); // Create a text summary of all files const textSummary = fileResults.map(result => { if (result.error) { return `${result.path}: Error - ${result.error}`; } else if (result.isPdf) { return `${result.path}: PDF file with ${result.payload?.pages?.length} pages`; } else if (result.mimeType) { return `${result.path}: ${result.mimeType} ${result.isImage ? '(image)' : '(text)'}`; } else { return `${result.path}: Unknown type`; } }).join("\n"); // Create content items for each file const contentItems: Array<{ type: string, text?: string, data?: string, mimeType?: string }> = []; // Add the text summary contentItems.push({ type: "text", text: textSummary }); // Add each file content for (const result of fileResults) { if (!result.error && result.content !== undefined) { if (result.isPdf) { result.payload?.pages.forEach((page, i) => { page.images.forEach((image, i) => { contentItems.push({ type: "image", data: image.data, mimeType: image.mimeType }); }); contentItems.push({ type: "text", text: page.text, }); }); } else if (result.isImage && result.mimeType) { // For image files, add an image content item contentItems.push({ type: "image", data: result.content, mimeType: result.mimeType }); } else { // For text files, add a text summary contentItems.push({ type: "text", text: `\n--- ${result.path} contents: ---\n${result.content}` }); } } } return { content: contentItems }; }
- src/tools/filesystem.ts:909-933 (helper)Core implementation: reads multiple files in parallel via Promise.all, validates paths, calls readFile per file, handles errors individually, returns array of results with content/mime/isImage/isPdf or error.export async function readMultipleFiles(paths: string[]): Promise<MultiFileResult[]> { return Promise.all( paths.map(async (filePath: string) => { try { const validPath = await validatePath(filePath); const fileResult = await readFile(validPath); const isPdf = isPdfFile(fileResult.mimeType); return { path: filePath, content: typeof fileResult === 'string' ? fileResult : fileResult.content, mimeType: typeof fileResult === 'string' ? "text/plain" : fileResult.mimeType, isImage: typeof fileResult === 'string' ? false : fileResult.isImage, isPdf: typeof fileResult === 'string' ? false : fileResult.isPdf, payload: typeof fileResult === 'string' ? undefined : fileResult.payload }; } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); return { path: filePath, error: errorMessage }; } }), ); }
- src/tools/schemas.ts:52-54 (schema)Zod input schema: requires 'paths' array of strings.export const ReadMultipleFilesArgsSchema = z.object({ paths: z.array(z.string()), });
- src/server.ts:1240-1242 (registration)Registration in CallToolRequestHandler switch: maps tool name 'read_multiple_files' to handleReadMultipleFiles.case "read_multiple_files": result = await handlers.handleReadMultipleFiles(args); break;
- src/server.ts:286-305 (registration)Tool definition in ListToolsRequestHandler: registers name, description, inputSchema (ReadMultipleFilesArgsSchema), and annotations for the MCP tools list.{ name: "read_multiple_files", description: ` Read the contents of multiple files simultaneously. Each file's content is returned with its path as a reference. Handles text files normally and renders images as viewable content. Recognized image types: PNG, JPEG, GIF, WebP. Failed reads for individual files won't stop the entire operation. Only works within allowed directories. ${PATH_GUIDANCE} ${CMD_PREFIX_DESCRIPTION}`, inputSchema: zodToJsonSchema(ReadMultipleFilesArgsSchema), annotations: { title: "Read Multiple Files", readOnlyHint: true, }, },