{"record":{"id":"e00414d46bf57d2d","repo":"googleapis/mcp-toolbox","slug":"error-getting-user-agent-from-context-w","errorCode":null,"errorMessage":"error getting user agent from context: %w","messagePattern":"error getting user agent from context: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/elasticsearch/elasticsearch.go","lineNumber":94,"sourceCode":"\n// tracerProviderAdapter adapts a Tracer to implement the TracerProvider interface\ntype tracerProviderAdapter struct {\n\ttrace.TracerProvider\n\ttracer trace.Tracer\n}\n\n// Tracer implements the TracerProvider interface\nfunc (t *tracerProviderAdapter) Tracer(name string, options ...trace.TracerOption) trace.Tracer {\n\treturn t.tracer\n}\n\n// Initialize creates a new Elasticsearch Source instance.\nfunc (c Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) {\n\ttracerProvider := &tracerProviderAdapter{tracer: tracer}\n\n\tua, err := util.UserAgentFromContext(ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error getting user agent from context: %w\", err)\n\t}\n\n\t// Create a new Elasticsearch client with the provided configuration\n\tcfg := elasticsearch.Config{\n\t\tAddresses:       c.Addresses,\n\t\tInstrumentation: elasticsearch.NewOpenTelemetryInstrumentation(tracerProvider, false),\n\t\tHeader:          http.Header{\"User-Agent\": []string{ua + \" go-elasticsearch/\" + elasticsearch.Version}},\n\t}\n\n\t// Client need either username and password or an API key\n\tif c.Username != \"\" && c.Password != \"\" {\n\t\tcfg.Username = c.Username\n\t\tcfg.Password = c.Password\n\t} else if c.APIKey != \"\" {\n\t\t// API key will be set below\n\t\tcfg.APIKey = c.APIKey\n\t} else {\n\t\t// If neither username/password nor API key is provided, we throw an error","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/elasticsearch/elasticsearch.go#L76-L112","documentation":"Thrown by the Elasticsearch source's Config.Initialize when util.UserAgentFromContext(ctx) fails, i.e. the incoming context does not carry the user agent value the toolbox requires for instrumentation. Initialize wraps the error with %w and aborts source creation, so the Elasticsearch client is never constructed.","triggerScenarios":"Config.Initialize is called with a context that lacks the user agent value (util.UserAgentFromContext returns an error) — usually when a source is initialized programmatically with a bare context.Background() instead of the server-provided request context.","commonSituations":"Custom code or tests constructing the Elasticsearch source directly with context.Background()/context.TODO(), bypassing the toolbox server which normally injects the user agent into the request context.","solutions":["Let the toolbox server initialize the source from its normal request lifecycle so the user agent is present in the context.","In tests or custom code, add the user agent to the context the way the server does (use the util package's context helpers) before calling Initialize.","Upgrade the toolbox if you believe a valid user-agent context is being dropped — check util.UserAgentFromContext expectations."],"exampleFix":"// before\nsrc, err := cfg.Initialize(context.Background(), tracer)\n// after\nctx := util.ContextWithUserAgent(context.Background(), \"mcp-toolbox/1.0\")\nsrc, err := cfg.Initialize(ctx, tracer)","handlingStrategy":"validation","validationCode":"if ua, ok := ctx.Value(util.UserAgentKey{}).(string); !ok || ua == \"\" {\n    return errors.New(\"user agent missing from context; use the server-provided request context\")\n}","typeGuard":"func hasUserAgent(ctx context.Context) bool {\n    ua, err := util.UserAgentFromContext(ctx)\n    return err == nil && ua != \"\"\n}","tryCatchPattern":"src, err := cfg.Initialize(ctx, tracer)\nif err != nil {\n    if strings.Contains(err.Error(), \"error getting user agent from context\") {\n        return nil, fmt.Errorf(\"initialize the elasticsearch source through the toolbox server (context must carry a user agent): %w\", err)\n    }\n    return nil, err\n}","preventionTips":["Initialize sources via the toolbox server's normal lifecycle rather than calling Config.Initialize directly with context.Background().","In unit tests, inject the user agent into the context using the util package helpers.","Never construct sources with context.TODO() in production code paths."],"tags":["elasticsearch","context","user-agent","initialization"],"backgroundTag":"missing-context-value","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}