get_shards
Retrieve shard details for all or specified indices in the octodet-elasticsearch-mcp server to monitor and manage Elasticsearch cluster data distribution.
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
- src/index.ts:377-411 (handler)The MCP tool handler function for 'get_shards' that calls the Elasticsearch service method and formats the response as MCP-compliant content.async ({ index }) => { try { const shardsInfo = await esService.getShards(index); return { content: [ { type: "text", text: `Found ${shardsInfo.length} shards${ index ? ` for index ${index}` : "" }`, }, { type: "text", 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", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; }
- Core helper function in ElasticsearchService that executes the cat.shards API and maps results to ShardInfo array.async getShards(index?: string): Promise<ShardInfo[]> { const response = await this.client.cat.shards({ index, format: "json", }); return response.map((shard: any) => ({ index: shard.index, shard: shard.shard, prirep: shard.prirep, state: shard.state, docs: shard.docs, store: shard.store, ip: shard.ip, node: shard.node, })); }
- src/utils/models.ts:25-33 (schema)TypeScript interface defining the structure of individual shard information objects returned by the getShards function.export interface ShardInfo { index: string; shard: string; prirep: string; state: string; docs: string; store: string; ip: string; node: string;
- src/index.ts:368-413 (registration)Registration of the 'get_shards' tool on the MCP server, including name, description, input schema, and handler reference.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 }) => { try { const shardsInfo = await esService.getShards(index); return { content: [ { type: "text", text: `Found ${shardsInfo.length} shards${ index ? ` for index ${index}` : "" }`, }, { type: "text", 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", text: `Error: ${ error instanceof Error ? error.message : String(error) }`, }, ], }; } } );
- src/index.ts:371-375 (schema)Zod schema for the input parameter 'index' of the get_shards tool.{ index: z .string() .optional() .describe("Optional index name to get shard information for"),