firestore_add_document
Add a document to a Firestore collection by specifying the collection name and document data. This tool enables structured data storage in Firebase's NoSQL database.
Instructions
Add a document to a Firestore collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| data | Yes | Document data |
Implementation Reference
- Executes the firestore_add_document tool: adds a document to Firestore collection and returns ID, console URL, and processed data.export async function addDocument(collection: string, data: any) { try { if (!db) { return { content: [{ type: 'text', text: 'Firebase is not initialized. SERVICE_ACCOUNT_KEY_PATH environment variable is required.' }], isError: true }; } const docRef = await db.collection(collection).add(data); const projectId = getProjectId(); convertTimestampsToISO(data); const consoleUrl = `https://console.firebase.google.com/project/${projectId}/firestore/data/${collection}/${docRef.id}`; return { content: [{ type: 'text', text: JSON.stringify({ id: docRef.id, url: consoleUrl, document: data }) }] }; } catch (error) { return { content: [{ type: 'text', text: `Error adding document: ${(error as Error).message}` }], isError: true }; } }
- src/index.ts:40-53 (schema)Input schema for firestore_add_document tool defining collection and data parameters.inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name' }, data: { type: 'object', description: 'Document data' } }, required: ['collection', 'data'] }
- src/index.ts:37-54 (registration)Registration of firestore_add_document tool in ListToolsRequestSchema handler.{ name: 'firestore_add_document', description: 'Add a document to a Firestore collection', inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name' }, data: { type: 'object', description: 'Document data' } }, required: ['collection', 'data'] } },
- src/index.ts:229-230 (registration)Dispatch to handler in CallToolRequestSchema switch statement.case 'firestore_add_document': return addDocument(args.collection as string, args.data as object);
- Helper utility to convert Firestore Timestamps to ISO strings, used in addDocument.function convertTimestampsToISO(data: any) { for (const key in data) { if (data[key] instanceof Timestamp) { data[key] = data[key].toDate().toISOString(); } } return data; }