Elasticsearch MCP服务器

Elasticsearch MCP服务器是一个为Elasticsearch集群提供交互工具和资源的服务,支持索引列表、映射获取、搜索等功能。

  • by sajitsasi
  • Python
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 sajitsasi/es_mcp_server

使用说明

Elasticsearch MCP服务器

该项目为Elasticsearch实现了一个MCP(模型上下文协议)服务器,提供了与Elasticsearchs集群交互的工具和资源。

特性

工具

  • list_indices:列出Elasticsearch集群中的所有索引
  • get_mappings:获取特定索引的映射
  • search:使用提供的查询DSL执行Elasticsearch搜索
  • search_with_query_string:使用简单的查询字符串执行搜索
  • get_index_stats:获取特定索引的统计信息

资源

  • elasticsearch://indices:列出所有Elasticsearch索引
  • elasticsearch://index/{index_name}:获取特定索引的详细信息
  • elasticsearch://mapping/{index_name}:获取特定索引的映射信息

先决条件

  • Python 3.7+
  • Elasticsearch Python客户端
  • MCP-SDK
  • Elasticsearch集群凭据(云ID和API密钥)

设置

  1. 克隆存储库:

    git clone https://github.com/yourusername/elasticsearch-mcp-server.git
    cd elasticsearch-mcp-server
    
  2. 安装所需的依赖项:

    pip install -r requirements.txt
    
  3. 设置环境变量:

    • 复制示例环境文件: cp .env.example .env
    • 编辑 .env 文件并添加您的Elasticsearch凭据

    或者直接在shell中设置它们:

    export ES_CLOUD_ID=your_elasticsearch_cloud_id
    export ES_API_KEY=your_elasticsearch_api_key
    

为Claude配置MCP服务器

这 configure_mcp_server.py 该脚本帮助您在Claude的MCP设置文件中设置Elasticsearch MCP服务器。这允许Claude通过MCP服务器连接到您的Elasticsearch集群。

python configure_mcp_server.py your_cloud_id your_api_key

此脚本:

  1. 将您的Elasticsearch云ID和API密钥作为命令行参数
  2. 查找或创建Claude MCP设置文件
  3. 添加或更新Elasticsearch MCP服务器配置
  4. 设置服务器连接到Elasticsearch集群所需的环境变量

运行此脚本后,重新启动VS Code以应用更改。然后,Claude将能够使用Elasticsearch MCP服务器与您的Elasticsearchs集群进行交互。

测试MCP资源

选项1:使用测试脚本

我们提供了一个测试脚本,用于启动MCP服务器并提供测试说明:

# Make the script executable if needed
chmod +x test_es_mcp.sh

# Run the test script
ES_CLOUD_ID=your_cloud_id ES_API_KEY=your_api_key ./test_es_mcp.sh

脚本将:

  1. 在后台启动MCP服务器
  2. 提供测试资源的说明
  3. 保持服务器运行,直到按Ctrl+C

选项2:手动测试

  1. 启动MCP服务器:

    ES_CLOUD_ID=your_cloud_id ES_API_KEY=your_api_key python es_mcp_server.py
    
  2. 在克劳德,使用 access_mcp_resource 访问资源的工具:

    a.列出所有指标:

    <access_mcp_resource>
    <server_name>elasticsearch-mcp-server</server_name>
    <uri>elasticsearch://indices</uri>
    </access_mcp_resource>
    

    b.获取特定索引的信息:

    <access_mcp_resource>
    <server_name>elasticsearch-mcp-server</server_name>
    <uri>elasticsearch://index/your_index_name</uri>
    </access_mcp_resource>
    

    c.获取特定索引的映射:

    <access_mcp_resource>
    <server_name>elasticsearch-mcp-server</server_name>
    <uri>elasticsearch://mapping/your_index_name</uri>
    </access_mcp_resource>
    

选项3:使用Python测试脚本

我们还提供了一个Python测试脚本,演示了如何访问资源:

ES_CLOUD_ID=your_cloud_id ES_API_KEY=your_api_key python test_es_resources.py

资源详细信息

elasticsearch://indices

返回Elasticsearch集群中所有索引的JSON数组,包括:

  • 索引名称
  • 健康状况
  • 状态
  • 文档计数
  • 尺寸

elasticsearch://index/{index_name}

返回特定索引的详细信息,包括:

  • 索引名称
  • 设置
  • 统计数据(文档计数、字节大小和MB)

elasticsearch://mapping/{index_name}

返回特定索引的映射信息,包括:

  • 完整的映射定义
  • 字段计数
  • 字段类型分布

错误处理

所有资源都包括正确的错误处理和验证:

  • 如果索引不存在,资源将返回相应的错误消息
  • 如果连接到Elasticsearch时出现问题,资源将返回错误消息
  • 所有异常都会被捕获并作为可读的错误消息返回

贡献

欢迎投稿!以下是您如何做出贡献:

  1. 分叉存储库
  2. 创建要素分支: git checkout -b feature/your-feature-name
  3. 提交您的更改: git commit -am 'Add some feature'
  4. 推到分支: git push origin feature/your-feature-name
  5. 提交拉取请求

许可证

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