Skip to main content
Glama
awesimon

Elasticsearch MCP Server

bulk

Import multiple documents into a specified Elasticsearch index in a single operation, with optional document ID field for identification.

Instructions

Bulk data into an Elasticsearch index

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
indexYesTarget Elasticsearch index name
documentsYesArray of documents to import
idFieldNoOptional document ID field name, if specified, the value of this field will be used as the document ID

Implementation Reference

  • The `bulk` function is the core handler that performs bulk import of documents into an Elasticsearch index. It accepts esClient, index name, an array of documents, and an optional idField. It constructs bulk operations with index actions, executes them via esClient.bulk(), and returns a summary with success/failure counts and error details.
    export async function bulk(
      esClient: Client,
      index: string,
      documents: Record<string, any>[],
      idField?: string
    ) {
      try {
        if (!documents || documents.length === 0) {
          return {
            content: [
              {
                type: "text" as const,
                text: "Error: No documents provided for import"
              }
            ]
          };
        }
    
        // 准备批量操作的数据
        const operations: Record<string, any>[] = [];
        
        for (const doc of documents) {
          const action: Record<string, any> = {
            index: { _index: index }
          };
          
          // 如果指定了ID字段且文档中存在该字段,使用该值作为文档ID
          if (idField && doc[idField]) {
            action.index._id = doc[idField];
          }
          
          operations.push(action);
          operations.push(doc);
        }
    
        // 执行批量操作
        const response = await esClient.bulk({
          refresh: true,  // 立即刷新索引,使数据可搜索
          operations
        });
    
        // 处理结果
        const content: { type: "text"; text: string }[] = [];
        
        // 统计成功和失败的操作
        const successCount = response.items.filter(item => !item.index?.error).length;
        const failureCount = response.items.filter(item => item.index?.error).length;
        
        content.push({
          type: "text" as const,
          text: `Bulk import completed:\nTotal documents: ${documents.length}\nSuccessfully imported: ${successCount}\nFailed: ${failureCount}\nProcessing time: ${response.took}ms`
        });
    
        // 如果有失败的操作,添加详细信息
        if (failureCount > 0) {
          const errors = response.items
            .filter(item => item.index?.error)
            .map(item => {
              const error = item.index?.error;
              const id = item.index?._id || 'unknown';
              return `ID: ${id} - Error type: ${error?.type}, Reason: ${error?.reason}`;
            });
          
          content.push({
            type: "text" as const,
            text: `Failed details:\n${errors.join('\n')}`
          });
        }
    
        return {
          content
        };
      } catch (error) {
        console.error(`Bulk import failed: ${error instanceof Error ? error.message : String(error)}`);
        return {
          content: [
            {
              type: "text" as const,
              text: `Error: ${error instanceof Error ? error.message : String(error)}`
            }
          ]
        };
      }
    }
  • src/server.ts:168-192 (registration)
    The 'bulk' tool is registered as an MCP tool using server.tool() with name 'bulk'. It defines input schema: index (required string), documents (required array of objects), and idField (optional string). The handler calls the bulk() function from tools/bulk.ts.
    // Bulk import data into an Elasticsearch index
    server.tool(
      "bulk",
      "Bulk data into an Elasticsearch index",
      {
        index: z
          .string()
          .trim()
          .min(1, "Index name is required")
          .describe("Target Elasticsearch index name"),
        
        documents: z
          .array(z.record(z.any()))
          .min(1, "At least one document is required")
          .describe("Array of documents to import"),
        
        idField: z
          .string()
          .optional()
          .describe("Optional document ID field name, if specified, the value of this field will be used as the document ID")
      },
      async ({ index, documents, idField }) => {
        return await bulk(esClient, index, documents, idField);
      }
    );
  • Input schema for the bulk tool: index (z.string, required), documents (z.array(z.record(z.any())), at least 1 required), idField (z.string, optional). These are Zod schemas used for input validation.
    {
      index: z
        .string()
        .trim()
        .min(1, "Index name is required")
        .describe("Target Elasticsearch index name"),
      
      documents: z
        .array(z.record(z.any()))
        .min(1, "At least one document is required")
        .describe("Array of documents to import"),
      
      idField: z
        .string()
        .optional()
        .describe("Optional document ID field name, if specified, the value of this field will be used as the document ID")
    },
  • src/server.ts:15-27 (registration)
    The bulk function is exported from server.ts, making it available as part of the module's public API.
    export { 
      listIndices, 
      getMappings, 
      search, 
      getClusterHealth, 
      createIndex, 
      createMapping, 
      bulk, 
      reindex, 
      createIndexTemplate,
      getIndexTemplate,
      deleteIndexTemplate
    }; 
  • src/server.ts:11-11 (registration)
    The bulk function is imported from './tools/bulk.js' into server.ts for registration and use.
    import { bulk } from "./tools/bulk.js";
Behavior1/5

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

Without annotations, the description must disclose behavior. It only says 'Bulk data', omitting whether it overwrites, appends, error handling, limits, or idempotency. Completely opaque.

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

Conciseness3/5

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

The description is concise (one sentence), but this brevity sacrifices critical detail. It is not front-loaded with essential information.

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?

No output schema or return value information is provided. For a bulk operation, details on success/failure reporting, batch limits, and error handling are missing, leaving the tool incomplete.

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

Parameters2/5

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

Although the schema covers all parameters with descriptions, the tool description adds no extra meaning. It does not explain how the parameters interact (e.g., how idField is used), offering minimal value.

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

Purpose2/5

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

The description uses 'Bulk' as a verb, which is ambiguous and does not clearly specify the action (e.g., indexing, updating). It fails to distinguish from sibling tools like 'index' or 'reindex'.

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?

No guidance is provided on when to use this tool versus alternatives like create_index, reindex, or search. The description does not mention context or prerequisites.

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

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

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