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" ;
@@ -22,7 +22,10 @@ import fs from "fs";
2222class CustomTransport extends Transport {
2323 private readonly pathPrefix : string ;
2424
25- constructor ( opts : ConstructorParameters < typeof Transport > [ 0 ] , pathPrefix : string ) {
25+ constructor (
26+ opts : ConstructorParameters < typeof Transport > [ 0 ] ,
27+ pathPrefix : string
28+ ) {
2629 super ( opts ) ;
2730 this . pathPrefix = pathPrefix ;
2831 }
@@ -65,11 +68,14 @@ const ConfigSchema = z
6568 . string ( )
6669 . optional ( )
6770 . describe ( "Path to custom CA certificate for Elasticsearch" ) ,
68-
69- pathPrefix : z
71+
72+ pathPrefix : z . string ( ) . optional ( ) . describe ( "Path prefix for Elasticsearch" ) ,
73+
74+ version : z
7075 . string ( )
7176 . optional ( )
72- . describe ( "Path prefix for Elasticsearch" ) ,
77+ . transform ( ( val ) => ( [ "8" , "9" ] . includes ( val || "" ) ? val : "9" ) )
78+ . describe ( "Elasticsearch version (8, or 9)" ) ,
7379 } )
7480 . refine (
7581 ( data ) => {
@@ -104,7 +110,8 @@ export async function createElasticsearchMcpServer(
104110 config : ElasticsearchConfig
105111) {
106112 const validatedConfig = ConfigSchema . parse ( config ) ;
107- const { url, apiKey, username, password, caCert, pathPrefix } = validatedConfig ;
113+ const { url, apiKey, username, password, caCert, version, pathPrefix } =
114+ validatedConfig ;
108115
109116 const clientOptions : ClientOptions = {
110117 node : url ,
@@ -140,6 +147,16 @@ export async function createElasticsearchMcpServer(
140147 }
141148 }
142149
150+ // Add version-specific configuration
151+ if ( version === "8" ) {
152+ clientOptions . maxRetries = 5 ;
153+ clientOptions . requestTimeout = 30000 ;
154+ clientOptions . headers = {
155+ accept : "application/vnd.elasticsearch+json;compatible-with=8" ,
156+ "content-type" : "application/vnd.elasticsearch+json;compatible-with=8" ,
157+ } ;
158+ }
159+
143160 const esClient = new Client ( clientOptions ) ;
144161
145162 const server = new McpServer ( {
@@ -158,11 +175,11 @@ export async function createElasticsearchMcpServer(
158175 . min ( 1 , "Index pattern is required" )
159176 . describe ( "Index pattern of Elasticsearch indices to list" ) ,
160177 } ,
161- async ( { indexPattern} ) => {
178+ async ( { indexPattern } ) => {
162179 try {
163- const response = await esClient . cat . indices ( {
164- index : indexPattern ,
165- format : "json"
180+ const response = await esClient . cat . indices ( {
181+ index : indexPattern ,
182+ format : "json" ,
166183 } ) ;
167184
168185 const indicesInfo = response . map ( ( index ) => ( {
@@ -456,6 +473,7 @@ const config: ElasticsearchConfig = {
456473 username : process . env . ES_USERNAME || "" ,
457474 password : process . env . ES_PASSWORD || "" ,
458475 caCert : process . env . ES_CA_CERT || "" ,
476+ version : process . env . ES_VERSION || "" ,
459477 pathPrefix : process . env . ES_PATH_PREFIX || "" ,
460478} ;
461479
0 commit comments