Elasticsearch MCP服务器

一个基于Model Context Protocol (MCP)的Elasticsearch操作服务器,提供完整的CRUD操作、批量处理、查询更新/删除、集群管理和高级搜索功能,适用于需要与Elasticsearch集群交互的LLM应用。

  • by Octodet
  • JavaScript
  • 本地部署
  • 3 星标
  • 更新于
在 GitHub 查看 Octodet/elasticsearch-mcp

使用说明

Octodet Elasticsearch MCP服务器

一个用于Elasticsearch操作的模型上下文协议(MCP)服务器,提供了一套全面的工具,用于通过标准化模型上下文协议与Elasticsearchs集群进行交互。此服务器使LLM驱动的应用程序能够搜索、更新和管理Elasticsearch数据。

特性

  • 完成Elasticsearch操作:文档和索引的完整CRUD操作
  • 批量操作:在单个API调用中处理多个操作
  • 基于查询的更新/删除:根据查询修改或删除文档
  • 群集管理:监视运行状况、分片和模板
  • 高级搜索:完全支持Elasticsearch DSL查询,突出显示

安装

作为NPM包

全局安装软件包:

npm install -g @octodet/elasticsearch-mcp

或者直接与npx一起使用:

npx @octodet/elasticsearch-mcp

来源

  1. 克隆此存储库
  2. 安装依赖项:
npm install
  1. 构建服务器:
npm run build

与MCP客户端集成

VS代码集成

将以下配置添加到您的VS Code settings.json中,以与VS Code MCP扩展集成:

"mcp.servers": {
  "elasticsearch": {
    "command": "npx",
    "args": [
      "-y", "@octodet/elasticsearch-mcp"
    ],
    "env": {
      "ES_URL": "http://localhost:9200",
      "ES_API_KEY": "your_api_key",
      "ES_VERSION": "8"
    }
  }
}

Claude桌面集成

在Claude Desktop配置文件中配置:

{
  "mcpServers": {
    "elasticsearch": {
      "command": "npx",
      "args": ["-y", "@octodet/elasticsearch-mcp"],
      "env": {
        "ES_URL": "http://localhost:9200",
        "ES_API_KEY": "your_api_key",
        "ES_VERSION": "8"
      }
    }
  }
}

促进地方发展

如果您正在本地开发MCP服务器,则可以配置客户端以使用您的本地版本:

{
  "mcpServers": {
    "elasticsearch": {
      "command": "node",
      "args": ["path/to/build/index.js"],
      "env": {
        "ES_URL": "http://localhost:9200",
        "ES_API_KEY": "your_api_key",
        "ES_VERSION": "8"
      }
    }
  }
}

配置

服务器使用以下环境变量进行配置:

变量 描述 默认值
es URL 弹性服务器URL http://localhost:9200
ES_API_KEY 用于身份验证的API密钥
ES_USENAME 身份验证用户名
ES_PASSWORD 身份验证密码
ES_CA_CERT 自定义CA证书的路径
ES_VERSION Elasticsearch版本(8或9) 8
ES_SSL_SKIP_VERIFY 跳过SSL验证 false
ES_PATH_PREFIX Elasticsearch的路径前缀

工具

服务器为Elasticsearch操作提供了16个MCP工具。每个工具都记录了其必需和可选参数:

1.列表索引

列出所有可用的Elasticsearch索引及其详细信息。

参数:

  • indexPattern (可选,字符串):过滤索引的模式(例如,“日志-“,”我的索引-")

例子:

{
  "indexPattern": "logs-*"
}

2.获取映射

获取特定Elasticsearch索引的字段映射。

参数:

  • index (必填,字符串):要获取映射的索引的名称

例子:

{
  "index": "my-index"
}

3.搜索

使用提供的查询DSL和突出显示执行Elasticsearch搜索。

参数:

  • index (必填,字符串):要搜索的索引(支持逗号分隔值)
  • queryBody (必填,对象):Elasticsearch查询DSL主体
  • highlight (可选,布尔值):启用搜索结果突出显示(默认值:true)

例子:

{
  "index": "my-index",
  "queryBody": {
    "query": {
      "match": {
        "content": "search term"
      }
    },
    "size": 10,
    "from": 0,
    "sort": [{ "_score": { "order": "desc" } }]
  },
  "highlight": true
}

4.获取群集运行状况

获取Elasticsearch集群的运行状况信息。

参数:

  • 无需

例子:

{}

5.获取碎片

获取所有或特定索引的分片信息。

参数:

  • index (可选,字符串):获取分片信息的特定索引。如果省略,则返回所有索引的分片

例子:

{
  "index": "my-index"
}

6.添加文档

将新文档添加到特定的Elasticsearch索引中。

参数:

  • index (必填,字符串):将文档添加到的索引
  • document (必填,object):要添加的文档内容
  • id (可选,字符串):文档ID。如果省略,Elasticsearch将自动生成一个

例子:

{
  "index": "my-index",
  "id": "doc1",
  "document": {
    "title": "My Document",
    "content": "Document content here",
    "timestamp": "2025-06-23T10:30:00Z",
    "tags": ["important", "draft"]
  }
}

7.更新文档

更新特定Elasticsearch索引中的现有文档。

参数:

  • index (必填,字符串):包含文档的索引
  • id (必填,字符串):要更新的文档的ID
  • document (必填,object):包含要更新的字段的部分文档

例子:

{
  "index": "my-index",
  "id": "doc1",
  "document": {
    "title": "Updated Document Title",
    "last_modified": "2025-06-23T10:30:00Z"
  }
}

8.删除文档

从特定的Elasticsearch索引中删除文档。

参数:

  • index (必填,字符串):包含文档的索引
  • id (必填,字符串):要删除的文档的ID

例子:

{
  "index": "my-index",
  "id": "doc1"
}

9.按查询更新

根据查询更新Elasticsearch索引中的文档。

参数:

  • index (必填,字符串):用于更新文档的索引
  • query (必填,对象):Elasticsearch查询以匹配要更新的文档
  • script (必填,对象):用于更新匹配文档的脚本
  • conflicts (可选,字符串):如何处理版本冲突(“中止”或“继续”,默认:“中止”)
  • refresh (可选,布尔值):操作后是否刷新索引(默认值:false)

例子:

{
  "index": "my-index",
  "query": {
    "term": {
      "status": "active"
    }
  },
  "script": {
    "source": "ctx._source.status = params.newStatus; ctx._source.updated_at = params.timestamp",
    "params": {
      "newStatus": "inactive",
      "timestamp": "2025-06-23T10:30:00Z"
    }
  },
  "conflicts": "proceed",
  "refresh": true
}

10.按查询删除

根据查询删除Elasticsearch索引中的文档。

参数:

  • index (必填,字符串):从中删除文档的索引
  • query (必填,对象):Elasticsearch查询以匹配要删除的文档
  • conflicts (可选,字符串):如何处理版本冲突(“中止”或“继续”,默认:“中止”)
  • refresh (可选,布尔值):操作后是否刷新索引(默认值:false)

例子:

{
  "index": "my-index",
  "query": {
    "range": {
      "created_date": {
        "lt": "2025-01-01"
      }
    }
  },
  "conflicts": "proceed",
  "refresh": true
}

11.批量操作

在单个API调用中执行多个文档操作以获得更好的性能。

参数:

  • operations (必填,数组):操作对象数组,每个对象包含:
    • action (必填,字符串):操作类型(“索引”、“创建”、“更新”或“删除”)
    • index (必填,字符串):此操作的索引
    • id (可选,字符串):文档ID(更新/删除时需要,索引/创建时可选)
    • document (条件,对象):文档内容(索引/创建/更新操作所需)

例子:

{
  "operations": [
    {
      "action": "index",
      "index": "my-index",
      "id": "doc1",
      "document": { "title": "Document 1", "content": "Content here" }
    },
    {
      "action": "update",
      "index": "my-index",
      "id": "doc2",
      "document": { "title": "Updated Title" }
    },
    {
      "action": "delete",
      "index": "my-index",
      "id": "doc3"
    }
  ]
}

12.创建索引

使用可选设置和映射创建新的Elasticsearch索引。

参数:

  • index (必填,字符串):要创建的索引的名称
  • settings (可选,对象):索引设置,如分片数、副本数等。
  • mappings (可选,对象):定义文档索引方式的字段映射

例子:

{
  "index": "new-index",
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1,
    "analysis": {
      "analyzer": {
        "custom_analyzer": {
          "type": "standard",
          "stopwords": "_english_"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "custom_analyzer"
      },
      "created": {
        "type": "date",
        "format": "yyyy-MM-dd'T'HH:mm:ss'Z'"
      },
      "tags": {
        "type": "keyword"
      }
    }
  }
}

13.删除索引

永久删除Elasticsearch索引。

参数:

  • index (必填,字符串):要删除的索引的名称

例子:

{
  "index": "my-index"
}

14.清点文件

统计索引中的文档,可选择按查询筛选。

参数:

  • index (必填,字符串):用于统计文档数量的索引
  • query (可选,对象):Elasticsearch查询,用于过滤文档以进行计数

例子:

{
  "index": "my-index",
  "query": {
    "bool": {
      "must": [
        { "term": { "status": "active" } },
        { "range": { "created_date": { "gte": "2025-01-01" } } }
      ]
    }
  }
}

15.获取模板

从Elasticsearch获取索引模板。

参数:

  • name (可选,字符串):要检索的特定模板名称。如果省略,则返回所有模板

例子:

{
  "name": "logs-template"
}

16.获取别名

从Elasticsearch获取索引别名。

参数:

  • name (可选,字符串):要检索的特定别名。如果省略,则返回所有别名

例子:

{
  "name": "logs-alias"
}

发展

以开发模式运行

在开发过程中以监视模式运行服务器:

npm run dev

协议实现

此服务器实现 模型上下文协议 以实现LLM客户端和Elasticsearch之间的标准化通信。它提供了一组工具,MCP客户端可以调用这些工具来执行各种Elasticsearch操作。

添加新工具

要向服务器添加新工具,请执行以下操作:

  1. 在中定义工具 src/index.ts 使用MCP服务器的工具注册格式
  2. 在中实现必要的功能 src/utils/elasticsearchService.ts
  3. 更新此README以记录新工具

其他MCP客户端

此服务器可以与任何兼容MCP的客户端一起使用,包括:

  • 通过MCP插件实现OpenAI的ChatGPT
  • Anthropic的克劳德桌面
  • 克劳德在VS代码
  • 使用MCP SDK的自定义应用程序

程序化使用

您还可以在Node.js应用程序中以编程方式使用服务器:

import { createOctodetElasticsearchMcpServer } from "@octodet/elasticsearch-mcp";
import { CustomTransport } from "@modelcontextprotocol/sdk/server";

// Configure the Elasticsearch connection
const config = {
  url: "http://localhost:9200",
  apiKey: "your_api_key",
  version: "8",
};

// Create and start the server
async function startServer() {
  const server = await createOctodetElasticsearchMcpServer(config);

  // Connect to your custom transport
  const transport = new CustomTransport();
  await server.connect(transport);

  console.log("Elasticsearch MCP server started");
}

startServer().catch(console.error);

许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。