get_shards
Retrieve detailed shard information for all or specific indices in an Elasticsearch database, enabling efficient index management and monitoring.
Instructions
Get shard information for all or specific indices
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| index | No | Optional index name to get shard information for |
Implementation Reference
- index.ts:650-702 (handler)Handler function that retrieves shard information from Elasticsearch using esClient.cat.shards, formats it into a structured array, and returns metadata and JSON stringified content.async ({ index }) => { console.error("[DEBUG] get_shards tool called", index); try { const response = await esClient.cat.shards({ index, format: "json", }); const shardsInfo = response.map((shard) => ({ index: shard.index, shard: shard.shard, prirep: shard.prirep, state: shard.state, docs: shard.docs, store: shard.store, ip: shard.ip, node: shard.node, })); const metadataFragment = { type: "text" as const, text: `Found ${shardsInfo.length} shards${ index ? ` for index ${index}` : "" }`, }; return { content: [ metadataFragment, { type: "text" as const, text: JSON.stringify(shardsInfo, null, 2), }, ], }; } catch (error) { console.error( `Failed to get shard information: ${ error instanceof Error ? error.message : String(error) }` ); return { content: [ { type: "text" as const, text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } }
- index.ts:645-649 (schema)Zod schema defining the optional 'index' parameter as a string for specifying the Elasticsearch index.index: z .string() .optional() .describe("Optional index name to get shard information for"), },
- index.ts:641-703 (registration)Full registration of the 'get_shards' tool using McpServer.tool method, specifying name, description, input schema, and handler function.server.tool( "get_shards", "Get shard information for all or specific indices", { index: z .string() .optional() .describe("Optional index name to get shard information for"), }, async ({ index }) => { console.error("[DEBUG] get_shards tool called", index); try { const response = await esClient.cat.shards({ index, format: "json", }); const shardsInfo = response.map((shard) => ({ index: shard.index, shard: shard.shard, prirep: shard.prirep, state: shard.state, docs: shard.docs, store: shard.store, ip: shard.ip, node: shard.node, })); const metadataFragment = { type: "text" as const, text: `Found ${shardsInfo.length} shards${ index ? ` for index ${index}` : "" }`, }; return { content: [ metadataFragment, { type: "text" as const, text: JSON.stringify(shardsInfo, null, 2), }, ], }; } catch (error) { console.error( `Failed to get shard information: ${ error instanceof Error ? error.message : String(error) }` ); return { content: [ { type: "text" as const, text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } } );