firestore_update_document
Modify existing document data in a Firestore collection by specifying the collection name, document ID, and updated fields.
Instructions
Update a document in a Firestore collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| id | Yes | Document ID | |
| data | Yes | Updated document data |
Implementation Reference
- The core handler function that updates a Firestore document using the Firebase Admin SDK, handles errors, converts timestamps, and returns a formatted response with console URL.export async function updateDocument(collection: string, id: 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 }; } await db.collection(collection).doc(id).update(data); const projectId = getProjectId(); convertTimestampsToISO(data); const consoleUrl = `https://console.firebase.google.com/project/${projectId}/firestore/data/${collection}/${id}`; return { content: [{ type: 'text', text: JSON.stringify({ id, url: consoleUrl, document: data }) }] }; } catch (error) { return { content: [{ type: 'text', text: `Error updating document: ${(error as Error).message}` }], isError: true }; } }
- src/index.ts:141-162 (registration)Tool registration in the listTools response, including name, description, and input schema.{ name: 'firestore_update_document', description: 'Update a document in a Firestore collection', inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name' }, id: { type: 'string', description: 'Document ID' }, data: { type: 'object', description: 'Updated document data' } }, required: ['collection', 'id', 'data'] } },
- src/index.ts:240-241 (registration)Dispatch in the CallToolRequest handler switch statement that routes to the updateDocument implementation.case 'firestore_update_document': return updateDocument(args.collection as string, args.id as string, args.data as object);
- Helper function used in updateDocument (and others) to convert Firestore Timestamp objects to ISO strings for JSON serialization.function convertTimestampsToISO(data: any) { for (const key in data) { if (data[key] instanceof Timestamp) { data[key] = data[key].toDate().toISOString(); } } return data; }