Skip to main content
Glama
johnreitano

MCP Datastore Server

by johnreitano

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

TableJSON Schema
NameRequiredDescriptionDefault
fieldNoThe field name to filter on (optional)
kindYesThe entity kind to count
valueNoThe value to match exactly (required if field is provided)

Implementation Reference

  • 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'}`);
      }
    }
  • 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'],
      },
    },
  • 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'],
  • 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;
    }
Install Server

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/johnreitano/daisy'

If you have feedback or need assistance with the MCP directory API, please join our Discord server