Skip to main content
Glama
Octodet

octodet-elasticsearch-mcp

bulk

Efficiently execute multiple Elasticsearch document operations—create, update, delete—in one API call, reducing requests and optimizing performance.

Instructions

Perform multiple document operations (create, update, delete) in a single API call

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
operationsYesArray of operations to perform in bulk
pipelineNoOptional pipeline to use for preprocessing documents

Implementation Reference

  • Main handler logic for the 'bulk' tool: validates input operations, builds Elasticsearch bulk operations array in correct format, calls esService.bulk, processes the response to summarize successes/failures, formats and returns text output.
    async ({ operations, pipeline }) => {
      try {
        // Validate operations
        operations.forEach((op, idx) => {
          if ((op.action === "update" || op.action === "delete") && !op.id) {
            throw new Error(
              `Operation #${idx + 1} (${op.action}): Document ID is required`
            );
          }
          if (
            (op.action === "index" ||
              op.action === "create" ||
              op.action === "update") &&
            !op.document
          ) {
            throw new Error(
              `Operation #${idx + 1} (${op.action}): Document body is required`
            );
          }
        });
    
        // Build the bulk operations array
        const bulkOperations: any[] = [];
    
        operations.forEach((op) => {
          const actionMeta: any = { _index: op.index };
          if (op.id) actionMeta._id = op.id;
    
          bulkOperations.push({ [op.action]: actionMeta });
    
          if (op.action !== "delete") {
            if (op.action === "update") {
              bulkOperations.push({ doc: op.document });
            } else {
              bulkOperations.push(op.document);
            }
          }
        });
    
        // Execute the bulk operation
        const response = await esService.bulk(bulkOperations, pipeline);
    
        // Process the response
        const summary = {
          took: response.took,
          errors: response.errors,
          successes: 0,
          failures: 0,
          actionResults: [] as any[],
        };
    
        // Count successes and failures
        response.items.forEach((item: any, idx: number) => {
          const actionType = Object.keys(item)[0];
          const result = item[actionType as keyof typeof item] as any;
    
          if (!result) return;
    
          if (result.error) {
            summary.failures++;
            summary.actionResults.push({
              operation: idx,
              action: actionType,
              id: result._id || "unknown",
              index: result._index || "unknown",
              status: result.status || 0,
              error: {
                type: result.error?.type || "unknown_error",
                reason: result.error?.reason || "Unknown error",
              },
            });
          } else {
            summary.successes++;
            summary.actionResults.push({
              operation: idx,
              action: actionType,
              id: result._id || "unknown",
              index: result._index || "unknown",
              status: result.status || 0,
              result: result.result || "unknown",
            });
          }
        });
    
        // Format the response
        let resultText = `Bulk operation completed in ${summary.took}ms\n`;
        resultText += `- Total operations: ${operations.length}\n`;
        resultText += `- Successful: ${summary.successes}\n`;
        resultText += `- Failed: ${summary.failures}\n`;
    
        // Add failure details
        if (summary.failures > 0) {
          resultText += "\nFailures:\n";
          const failures = summary.actionResults.filter((r) => r.error);
          failures.slice(0, 5).forEach((failure, idx) => {
            resultText += `${idx + 1}. Operation #${failure.operation} (${
              failure.action
            }): ${failure.error.reason} [${failure.error.type}]\n`;
          });
    
          if (failures.length > 5) {
            resultText += `...and ${failures.length - 5} more failures.\n`;
          }
        }
    
        return {
          content: [{ type: "text", text: resultText }],
        };
      } catch (error) {
        console.error(
          `Bulk operation failed: ${
            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 'bulk' tool parameters: operations array and optional pipeline.
    {
      operations: z
        .array(
          z.object({
            action: z
              .enum(["index", "create", "update", "delete"])
              .describe(
                "The action to perform: index (create/replace), create (fail if exists), update, or delete"
              ),
            index: z
              .string()
              .trim()
              .min(1, "Index name is required")
              .describe("Name of the Elasticsearch index for this operation"),
            id: z
              .string()
              .optional()
              .describe(
                "Document ID (required for update and delete, optional for index/create)"
              ),
            document: z
              .record(z.any())
              .optional()
              .describe(
                "Document body (required for index/create/update, not used for delete)"
              ),
          })
        )
        .min(1, "At least one operation is required")
        .describe("Array of operations to perform in bulk"),
      pipeline: z
        .string()
        .optional()
        .describe("Optional pipeline to use for preprocessing documents"),
    },
  • src/index.ts:770-934 (registration)
    Registration of the 'bulk' MCP tool via server.tool, specifying name, description, input schema, and handler function.
    server.tool(
      "bulk",
      "Perform multiple document operations (create, update, delete) in a single API call",
      {
        operations: z
          .array(
            z.object({
              action: z
                .enum(["index", "create", "update", "delete"])
                .describe(
                  "The action to perform: index (create/replace), create (fail if exists), update, or delete"
                ),
              index: z
                .string()
                .trim()
                .min(1, "Index name is required")
                .describe("Name of the Elasticsearch index for this operation"),
              id: z
                .string()
                .optional()
                .describe(
                  "Document ID (required for update and delete, optional for index/create)"
                ),
              document: z
                .record(z.any())
                .optional()
                .describe(
                  "Document body (required for index/create/update, not used for delete)"
                ),
            })
          )
          .min(1, "At least one operation is required")
          .describe("Array of operations to perform in bulk"),
        pipeline: z
          .string()
          .optional()
          .describe("Optional pipeline to use for preprocessing documents"),
      },
      async ({ operations, pipeline }) => {
        try {
          // Validate operations
          operations.forEach((op, idx) => {
            if ((op.action === "update" || op.action === "delete") && !op.id) {
              throw new Error(
                `Operation #${idx + 1} (${op.action}): Document ID is required`
              );
            }
            if (
              (op.action === "index" ||
                op.action === "create" ||
                op.action === "update") &&
              !op.document
            ) {
              throw new Error(
                `Operation #${idx + 1} (${op.action}): Document body is required`
              );
            }
          });
    
          // Build the bulk operations array
          const bulkOperations: any[] = [];
    
          operations.forEach((op) => {
            const actionMeta: any = { _index: op.index };
            if (op.id) actionMeta._id = op.id;
    
            bulkOperations.push({ [op.action]: actionMeta });
    
            if (op.action !== "delete") {
              if (op.action === "update") {
                bulkOperations.push({ doc: op.document });
              } else {
                bulkOperations.push(op.document);
              }
            }
          });
    
          // Execute the bulk operation
          const response = await esService.bulk(bulkOperations, pipeline);
    
          // Process the response
          const summary = {
            took: response.took,
            errors: response.errors,
            successes: 0,
            failures: 0,
            actionResults: [] as any[],
          };
    
          // Count successes and failures
          response.items.forEach((item: any, idx: number) => {
            const actionType = Object.keys(item)[0];
            const result = item[actionType as keyof typeof item] as any;
    
            if (!result) return;
    
            if (result.error) {
              summary.failures++;
              summary.actionResults.push({
                operation: idx,
                action: actionType,
                id: result._id || "unknown",
                index: result._index || "unknown",
                status: result.status || 0,
                error: {
                  type: result.error?.type || "unknown_error",
                  reason: result.error?.reason || "Unknown error",
                },
              });
            } else {
              summary.successes++;
              summary.actionResults.push({
                operation: idx,
                action: actionType,
                id: result._id || "unknown",
                index: result._index || "unknown",
                status: result.status || 0,
                result: result.result || "unknown",
              });
            }
          });
    
          // Format the response
          let resultText = `Bulk operation completed in ${summary.took}ms\n`;
          resultText += `- Total operations: ${operations.length}\n`;
          resultText += `- Successful: ${summary.successes}\n`;
          resultText += `- Failed: ${summary.failures}\n`;
    
          // Add failure details
          if (summary.failures > 0) {
            resultText += "\nFailures:\n";
            const failures = summary.actionResults.filter((r) => r.error);
            failures.slice(0, 5).forEach((failure, idx) => {
              resultText += `${idx + 1}. Operation #${failure.operation} (${
                failure.action
              }): ${failure.error.reason} [${failure.error.type}]\n`;
            });
    
            if (failures.length > 5) {
              resultText += `...and ${failures.length - 5} more failures.\n`;
            }
          }
    
          return {
            content: [{ type: "text", text: resultText }],
          };
        } catch (error) {
          console.error(
            `Bulk operation failed: ${
              error instanceof Error ? error.message : String(error)
            }`
          );
          return {
            content: [
              {
                type: "text",
                text: `Error: ${
                  error instanceof Error ? error.message : String(error)
                }`,
              },
            ],
          };
        }
      }
    );
  • Helper method in ElasticsearchService that executes the Elasticsearch bulk API call with automatic refresh and optional pipeline.
    async bulk(operations: any[], pipeline?: string): Promise<any> {
      return await this.client.bulk({
        refresh: true,
        pipeline,
        operations,
      });
    }
  • TypeScript interface defining the structure of the bulk operation result from Elasticsearch.
    export interface BulkOperationResult {
      took: number;
      errors: boolean;
      items: Array<Record<string, any>>;
    }
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. It states the tool performs 'multiple document operations' but lacks critical behavioral details: whether operations are atomic, error handling for partial failures, rate limits, authentication requirements, or response format. For a mutation tool with no annotation coverage, this is a significant gap.

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 functionality. It uses no unnecessary words and directly communicates the tool's essence without redundancy.

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

Completeness2/5

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

For a complex mutation tool with no annotations and no output schema, the description is inadequate. It doesn't cover behavioral traits like atomicity or error handling, lacks usage guidelines compared to siblings, and provides no output information. The high schema coverage doesn't compensate for these gaps in context.

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 fully documents both parameters ('operations' and 'pipeline'). The description adds no parameter-specific semantics beyond implying bulk operations. It doesn't explain the 'operations' array structure or 'pipeline' usage beyond what the schema provides, meeting the baseline for high schema coverage.

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: 'Perform multiple document operations (create, update, delete) in a single API call.' It specifies the verb ('perform'), resource ('document operations'), and scope ('multiple...in a single API call'). However, it doesn't explicitly differentiate from siblings like 'add_document' or 'update_document' beyond the bulk aspect.

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. It doesn't mention prerequisites, performance considerations, or compare it to single-operation siblings like 'add_document' or 'update_document'. The agent must infer usage from the 'bulk' nature alone.

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