基于 github.com/modelcontextprotocol/go-sdk 构建的 Elasticsearch MCP(模型上下文协议)服务器,提供与 Elasticsearch 7、8 和 9 版本的无缝集成。
es_cluster_info:获取集群信息和版本详情es_cluster_health:获取集群健康状态和指标es_index_create:创建具有设置和映射的新索引es_index_delete:删除现有索引es_index_exists:检查索引是否存在es_index_list:列出所有索引及其元数据es_document_index:使用可选ID索引文档es_document_get:通过ID检索文档es_document_update:更新现有文档es_document_delete:通过ID删除文档es_search:执行带有过滤器、排序和字段选择的搜索查询
index、query、size、from、sort、_sourcees_bulk:在一个请求中执行多个操作选择以下方法之一运行 Elasticsearch MCP 服务器:
# 基本用法,本地 Elasticsearch
docker run --rm \
-e ES_ADDRESSES=http://localhost:9200 \
ghcr.io/aeazer/mcp-elasticsearch:latest
# HTTP 模式,远程访问
docker run -d -p 8080:8080 \
-e MCP_PROTOCOL=http \
-e ES_ADDRESSES=http://your-elasticsearch:9200 \
ghcr.io/aeazer/mcp-elasticsearch:latest
# 使用认证
docker run -d -p 8080:8080 \
-e MCP_PROTOCOL=http \
-e ES_ADDRESSES=https://your-elasticsearch:9200 \
-e ES_USERNAME=elastic \
-e ES_PASSWORD=your-password \
-e ES_SSL=true \
ghcr.io/aeazer/mcp-elasticsearch:latest
# 克隆仓库
git clone https://github.com/AeaZer/mcp-elasticsearch.git
cd mcp-elasticsearch
# 构建 Docker 镜像
docker build -t mcp-elasticsearch .
# 运行容器
docker run -e ES_ADDRESSES=http://localhost:9200 -e ES_VERSION=8 mcp-elasticsearch
# 克隆仓库
git clone https://github.com/AeaZer/mcp-elasticsearch.git
cd mcp-elasticsearch
# 下载依赖并构建
go mod download
go build -o mcp-elasticsearch main.go
# 使用环境变量运行
export ES_ADDRESSES=http://localhost:9200
export ES_VERSION=8
export MCP_PROTOCOL=stdio
./mcp-elasticsearch
对于支持 MCP 的桌面应用集成,可以通过配置文件来配置服务器:
首先确保你有可用的 mcp-elasticsearch 可执行文件:
# 选项 A:直接从 GitHub 安装(推荐)
go install github.com/AeaZer/mcp-elasticsearch@latest
# 选项 B:从源代码构建
git clone https://github.com/AeaZer/mcp-elasticsearch.git
cd mcp-elasticsearch
go mod download
go build -o mcp-elasticsearch main.go
# 选项 C:下载预构建的二进制文件(仅限 Windows)
# 从 GitHub Releases 下载 mcp-elasticsearch.exe
# macOS 和 Linux 用户应使用选项 A 或 B
注意:Docker 不适合桌面应用集成,因为它在这种情况下不完全支持 stdio 模式。请使用原生可执行文件。
如果你使用了 go install(选项 A),可执行文件会自动放置在 $GOPATH/bin 或 $GOBIN 中,这通常已经在你的 PATH 环境变量中。
如果你是从源代码构建(选项 B)或下载了预构建的二进制文件(选项 C),请确保 mcp-elasticsearch 可执行文件在你的系统 PATH 环境变量中,或者在配置中使用完整路径。
创建或修改桌面应用的 MCP 配置文件:
Cursor:创建或编辑 ~/.cursor/mcp.json(Linux/Mac)或 %APPDATA%\Cursor\User\mcp.json(Windows)
Claude Desktop:在适当的位置创建或编辑配置。
基本配置:
{
"mcpServers": {
"elasticsearch": {
"command": "mcp-elasticsearch",
"env": {
"ES_ADDRESSES": "http://localhost:9200",
"ES_VERSION": "8"
}
}
}
}
带认证:
{
"mcpServers": {
"elasticsearch": {
"command": "mcp-elasticsearch",
"env": {
"ES_ADDRESSES": "https://your-elasticsearch.com:9200",
"ES_USERNAME": "elastic",
"ES_PASSWORD": "your-password",
"ES_VERSION": "8",
"ES_SSL": "true"
}
}
}
}
带 API 密钥:
{
"mcpServers": {
"elasticsearch": {
"command": "mcp-elasticsearch",
"env": {
"ES_ADDRESSES": "https://your-elasticsearch.com:9200",
"ES_API_KEY": "your-api-key",
"ES_VERSION": "8"
}
}
}
}
Elastic Cloud 配置:
{
"mcpServers": {
"elasticsearch": {
"command": "mcp-elasticsearch",
"env": {
"ES_CLOUD_ID": "your-cloud-id",
"ES_USERNAME": "elastic",
"ES_PASSWORD": "your-password",
"ES_VERSION": "8"
}
}
}
}
使用完整路径(如果不在 PATH 中):
{
"mcpServers": {
"elasticsearch": {
"command": "/full/path/to/mcp-elasticsearch",
"env": {
"ES_ADDRESSES": "http://localhost:9200",
"ES_VERSION": "8"
}
}
}
}
创建或修改配置文件后,请重启桌面应用以加载新的 MCP 服务器配置。
配置完成后,你应该能在桌面应用中看到 Elasticsearch 工具和资源。可用的工具包括集群操作、索引管理、文档操作、搜索能力和批量操作,如上“支持的工具”部分所述。
docker run -d -p 8080:8080 \
--name mcp-elasticsearch \
-e MCP_PROTOCOL=http \
-e ES_ADDRESSES=http://host.docker.internal:9200 \
ghcr.io/aeazer/mcp-elasticsearch:latest
# 测试服务器端点
curl http://localhost:8080/health # 健康检查
curl http://localhost:8080/mcp # MCP 端点(需要适当的 MCP 客户端)
docker run -d -p 8080:8080 \
-e MCP_PROTOCOL=http \
-e ES_CLOUD_ID="your-cloud-id" \
-e ES_USERNAME=elastic \
-e ES_PASSWORD="your-password" \
-e ES_VERSION=8 \
ghcr.io/aeazer/mcp-elasticsearch:latest
⚠️ 警告:SSE 协议已弃用,不建议用于生产用途。请使用 HTTP 模式。
docker run -d -p 8080:8080 \
--name mcp-elasticsearch-sse \
-e MCP_PROTOCOL=sse \
-e ES_ADDRESSES=http://host.docker.internal:9200 \
-e ES_VERSION=8 \
ghcr.io/aeazer/mcp-elasticsearch:latest
# SSE 端点(已弃用)
curl http://localhost:8080/sse
创建一个 docker-compose.yml 文件:
version: '3.8'
services:
mcp-elasticsearch:
image: ghcr.io/aeazer/mcp-elasticsearch:latest
ports:
- "8080:8080"
environment:
- MCP_PROTOCOL=http
- ES_ADDRESSES=http://elasticsearch:9200
- ES_VERSION=8
depends_on:
- elasticsearch
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0
environment:
- discovery.type=single-node
- xpack.security.enabled=false
ports:
- "9200:9200"
运行:docker-compose up -d
所有配置均通过环境变量完成:
| 变量 | 描述 | 默认值 |
|---|---|---|
ES_ADDRESSES | Elasticsearch 集群地址(逗号分隔) | http://localhost:9200 |
ES_USERNAME | 基本认证用户名 | - |
ES_PASSWORD | 基本认证密码 | - |
ES_API_KEY | 认证 API 密钥 | - |
ES_CLOUD_ID | Elastic Cloud ID | - |
ES_SSL | 启用 SSL/TLS | false |
ES_INSECURE_SKIP_VERIFY | 跳过 SSL 证书验证 | false |
ES_TIMEOUT | 连接超时 | 30s |
ES_MAX_RETRIES | 最大重试次数 | 3 |
ES_VERSION | 目标 Elasticsearch 版本(7、8 或 9) | 8 |
| 变量 | 描述 | 默认值 |
|---|---|---|
MCP_SERVER_NAME | MCP 服务器名称 | Elasticsearch MCP Server |
MCP_SERVER_VERSION | 服务器版本 | 1.0.0 |
MCP_PROTOCOL | 使用的协议(stdio、http 或 sse - 已弃用) | http(在 Docker 中),stdio(原生) |
MCP_ADDRESS | Streamable HTTP 服务器地址(仅 HTTP 模式) | 0.0.0.0(在 Docker 中),localhost(原生) |
MCP_PORT | Streamable HTTP 服务器端口(仅 HTTP 模式) | 8080 |
不同的协议使用不同的访问方法:
http://host:port/mcphttp://host:port/healthhttp://localhost:8080/mcphttp://host:port/ssehttp://localhost:8080/sseexport ES_ADDRESSES=http://localhost:9200
export MCP_PROTOCOL=stdio
./mcp-elasticsearch
export ES_ADDRESSES=http://localhost:9200
export MCP_PROTOCOL=http
export MCP_PORT=8080
./mcp-elasticsearch
⚠️ 警告:SSE 协议已弃用,不建议用于生产用途。请使用 Streamable HTTP。
export ES_ADDRESSES=http://localhost:9200
export MCP_PROTOCOL=sse
export MCP_PORT=8080
./mcp-elasticsearch
export ES_CLOUD_ID=your_cloud_id
export ES_USERNAME=elastic
export ES_PASSWORD=your_password
export ES_VERSION=8
./mcp-elasticsearch
go mod download
go build -o mcp-elasticsearch main.go
go test ./...
docker build -t mcp-elasticsearch .
{
"tool": "es_cluster_info",
"arguments": {}
}
{
"tool": "es_index_create",
"arguments": {
"index": "my-index",
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"title": {"type": "text"},
"timestamp": {"type": "date"}
}
}
}
}
{
"tool": "es_document_index",
"arguments": {
"index": "my-index",
"id": "doc1",
"body": {
"title": "Hello World",
"content": "This is a test document",
"timestamp": "2024-01-01T00:00:00Z"
}
}
}
{
"tool": "es_search",
"arguments": {
"index": "my-index",
"query": {
"bool": {
"must": [
{"match": {"title": "Hello"}}
],
"filter": [
{"range": {"timestamp": {"gte": "2024-01-01"}}}
]
}
},
"sort": [
{"timestamp": {"order": "desc"}},
{"_score": {"order": "desc"}}
],
"_source": ["title", "content", "timestamp"],
"size": 20,
"from": 0
}
}
在 HTTP 模式下运行时,服务器提供了多个端点:
# 检查服务器健康(公开访问)
curl http://localhost:8080/health
# 响应
{"status":"healthy","server":"elasticsearch-mcp"}
# MCP 通信端点(需要 MCP 客户端)
# URL: http://localhost:8080/mcp
# 该端点处理 MCP 协议消息和工具调用
# 不能通过简单的 HTTP GET 请求直接访问
/health):简单的 HTTP GET 用于监控/mcp):仅用于 MCP 协议通信/sse):已弃用,避免使用所有错误都在 MCP 工具结果中报告,并带有 isError: true,允许 LLMs 查看并适当地处理错误。协议级别的错误保留给缺失工具或服务器故障等异常情况。
host.docker.internal:9200 而不是 localhost:9200