Elasticsearch MCP Server

repository·main·Indexed 17 days ago

https://github.com/elastic/mcp-server-elasticsearch

A Model Context Protocol (MCP) server that allows AI agents to interact with Elasticsearch data (version 8.x or 9.x) using natural language. It provides tools for listing indices, retrieving mappings, performing searches via query DSL, executing ES|QL queries, and getting shard information. The server supports both stdio and streamable-HTTP protocols and can be deployed as a Docker container. Note: This project is deprecated and superseded by Elastic Agent Builder.

Tokens
5.4K
Snippets
25
Records
29
Agent score
68%

What's inside elastic-mcp-server-elasticsearch

  1. Configure the streamable-HTTP protocol

    main

    Use the streamable-HTTP protocol for web-based integrations or to support multiple concurrent clients.

    Required Environment Variables:

    • ES_URL: The URL of your Elasticsearch cluster.
    • Authentication (choose one):
      • ES_API_KEY: Your Elasticsearch API key.
      • ES_USERNAME and ES_PASSWORD: Your Elasticsearch credentials.
    • Optional:
      • ES_SSL_SKIP_VERIFY: Set to true to skip SSL/TLS certificate verification.

    Run via Docker:

    docker run --rm \
      -e ES_URL \
      -e ES_API_KEY \
      -p 8080:8080 \
      docker.elastic.co/mcp/elasticsearch \
      http

    Endpoints:

    • MCP endpoint: http://<host>:8080/mcp
    • Health check: http://<host>:8080/ping (returns pong if healthy).

    Claude Desktop with HTTP proxy: If your client only supports stdio (like the free edition of Claude Desktop), use mcp-proxy to bridge the connection:

    1. Install mcp-proxy:
    uv tool install mcp-proxy
    1. Add this to Claude Desktop configuration:
    {
      "mcpServers": {
        "elasticsearch-mcp-server": {
          "command": "/<home-directory>/.local/bin/mcp-proxy",
          "args": [
            "--transport=streamablehttp",
            "--header", "Authorization", "ApiKey <elasticsearch-API-key>",
            "http://<mcp-server-host>:<mcp-server-port>/mcp"
          ]
        }
      }
    }
  2. Deploy the Elasticsearch MCP Server

    main

    The Elasticsearch MCP Server is provided as a Docker container image from AWS Marketplace. It allows AI agents to interact with Elasticsearch data using the Model Context Protocol (MCP). You can deploy it using either the stdio protocol for direct client connections or the streamable-HTTP protocol for web-based integrations.

    Prerequisites:

    • An Elasticsearch cluster (version 8.x or 9.x).
    • Authentication credentials (API key or username/password).
    • Docker installed in your environment.
    • An MCP client (e.g., Claude Desktop, Cursor, VS Code).
    • Network connectivity between the deployment environment and the Elasticsearch cluster.
  3. Development workflow for contributors

    main

    Follow these steps to contribute to the repository:

    1. Fork and clone the repository.
    2. Create a new branch: git checkout -b my-branch-name.
    3. Implement changes and include tests.
    4. Ensure code quality by running:
      • cargo clippy (fix warnings)
      • cargo fmt (format code)
      • cargo test (run tests)
    5. Test the MCP server locally using the MCP Inspector:
      npx @modelcontextprotocol/inspector
    6. Test with an MCP Client (refer to the main README for installation).
    7. Push to your fork and submit a pull request.
    npx @modelcontextprotocol/inspector
  4. Start a local Elasticsearch instance for development

    main

    For development purposes, you can run Elasticsearch and Kibana locally using the start-local script. This script uses Docker to spin up the services.

    Note: This setup is for development only. It uses basic authentication and disables HTTPS.

    • Elasticsearch URL: http://localhost:9200
    • Kibana URL: http://localhost:5601
    curl -fsSL https://elastic.co/start-local | sh
  5. Configure the stdio protocol

    main

    Use the stdio protocol when your MCP client connects directly to the server process in the same environment.

    Required Environment Variables:

    • ES_URL: The URL of your Elasticsearch cluster (e.g., https://your-cluster.es.amazonaws.com:9200).
    • Authentication (choose one):
      • ES_API_KEY: Your Elasticsearch API key.
      • ES_USERNAME and ES_PASSWORD: Your Elasticsearch credentials.
    • Optional:
      • ES_SSL_SKIP_VERIFY: Set to true to skip SSL/TLS certificate verification (use only for development/testing).

    Run via Docker:

    docker run -i --rm \
      -e ES_URL \
      -e ES_API_KEY \
      docker.elastic.co/mcp/elasticsearch \
      stdio

    Claude Desktop Configuration: Add the following to your Claude Desktop configuration file:

    {
      "mcpServers": {
        "elasticsearch-mcp-server": {
          "command": "docker",
          "args": [
            "run", "-i", "--rm",
            "-e", "ES_URL",
            "-e", "ES_API_KEY",
            "docker.elastic.co/mcp/elasticsearch",
            "stdio"
          ],
          "env": {
            "ES_URL": "<elasticsearch-cluster-url>",
            "ES_API_KEY": "<elasticsearch-API-key>"
          }
        }
      }
    }
  6. Use environment variable interpolation in configuration

    main

    The Elasticsearch MCP Server supports injecting environment variables into configuration files using a specific interpolation syntax. This allows you to keep sensitive information or environment-specific settings out of your static configuration files.

    Syntax

    You can use the following patterns within your configuration strings:

    1. Standard Variable: ${VARIABLE_NAME}

      • Replaces the placeholder with the value of the environment variable VARIABLE_NAME.
      • If the variable is not defined in the environment, the interpolation will fail with an error.
    2. Variable with Default Value: ${VARIABLE_NAME:default_value}

      • Replaces the placeholder with the value of VARIABLE_NAME if it exists.
      • If VARIABLE_NAME is not defined, it falls back to default_value.

    Error Handling

    If an interpolation fails (e.g., a variable is missing and no default is provided, or braces are mismatched), the server will report an InterpolationError specifying the reason, the line number, and the character position where the error occurred.

    # Examples of interpolation syntax
    
    # Uses the value of the ES_URL environment variable
    url: "${ES_URL}"
    
    # Uses the value of ES_URL, or falls back to localhost if not set
    url: "${ES_URL:http://localhost:9200}"
  7. Define Custom MCP Tools for ES|QL and Search Templates

    main

    You can extend the server's capabilities by defining CustomTool objects within the tools configuration. This allows AI agents to execute specific ES|QL queries or predefined Search Templates.

    Custom Tool Types

    • esql: Executes an ES|QL query.
      • Requires a query string.
      • Supports format options: json (default) or value.
    • search_template: Executes a predefined Elasticsearch Search Template.
      • Can be identified by a template_id (string) or a full template (JSON object).

    Both types inherit a base configuration containing a description, parameters (JSON schema), and optional annotations.

    {
      "tools": {
        "custom": {
          "my_esql_tool": {
            "type": "esql",
            "base": {
              "description": "Runs a specific analytics query",
              "parameters": {}
            },
            "query": "FROM logs | STATS count() BY host",
            "format": "json"
          },
          "my_template_tool": {
            "type": "search_template",
            "base": {
              "description": "Uses the standard search template",
              "parameters": {}
            },
            "template": {
              "template_id": "my-template-id"
            }
          }
        }
      }
    }
  8. Run the Elasticsearch MCP Server via CLI

    main

    The Elasticsearch MCP Server can be executed as a CLI tool. It supports standard command-line arguments and can also be configured via environment variables.

    Configuration via Environment Variables

    • .env files: The server automatically attempts to load configuration from a .env file in the current directory.
    • CLI_ARGS: You can pass command-line arguments through the CLI_ARGS environment variable. If this variable is set, its contents are split by whitespace and appended to the command.

    Testing with MCP Inspector

    To test the server using the Model Context Protocol (MCP) inspector with stdio transport, use the following command:

    npx @modelcontextprotocol/inspector cargo run -p elastic-mcp
    npx @modelcontextprotocol/inspector cargo run -p elastic-mcp
  9. Troubleshoot and monitor the Elasticsearch MCP Server

    main

    If you encounter issues, use the following methods to verify connectivity and health:

    1. Check Container Status:

    docker ps | grep elasticsearch-mcp-server

    2. Test HTTP Health Endpoint:

    curl http://<host>:8080/ping

    3. Check Container Logs:

    docker logs <container-id>

    Look for Elasticsearch connection failures, authentication errors, or network issues.

    4. Verify Elasticsearch Connectivity from inside the container: Using an API key:

    docker exec <container-id> curl -k -H "Authorization: ApiKey <api-key>" <ES_URL>

    Using Basic Auth:

    docker exec <container-id> curl -k -u <username>:<password> <ES_URL>
  10. Use Container Mode for deployment

    main

    When running the server inside a container (e.g., Docker), use the global --container-mode flag. This mode changes the default HTTP address and rewrites localhost to the host's address to ensure connectivity between the container and the host machine.

    This flag can also be enabled by setting the CONTAINER_MODE environment variable.

    elasticsearch-mcp-server --container-mode stdio
  11. Configure the Elasticsearch MCP Server

    main

    The server is configured using a JSON5 format (which supports comments and multiline strings, useful for ES|QL queries). Configuration can be provided via a file path or by using environment variables with a built-in default template.

    Default Configuration Template

    If no configuration file is provided, the server uses the following template, which interpolates environment variables:

    {
        "elasticsearch": {
            "url": "${ES_URL}",
            "api_key": "${ES_API_KEY:}",
            "username": "${ES_USERNAME:}",
            "password": "${ES_PASSWORD:}",
            "ssl_skip_verify": "${ES_SSL_SKIP_VERIFY:false}"
        }
    }

    Configuration Keys

    • elasticsearch.url: The URL of the Elasticsearch instance.
    • elasticsearch.api_key: The API key for authentication.
    • elasticsearch.username: The username for authentication.
    • elasticsearch.password: The password for authentication.
    • elasticsearch.ssl_skip_verify: Boolean flag to skip SSL verification (defaults to false).
    {
        "elasticsearch": {
            "url": "https://your-es-cluster:9243",
            "api_key": "your-api-key",
            "ssl_skip_verify": false
        }
    }