Skip to content

Commit 2abcaa6

Browse files
authored
Add get_shards tool for monitoring shard status (#25)
* feat: add tool to retrieve shard information for indices * docs: add get_shards tool
1 parent 9746d00 commit 2abcaa6

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This server connects agents to your Elasticsearch data using the Model Context P
1313
* `list_indices`: List all available Elasticsearch indices
1414
* `get_mappings`: Get field mappings for a specific Elasticsearch index
1515
* `search`: Perform an Elasticsearch search with the provided query DSL
16+
* `get_shards`: Get shard information for all or specific indices
1617

1718
## Prerequisites
1819

index.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,68 @@ export async function createElasticsearchMcpServer(
315315
}
316316
);
317317

318+
// Tool 4: Get shard information
319+
server.tool(
320+
"get_shards",
321+
"Get shard information for all or specific indices",
322+
{
323+
index: z
324+
.string()
325+
.optional()
326+
.describe("Optional index name to get shard information for"),
327+
},
328+
async ({ index }) => {
329+
try {
330+
const response = await esClient.cat.shards({
331+
index,
332+
format: "json",
333+
});
334+
335+
const shardsInfo = response.map((shard) => ({
336+
index: shard.index,
337+
shard: shard.shard,
338+
prirep: shard.prirep,
339+
state: shard.state,
340+
docs: shard.docs,
341+
store: shard.store,
342+
ip: shard.ip,
343+
node: shard.node,
344+
}));
345+
346+
const metadataFragment = {
347+
type: "text" as const,
348+
text: `Found ${shardsInfo.length} shards${index ? ` for index ${index}` : ""}`,
349+
};
350+
351+
return {
352+
content: [
353+
metadataFragment,
354+
{
355+
type: "text" as const,
356+
text: JSON.stringify(shardsInfo, null, 2),
357+
},
358+
],
359+
};
360+
} catch (error) {
361+
console.error(
362+
`Failed to get shard information: ${
363+
error instanceof Error ? error.message : String(error)
364+
}`
365+
);
366+
return {
367+
content: [
368+
{
369+
type: "text" as const,
370+
text: `Error: ${
371+
error instanceof Error ? error.message : String(error)
372+
}`,
373+
},
374+
],
375+
};
376+
}
377+
}
378+
);
379+
318380
return server;
319381
}
320382

0 commit comments

Comments
 (0)