googleapis/mcp-toolbox · error

elasticsearch connection failed: status %d

Error message

elasticsearch connection failed: status %d

What it means

Thrown in Config.Initialize after the client sends an ES _info request to test connectivity: the server responded with an HTTP error status (res.IsError()), so the source is marked failed with the numeric status code. It means the cluster was reachable at the network level but rejected the request.

Source

Thrown at internal/sources/elasticsearch/elasticsearch.go:132

	}

	client, err := elasticsearch.NewBaseClient(cfg)
	if err != nil {
		return nil, err
	}

	// Test connection
	res, err := esapi.InfoRequest{
		Instrument: client.InstrumentationEnabled(),
	}.Do(ctx, client)

	if err != nil {
		return nil, err
	}
	defer res.Body.Close()

	if res.IsError() {
		return nil, fmt.Errorf("elasticsearch connection failed: status %d", res.StatusCode)
	}

	s := &Source{
		Config: c,
		Client: client,
	}
	return s, nil
}

// SourceType returns the resourceType string for this source.
func (s *Source) IsReadOnly() bool {
	return false
}

func (s *Source) SourceType() string {
	return SourceType
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Read the status code: 401/403 means fix credentials or API key permissions
  2. Verify the address is the Elasticsearch HTTP port (9200), not Kibana or a proxy
  3. Confirm the cluster is healthy: `curl -u user:pass http://host:9200/_cluster/health`
  4. If 503, wait for the cluster to become ready and retry

Example fix

// before (tools.yaml)
    addresses: [http://localhost:5600]
// after
    addresses: [http://localhost:9200]
Defensive patterns

Strategy: retry

Validate before calling

// preflight: cluster must answer _cluster/health with 200
curl -u "$ES_USER:$ES_PASS" http://localhost:9200/_cluster/health

Try / catch

// Go
src, err := cfg.Initialize(ctx, tracer)
if err != nil && strings.Contains(err.Error(), "connection failed: status") {
    // inspect status code, fix credentials/address, then retry with backoff
    return retryWithBackoff(ctx, func() error { _, err := cfg.Initialize(ctx, tracer); return err })
}

Prevention

When it happens

Trigger: Elasticsearch returns 401 when credentials are wrong or the API key is invalid; 403 when the user lacks permissions; 404/400 when the address points at a wrong path, a proxy, or a non-Elasticsearch service; 503 when the cluster is starting up or unavailable.

Common situations: Wrong password after a rotation; expired or revoked API key; pointing at Kibana (5601) instead of Elasticsearch (9200); cluster still booting in Kubernetes before readiness; security plugin returning 401 for anonymous requests.

Understand the failure class

Background: "API error: {status}" and "HTTP 401/403/404/429/5xx" errors: non-2xx HTTP responses explained — this error's family across 27 libraries.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/4a904e9a4fd2f70d. Report an issue: GitHub.