自定义Elasticsearch MCP服务器
Elasticsearch的一个简单的MCP(模型上下文协议)服务器,专为您的公钥已经在服务器上授权的云环境而设计。
为什么是这个自定义版本?
不需要API密钥 -与需要两者的官方Elasticsearch MCP服务器不同 ES_URL 和 ES_API_KEY,此版本只需要URL,因为您的公钥在云服务器上已受信任。
增强工具 -与官方版本相比,可选参数和改进的默认值具有更好的可用性。
这有什么作用
此MCP服务器使用4个强大的工具将Cursor连接到您的Elasticsearch集群:
list_indices-列出所有索引(可选模式过滤器)search-完全支持Elasticsearch查询DSLget_mappings-获取任何索引的字段映射get_shards-查看集群碎片信息
快速开始
从源代码构建
git clone https://github.com/M0-AR/Custom-Elasticsearch-MCP-Server.git
cd Custom-Elasticsearch-MCP-Server
docker build -t elasticsearch-mcp:latest .2.添加到光标MCP配置
将此添加到您的 .cursor/mcp.json 文件:
配置:
{
"mcpServers": {
"elasticsearch-custom": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--add-host=host.docker.internal:host-gateway",
"-e",
"ES_URL=http://host.docker.internal:9400",
"elasticsearch-mcp:latest"
]
}
}
}3.重新启动游标
关闭并重新打开游标。您应该看到启用了4个工具的elasticsearch自定义服务器。
配置
环境变量:
ES_URL-您的Elasticsearch URL(默认值:http://localhost:9400)MAX_CONNECTIONS-最大并发连接数(默认值:100)MAX_KEEPALIVE_CONNECTIONS-最大保活连接数(默认值:20)CONNECTION_TIMEOUT-连接超时(秒)(默认值:30)REQUEST_TIMEOUT-请求超时(秒)(默认值:30)
对于不同的Elasticsearch端口:
"ES_URL=http://host.docker.internal:9200"对于高流量环境:
"MAX_CONNECTIONS=200",
"MAX_KEEPALIVE_CONNECTIONS=50",
"CONNECTION_TIMEOUT=60",
"REQUEST_TIMEOUT=60"示例用法
在Cursor中连接后,您可以:
- 列出所有指标: “显示所有弹性搜索索引”
- 搜索数据: “在总部销售指数中搜索销售数据”
- 获取映射: “hq.menuitems索引中有哪些字段?”
- 检查集群: “显示elasticsearch集群状态”
与官方服务器比较
| 功能 | 官方服务器 | 此自定义服务器 |
|---|---|---|
| 认证 | 需要 ES_URL + ES_API_KEY | 只需要 ES_URL (公钥授权) |
| list_indices | 需要 indexPattern parameter | 默认为“\*”的可选参数 |
| 可用工具 | 4工具(功能相同) | 4工具(增强可用性) |
| 安全 | 基于API密钥 | 公钥授权 |
| 并发 | 同步阻塞 | 与连接池异步 |
| 演出 | 一次一个请求 | 100+并发请求 |
并发请求处理
此MCP服务器旨在使用行业最佳实践同时处理来自多个应用程序的多个并行请求:
主要特点:
✅ 异步/等待架构 -用于并行请求处理的非阻塞I/O ✅ 连接池 -重用HTTP连接(最多100个并发连接) ✅ HTTP/2支持 -在单个连接上复用多个请求 ✅ 可配置限制 -调整工作负载的连接限制 ✅ 线程安全 -FastMCP安全地处理并发工具执行
性能特点:
- 违约: 100个并发连接,20个保活连接
- 可扩展性: 配置多达1000+个并发连接
- 高效: 连接重用可将延迟减少约50%
- 可靠: 适当的超时处理可防止连接耗尽
高流量配置:
{
"mcpServers": {
"elasticsearch-custom": {
"command": "docker",
"args": [
"run", "-i", "--rm",
"--add-host=host.docker.internal:host-gateway",
"-e", "ES_URL=http://host.docker.internal:9400",
"-e", "MAX_CONNECTIONS=200",
"-e", "MAX_KEEPALIVE_CONNECTIONS=50",
"-e", "CONNECTION_TIMEOUT=60",
"-e", "REQUEST_TIMEOUT=60",
"elasticsearch-mcp:latest"
]
}
}
}测试并发请求:
# Test 10 parallel requests
for i in {1..10}; do
echo '{"jsonrpc": "2.0", "id": '$i', "method": "tools/call", "params": {"name": "list_indices", "arguments": {}}}' | \
python3 simple_elasticsearch_mcp.py &
done
wait文件
simple_elasticsearch_mcp.py-主MCP服务器Dockerfile-容器构建说明requirements.txt-Python依赖关系
手动测试
直接测试服务器:
python3 simple_elasticsearch_mcp.py使用JSON-RPC命令进行测试:
1.列出所有工具:
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}' | python3 simple_elasticsearch_mcp.py2.列出所有指标:
echo '{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "list_indices", "arguments": {}}}' | python3 simple_elasticsearch_mcp.py3.搜索数据:
echo '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "search", "arguments": {"index": "hq.sales", "queryBody": {"query": {"match_all": {}}, "size": 3}}}}' | python3 simple_elasticsearch_mcp.py4.获取索引映射:
echo '{"jsonrpc": "2.0", "id": 4, "method": "tools/call", "params": {"name": "get_mappings", "arguments": {"index": "hq.menuitems"}}}' | python3 simple_elasticsearch_mcp.py5.检查集群碎片:
echo '{"jsonrpc": "2.0", "id": 5, "method": "tools/call", "params": {"name": "get_shards", "arguments": {}}}' | python3 simple_elasticsearch_mcp.py设置自定义Elasticsearch URL:
ES_URL="http://your-es-host:9200" python3 simple_elasticsearch_mcp.py故障排除
❌ “连接被拒绝”或“超时”错误
根本原因: 当Elasticsearch可以通过SSH隧道访问时,最常见的问题是Docker容器网络。
解决方案: 确保满足以下要求:
1.SSH隧道必须处于活动状态
如果你的Elasticsearch位于SSH隧道之后(常见于云部署):
# Start SSH tunnel to forward port 9400
ssh -L 9400:localhost:9400 -N -f -l username your-server-ip
# Verify tunnel is working
curl -X GET "localhost:9400/_cluster/health?pretty"2.正确的Docker配置
你的 mcp.json 应该使用 确切地 此配置:
"elasticsearch-custom": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"--add-host=host.docker.internal:host-gateway",
"-e",
"ES_URL=http://host.docker.internal:9400",
"elasticsearch-mcp:latest"
]
}要点:
- ✅ 使用
--add-host=host.docker.internal:host-gateway(不是IP地址) - ✅ 使用
ES_URL=http://host.docker.internal:9400(不是本地主机) - ✅ 启动Cursor之前,SSH隧道必须正在运行
3.测试Docker连接
# Test if Docker can reach your Elasticsearch
docker run --rm --add-host=host.docker.internal:host-gateway alpine/curl \
curl -s http://host.docker.internal:9400/_cluster/health4.完成MCP Docker测试
使用此综合命令测试完整的MCP工作流程:
# Full MCP server test with proper initialization
{
echo '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test-client", "version": "1.0.0"}}}';
echo '{"jsonrpc": "2.0", "method": "notifications/initialized", "params": {}}';
echo '{"jsonrpc": "2.0", "id": 3, "method": "tools/call", "params": {"name": "list_indices", "arguments": {}}}';
} | docker run -i --rm --add-host=host.docker.internal:host-gateway -e ES_URL="http://host.docker.internal:9400" elasticsearch-mcp:latest预期产量:
- 带有服务器信息的初始化响应
- JSON格式的所有Elasticsearch索引列表
- 无错误消息
5.替代方案:网络主机模式
如果 host-gateway 不起作用,请尝试网络主机模式:
"args": [
"run", "-i", "--rm", "--network=host",
"-e", "ES_URL=http://localhost:9400",
"elasticsearch-mcp:latest"
]❌ “初始化完成前收到请求”
根本原因: MCP协议需要正确的初始化顺序。
解决方案: 在调用工具之前始终进行初始化:
# Correct sequence:
echo '{"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2024-11-05", "capabilities": {}, "clientInfo": {"name": "test", "version": "1.0"}}}'
echo '{"jsonrpc": "2.0", "method": "notifications/initialized", "params": {}}'
echo '{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "list_indices", "arguments": {}}}'就是这样!
构建→ 添加到配置→ 重新启动游标→ Done! 🚀
