yahito/opensearch-mcp-server
If you are the rightful owner of opensearch-mcp-server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
A Model Context Protocol (MCP) server for interacting with OpenSearch clusters, providing tools to search, query, and retrieve data from OpenSearch indices.
OpenSearch MCP Python Server
A Model Context Protocol (MCP) server for interacting with OpenSearch clusters. This server provides tools to search, query, and retrieve data from OpenSearch indices.
Features
- Search Operations: Execute various types of search queries (simple string, match, range queries)
- Index Management: List and inspect OpenSearch indices
- Document Retrieval: Get specific documents by ID
- Cluster Health: Monitor cluster status and health
- Index Mapping: Retrieve index mappings and schema information
Installation
-
Clone or create the project directory
-
Install dependencies:
pip install -r requirements.txt
-
Copy the environment configuration:
cp .env.example .env
-
Edit
.env
with your OpenSearch connection details:OPENSEARCH_HOST=your-opensearch-host OPENSEARCH_PORT=9200 OPENSEARCH_USERNAME=your-username OPENSEARCH_PASSWORD=your-password OPENSEARCH_USE_SSL=true OPENSEARCH_VERIFY_CERTS=true
Configuration
The server uses environment variables for configuration:
OPENSEARCH_HOST
: OpenSearch host (default: localhost)OPENSEARCH_PORT
: OpenSearch port (default: 9200)OPENSEARCH_USERNAME
: Username for authenticationOPENSEARCH_PASSWORD
: Password for authenticationOPENSEARCH_USE_SSL
: Use SSL connection (default: false)OPENSEARCH_VERIFY_CERTS
: Verify SSL certificates (default: false)
Usage
Running the Server
python opensearch_mcp_server.py
Available Operations
The OpenSearch service provides the following methods:
Search Operations
search(index, query, size)
: Execute custom querysimple_search(index, query_string, size)
: Simple string-based searchmatch_search(index, field, value, size)
: Match query for specific fieldrange_search(index, field, gte, lte, gt, lt, size)
: Range-based queries
Data Retrieval
get_document(index, doc_id)
: Retrieve specific documentget_indices()
: List all available indicesget_index_mapping(index)
: Get index mapping/schema
Cluster Information
get_cluster_health()
: Get cluster health status
Example Queries
Simple Text Search
results = opensearch_service.simple_search("my-index", "search terms", size=10)
Match Query
results = opensearch_service.match_search("my-index", "title", "search value", size=5)
Range Query
results = opensearch_service.range_search("my-index", "timestamp", gte="2024-01-01", lte="2024-12-31")
Custom Query
custom_query = {
"query": {
"bool": {
"must": [
{"match": {"title": "important"}},
{"range": {"date": {"gte": "2024-01-01"}}}
]
}
}
}
results = opensearch_service.search("my-index", custom_query, size=20)
Data Models
The server uses structured data models:
SearchResponse
: Contains search results with hits, total count, and metadataSearchHit
: Individual search result with document data and scoreIndexInfo
: Index metadata including health, document count, and sizeClusterHealth
: Cluster status and node information
Error Handling
The service handles common OpenSearch errors:
- Connection errors
- Authentication failures
- Invalid queries
- Missing indices
- Network timeouts
Security
- Supports SSL/TLS connections
- Username/password authentication
- Certificate verification options
- Environment-based credential management
Development
To extend the server:
- Add new methods to
OpenSearchService
class - Create corresponding data models in
opensearch_models.py
- Update configuration in
config.py
if needed - Test with your OpenSearch cluster
Dependencies
opensearch-py
: Official OpenSearch Python clientpython-dotenv
: Environment variable managementrequests
: HTTP client librarymcp
: Model Context Protocol frameworkfastapi
: Web framework for HTTP endpointsuvicorn
: ASGI server