create_index
Create an Elasticsearch index with optional settings and mappings to define field types and index behavior.
Instructions
Create an Elasticsearch index, optionally configure settings and mappings
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"index": "<index>"
},
"name": "create_index"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | Yes | Name of the Elasticsearch index to create | |
| settings | No | Index settings, such as number of shards and replicas | |
| mappings | No | Index mappings, defining field types, etc. |
Implementation Reference
- src/tools/createIndex.ts:3-53 (handler)The handler function that executes the create_index tool's logic. It takes esClient, index, optional settings and mappings, calls esClient.indices.create() to create the Elasticsearch index, and returns a response with success/error text.
export async function createIndex( esClient: Client, index: string, settings?: Record<string, any>, mappings?: Record<string, any> ) { try { const body: Record<string, any> = {}; if (settings) { body.settings = settings; } if (mappings) { body.mappings = mappings; } const response = await esClient.indices.create({ index, ...body }); const content: { type: "text"; text: string }[] = []; if (response.acknowledged) { content.push({ type: "text" as const, text: `索引 "${index}" 创建成功!\n分片数: ${response.shards_acknowledged ? '已确认' : '等待确认'}` }); } else { content.push({ type: "text" as const, text: `索引 "${index}" 创建请求已发送,但未得到确认。请检查集群状态。` }); } return { content }; } catch (error) { console.error(`创建索引失败: ${error instanceof Error ? error.message : String(error)}`); return { content: [ { type: "text" as const, text: `错误: ${error instanceof Error ? error.message : String(error)}` } ] }; } } - src/server.ts:122-146 (registration)Registration of the 'create_index' tool on the MCP server via server.tool(), defining the schema with Zod for index (required string), settings (optional Record), and mappings (optional Record), and delegating to the createIndex handler.
// Create an Elasticsearch index, optionally configure settings and mappings server.tool( "create_index", "Create an Elasticsearch index, optionally configure settings and mappings", { index: z .string() .trim() .min(1, "Index name is required") .describe("Name of the Elasticsearch index to create"), settings: z .record(z.any()) .optional() .describe("Index settings, such as number of shards and replicas"), mappings: z .record(z.any()) .optional() .describe("Index mappings, defining field types, etc.") }, async ({ index, settings, mappings }) => { return await createIndex(esClient, index, settings, mappings); } ); - src/server.ts:126-142 (schema)Input parameter schemas for create_index: 'index' (required string, trimmed, min 1), 'settings' (optional Record<string, any>), 'mappings' (optional Record<string, any>).
{ index: z .string() .trim() .min(1, "Index name is required") .describe("Name of the Elasticsearch index to create"), settings: z .record(z.any()) .optional() .describe("Index settings, such as number of shards and replicas"), mappings: z .record(z.any()) .optional() .describe("Index mappings, defining field types, etc.") }, - src/server.ts:9-9 (helper)Import of the createIndex function from the tools/createIndex module.
import { createIndex } from "./tools/createIndex.js";