datastore_count
Query and count entities in Google Cloud Datastore by specifying the entity kind and optional filters to match exact field values.
Instructions
Count entities in a kind, optionally with a filter
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| field | No | The field name to filter on (optional) | |
| kind | Yes | The entity kind to count | |
| value | No | The value to match exactly (required if field is provided) |
Input Schema (JSON Schema)
{
"properties": {
"field": {
"description": "The field name to filter on (optional)",
"type": "string"
},
"kind": {
"description": "The entity kind to count",
"type": "string"
},
"value": {
"description": "The value to match exactly (required if field is provided)",
"type": "string"
}
},
"required": [
"kind"
],
"type": "object"
}
Implementation Reference
- src/datastore.ts:94-114 (handler)Core implementation of the datastore_count tool logic: creates a Datastore query for the given kind, applies optional filter on field/value, runs the query, and returns the count of matching entities.async countEntities(kind: string, field?: string, value?: string): Promise<number> { try { let query = this.datastore.createQuery(kind).select('__key__'); if (field && value !== undefined) { if (field === '__key__' || field === 'key') { const keyValue = isNaN(Number(value)) ? value : parseInt(value); const entityKey = this.datastore.key([kind, keyValue]); query = query.filter('__key__', '=', entityKey); } else { const convertedValue = this.convertValue(value); query = query.filter(field, '=', convertedValue); } } const [entities] = await this.datastore.runQuery(query); return entities.length; } catch (error) { throw new Error(`Failed to count entities: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:197-210 (handler)MCP CallTool handler for 'datastore_count': extracts arguments, calls DatastoreClient.countEntities, formats and returns the count as text content.case 'datastore_count': const count = await datastoreClient.countEntities( args.kind as string, args.field as string | undefined, args.value as string | undefined ); return { content: [ { type: 'text', text: `Count: ${count}`, }, ], };
- src/index.ts:106-127 (registration)Registers the 'datastore_count' tool in the ListTools response, including name, description, and input schema.{ name: 'datastore_count', description: 'Count entities in a kind, optionally with a filter', inputSchema: { type: 'object', properties: { kind: { type: 'string', description: 'The entity kind to count', }, field: { type: 'string', description: 'The field name to filter on (optional)', }, value: { type: 'string', description: 'The value to match exactly (required if field is provided)', }, }, required: ['kind'], }, },
- src/index.ts:109-125 (schema)Input schema definition for the datastore_count tool, specifying parameters: kind (required), field and value (optional for filtering).inputSchema: { type: 'object', properties: { kind: { type: 'string', description: 'The entity kind to count', }, field: { type: 'string', description: 'The field name to filter on (optional)', }, value: { type: 'string', description: 'The value to match exactly (required if field is provided)', }, }, required: ['kind'],
- src/datastore.ts:116-125 (helper)Helper method used in countEntities (and others) to convert string filter values to appropriate types (boolean, number, etc.).private convertValue(value: string): any { if (value.toLowerCase() === 'true') return true; if (value.toLowerCase() === 'false') return false; if (value.toLowerCase() === 'null') return null; const numValue = Number(value); if (!isNaN(numValue)) return numValue; return value; }