list_indices
List all Elasticsearch indices to view available data sources; optionally filter by regex pattern for precise index discovery.
Instructions
List all available Elasticsearch indices, support regex
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pattern | No | Optional regex pattern to filter indices by name |
Implementation Reference
- src/tools/listIndices.ts:9-50 (handler)The core handler function for the 'list_indices' tool. Calls esClient.cat.indices() to fetch all Elasticsearch indices (optionally filtered by a pattern), maps results to a simplified format (index, health, status, docsCount), and returns a text response with the count and JSON details.
export async function listIndices(esClient: Client, pattern?: string) { try { const response = await esClient.cat.indices({ format: "json", index: pattern || "*" // if pattern is undefined, use "*" as default }); const indicesInfo = response.map((index) => ({ index: index.index, health: index.health, status: index.status, docsCount: index.docsCount, })); return { content: [ { type: "text" as const, text: `Found ${indicesInfo.length} indices`, }, { type: "text" as const, text: JSON.stringify(indicesInfo, null, 2), }, ], }; } catch (error) { console.error( `Failed to list indices: ${error instanceof Error ? error.message : String(error) }` ); return { content: [ { type: "text" as const, text: `Error: ${error instanceof Error ? error.message : String(error) }`, }, ], }; } } - src/server.ts:44-49 (schema)Zod schema (input validation) for the 'list_indices' tool. Defines an optional 'pattern' string parameter to filter indices by name.
{ pattern: z .string() .optional() .describe("Optional regex pattern to filter indices by name"), }, - src/server.ts:41-53 (registration)Registration of the 'list_indices' tool on the MCP server using server.tool(), with the name 'list_indices', a description, the zod schema, and a handler that calls the listIndices function.
server.tool( "list_indices", "List all available Elasticsearch indices, support regex", { pattern: z .string() .optional() .describe("Optional regex pattern to filter indices by name"), }, async ({ pattern }) => { return await listIndices(esClient, pattern); } ); - src/config/schema.ts:40-70 (helper)Helper function createClientOptions() used in server.ts to build the Elasticsearch Client options from configuration. This is indirectly related as it sets up the esClient passed to listIndices.
export function createClientOptions(config: ElasticsearchConfig): ClientOptions { const validatedConfig = ConfigSchema.parse(config); const { urls, apiKey, username, password, caCert } = validatedConfig; const clientOptions: ClientOptions = { nodes: urls, }; // 设置认证 if (apiKey) { clientOptions.auth = { apiKey }; } else if (username && password) { clientOptions.auth = { username, password }; } // 如果提供了证书,设置 SSL/TLS if (caCert) { try { const ca = fs.readFileSync(caCert); clientOptions.tls = { ca }; } catch (error) { console.error( `Failed to read certificate file: ${ error instanceof Error ? error.message : String(error) }` ); } } return clientOptions; }