Skip to main content
Glama
Octodet

octodet-elasticsearch-mcp

count_documents

Count documents within a specified Elasticsearch index, optionally filtered by a query, to efficiently track data volume using the octodet-elasticsearch-mcp server.

Instructions

Count documents in an index, optionally filtered by a query

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
indexYesName of the Elasticsearch index to count documents in
queryNoOptional Elasticsearch query to filter documents to count

Implementation Reference

  • Handler function that executes the count_documents tool logic by calling esService.countDocuments, formatting the success response or error message.
    async ({ index, query }) => {
      try {
        const count = await esService.countDocuments(index, query);
        return {
          content: [
            {
              type: "text",
              text: `Count of documents in index '${index}'${
                query ? " matching the provided query" : ""
              }: ${count}`,
            },
          ],
        };
      } catch (error) {
        console.error(
          `Failed to count documents: ${
            error instanceof Error ? error.message : String(error)
          }`
        );
        return {
          content: [
            {
              type: "text",
              text: `Error: ${
                error instanceof Error ? error.message : String(error)
              }`,
            },
          ],
        };
      }
    }
  • Input schema using Zod for the count_documents tool: required 'index' string and optional 'query' object.
    {
      index: z
        .string()
        .trim()
        .min(1, "Index name is required")
        .describe("Name of the Elasticsearch index to count documents in"),
      query: z
        .record(z.any())
        .optional()
        .describe("Optional Elasticsearch query to filter documents to count"),
    },
  • src/index.ts:1030-1075 (registration)
    Registration of the 'count_documents' tool on the MCP server using server.tool().
    server.tool(
      "count_documents",
      "Count documents in an index, optionally filtered by a query",
      {
        index: z
          .string()
          .trim()
          .min(1, "Index name is required")
          .describe("Name of the Elasticsearch index to count documents in"),
        query: z
          .record(z.any())
          .optional()
          .describe("Optional Elasticsearch query to filter documents to count"),
      },
      async ({ index, query }) => {
        try {
          const count = await esService.countDocuments(index, query);
          return {
            content: [
              {
                type: "text",
                text: `Count of documents in index '${index}'${
                  query ? " matching the provided query" : ""
                }: ${count}`,
              },
            ],
          };
        } catch (error) {
          console.error(
            `Failed to count documents: ${
              error instanceof Error ? error.message : String(error)
            }`
          );
          return {
            content: [
              {
                type: "text",
                text: `Error: ${
                  error instanceof Error ? error.message : String(error)
                }`,
              },
            ],
          };
        }
      }
    );
  • Helper method in ElasticsearchService class that performs the actual document count using Elasticsearch client.count API.
    async countDocuments(index: string, query?: any): Promise<number> {
      const response = await this.client.count({
        index,
        ...(query ? { query } : {}),
      });
    
      return response.count;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool counts documents with optional filtering, but doesn't mention performance characteristics (e.g., whether it's efficient for large indices), error handling, or what the return value looks like (e.g., a numeric count or structured response). For a tool with no annotation coverage, this leaves significant gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose ('count documents in an index') and adds a useful qualifier ('optionally filtered by a query'). There is no wasted language or redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's moderate complexity (counting with filtering), lack of annotations, and no output schema, the description is minimally adequate. It covers the basic operation but doesn't address behavioral aspects like return format, error cases, or performance implications, which would be helpful for an agent to use it effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents both parameters ('index' and 'query') with clear descriptions. The description adds minimal value beyond the schema by mentioning optional filtering, but doesn't provide additional context like query format examples or index naming conventions. Baseline 3 is appropriate when the schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose with a specific verb ('count') and resource ('documents in an index'), and mentions optional filtering. However, it doesn't explicitly differentiate from sibling tools like 'search' (which might also count) or 'list_indices' (which lists indices rather than counting documents within them).

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'search' (which could return counts) or 'list_indices' (for index-level operations). It mentions optional filtering but doesn't explain when filtering is appropriate or what other tools might be better for related tasks.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

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/Octodet/elasticsearch-mcp'

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