Skip to main content
Glama
awesimon

Elasticsearch MCP Server

create_mapping

Create or update the mapping structure for an Elasticsearch index. Specify the index name and field type definitions to control how documents are indexed and searched.

Instructions

Create or update the mapping structure of an Elasticsearch index

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
indexYesElasticsearch index name
mappingsYesIndex mappings, defining field types, etc.

Implementation Reference

  • The main handler function for the 'create_mapping' tool. It checks if an index exists, creates/updates mappings accordingly, and returns the current mapping structure.
    import { Client } from "@elastic/elasticsearch";
    
    export async function createMapping(
      esClient: Client,
      index: string,
      mappings: Record<string, any>
    ) {
      try {
        // check if index exists
        const indexExists = await esClient.indices.exists({ index });
        
        let response;
        const content: { type: "text"; text: string }[] = [];
        
        if (!indexExists) {
          // if index does not exist, create it and apply mapping
          response = await esClient.indices.create({
            index,
            mappings
          });
          
          content.push({
            type: "text" as const,
            text: `Index "${index}" does not exist. Created new index and applied mapping.`
          });
        } else {
          // if index exists, update mapping
          response = await esClient.indices.putMapping({
            index,
            ...mappings
          });
          
          content.push({
            type: "text" as const,
            text: `Updated mapping for index "${index}".`
          });
        }
    
        // get current mapping structure
        const updatedMappings = await esClient.indices.getMapping({ index });
        
        content.push({
          type: "text" as const,
          text: `\nCurrent mapping structure:\n${JSON.stringify(updatedMappings[index].mappings, null, 2)}`
        });
    
        return {
          content
        };
      } catch (error) {
        console.error(`Failed to set mapping: ${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:148-166 (registration)
    Registration of the 'create_mapping' tool using server.tool() with Zod schema for input validation (index and mappings parameters).
    // Create or update the mapping structure of an Elasticsearch index
    server.tool(
      "create_mapping",
      "Create or update the mapping structure of an Elasticsearch index",
      {
        index: z
          .string()
          .trim()
          .min(1, "Index name is required")
          .describe("Elasticsearch index name"),
        
        mappings: z
          .record(z.any())
          .describe("Index mappings, defining field types, etc.")
      },
      async ({ index, mappings }) => {
        return await createMapping(esClient, index, mappings);
      }
    );
  • Import of the createMapping function from its implementation file.
    import { createMapping } from "./tools/createMapping.js";
  • Re-export of createMapping from the server module.
    createMapping, 
  • Zod schema definitions for the 'create_mapping' tool input validation: 'index' (required string) and 'mappings' (required record of any).
    {
      index: z
        .string()
        .trim()
        .min(1, "Index name is required")
        .describe("Elasticsearch index name"),
      
      mappings: z
        .record(z.any())
        .describe("Index mappings, defining field types, etc.")
Behavior2/5

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

No annotations are provided, and the description does not disclose whether the operation is destructive or merges with existing mappings. The term 'create or update' is ambiguous; it is unclear if existing mappings are replaced or added to.

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

Conciseness4/5

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

The description is a single sentence with no filler words. It is concise but could be slightly more informative without becoming verbose.

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?

Given the lack of output schema and annotations, the description should explain return values, error cases, or idempotency. It does not cover these, leaving the agent underinformed about the tool's behavior.

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?

The input schema has 100% coverage with descriptions for both parameters. The description does not add additional meaning beyond what the schema already provides, so it meets the baseline but does not enhance understanding.

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

Purpose5/5

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

The description explicitly states the action 'Create or update' and the resource 'mapping structure of an Elasticsearch index'. It clearly distinguishes from sibling tools like 'create_index' (which creates an index) and 'get_mappings' (which retrieves mappings).

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 on when to use this tool versus alternatives like 'create_index' or 'get_mappings'. The description mentions 'create or update' but does not specify prerequisites (e.g., index must exist) or when to choose update over creation.

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