Skip to main content
Glama

read_multiple_files

Read and retrieve contents from multiple files in one operation, including text files and images (PNG, JPEG, GIF, WebP). Uses absolute paths for reliability and continues execution 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

NameRequiredDescriptionDefault
pathsYes

Input Schema (JSON Schema)

{ "$schema": "http://json-schema.org/draft-07/schema#", "additionalProperties": false, "properties": { "paths": { "items": { "type": "string" }, "type": "array" } }, "required": [ "paths" ], "type": "object" }

Implementation Reference

  • Main handler function that executes the read_multiple_files tool. Parses args with schema, calls readMultipleFiles helper, handles PDF/image/text responses, builds summary and content array.
    /** * 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 }; }
  • Registration/dispatch in the main CallToolRequestHandler switch statement, mapping tool name 'read_multiple_files' to the handleReadMultipleFiles handler.
    case "read_multiple_files": result = await handlers.handleReadMultipleFiles(args); break;
  • Zod schema defining input arguments for read_multiple_files tool: array of file paths.
    export const ReadMultipleFilesArgsSchema = z.object({ paths: z.array(z.string()), });
  • src/server.ts:287-305 (registration)
    Tool definition in ListToolsRequestHandler, including name, description, inputSchema from ReadMultipleFilesArgsSchema, and annotations.
    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, }, },
  • Core helper function that reads multiple files in parallel using Promise.all, validates paths, calls readFile for each, handles errors per file, returns array of results with metadata.
    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 }; } }), ); }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/wonderwhy-er/DesktopCommanderMCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server