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
| Name | Required | Description | Default |
|---|---|---|---|
| document | Yes | Document body to index | |
| id | No | Optional document ID (if not provided, Elasticsearch will generate one) | |
| index | Yes | Name of the Elasticsearch index |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"document": {
"additionalProperties": {},
"description": "Document body to index",
"type": "object"
},
"id": {
"description": "Optional document ID (if not provided, Elasticsearch will generate one)",
"type": "string"
},
"index": {
"description": "Name of the Elasticsearch index",
"minLength": 1,
"type": "string"
}
},
"required": [
"index",
"document"
],
"type": "object"
}
Implementation Reference
- src/index.ts:433-456 (handler)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) }`, }, ], }; } }
- src/index.ts:419-432 (schema)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); }