elasticsearch_health
Monitor the health status of an Elasticsearch cluster and optionally retrieve index-level details to assess and manage cluster performance.
Instructions
Get the health status of the Elasticsearch cluster, optionally include index-level details
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| includeIndices | No | Whether to include index-level details |
Implementation Reference
- src/tools/getClusterHealth.ts:3-50 (handler)Implements the core logic for the 'elasticsearch_health' tool: fetches cluster health from Elasticsearch client, optionally includes index details, formats into MCP-compatible text content, and handles errors.export async function getClusterHealth( esClient: Client, includeIndices: boolean = false ) { try { const response = await esClient.cluster.health({ level: includeIndices ? "indices" : "cluster" }); const content: { type: "text"; text: string }[] = []; // 添加集群状态概述 content.push({ type: "text" as const, text: `Cluster Name: ${response.cluster_name}\nStatus: ${response.status}\nNodes: ${response.number_of_nodes}\nData Nodes: ${response.number_of_data_nodes}\nActive Shards: ${response.active_shards}\nActive Primary Shards: ${response.active_primary_shards}\nRelocating Shards: ${response.relocating_shards}\nInitializing Shards: ${response.initializing_shards}\nUnassigned Shards: ${response.unassigned_shards}\nPending Tasks: ${response.number_of_pending_tasks}\n` }); // 如果请求了索引级别的健康状态 if (includeIndices && response.indices) { const indicesHealth: string[] = []; for (const [indexName, indexHealth] of Object.entries(response.indices)) { indicesHealth.push(`Index: ${indexName}\n Status: ${indexHealth.status}\n Primary Shards: ${indexHealth.number_of_shards}\n Replicas: ${indexHealth.number_of_replicas}\n Active Shards: ${indexHealth.active_shards}\n Active Primary Shards: ${indexHealth.active_primary_shards}\n Unassigned Shards: ${indexHealth.unassigned_shards}`); } if (indicesHealth.length > 0) { content.push({ type: "text" as const, text: `\nIndices Health Status:\n${indicesHealth.join('\n\n')}` }); } } 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:107-120 (registration)Registers the 'elasticsearch_health' MCP tool with name, description, input schema (includeIndices boolean), and delegates to the getClusterHealth handler.server.tool( "elasticsearch_health", "Get the health status of the Elasticsearch cluster, optionally include index-level details", { includeIndices: z .boolean() .optional() .default(false) .describe("Whether to include index-level details"), }, async ({ includeIndices }) => { return await getClusterHealth(esClient, includeIndices); } );
- src/server.ts:111-116 (schema)Zod input schema for the tool: optional boolean 'includeIndices' to control index-level details in health response.includeIndices: z .boolean() .optional() .default(false) .describe("Whether to include index-level details"), },