Skip to main content
Glama
Octodet

octodet-elasticsearch-mcp

add_document

Insert or update documents into a specified Elasticsearch index, enabling structured data storage and retrieval for efficient search operations.

Instructions

Add a new document to a specific Elasticsearch index

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
documentYesDocument body to index
idNoOptional document ID (if not provided, Elasticsearch will generate one)
indexYesName of the Elasticsearch index

Implementation Reference

  • The handler function for the 'add_document' MCP tool. It calls the Elasticsearch service to add the document and returns a formatted response or error message.
    async ({ index, id, document }) => {
      try {
        const response = await esService.addDocument(index, document, id);
        return {
          content: [
            {
              type: "text",
              text: `Document added to index '${index}' with ID: ${response._id}`,
            },
          ],
        };
      } catch (error) {
        return {
          content: [
            {
              type: "text",
              text: `Error adding document: ${
                error instanceof Error ? error.message : String(error)
              }`,
            },
          ],
        };
      }
    }
  • Zod input schema defining parameters for the add_document tool: required index name, optional id, and document body.
    {
      index: z
        .string()
        .trim()
        .min(1, "Index name is required")
        .describe("Name of the Elasticsearch index"),
      id: z
        .string()
        .optional()
        .describe(
          "Optional document ID (if not provided, Elasticsearch will generate one)"
        ),
      document: z.record(z.any()).describe("Document body to index"),
    },
  • src/index.ts:416-457 (registration)
    Registration of the 'add_document' tool on the MCP server using server.tool, specifying name, description, input schema, and handler.
    server.tool(
      "add_document",
      "Add a new document to a specific Elasticsearch index",
      {
        index: z
          .string()
          .trim()
          .min(1, "Index name is required")
          .describe("Name of the Elasticsearch index"),
        id: z
          .string()
          .optional()
          .describe(
            "Optional document ID (if not provided, Elasticsearch will generate one)"
          ),
        document: z.record(z.any()).describe("Document body to index"),
      },
      async ({ index, id, document }) => {
        try {
          const response = await esService.addDocument(index, document, id);
          return {
            content: [
              {
                type: "text",
                text: `Document added to index '${index}' with ID: ${response._id}`,
              },
            ],
          };
        } catch (error) {
          return {
            content: [
              {
                type: "text",
                text: `Error adding document: ${
                  error instanceof Error ? error.message : String(error)
                }`,
              },
            ],
          };
        }
      }
    );
  • Supporting helper method in ElasticsearchService class that performs the core Elasticsearch client.index operation to add the document.
    async addDocument(index: string, document: any, id?: string): Promise<any> {
      const params: any = { index, document };
      if (id) params.id = id;
      return await this.client.index(params);
    }
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