Skip to content

Commit 7b77627

Browse files
author
huamihe
committed
use official GO MCP SDK
1 parent 0eed916 commit 7b77627

File tree

10 files changed

+914
-522
lines changed

10 files changed

+914
-522
lines changed

Dockerfile

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.21-alpine AS builder
1+
FROM golang:1.24-alpine AS builder
22

33
WORKDIR /app
44

@@ -12,15 +12,27 @@ RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags="-w -s" -o
1212

1313
FROM alpine:latest
1414

15-
RUN apk --no-cache add ca-certificates
15+
RUN apk --no-cache add ca-certificates curl
1616
WORKDIR /root/
1717

1818
COPY --from=builder /app/mcp-elasticsearch .
1919

20-
ENV MCP_PROTOCOL=stdio
20+
# Use HTTP protocol for container environments
21+
ENV MCP_PROTOCOL=http
22+
ENV MCP_ADDRESS=0.0.0.0
23+
ENV MCP_PORT=8080
2124
ENV ES_ADDRESSES=http://localhost:9200
2225
ENV ES_VERSION=8
2326

2427
EXPOSE 8080
2528

29+
# Health check for HTTP mode
30+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
31+
CMD if [ "$MCP_PROTOCOL" = "http" ]; then curl -f http://localhost:${MCP_PORT}/health || exit 1; else exit 0; fi
32+
33+
# Add labels
34+
LABEL org.opencontainers.image.title="Elasticsearch MCP Server"
35+
LABEL org.opencontainers.image.description="Model Context Protocol server for Elasticsearch"
36+
LABEL org.opencontainers.image.url="https://github.com/AeaZer/mcp-elasticsearch"
37+
2638
CMD ["./mcp-elasticsearch"]

README.md

Lines changed: 193 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44

55
## Overview
66

7-
An Elasticsearch MCP (Model Context Protocol) server built on [github.com/mark3labs/mcp-go](https://github.com/mark3labs/mcp-go), providing seamless integration with Elasticsearch 7, 8, and 9 versions.
7+
An Elasticsearch MCP (Model Context Protocol) server built on [github.com/modelcontextprotocol/go-sdk](https://github.com/modelcontextprotocol/go-sdk), providing seamless integration with Elasticsearch 7, 8, and 9 versions.
88

99
## Features
1010

11-
- 🔗 **Multi-Protocol Support**: Supports both stdio and Streamable HTTP protocols
11+
- 🔗 **Multi-Protocol Support**: Supports stdio, Streamable HTTP, and SSE protocols (SSE deprecated)
1212
- 📊 **Multi-Version Compatibility**: Compatible with Elasticsearch 7, 8, and 9
1313
- ⚙️ **Environment Configuration**: Configure via environment variables
1414
- 🔧 **Rich Toolset**: Complete set of Elasticsearch operation tools
1515
- 🌐 **Production Ready**: Docker support with optimized builds
16+
- 🐳 **Container Ready**: Pre-built Docker images available
1617

1718
## Supported Tools
1819

@@ -33,7 +34,9 @@ An Elasticsearch MCP (Model Context Protocol) server built on [github.com/mark3l
3334
- `es_document_delete`: Delete documents by ID
3435

3536
### Search Operations
36-
- `es_search`: Execute search queries with filters and sorting
37+
- `es_search`: Execute search queries with filters, sorting, and field selection
38+
- Supports: `index`, `query`, `size`, `from`, `sort`, `_source`
39+
- Full Elasticsearch Query DSL support
3740

3841
### Bulk Operations
3942
- `es_bulk`: Execute multiple operations in a single request
@@ -42,7 +45,31 @@ An Elasticsearch MCP (Model Context Protocol) server built on [github.com/mark3l
4245

4346
Choose one of the following methods to run the Elasticsearch MCP server:
4447

45-
### Method 1: Build Docker Image (Recommended)
48+
### Method 1: Use Pre-built Docker Image (Recommended)
49+
50+
```bash
51+
# Basic usage with local Elasticsearch
52+
docker run --rm \
53+
-e ES_ADDRESSES=http://localhost:9200 \
54+
ghcr.io/aeazer/mcp-elasticsearch:latest
55+
56+
# HTTP mode for remote access
57+
docker run -d -p 8080:8080 \
58+
-e MCP_PROTOCOL=http \
59+
-e ES_ADDRESSES=http://your-elasticsearch:9200 \
60+
ghcr.io/aeazer/mcp-elasticsearch:latest
61+
62+
# With authentication
63+
docker run -d -p 8080:8080 \
64+
-e MCP_PROTOCOL=http \
65+
-e ES_ADDRESSES=https://your-elasticsearch:9200 \
66+
-e ES_USERNAME=elastic \
67+
-e ES_PASSWORD=your-password \
68+
-e ES_SSL=true \
69+
ghcr.io/aeazer/mcp-elasticsearch:latest
70+
```
71+
72+
### Method 2: Build Docker Image
4673

4774
```bash
4875
# Clone the repository
@@ -56,15 +83,6 @@ docker build -t mcp-elasticsearch .
5683
docker run -e ES_ADDRESSES=http://localhost:9200 -e ES_VERSION=8 mcp-elasticsearch
5784
```
5885

59-
### Method 2: Use Pre-built Image (Coming Soon)
60-
61-
```bash
62-
# This will be available when the image is published to a registry
63-
# docker run -e ES_ADDRESSES=http://localhost:9200 ghcr.io/aeazer/mcp-elasticsearch:latest
64-
```
65-
66-
*Note: Pre-built images are not yet available. Please use Method 1 or Method 3.*
67-
6886
### Method 3: Compile from Source
6987

7088
```bash
@@ -83,6 +101,67 @@ export MCP_PROTOCOL=stdio
83101
./mcp-elasticsearch
84102
```
85103

104+
## Docker Usage Examples
105+
106+
### Basic Stdio Mode (for LLM tool integration)
107+
```bash
108+
docker run -it --rm \
109+
-e ES_ADDRESSES=http://host.docker.internal:9200 \
110+
ghcr.io/aeazer/mcp-elasticsearch:latest
111+
```
112+
113+
### HTTP Server Mode (for n8n, API access)
114+
```bash
115+
docker run -d -p 8080:8080 \
116+
--name mcp-elasticsearch \
117+
-e MCP_PROTOCOL=http \
118+
-e ES_ADDRESSES=http://host.docker.internal:9200 \
119+
ghcr.io/aeazer/mcp-elasticsearch:latest
120+
121+
# Test the server endpoints
122+
curl http://localhost:8080/health # Health check
123+
curl http://localhost:8080/mcp # MCP endpoint (requires proper MCP client)
124+
```
125+
126+
### With Elastic Cloud
127+
```bash
128+
docker run -d -p 8080:8080 \
129+
-e MCP_PROTOCOL=http \
130+
-e ES_CLOUD_ID="your-cloud-id" \
131+
-e ES_USERNAME=elastic \
132+
-e ES_PASSWORD="your-password" \
133+
-e ES_VERSION=8 \
134+
ghcr.io/aeazer/mcp-elasticsearch:latest
135+
```
136+
137+
### Using Docker Compose
138+
Create a `docker-compose.yml` file:
139+
140+
```yaml
141+
version: '3.8'
142+
services:
143+
mcp-elasticsearch:
144+
image: ghcr.io/aeazer/mcp-elasticsearch:latest
145+
ports:
146+
- "8080:8080"
147+
environment:
148+
- MCP_PROTOCOL=http
149+
- ES_ADDRESSES=http://elasticsearch:9200
150+
- ES_VERSION=8
151+
depends_on:
152+
- elasticsearch
153+
154+
elasticsearch:
155+
image: docker.elastic.co/elasticsearch/elasticsearch:8.11.0
156+
environment:
157+
- discovery.type=single-node
158+
- xpack.security.enabled=false
159+
ports:
160+
- "9200:9200"
161+
```
162+
163+
Run with: `docker-compose up -d`
164+
86165
## Configuration
87166

88167
All configuration is done via environment variables:
@@ -108,27 +187,58 @@ All configuration is done via environment variables:
108187
|----------|-------------|---------|
109188
| `MCP_SERVER_NAME` | Server name for MCP | `Elasticsearch MCP Server` |
110189
| `MCP_SERVER_VERSION` | Server version | `1.0.0` |
111-
| `MCP_PROTOCOL` | Protocol to use (`stdio` or `http`) | `stdio` |
112-
| `MCP_ADDRESS` | Streamable HTTP server address (HTTP mode only) | `localhost` |
190+
| `MCP_PROTOCOL` | Protocol to use (`stdio`, `http`, or `sse` - deprecated) | `http` (in Docker), `stdio` (native) |
191+
| `MCP_ADDRESS` | Streamable HTTP server address (HTTP mode only) | `0.0.0.0` (in Docker), `localhost` (native) |
113192
| `MCP_PORT` | Streamable HTTP server port (HTTP mode only) | `8080` |
114193

194+
### Protocol Endpoints
195+
196+
Different protocols use different access methods:
197+
198+
#### Stdio Protocol
199+
- **Access method**: Direct stdin/stdout communication
200+
- **Use case**: LLM tool integration (Claude Desktop, etc.)
201+
- **Endpoint**: N/A (direct process communication)
202+
203+
#### Streamable HTTP Protocol (Recommended)
204+
- **MCP endpoint**: `http://host:port/mcp`
205+
- **Health check**: `http://host:port/health`
206+
- **Use case**: Remote access, n8n integration, API usage
207+
- **Example**: `http://localhost:8080/mcp`
208+
209+
#### SSE Protocol (Deprecated)
210+
- **MCP endpoint**: `http://host:port/sse`
211+
- **Use case**: Legacy SSE clients (not recommended)
212+
- **Example**: `http://localhost:8080/sse`
213+
- ⚠️ **Warning**: Deprecated, use HTTP protocol instead
214+
115215
## Usage Examples
116216

117-
### Stdio Mode (Default)
217+
### Stdio Mode (Default for native builds)
118218
```bash
119219
export ES_ADDRESSES=http://localhost:9200
120220
export MCP_PROTOCOL=stdio
121221
./mcp-elasticsearch
122222
```
123223

124-
### Streamable HTTP Mode
224+
### Streamable HTTP Mode (Default for Docker)
125225
```bash
126226
export ES_ADDRESSES=http://localhost:9200
127227
export MCP_PROTOCOL=http
128228
export MCP_PORT=8080
129229
./mcp-elasticsearch
130230
```
131231

232+
### SSE Mode (Deprecated - Not Recommended)
233+
⚠️ **WARNING**: SSE protocol is deprecated and not recommended for production use. Use Streamable HTTP instead.
234+
235+
```bash
236+
export ES_ADDRESSES=http://localhost:9200
237+
export MCP_PROTOCOL=sse
238+
export MCP_PORT=8080
239+
./mcp-elasticsearch
240+
```
241+
132242
### Using with Elastic Cloud
133243
```bash
134244
export ES_CLOUD_ID=your_cloud_id
@@ -138,12 +248,12 @@ export ES_VERSION=8
138248
./mcp-elasticsearch
139249
```
140250

141-
142251
## Development
143252

144253
### Prerequisites
145-
- Go 1.21 or higher
254+
- Go 1.23 or higher
146255
- Access to an Elasticsearch cluster
256+
- Docker (optional, for containerized development)
147257

148258
### Building
149259
```bash
@@ -156,6 +266,11 @@ go build -o mcp-elasticsearch main.go
156266
go test ./...
157267
```
158268

269+
### Building Docker Image
270+
```bash
271+
docker build -t mcp-elasticsearch .
272+
```
273+
159274
## Tool Usage Examples
160275

161276
### Get Cluster Information
@@ -193,7 +308,7 @@ go test ./...
193308
"arguments": {
194309
"index": "my-index",
195310
"id": "doc1",
196-
"document": {
311+
"body": {
197312
"title": "Hello World",
198313
"content": "This is a test document",
199314
"timestamp": "2024-01-01T00:00:00Z"
@@ -202,26 +317,74 @@ go test ./...
202317
}
203318
```
204319

205-
### Search Documents
320+
### Advanced Search with Sorting and Field Selection
206321
```json
207322
{
208323
"tool": "es_search",
209324
"arguments": {
210325
"index": "my-index",
211326
"query": {
212-
"match": {
213-
"title": "Hello"
327+
"bool": {
328+
"must": [
329+
{"match": {"title": "Hello"}}
330+
],
331+
"filter": [
332+
{"range": {"timestamp": {"gte": "2024-01-01"}}}
333+
]
214334
}
215335
},
216-
"size": 10
336+
"sort": [
337+
{"timestamp": {"order": "desc"}},
338+
{"_score": {"order": "desc"}}
339+
],
340+
"_source": ["title", "content", "timestamp"],
341+
"size": 20,
342+
"from": 0
217343
}
218344
}
219345
```
220346

347+
## Health Monitoring
348+
349+
When running in HTTP mode, the server provides multiple endpoints:
350+
351+
### Health Check Endpoint
352+
```bash
353+
# Check server health (publicly accessible)
354+
curl http://localhost:8080/health
355+
356+
# Response
357+
{"status":"healthy","server":"elasticsearch-mcp"}
358+
```
359+
360+
### MCP Protocol Endpoint
361+
```bash
362+
# MCP communication endpoint (requires MCP client)
363+
# URL: http://localhost:8080/mcp
364+
# This endpoint handles MCP protocol messages and tool calls
365+
# Not directly accessible via simple HTTP GET requests
366+
```
367+
368+
### Important Notes
369+
- **Health endpoint** (`/health`): Simple HTTP GET for monitoring
370+
- **MCP endpoint** (`/mcp`): For MCP protocol communication only
371+
- **SSE endpoint** (`/sse`): Deprecated, avoid using
372+
221373
## Error Handling
222374

223375
All errors are reported within the MCP tool results with `isError: true`, allowing LLMs to see and handle errors appropriately. Protocol-level errors are reserved for exceptional conditions like missing tools or server failures.
224376

377+
## Troubleshooting
378+
379+
### Container Issues
380+
- **Container exits immediately**: Ensure you're using HTTP protocol for Docker containers
381+
- **Cannot connect to Elasticsearch**: Use `host.docker.internal:9200` instead of `localhost:9200` in Docker
382+
- **Permission denied**: Check Docker daemon permissions and image access
383+
384+
### Network Issues
385+
- **Connection refused**: Verify Elasticsearch is running and accessible
386+
- **SSL errors**: Set `ES_INSECURE_SKIP_VERIFY=true` for testing with self-signed certificates
387+
225388
## Contributing
226389

227390
1. Fork the repository
@@ -234,9 +397,12 @@ All errors are reported within the MCP tool results with `isError: true`, allowi
234397

235398
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
236399

237-
238400
## Acknowledgments
239401

240-
- [Mark3Labs MCP-Go](https://github.com/mark3labs/mcp-go) - MCP implementation for Go
402+
- [Official MCP Go SDK](https://github.com/modelcontextprotocol/go-sdk) - Official MCP implementation for Go
241403
- [Elastic](https://github.com/elastic/go-elasticsearch) - Official Elasticsearch Go client
242-
- [Model Context Protocol](https://modelcontextprotocol.io/) - Protocol specification
404+
- [Model Context Protocol](https://modelcontextprotocol.io/) - Protocol specification
405+
406+
<div align="center">
407+
<sub>Built with ❤️ for the Go community</sub>
408+
</div>

0 commit comments

Comments
 (0)