firestore_get_document
Retrieve a specific document from a Firestore collection by providing the collection name and document ID. This tool enables reading data from Firebase's Firestore database through the Firebase MCP Server interface.
Instructions
Get a document from a Firestore collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| collection | Yes | Collection name | |
| id | Yes | Document ID |
Implementation Reference
- Implements the core logic for retrieving a specific document from a Firestore collection by collection name and document ID, including error handling, timestamp conversion, and generating console URL.export async function getDocument(collection: string, id: string) { try { if (!db) { return { content: [{ type: 'text', text: 'Firebase is not initialized. SERVICE_ACCOUNT_KEY_PATH environment variable is required.' }], isError: true }; } const doc = await db.collection(collection).doc(id).get(); if (!doc.exists) { return { content: [{ type: 'text', text: 'Document not found' }], isError: true }; } const projectId = getProjectId(); const data = doc.data(); 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 getting document: ${(error as Error).message}` }], isError: true }; } }
- src/index.ts:126-139 (schema)Defines the input schema for the firestore_get_document tool, specifying required collection and id parameters.inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name' }, id: { type: 'string', description: 'Document ID' } }, required: ['collection', 'id'] }
- src/index.ts:123-140 (registration)Registers the firestore_get_document tool in the MCP server's tool list, including name, description, and input schema.{ name: 'firestore_get_document', description: 'Get a document from a Firestore collection', inputSchema: { type: 'object', properties: { collection: { type: 'string', description: 'Collection name' }, id: { type: 'string', description: 'Document ID' } }, required: ['collection', 'id'] } },
- src/index.ts:238-239 (registration)Registers the handler dispatch for firestore_get_document tool calls in the CallToolRequestHandler switch statement.case 'firestore_get_document': return getDocument(args.collection as string, args.id as string);
- Utility function to convert Firestore Timestamp objects to ISO date strings, used in document retrieval.function convertTimestampsToISO(data: any) { for (const key in data) { if (data[key] instanceof Timestamp) { data[key] = data[key].toDate().toISOString(); } } return data; }