list_files
Retrieve files from Google Drive using filters like folder location, modification date, file type, or specific drive to organize and access your documents efficiently.
Instructions
List files from Google Drive with optional filters (driveId, folderId, modifiedAfter/Before, mimeType)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| driveId | No | ||
| folderId | No | ||
| mimeType | No | ||
| modifiedAfter | No | ||
| modifiedBefore | No | ||
| pageSize | No |
Implementation Reference
- src/mcp/tools/list-files.ts:48-65 (handler)The handler function that implements the core logic of the 'list_files' tool. It calls the Google Drive service to list files based on parameters and formats the response with total count and file list.handler: async (params: { driveId?: string; folderId?: string; modifiedAfter?: string; modifiedBefore?: string; mimeType?: string; pageSize?: number; }) => { const files = await googleDriveService.listFiles(params); const output = { totalFiles: files.length, files }; return { content: [ { type: "text" as const, text: JSON.stringify(output, null, 2) }, ], structuredContent: output, }; },
- src/mcp/tools/list-files.ts:11-46 (schema)Zod schemas defining the input parameters and output structure for the 'list_files' tool, including optional filters like driveId, folderId, date ranges, mimeType, and pageSize.config: { title: "List Google Drive Files", description: "List files from Google Drive with optional filters (driveId, folderId, modifiedAfter/Before, mimeType)", inputSchema: { driveId: z.string().optional().describe("Drive ID to list files from"), folderId: z.string().optional().describe("Folder ID to list files from"), modifiedAfter: z .string() .optional() .describe("Filter files modified after this date (ISO 8601)"), modifiedBefore: z .string() .optional() .describe("Filter files modified before this date (ISO 8601)"), mimeType: z.string().optional().describe("Filter by MIME type"), pageSize: z .number() .optional() .default(100) .describe("Number of files to return (max 1000)"), }, outputSchema: { totalFiles: z.number(), files: z.array( z.object({ id: z.string(), name: z.string(), mimeType: z.string(), modifiedTime: z.string(), size: z.string().optional(), webViewLink: z.string().optional(), parents: z.array(z.string()).optional(), }) ), },
- src/mcp/server.ts:27-30 (registration)Automatic registration loop that registers the 'list_files' tool (imported via tools/index.ts) using McpServer.registerTool with its name, config, and handler.const toolList = Object.values(tools); toolList.forEach((tool) => { server.registerTool(tool.name, tool.config as any, tool.handler as any); });
- src/mcp/tools/index.ts:10-10 (registration)Barrel export that makes the listFilesTool available for import in the server registration.export { listFilesTool } from "@/mcp/tools/list-files.js";
- src/services/drive-service.ts:83-138 (helper)The GoogleDriveService.listFiles method called by the tool handler, which constructs a Google Drive API query with filters and retrieves the list of files.async listFiles(params: ListFilesParams): Promise<DriveFile[]> { // Usar primer Drive si no se especifica uno const driveId = params.driveId || Object.keys(drivesConfigLoader.getConfig().drives)[0]; const drive = await this.getDriveClient(driveId); // Construir query de filtrado de Google Drive const queryParts: string[] = ["trashed = false"]; // Excluir archivos en papelera if (params.folderId) { queryParts.push(`'${params.folderId}' in parents`); } if (params.modifiedAfter) { queryParts.push(`modifiedTime >= '${params.modifiedAfter}'`); } if (params.modifiedBefore) { queryParts.push(`modifiedTime <= '${params.modifiedBefore}'`); } if (params.mimeType) { queryParts.push(`mimeType = '${params.mimeType}'`); } const query = queryParts.join(" and "); try { // Ejecutar consulta a Google Drive API const response = await drive.files.list({ q: query, // Query construido dinámicamente fields: "files(id, name, mimeType, modifiedTime, size, webViewLink, parents)", // Campos específicos pageSize: params.pageSize || 100, // Límite por defecto: 100 archivos orderBy: "modifiedTime desc", // Ordenar por más recientes primero }); const files = response.data.files || []; logger.info(`Listed ${files.length} files from drive: ${driveId}`); // Mapear a tipo DriveFile tipado return files.map((file: Record<string, any>) => ({ id: file.id!, name: file.name!, mimeType: file.mimeType!, modifiedTime: file.modifiedTime!, size: file.size, webViewLink: file.webViewLink, parents: file.parents, })); } catch (error) { logger.error("Error listing files", { driveId, error }); throw error; } }