77
88import { z } from "zod" ;
99import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
10- import {
11- Client ,
12- estypes ,
13- ClientOptions ,
10+ import {
11+ Client ,
12+ estypes ,
13+ ClientOptions ,
1414 Transport ,
1515 TransportRequestOptions ,
16- TransportRequestParams
16+ TransportRequestParams ,
1717} from "@elastic/elasticsearch" ;
1818import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" ;
1919import fs from "fs" ;
@@ -31,7 +31,10 @@ const product = {
3131class CustomTransport extends Transport {
3232 private readonly pathPrefix : string ;
3333
34- constructor ( opts : ConstructorParameters < typeof Transport > [ 0 ] , pathPrefix : string ) {
34+ constructor (
35+ opts : ConstructorParameters < typeof Transport > [ 0 ] ,
36+ pathPrefix : string
37+ ) {
3538 super ( opts ) ;
3639 this . pathPrefix = pathPrefix ;
3740 }
@@ -74,11 +77,14 @@ const ConfigSchema = z
7477 . string ( )
7578 . optional ( )
7679 . describe ( "Path to custom CA certificate for Elasticsearch" ) ,
77-
78- pathPrefix : z
80+
81+ pathPrefix : z . string ( ) . optional ( ) . describe ( "Path prefix for Elasticsearch" ) ,
82+
83+ version : z
7984 . string ( )
8085 . optional ( )
81- . describe ( "Path prefix for Elasticsearch" ) ,
86+ . transform ( ( val ) => ( [ "8" , "9" ] . includes ( val || "" ) ? val : "9" ) )
87+ . describe ( "Elasticsearch version (8, or 9)" ) ,
8288 } )
8389 . refine (
8490 ( data ) => {
@@ -113,7 +119,8 @@ export async function createElasticsearchMcpServer(
113119 config : ElasticsearchConfig
114120) {
115121 const validatedConfig = ConfigSchema . parse ( config ) ;
116- const { url, apiKey, username, password, caCert, pathPrefix } = validatedConfig ;
122+ const { url, apiKey, username, password, caCert, version, pathPrefix } =
123+ validatedConfig ;
117124
118125 const clientOptions : ClientOptions = {
119126 node : url ,
@@ -152,6 +159,16 @@ export async function createElasticsearchMcpServer(
152159 }
153160 }
154161
162+ // Add version-specific configuration
163+ if ( version === "8" ) {
164+ clientOptions . maxRetries = 5 ;
165+ clientOptions . requestTimeout = 30000 ;
166+ clientOptions . headers = {
167+ accept : "application/vnd.elasticsearch+json;compatible-with=8" ,
168+ "content-type" : "application/vnd.elasticsearch+json;compatible-with=8" ,
169+ } ;
170+ }
171+
155172 const esClient = new Client ( clientOptions ) ;
156173
157174 const server = new McpServer ( product ) ;
@@ -167,11 +184,11 @@ export async function createElasticsearchMcpServer(
167184 . min ( 1 , "Index pattern is required" )
168185 . describe ( "Index pattern of Elasticsearch indices to list" ) ,
169186 } ,
170- async ( { indexPattern} ) => {
187+ async ( { indexPattern } ) => {
171188 try {
172- const response = await esClient . cat . indices ( {
173- index : indexPattern ,
174- format : "json"
189+ const response = await esClient . cat . indices ( {
190+ index : indexPattern ,
191+ format : "json" ,
175192 } ) ;
176193
177194 const indicesInfo = response . map ( ( index ) => ( {
@@ -465,6 +482,7 @@ const config: ElasticsearchConfig = {
465482 username : process . env . ES_USERNAME || "" ,
466483 password : process . env . ES_PASSWORD || "" ,
467484 caCert : process . env . ES_CA_CERT || "" ,
485+ version : process . env . ES_VERSION || "" ,
468486 pathPrefix : process . env . ES_PATH_PREFIX || "" ,
469487} ;
470488
0 commit comments