@@ -10,7 +10,7 @@ import morgan from "morgan";
10
10
import { z } from "zod" ;
11
11
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
12
12
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js" ;
13
- import { Client , estypes , ClientOptions } from "@elastic/elasticsearch" ;
13
+ import { Client , ClientOptions , estypes } from "@elastic/elasticsearch" ;
14
14
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js" ;
15
15
import fs from "fs" ;
16
16
@@ -74,13 +74,13 @@ const ConfigSchema = z
74
74
message :
75
75
"Either ES_API_KEY or both ES_USERNAME and ES_PASSWORD must be provided, or no auth for local development" ,
76
76
path : [ "username" , "password" ] ,
77
- }
77
+ } ,
78
78
) ;
79
79
80
80
type ElasticsearchConfig = z . infer < typeof ConfigSchema > ;
81
81
82
82
export async function createElasticsearchMcpServer (
83
- config : ElasticsearchConfig
83
+ config : ElasticsearchConfig ,
84
84
) {
85
85
const validatedConfig = ConfigSchema . parse ( config ) ;
86
86
const { url, apiKey, username, password, caCert } = validatedConfig ;
@@ -105,7 +105,7 @@ export async function createElasticsearchMcpServer(
105
105
console . error (
106
106
`Failed to read certificate file: ${
107
107
error instanceof Error ? error . message : String ( error )
108
- } `
108
+ } `,
109
109
) ;
110
110
}
111
111
}
@@ -149,7 +149,7 @@ export async function createElasticsearchMcpServer(
149
149
console . error (
150
150
`Failed to list indices: ${
151
151
error instanceof Error ? error . message : String ( error )
152
- } `
152
+ } `,
153
153
) ;
154
154
return {
155
155
content : [
@@ -162,7 +162,7 @@ export async function createElasticsearchMcpServer(
162
162
] ,
163
163
} ;
164
164
}
165
- }
165
+ } ,
166
166
) ;
167
167
168
168
// Tool 2: Get mappings for an index
@@ -190,19 +190,21 @@ export async function createElasticsearchMcpServer(
190
190
} ,
191
191
{
192
192
type : "text" as const ,
193
- text : `Mappings for index ${ index } : ${ JSON . stringify (
194
- mappingResponse [ index ] ?. mappings || { } ,
195
- null ,
196
- 2
197
- ) } `,
193
+ text : `Mappings for index ${ index } : ${
194
+ JSON . stringify (
195
+ mappingResponse [ index ] ?. mappings || { } ,
196
+ null ,
197
+ 2 ,
198
+ )
199
+ } `,
198
200
} ,
199
201
] ,
200
202
} ;
201
203
} catch ( error ) {
202
204
console . error (
203
205
`Failed to get mappings: ${
204
206
error instanceof Error ? error . message : String ( error )
205
- } `
207
+ } `,
206
208
) ;
207
209
return {
208
210
content : [
@@ -215,7 +217,7 @@ export async function createElasticsearchMcpServer(
215
217
] ,
216
218
} ;
217
219
}
218
- }
220
+ } ,
219
221
) ;
220
222
221
223
// Tool 3: Search an index with simplified parameters
@@ -242,10 +244,10 @@ export async function createElasticsearchMcpServer(
242
244
} ,
243
245
{
244
246
message : "queryBody must be a valid Elasticsearch query DSL object" ,
245
- }
247
+ } ,
246
248
)
247
249
. describe (
248
- "Complete Elasticsearch query DSL object that can include query, size, from, sort, etc."
250
+ "Complete Elasticsearch query DSL object that can include query, size, from, sort, etc." ,
249
251
) ,
250
252
} ,
251
253
async ( { index, queryBody } ) => {
@@ -266,9 +268,11 @@ export async function createElasticsearchMcpServer(
266
268
if ( indexMappings . properties ) {
267
269
const textFields : Record < string , estypes . SearchHighlightField > = { } ;
268
270
269
- for ( const [ fieldName , fieldData ] of Object . entries (
270
- indexMappings . properties
271
- ) ) {
271
+ for (
272
+ const [ fieldName , fieldData ] of Object . entries (
273
+ indexMappings . properties ,
274
+ )
275
+ ) {
272
276
if ( fieldData . type === "text" || "dense_vector" in fieldData ) {
273
277
textFields [ fieldName ] = { } ;
274
278
}
@@ -294,9 +298,11 @@ export async function createElasticsearchMcpServer(
294
298
295
299
for ( const [ field , highlights ] of Object . entries ( highlightedFields ) ) {
296
300
if ( highlights && highlights . length > 0 ) {
297
- content += `${ field } (highlighted): ${ highlights . join (
298
- " ... "
299
- ) } \n`;
301
+ content += `${ field } (highlighted): ${
302
+ highlights . join (
303
+ " ... " ,
304
+ )
305
+ } \n`;
300
306
}
301
307
}
302
308
@@ -328,7 +334,7 @@ export async function createElasticsearchMcpServer(
328
334
console . error (
329
335
`Search failed: ${
330
336
error instanceof Error ? error . message : String ( error )
331
- } `
337
+ } `,
332
338
) ;
333
339
return {
334
340
content : [
@@ -341,7 +347,7 @@ export async function createElasticsearchMcpServer(
341
347
] ,
342
348
} ;
343
349
}
344
- }
350
+ } ,
345
351
) ;
346
352
347
353
// Tool 4: Get shard information
@@ -392,7 +398,7 @@ export async function createElasticsearchMcpServer(
392
398
console . error (
393
399
`Failed to get shard information: ${
394
400
error instanceof Error ? error . message : String ( error )
395
- } `
401
+ } `,
396
402
) ;
397
403
return {
398
404
content : [
@@ -405,7 +411,7 @@ export async function createElasticsearchMcpServer(
405
411
] ,
406
412
} ;
407
413
}
408
- }
414
+ } ,
409
415
) ;
410
416
411
417
return server ;
@@ -426,10 +432,10 @@ async function create_sse_server(server: any) {
426
432
// Use morgan to log every request
427
433
app . use ( morgan ( "combined" ) ) ;
428
434
429
- const transports : { [ sessionId : string ] : SSEServerTransport } = { } ;
435
+ const transports : { [ sessionId : string ] : SSEServerTransport } = { } ;
430
436
431
437
app . get ( "/sse" , async ( _ : Request , res : Response ) => {
432
- const transport = new SSEServerTransport ( ' /messages' , res ) ;
438
+ const transport = new SSEServerTransport ( " /messages" , res ) ;
433
439
transports [ transport . sessionId ] = transport ;
434
440
res . on ( "close" , ( ) => {
435
441
delete transports [ transport . sessionId ] ;
@@ -443,7 +449,7 @@ async function create_sse_server(server: any) {
443
449
if ( transport ) {
444
450
await transport . handlePostMessage ( req , res ) ;
445
451
} else {
446
- res . status ( 400 ) . send ( ' No transport found for sessionId' ) ;
452
+ res . status ( 400 ) . send ( " No transport found for sessionId" ) ;
447
453
}
448
454
} ) ;
449
455
@@ -479,7 +485,7 @@ async function main() {
479
485
main ( ) . catch ( ( error ) => {
480
486
console . error (
481
487
"Server error:" ,
482
- error instanceof Error ? error . message : String ( error )
488
+ error instanceof Error ? error . message : String ( error ) ,
483
489
) ;
484
490
process . exit ( 1 ) ;
485
491
} ) ;
0 commit comments