Skip to content

Commit 4f75696

Browse files
authored
feat: allow es8 server version (#71)
#50
1 parent 3af9580 commit 4f75696

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

index.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
import { z } from "zod";
99
import { 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";
1818
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
1919
import fs from "fs";
@@ -31,7 +31,10 @@ const product = {
3131
class 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

Comments
 (0)