7
7
8
8
import { z } from "zod" ;
9
9
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
10
- import {
11
- Client ,
12
- estypes ,
13
- ClientOptions ,
10
+ import {
11
+ Client ,
12
+ estypes ,
13
+ ClientOptions ,
14
14
Transport ,
15
15
TransportRequestOptions ,
16
- TransportRequestParams
16
+ TransportRequestParams ,
17
17
} from "@elastic/elasticsearch" ;
18
18
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" ;
19
19
import fs from "fs" ;
@@ -31,7 +31,10 @@ const product = {
31
31
class CustomTransport extends Transport {
32
32
private readonly pathPrefix : string ;
33
33
34
- constructor ( opts : ConstructorParameters < typeof Transport > [ 0 ] , pathPrefix : string ) {
34
+ constructor (
35
+ opts : ConstructorParameters < typeof Transport > [ 0 ] ,
36
+ pathPrefix : string
37
+ ) {
35
38
super ( opts ) ;
36
39
this . pathPrefix = pathPrefix ;
37
40
}
@@ -74,11 +77,14 @@ const ConfigSchema = z
74
77
. string ( )
75
78
. optional ( )
76
79
. 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
79
84
. string ( )
80
85
. optional ( )
81
- . describe ( "Path prefix for Elasticsearch" ) ,
86
+ . transform ( ( val ) => ( [ "8" , "9" ] . includes ( val || "" ) ? val : "9" ) )
87
+ . describe ( "Elasticsearch version (8, or 9)" ) ,
82
88
} )
83
89
. refine (
84
90
( data ) => {
@@ -113,7 +119,8 @@ export async function createElasticsearchMcpServer(
113
119
config : ElasticsearchConfig
114
120
) {
115
121
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 ;
117
124
118
125
const clientOptions : ClientOptions = {
119
126
node : url ,
@@ -152,6 +159,16 @@ export async function createElasticsearchMcpServer(
152
159
}
153
160
}
154
161
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
+
155
172
const esClient = new Client ( clientOptions ) ;
156
173
157
174
const server = new McpServer ( product ) ;
@@ -167,11 +184,11 @@ export async function createElasticsearchMcpServer(
167
184
. min ( 1 , "Index pattern is required" )
168
185
. describe ( "Index pattern of Elasticsearch indices to list" ) ,
169
186
} ,
170
- async ( { indexPattern} ) => {
187
+ async ( { indexPattern } ) => {
171
188
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" ,
175
192
} ) ;
176
193
177
194
const indicesInfo = response . map ( ( index ) => ( {
@@ -465,6 +482,7 @@ const config: ElasticsearchConfig = {
465
482
username : process . env . ES_USERNAME || "" ,
466
483
password : process . env . ES_PASSWORD || "" ,
467
484
caCert : process . env . ES_CA_CERT || "" ,
485
+ version : process . env . ES_VERSION || "" ,
468
486
pathPrefix : process . env . ES_PATH_PREFIX || "" ,
469
487
} ;
470
488
0 commit comments