{"record":{"id":"33e1eb658a21044c","repo":"googleapis/mcp-toolbox","slug":"failed-to-get-logger-from-context-w","errorCode":null,"errorMessage":"failed to get logger from context: %w","messagePattern":"failed to get logger from context: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/auth/generic/generic.go","lineNumber":346,"sourceCode":"\taud, err := claims.GetAudience()\n\tif err != nil {\n\t\treturn nil, &MCPAuthError{Code: http.StatusUnauthorized, Message: \"could not parse audience from token\", ScopesRequired: a.ScopesRequired}\n\t}\n\n\tscopeClaim, _ := claims[\"scope\"].(string)\n\n\terr = a.validateClaims(ctx, iss, aud, scopeClaim)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn claims, nil\n}\n\n// validateOpaqueToken validates an opaque token by calling the introspection endpoint\nfunc (a AuthService) validateOpaqueToken(ctx context.Context, tokenStr string) (map[string]any, error) {\n\tlogger, err := util.LoggerFromContext(ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get logger from context: %w\", err)\n\t}\n\n\tintrospectionURL := a.introspectionURL\n\tif introspectionURL == \"\" {\n\t\tintrospectionURL, err = url.JoinPath(a.AuthorizationServer, \"introspect\")\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to construct introspection URL: %w\", err)\n\t\t}\n\t}\n\n\tparamName := a.IntrospectionParamName\n\tif paramName == \"\" {\n\t\tparamName = \"token\"\n\t}\n\n\tvar req *http.Request\n\tif a.IntrospectionMethod == \"GET\" {\n\t\tu, err := url.Parse(introspectionURL)","sourceCodeStart":328,"sourceCodeEnd":364,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/auth/generic/generic.go#L328-L364","documentation":"validateOpaqueToken begins by extracting the structured logger from the context via util.LoggerFromContext; if no logger is present in ctx, the introspection flow cannot proceed and this wrapped error is returned. It indicates a programming/setup issue rather than a token problem — the caller passed a bare context.Context.","triggerScenarios":"ValidateMCPAuth (or another caller) invokes validateOpaqueToken with a ctx that never had a logger attached, so util.LoggerFromContext returns an error which is wrapped here.","commonSituations":"Custom embedding of the toolbox server that constructs requests without the project's logger middleware, unit tests passing context.Background(), or a request path that bypasses the logging middleware that injects the logger.","solutions":["Ensure the server's logging middleware runs before auth so the logger is stored in the request context.","In custom code, attach the logger: ctx := util.WithLogger(ctx, logger) before calling ValidateMCPAuth.","If in tests, use the project's context helpers to inject a test logger instead of context.Background()."],"exampleFix":"// before\nsvc.ValidateMCPAuth(ctx, header) // ctx = context.Background()\n// after\nctx = util.WithLogger(ctx, slog.Default())\nsvc.ValidateMCPAuth(ctx, header)","handlingStrategy":"try-catch","validationCode":"if _, err := util.LoggerFromContext(ctx); err != nil {\n    ctx = util.WithLogger(ctx, slog.Default())\n}\nreturn svc.ValidateMCPAuth(ctx, header)","typeGuard":"func ctxHasLogger(ctx context.Context) bool {\n    _, err := util.LoggerFromContext(ctx)\n    return err == nil\n}","tryCatchPattern":"claims, err := svc.ValidateMCPAuth(ctx, header)\nif err != nil {\n    if strings.Contains(err.Error(), \"failed to get logger from context\") {\n        // programmer error: attach logger to ctx before calling auth\n        ctx = util.WithLogger(ctx, slog.Default())\n        claims, err = svc.ValidateMCPAuth(ctx, header)\n    }\n    return claims, err\n}","preventionTips":["Always run the server's logging middleware before auth handlers","In tests, never pass bare context.Background() to auth APIs","Wrap context injection in a helper so callers can't forget it"],"tags":["context","logging","auth"],"backgroundTag":"missing-logger-in-context","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"}