{"record":{"id":"09875c00b0b88388","repo":"googleapis/mcp-toolbox","slug":"unable-to-get-logger-from-ctx-s-09875c","errorCode":null,"errorMessage":"unable to get logger from ctx: %s","messagePattern":"unable to get logger from ctx: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/http/http.go","lineNumber":103,"sourceCode":"}\n\n// Initialize initializes an HTTP Source instance.\nfunc (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) {\n\tduration, err := time.ParseDuration(r.Timeout)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to parse Timeout string as time.Duration: %s\", err)\n\t}\n\n\tvar tr *http.Transport\n\tif defaultTr, ok := http.DefaultTransport.(*http.Transport); ok {\n\t\ttr = defaultTr.Clone()\n\t} else {\n\t\ttr = &http.Transport{}\n\t}\n\n\tlogger, err := util.LoggerFromContext(ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"unable to get logger from ctx: %s\", err)\n\t}\n\n\tif r.DisableSslVerification {\n\t\ttr.TLSClientConfig = &tls.Config{\n\t\t\tInsecureSkipVerify: true,\n\t\t}\n\n\t\tlogger.WarnContext(ctx, \"WARNING: TLS certificate verification is skipped (InsecureSkipVerify: true) for HTTP source %s. This exposes all traffic for this source to Man-in-the-Middle (MITM) attacks. Do not use in production.\", r.Name)\n\t}\n\n\t// Validate BaseURL\n\tparsedURL, err := url.ParseRequestURI(r.BaseURL)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to parse BaseUrl %v\", err)\n\t}\n\n\tallowedRanges, err := parseCIDRs(r.AllowedIPRanges)\n\tif err != nil {","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/http/http.go#L85-L121","documentation":"This error is returned when util.LoggerFromContext(ctx) cannot find a logger in the context passed to the HTTP source's Initialize. The toolbox pipeline normally injects a slog logger into the context; receiving this error means Initialize was invoked with a bare context that skipped that instrumentation step.","triggerScenarios":"Config.Initialize(ctx, tracer) is called with a ctx that never had a logger attached via the util logging middleware/context helper — e.g. direct programmatic construction of the source outside the normal server startup path, or a custom caller building sources without the toolbox's context decoration.","commonSituations":"Embedding the toolbox as a library and calling source Initialize directly with context.Background(); tests constructing sources without the logger-injecting middleware; refactors that dropped the logger setup before initialization.","solutions":["Initialize sources through the standard toolbox server startup path, which injects the logger into the context","If calling Initialize directly, attach a logger first using the same util helper the server uses (e.g. util.WithLogger(ctx, logger) or equivalent)","In tests, build the context with the util logger helper before invoking Initialize"],"exampleFix":"// before\nsrc, err := cfg.Initialize(context.Background(), tracer)\n// after\nctx := util.NewContextWithLogger(context.Background(), slog.Default())\nsrc, err := cfg.Initialize(ctx, tracer)","handlingStrategy":"fallback","validationCode":"// ensure a logger-bearing context before Initialize\nif ctx.Value(loggerKey) == nil {\n    ctx = util.NewContextWithLogger(ctx, slog.Default())\n}","typeGuard":null,"tryCatchPattern":"src, err := cfg.Initialize(ctx, tracer)\nif err != nil && strings.Contains(err.Error(), \"unable to get logger from ctx\") {\n    log.Fatalf(\"initialize sources only via the toolbox server path or inject a logger first: %v\", err)\n}","preventionTips":["Construct sources through the standard server startup path, not ad-hoc","In tests and library embeddings, inject the util logger into ctx before Initialize","After refactors, verify the logger middleware still wraps source initialization"],"tags":["http","logging","context","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"}