Elasticsearch/OpenSearch MCP服务器
概述
提供Elasticsearch和OpenSearch交互的模型上下文协议(MCP)服务器实现。此服务器允许通过一组工具搜索文档、分析索引和管理集群。
演示
https://github.com/user-attachments/assets/f7409e31-fac4-4321-9c94-b0ff2ea7ff15
特性
一般操作
general_api_request:执行常规HTTP API请求。对于任何没有专用工具的Elasticsearch/OOpenSearch API,请使用此工具。
索引操作
list_indices:列出所有索引。get_index:返回有关一个或多个索引的信息(映射、设置、别名)。create_index:创建新索引。delete_index:删除索引。create_data_stream:创建新的数据流(需要匹配的索引模板)。get_data_stream:获取有关一个或多个数据流的信息。delete_data_stream:删除一个或多个数据流及其支持索引。
文档操作
search_documents:搜索文档。index_document:在索引中创建或更新文档。get_document:通过ID获取文档。delete_document:按ID删除文档。delete_by_query:删除与提供的查询匹配的文档。
集群操作
get_cluster_health:返回有关群集运行状况的基本信息。get_cluster_stats:返回集群统计信息的高级概述。
别名操作
list_aliases:列出所有别名。get_alias:获取特定索引的别名信息。put_alias:为特定索引创建或更新别名。delete_alias:删除特定索引的别名。
分析仪操作
analyze_text:使用指定的分析器或自定义分析链分析文本。有助于调试搜索查询和理解文本是如何标记的。
配置环境变量
MCP服务器支持以下环境变量:
基本身份验证(用户名/密码)
ELASTICSEARCH_USERNAME:基本身份验证的用户名ELASTICSEARCH_PASSWORD:基本身份验证密码OPENSEARCH_USERNAME:OpenSearch基本身份验证的用户名OPENSEARCH_PASSWORD:OpenSearch基本身份验证密码
API密钥验证(仅限Elasticsearch)-推荐
连接设置
ELASTICSEARCH_HOSTS/OPENSEARCH_HOSTS:以逗号分隔的主机列表(默认值:https://localhost:9200)VERIFY_CERTS:是否验证SSL证书(默认值:false)REQUEST_TIMEOUT:请求超时(秒)(可选,如果未设置,则使用客户端默认值)
MCP服务器身份验证(仅限HTTP传输)
当使用基于HTTP的传输(SSE或Streamable HTTP)运行MCP服务器时,您可以启用承载令牌身份验证,以保护服务器免受未经授权的访问。
MCP_API_KEY:用于MCP服务器身份验证的API密钥。客户必须包括Authorization: Bearer <MCP_API_KEY>头球
重要安全注意事项:
- 认证是 仅适用 用于HTTP传输(
sse,streamable-http).这stdio传输使用本地进程通信,不需要身份验证。 - 如果
MCP_API_KEY是 未设置,MCP服务器将可访问 未经认证当通过网络暴露服务器时,这是一个安全风险。 - 对于使用HTTP传输的生产部署, 始终设置
MCP_API_KEY.
BASH``` 1 2 3 4# Generate a secure API key (example using openssl) export MCP_API_KEY=$(openssl rand -base64 32)
Or set a custom API key
export MCP_API_KEY=“your-secure-api-key-here”
### 禁用高风险操作
- `DISABLE_HIGH_RISK_OPERATIONS`:设置为 `true` 禁用所有写入操作(默认值: `false`)
- `DISABLE_OPERATIONS`:逗号分隔的要禁用的特定操作列表(可选,如果未设置,则使用默认写入操作列表)
当 `DISABLE_HIGH_RISK_OPERATIONS` 如果设置为true,则执行写入操作的所有MCP工具都对MCP客户端完全隐藏。在此模式下,默认情况下禁用以下MCP工具。
- **索引操作:**
- `create_index`
- `delete_index`
- **文档操作:**
- `index_document`
- `delete_document`
- `delete_by_query`
- **数据流操作:**
- `create_data_stream`
- `delete_data_stream`
- **别名操作:**
- `put_alias`
- `delete_alias`
- **API一般操作:**
- `general_api_request`
(可选)您可以在 `DISABLE_OPERATIONS` 环境变量。
BASH```
1
2
3
4# Disable High-Risk Operations
export DISABLE_HIGH_RISK_OPERATIONS=true
# Disable specific operations only
export DISABLE_OPERATIONS="delete_index,delete_document,delete_by_query"
启动Elasticsearch/OpenSearch集群
使用Docker Compose启动Elasticsearch/OpenSearch集群:
BASH``` 1 2 3 4# For Elasticsearch docker-compose -f docker-compose-elasticsearch.yml up -d
For OpenSearch
docker-compose -f docker-compose-opensearch.yml up -d
默认的Elasticsearch用户名是 `elastic` 密码为 `test123`默认的OpenSearch用户名为 `admin` 密码为 `admin`.
您可以从以下网址访问Kibana/OpenSearch仪表板[http://localhost:5601](http://localhost:5601).
## 工作室
### 选项1:使用uvx
使用 `uvx` 将自动从PyPI安装软件包,无需在本地克隆存储库。将以下配置添加到的配置文件中 `claude_desktop_config.json`.
JSON```
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47// For Elasticsearch with username/password
{
"mcpServers": {
"elasticsearch-mcp-server": {
"command": "uvx",
"args": [
"elasticsearch-mcp-server"
],
"env": {
"ELASTICSEARCH_HOSTS": "https://localhost:9200",
"ELASTICSEARCH_USERNAME": "elastic",
"ELASTICSEARCH_PASSWORD": "test123"
}
}
}
}
// For Elasticsearch with API key
{
"mcpServers": {
"elasticsearch-mcp-server": {
"command": "uvx",
"args": [
"elasticsearch-mcp-server"
],
"env": {
"ELASTICSEARCH_HOSTS": "https://localhost:9200",
"ELASTICSEARCH_API_KEY": "<YOUR_ELASTICSEARCH_API_KEY>"
}
}
}
}
// For OpenSearch
{
"mcpServers": {
"opensearch-mcp-server": {
"command": "uvx",
"args": [
"opensearch-mcp-server"
],
"env": {
"OPENSEARCH_HOSTS": "https://localhost:9200",
"OPENSEARCH_USERNAME": "admin",
"OPENSEARCH_PASSWORD": "admin"
}
}
}
}
方案2:在当地发展中使用紫外线
使用 uv 需要在本地克隆存储库并指定源代码的路径。将以下配置添加到Claude Desktop的配置文件中 claude_desktop_config.json.
JSON``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56// For Elasticsearch with username/password { “mcpServers”: { “elasticsearch-mcp-server”: { “command”: “uv”, “args”: [ “—directory”, “path/to/elasticsearch-mcp-server”, “run”, “elasticsearch-mcp-server” ], “env”: { “ELASTICSEARCH_HOSTS”: “https://localhost:9200”, “ELASTICSEARCH_USERNAME”: “elastic”, “ELASTICSEARCH_PASSWORD”: “test123” } } } } // For Elasticsearch with API key { “mcpServers”: { “elasticsearch-mcp-server”: { “command”: “uv”, “args”: [ “—directory”, “path/to/elasticsearch-mcp-server”, “run”, “elasticsearch-mcp-server” ], “env”: { “ELASTICSEARCH_HOSTS”: “https://localhost:9200”, “ELASTICSEARCH_API_KEY”: “<YOUR_ELASTICSEARCH_API_KEY>” } } } } // For OpenSearch { “mcpServers”: { “opensearch-mcp-server”: { “command”: “uv”, “args”: [ “—directory”, “path/to/elasticsearch-mcp-server”, “run”, “opensearch-mcp-server” ], “env”: { “OPENSEARCH_HOSTS”: “https://localhost:9200”, “OPENSEARCH_USERNAME”: “admin”, “OPENSEARCH_PASSWORD”: “admin” } } } }
## 上海证券交易所
### 选项1:使用uvx
BASH```
1
2
3
4
5
6
7
8
9
10
11# export environment variables (with username/password)
export ELASTICSEARCH_HOSTS="https://localhost:9200"
export ELASTICSEARCH_USERNAME="elastic"
export ELASTICSEARCH_PASSWORD="test123"
# OR export environment variables (with API key)
export ELASTICSEARCH_HOSTS="https://localhost:9200"
export ELASTICSEARCH_API_KEY="<YOUR_ELASTICSEARCH_API_KEY>"
# By default, the SSE MCP server will serve on http://127.0.0.1:8000/sse
uvx elasticsearch-mcp-server --transport sse
# The host, port, and path can be specified using the --host, --port, and --path options
uvx elasticsearch-mcp-server --transport sse --host 0.0.0.0 --port 8000 --path /sse
选项2:使用紫外线
BASH``` 1 2 3 4# By default, the SSE MCP server will serve on http://127.0.0.1:8000/sse uv run src/server.py elasticsearch-mcp-server —transport sse
The host, port, and path can be specified using the —host, —port, and —path options
uv run src/server.py elasticsearch-mcp-server —transport sse —host 0.0.0.0 —port 8000 —path /sse
## 流式HTTP
### 选项1:使用uvx
BASH```
1
2
3
4
5
6
7
8
9
10
11# export environment variables (with username/password)
export ELASTICSEARCH_HOSTS="https://localhost:9200"
export ELASTICSEARCH_USERNAME="elastic"
export ELASTICSEARCH_PASSWORD="test123"
# OR export environment variables (with API key)
export ELASTICSEARCH_HOSTS="https://localhost:9200"
export ELASTICSEARCH_API_KEY="<YOUR_ELASTICSEARCH_API_KEY>"
# By default, the Streamable HTTP MCP server will serve on http://127.0.0.1:8000/mcp
uvx elasticsearch-mcp-server --transport streamable-http
# The host, port, and path can be specified using the --host, --port, and --path options
uvx elasticsearch-mcp-server --transport streamable-http --host 0.0.0.0 --port 8000 --path /mcp
选项2:使用紫外线
BASH``` 1 2 3 4# By default, the Streamable HTTP MCP server will serve on http://127.0.0.1:8000/mcp uv run src/server.py elasticsearch-mcp-server —transport streamable-http
The host, port, and path can be specified using the —host, —port, and —path options
uv run src/server.py elasticsearch-mcp-server —transport streamable-http —host 0.0.0.0 —port 8000 —path /mcp
## 兼容性
MCP服务器与Elasticsearch7.x、8.x和9.x兼容。默认情况下,它使用Elasticsearch8.x客户端(没有后缀)。
| MCP服务器 | Elasticsearch |
| --- | --- |
| elasticsearch-mcp-server-es7 | elasticsearch 7.x |
| elasticsearch mcp服务器 | Elasticsearch8.x |
| elasticsearch-mcp-server-es9 | elasticsearch 9.x |
| opensearch mcp服务器 | opensearch 1.x、2.x、3.x |
要使用Elasticsearch 7.x客户端,请运行 `elasticsearch-mcp-server-es7` 变体。对于Elasticsearch 9.x,请使用 `elasticsearch-mcp-server-es9`例如:
BASH```
1uvx elasticsearch-mcp-server-es7
如果你想在本地运行不同的Elasticsearch变体(例如7.x或9.x),只需更新 elasticsearch 依赖关系版本 pyproject.toml,然后使用以下命令启动服务器:
BASH``` 1uv run src/server.py elasticsearch-mcp-server
## 许可证
此项目根据Apache许可证版本2.0获得许可-请参阅 许可证 文件以获取详细信息。