Elasticsearch MCP服务端
为与Elasticsearch 7.x数据库交互提供了一个MCP协议接口,支持包括聚合、高亮显示和排序在内的全面搜索功能。
可用工具 (3 个)
该服务在 MCP 协议中暴露的工具,AI 可按需调用
es-ping 1 个参数 需填 1 项
Ping Elasticsearch server
必填参数:req
es-info 1 个参数 需填 1 项
Get Elasticsearch info
必填参数:req
es-search 1 个参数 需填 1 项
Search documents in Elasticsearch index
必填参数:req
服务介绍
Elasticsearch 7.x MCP Server
这是一个为Elasticsearch 7.x版本提供兼容性的MCP服务器。
特性
- 提供与Elasticsearch 7.x交互的MCP协议接口
- 支持基本的Elasticsearch操作(如ping、info等)
- 支持完整的搜索功能,包括聚合查询、高亮显示、排序及其他高级特性
- 可通过任意MCP客户端轻松访问Elasticsearch功能
需求
- Python 3.10+
- Elasticsearch 7.x (推荐7.17.x)
安装
通过Smithery安装
要通过Smithery自动安装适用于Claude Desktop的Elasticsearch 7.x MCP Server,请执行以下命令:
npx -y @smithery/cli install @imlewc/elasticsearch7-mcp-server --client claude
手动安装
pip install -e .
环境变量
该服务器需要以下环境变量:
ELASTIC_HOST: Elasticsearch主机地址(例如,http://localhost:9200)ELASTIC_USERNAME: Elasticsearch用户名ELASTIC_PASSWORD: Elasticsearch密码MCP_PORT: (可选) MCP服务器监听端口,默认为9999
使用Docker Compose
- 创建一个
.env文件并设置ELASTIC_PASSWORD:
ELASTIC_PASSWORD=your_secure_password
- 启动服务:
docker-compose up -d
这将启动一个包含三个节点的Elasticsearch 7.17.10集群、Kibana以及MCP服务器。
使用MCP客户端
您可以使用任何MCP客户端连接到MCP服务器:
from mcp import MCPClient
client = MCPClient("localhost:9999")
response = client.call("es-ping")
print(response) # {"success": true}
API文档
目前支持的MCP方法有:
es-ping: 检查Elasticsearch连接es-info: 获取Elasticsearch集群信息es-search: 在Elasticsearch索引中搜索文档
搜索API示例
基本搜索
# Basic search
search_response = client.call("es-search", {
"index": "my_index",
"query": {
"match": {
"title": "search keywords"
}
},
"size": 10,
"from": 0
})
聚合查询
# Aggregation query
agg_response = client.call("es-search", {
"index": "my_index",
"size": 0, # Only need aggregation results, no documents
"aggs": {
"categories": {
"terms": {
"field": "category.keyword",
"size": 10
}
},
"avg_price": {
"avg": {
"field": "price"
}
}
}
})
高级搜索
# Advanced search with highlighting, sorting, and filtering
advanced_response = client.call("es-search", {
"index": "my_index",
"query": {
"bool": {
"must": [
{"match": {"content": "search term"}}
],
"filter": [
{"range": {"price": {"gte": 100, "lte": 200}}}
]
}
},
"sort": [
{"date": {"order": "desc"}},
"_score"
],
"highlight": {
"fields": {
"content": {}
}
},
"_source": ["title", "date", "price"]
})
开发
- 克隆仓库
- 安装开发依赖
- 运行服务器:
elasticsearch7-mcp-server
许可证
[许可证见LICENSE文件]