Skip to content

feat: add create_index tool for index creation #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This server connects agents to your Elasticsearch data using the Model Context P
* `get_mappings`: Get field mappings for a specific Elasticsearch index
* `search`: Perform an Elasticsearch search with the provided query DSL
* `get_shards`: Get shard information for all or specific indices
* `create_index`: Create a new Elasticsearch index with mappings and settings

## Prerequisites

Expand Down Expand Up @@ -146,6 +147,7 @@ We welcome contributions from the community! For details on how to contribute, p
* "Show me the field mappings for the 'products' index."
* "Find all orders over $500 from last month."
* "Which products received the most 5-star reviews?"
* "Create a new index for users with fields for id, name, email, and created_at."

## How It Works

Expand Down
151 changes: 151 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,157 @@ export async function createElasticsearchMcpServer(
}
);

// Tool 5: Create Index
server.tool(
"create_index",
"Create a new Elasticsearch index with mappings. Accepts natural language descriptions or DDL statements.",
{
index: z
.string()
.trim()
.min(1, "Index name is required")
.describe("Name of the index to create"),

mappings: z
.record(z.any())
.describe(`
Elasticsearch mappings in one of the following formats:

1. Natural Language Description:
"Create a user table with:
- id: integer field for user identification
- name: text field searchable in Korean
- email: keyword field for exact matching
- created_at: date field for creation timestamp"

2. MySQL DDL:
"CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255),
created_at TIMESTAMP
);"

3. Elasticsearch Mapping (Final Format):
{
"properties": {
"id": { "type": "integer" },
"name": { "type": "text", "analyzer": "korean" },
"email": { "type": "keyword" },
"created_at": { "type": "date" }
}
}

Note: If you provide format 1 or 2, the LLM will automatically convert it to format 3.
`),

settings: z
.record(z.any())
.optional()
.describe(
"Optional index settings (e.g., number_of_shards, number_of_replicas)"
),

analyzer: z
.record(z.any())
.optional()
.describe(`
Elasticsearch analyzer configuration in one of the following formats:

1. Natural Language Description:
"Create a Korean text analyzer with:
- type: standard
- stopwords: Korean stopwords
- tokenizer: standard"

2. JSON Format:
{
"korean": {
"type": "standard",
"stopwords": "_korean_"
}
}

3. Elasticsearch Analyzer (Final Format):
{
"analyzer": {
"korean": {
"type": "standard",
"stopwords": "_korean_"
}
}
}

Note: If you provide format 1 or 2, the LLM will automatically convert it to format 3.
`),
},
async ({ index, mappings, settings, analyzer }) => {
try {
// Check if index already exists
const exists = await esClient.indices.exists({ index });
if (exists) {
return {
content: [
{
type: "text" as const,
text: `Index ${index} already exists. Please choose a different name or delete the existing index first.`,
},
],
};
}

// Create index with mappings
const createResponse = await esClient.indices.create({
index,
body: {
settings: {
...settings,
analysis: analyzer
? {
analyzer: {
...analyzer
}
}
: undefined,
},
mappings: mappings
},
});

const metadataFragment = {
type: "text" as const,
text: `Index ${index} created successfully with the following configuration:`,
};

return {
content: [
metadataFragment,
{
type: "text" as const,
text: JSON.stringify(createResponse, null, 2),
},
],
};
} catch (error) {
console.error(
`Failed to create index: ${
error instanceof Error ? error.message : String(error)
}`
);
return {
content: [
{
type: "text" as const,
text: `Error: ${
error instanceof Error ? error.message : String(error)
}`,
},
],
};
}
}
);

return server;
}

Expand Down