内容
<div align="center">
# ElasticMCP
### 为 Elasticsearch 构建的 MCP 服务器 — 使用 .NET 10 构建
[](https://github.com/riccardomerenda/elastic-mcp/actions/workflows/ci.yml)
[](https://dotnet.microsoft.com/)
[](https://www.elastic.co/elasticsearch)
[](https://modelcontextprotocol.io/)
[](LICENSE)
**让任何 AI 代理都可以通过自然语言查询、探索和分析您的 Elasticsearch 数据。**
[快速开始](#getting-started) · [工具和资源](#tools--resources) · [配置](#configuration) · [架构](#how-it-works)
</div>
---
## 为什么选择 ElasticMCP?
Elasticsearch 的查询 DSL 很强大,但也很复杂。编写 bool 查询、聚合和 kNN 搜索需要对语法有深入的了解。**ElasticMCP 弥补了这一差距。**
它是一个 [MCP](https://modelcontextprotocol.io/) 服务器,允许 AI 代理(如 Claude、GitHub Copilot、Cursor、ChatGPT)通过结构化工具调用来搜索、聚合和探索您的 Elasticsearch 集群。无需复制粘贴 JSON,无需记住 DSL。
> **零 AI/LLM 成本。** ElasticMCP 不调用任何 LLM。智能生活在客户端。服务器是 MCP 协议和 Elasticsearch API 之间的纯翻译层。
---
## 快速开始
### 先决条件
- [.NET 10 SDK](https://dotnet.microsoft.com/download/dotnet/10.0)
- 一个 Elasticsearch 9.x 集群(本地或远程)
### 选项 1 — 作为 .NET 工具安装
```bash
dotnet tool install -g ElasticMcp
elastic-mcp
```
### 选项 2 — 从源代码运行
```bash
git clone https://github.com/riccardomerenda/elastic-mcp.git
cd elastic-mcp
dotnet run --project src/ElasticMcp/ElasticMcp.csproj
```
### 选项 3 — Docker(HTTP 传输)
```bash
docker build -t elastic-mcp .
docker run -p 8080:8080 -e ElasticMcp__Nodes__0=http://host.docker.internal:9200 elastic-mcp
```
### 连接到 Claude Desktop
将以下内容添加到您的 `claude_desktop_config.json` 中:
```json
{
"mcpServers": {
"elasticsearch": {
"command": "dotnet",
"args": ["run", "--project", "/path/to/elastic-mcp/src/ElasticMcp/ElasticMcp.csproj"]
}
}
}
```
### 通过 HTTP 连接(远程 / 流式 HTTP)
运行 HTTP 服务器:
```bash
dotnet run --project src/ElasticMcp.Http/ElasticMcp.Http.csproj
```
然后使用流式 HTTP 传输将任何 MCP 客户端连接到 `http://localhost:5000/mcp`。
### 连接到 VS Code / Copilot
将以下内容添加到 `.vscode/mcp.json` 中:
```json
{
"servers": {
"elasticsearch": {
"type": "stdio",
"command": "dotnet",
"args": ["run", "--project", "/path/to/elastic-mcp/src/ElasticMcp/ElasticMcp.csproj"]
}
}
}
```
---
## 工具和资源
### 工具 — AI 可以调用的动作
| 工具 | 描述 |
|------|-------------|
| `search` | 使用 Lucene 查询语法、分页和结果大小限制进行全文搜索 |
| `semantic_search` | 在 `dense_vector` 字段上进行 kNN 矢量搜索 — 通过嵌入查找相似文档 |
| `count` | 统计与可选查询过滤器匹配的文档 |
| `aggregate` | 运行聚合 — 词条、日期直方图、平均值、总和、最小值、最大值、基数 |
| `get_document` | 通过 ID 检索单个文档 |
| `explain_query` | 显示生成的查询 DSL 而不执行它 |
### 提示 — 引导式工作流模板
| 提示 | 描述 |
|--------|-------------|
| `explore_index` | 逐步探索:映射、样本、计数、聚合 |
| `log_analysis` | 确定错误模式、主要服务、基于时间的趋势 |
| `semantic_qa` | 通过 kNN 在矢量索引的知识库中回答问题 |
### 资源 — AI 的只读上下文
| 资源 | URI | 描述 |
|----------|-----|-------------|
| 集群健康状态 | `elasticsearch://cluster/health` | 集群状态、节点计数、分片信息 |
| 集群索引 | `elasticsearch://cluster/indices` | 所有索引的文档计数和存储大小 |
| 索引映射 | `elasticsearch://index/{name}/mapping` | 字段名称、类型和功能 |
| 索引设置 | `elasticsearch://index/{name}/settings` | 分片、副本、分析器、刷新间隔 |
| 索引样本 | `elasticsearch://index/{name}/sample` | 样本文档以了解数据结构 |
---
## 配置
ElasticMCP 通过项目根目录下的 `appsettings.json` 进行配置:
```json
{
"ElasticMcp": {
"Nodes": ["https://localhost:9200"],
"Authentication": {
"Type": "ApiKey",
"ApiKey": "your-api-key-here"
},
"ReadOnly": true,
"MaxResultSize": 100,
"QueryTimeout": "30s",
"AllowedIndices": ["logs-*", "documents-*"],
"DeniedIndices": [".security-*", ".kibana*"],
"RedactedFields": ["password", "ssn", "credit_card"],
"SemanticSearch": {
"DefaultVectorField": "embedding",
"DefaultK": 10,
"DefaultNumCandidates": 100
}
}
}
```
| 设置 | 默认值 | 描述 |
|---------|---------|-------------|
| `Nodes` | `["http://localhost:9200"]` | Elasticsearch 节点 URL |
| `Authentication.Type` | `None` | `None`、`ApiKey` 或 `Basic` |
| `ReadOnly` | `true` | 防止写入/删除操作 |
| `MaxResultSize` | `100` | 返回文档数量上限 |
| `QueryTimeout` | `30s` | 每个查询的超时时间 |
| `AllowedIndices` | `[]` | AI 可以访问的索引模式 |
| `DeniedIndices` | `[]` | 阻止 AI 访问的索引模式 |
| `RedactedFields` | `[]` | 从结果和映射中删除的字段名称 |
| `SemanticSearch.DefaultVectorField` | `embedding` | kNN 搜索的默认密集向量字段 |
| `SemanticSearch.DefaultK` | `10` | 默认最近邻数量 |
| `SemanticSearch.DefaultNumCandidates` | `100` | kNN 的默认候选池大小 |
环境变量覆盖任何设置(例如 `ElasticMcp__Authentication__ApiKey`)。
---
## 工作原理
```
您:“显示我过去一小时生产日志中的主要错误类型”
│
▼
┌─────────────────────────────────────────────────┐
│ AI 客户端(Claude、Copilot、Cursor、ChatGPT) │
│ 推理请求,选取合适的工具,构建结构化参数 │
└────────────────────┬────────────────────────────┘
│ MCP(JSON-RPC via stdio
│ 或流式 HTTP)
▼
┌─────────────────────────────────────────────────┐
│ ElasticMCP 服务器 │
│ │
│ 将结构化参数翻译为查询 DSL │
│ 执行 Elasticsearch 查询 │
│ 返回格式化结果 │
│ │
│ 无 LLM 调用。纯 C# 翻译层。 │
└────────────────────┬────────────────────────────┘
│ HTTPS
▼
┌─────────────────────────────────────────────────┐
│ Elasticsearch 9.x 集群 │
└─────────────────────────────────────────────────┘
```
关键的洞察:**ElasticMCP 从不调用 LLM。** 它接收来自 AI 客户端的结构化工具调用,将它们翻译成 Elasticsearch 查询 DSL,使用纯 C# 逻辑,并返回结果。AI 客户端处理所有自然语言理解。
---
## 开发
### 构建和测试
```powershell
# 构建
dotnet build ElasticMcp.slnx
# 仅运行单元测试(无需 Docker)
dotnet test tests/ElasticMcp.Tests/
# 运行集成测试(需要 Docker)
dotnet test tests/ElasticMcp.IntegrationTests/
# 运行所有测试
dotnet test ElasticMcp.slnx
# 运行单个测试类
dotnet test --filter "FullyQualifiedName~SearchToolTests"
# 完整的本地流水线(构建 + 单元测试 + 集成测试)
.\test.ps1
# 仅单元测试
.\test.ps1 -UnitOnly
# 带有代码覆盖率
.\test.ps1 -Coverage
```
集成测试使用 [Testcontainers](https://dotnet.testcontainers.org/) 启动一个真正的 Elasticsearch 9.x 实例在 Docker 中 — 无模拟,无伪造。
### 使用演示数据试用
```powershell
# 启动 Elasticsearch
docker compose -f samples/docker-compose.yml up -d
# 种子样本数据(日志、产品、用户、向量嵌入)
powershell -File samples/seed-data.ps1
# 启动 HTTP 服务器
dotnet run --project src/ElasticMcp.Http/ElasticMcp.Http.csproj
# 打开 MCP 检验器
npx @modelcontextprotocol/inspector
# 使用流式 HTTP 连接到 http://localhost:5000/mcp
```
### 项目结构
```
src/
├── ElasticMcp/ # 主要项目(stdio 传输)
│ ├── Program.cs
│ ├── Configuration/ # ElasticMcpOptions + SemanticSearchOptions
│ ├── Tools/ # search, semantic_search, count, aggregate, get_document, explain_query
│ ├── Resources/ # cluster health, indices, mapping, settings, sample
│ ├── Prompts/ # explore_index, log_analysis, semantic_qa
│ └── Services/ # ES client, SecurityGuard
├── ElasticMcp.Http/ # HTTP 服务器(流式 HTTP 传输)
tests/
├── ElasticMcp.Tests/ # 单元测试(无需 Docker)
└── ElasticMcp.IntegrationTests/ # 集成测试(Testcontainers + 真实 ES)
samples/
├── docker-compose.yml # 单节点 ES 9.x 用于本地开发
└── seed-data.ps1 # 种子演示数据(日志、产品、用户、向量)
```
---
## 路线图
### v0.1 — 基础 ✅
项目设置、CI/CD、`search` + `count` 工具、集群健康 + 索引资源、stdio 传输、Testcontainers 集成测试。
### v0.2 — 核心工具 ✅
`aggregate`、`get_document`、`explain_query` 工具。索引映射、设置和样本资源。具有索引允许/拒绝列表、字段编辑、结果大小限制和审计日志的安全防护。
### v0.3 — 语义搜索 ✅
`semantic_search` 工具,支持 `dense_vector` 字段的 kNN 矢量搜索。三个提示模板(`explore_index`、`log_analysis`、`semantic_qa`)。带有流式 HTTP 传输的 HTTP 服务器。NuGet dotnet 工具打包。用于容器化部署的 Dockerfile。包含向量嵌入的演示环境。
### v0.4 — 打磨和发布
- OpenSearch 兼容性
- 额外的提示模板
- 性能优化
---
## 技术栈
| 组件 | 技术 |
|-----------|------------|
| 运行时 | .NET 10 |
| MCP SDK | [ModelContextProtocol](https://www.nuget.org/packages/ModelContextProtocol) 1.1.0 |
| ES 客户端 | [Elastic.Clients.Elasticsearch](https://www.nuget.org/packages/Elastic.Clients.Elasticsearch) 9.3.3 |
| 主机 | Microsoft.Extensions.Hosting |
| HTTP 传输 | ASP.NET Core + ModelContextProtocol.AspNetCore |
| 测试 | xUnit + Testcontainers |
| CI/CD | GitHub Actions |
---
## 许可证
[MIT](LICENSE)