firestore_list_collections
List collections in Firestore, including root collections or subcollections under a specific document path for database organization.
Instructions
List collections in Firestore. If documentPath is provided, returns subcollections under that document; otherwise returns root collections.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| documentPath | No | Optional parent document path | |
| limit | No | Number of collections to return | |
| pageToken | No | Token for pagination to get the next page of results |
Implementation Reference
- src/lib/firebase/firestoreClient.ts:6-49 (handler)The core handler function implementing the firestore_list_collections tool. Lists root or sub-collections in Firestore with pagination.export async function list_collections(documentPath?: string, limit: number = 20, pageToken?: string) { try { if (!db) { return { content: [{ type: 'text', text: 'Firebase is not initialized. SERVICE_ACCOUNT_KEY_PATH environment variable is required.' }], isError: true }; } let collections; if (documentPath) { const docRef = db.doc(documentPath); collections = await docRef.listCollections(); } else { collections = await db.listCollections(); } // Sort collections by name collections.sort((a, b) => a.id.localeCompare(b.id)); // Find start index const startIndex = pageToken ? collections.findIndex(c => c.id === pageToken) + 1 : 0; // Apply limit const paginatedCollections = collections.slice(startIndex, startIndex + limit); const projectId = getProjectId(); const collectionData = paginatedCollections.map((collection) => { const collectionUrl = `https://console.firebase.google.com/project/${projectId}/firestore/data/${documentPath}/${collection.id}`; return { name: collection.id, url: collectionUrl }; }); return { content: [{ type: 'text', text: JSON.stringify({ collections: collectionData, nextPageToken: collections.length > startIndex + limit ? paginatedCollections[paginatedCollections.length - 1].id : null, hasMore: collections.length > startIndex + limit }) }] }; } catch (error) { return { content: [{ type: 'text', text: `Error listing collections: ${(error as Error).message}` }], isError: true }; } }
- src/index.ts:55-77 (schema)The input schema and metadata registration for the firestore_list_collections tool in the ListTools handler.{ name: 'firestore_list_collections', description: 'List collections in Firestore. If documentPath is provided, returns subcollections under that document; otherwise returns root collections.', inputSchema: { type: 'object', properties: { documentPath: { type: 'string', description: 'Optional parent document path' }, limit: { type: 'number', description: 'Number of collections to return', default: 20 }, pageToken: { type: 'string', description: 'Token for pagination to get the next page of results' } }, required: [] } },
- src/index.ts:244-249 (registration)Tool dispatcher registration in the CallToolRequestHandler switch statement, mapping the tool name to the list_collections handler.case 'firestore_list_collections': return list_collections( args.documentPath as string | undefined, args.limit as number | undefined, args.pageToken as string | undefined );
- src/index.ts:4-4 (registration)Import of the list_collections handler function from firestoreClient.import { addDocument, getDocument, updateDocument, deleteDocument, listDocuments, list_collections } from './lib/firebase/firestoreClient';