googleapis/mcp-toolbox · error
unable to retrieve logger: %w
Error message
unable to retrieve logger: %w
What it means
The Gemini embedding model's Config.Initialize retrieves a structured logger from the passed context via util.LoggerFromContext and fails fast with 'unable to retrieve logger' if none is present. Like the auth-side equivalent, it guards against running with a nil logger.
Source
Thrown at internal/embeddingmodels/gemini/gemini.go:55
ApiKey string `yaml:"apiKey"`
Project string `yaml:"project"`
Location string `yaml:"location"`
Dimension int32 `yaml:"dimension"`
}
// Returns the embedding model type
func (cfg Config) EmbeddingModelConfigType() string {
return EmbeddingModelType
}
// Initialize a Gemini embedding model
func (cfg Config) Initialize(ctx context.Context) (embeddingmodels.EmbeddingModel, error) {
configs := &genai.ClientConfig{}
// Retrieve logger from context
l, err := util.LoggerFromContext(ctx)
if err != nil {
return nil, fmt.Errorf("unable to retrieve logger: %w", err)
}
// Get API Key
apiKey := cfg.ApiKey
if apiKey == "" {
apiKey = os.Getenv("GOOGLE_API_KEY")
}
if apiKey == "" {
apiKey = os.Getenv("GEMINI_API_KEY")
}
// Try to resolve Project and Location
project := cfg.Project
if project == "" {
project = os.Getenv("GOOGLE_CLOUD_PROJECT")
}
location := cfg.LocationView on GitHub (pinned to 8cc6e09de2)
Solutions
- Wrap the context before Initialize: ctx = util.NewLoggerContext(ctx, slog.Default())
- Use the toolbox's normal config-loading/server startup path, which injects the logger
- In tests, add a logger to the context fixture
- Check for middleware or helpers that strip context values
Example fix
// before model, err := cfg.Initialize(context.Background()) // after ctx := util.NewLoggerContext(context.Background(), slog.Default()) model, err := cfg.Initialize(ctx)
Defensive patterns
Strategy: validation
Validate before calling
if _, err := util.LoggerFromContext(ctx); err != nil {
ctx = util.NewLoggerContext(ctx, slog.Default())
}
model, err := cfg.Initialize(ctx) Type guard
func contextHasLogger(ctx context.Context) bool {
_, err := util.LoggerFromContext(ctx)
return err == nil
} Try / catch
model, err := cfg.Initialize(ctx)
if err != nil && strings.Contains(err.Error(), "unable to retrieve logger") {
ctx = util.NewLoggerContext(ctx, slog.Default())
model, err = cfg.Initialize(ctx)
} Prevention
- Always build contexts through the toolbox's logger-injecting helpers
- Fix test fixtures to supply a logger-bearing context
- Avoid context-rewriting helpers that drop context values
- Centralize context creation so logger injection cannot be skipped
When it happens
Trigger: Calling Initialize with a context lacking a logger: tests passing context.Background(), or application code not wrapping context with the toolbox logger utility before configuring embedding models.
Common situations: Custom embedding-model bootstrap code outside the standard server startup path; unit tests constructing contexts manually; frameworks that rebuild contexts and drop values.
Related errors
- missing credentials for Gemini embedding: For Google AI: Pro
- unable to get logger from ctx: %s
- unable to initialize logger: %w
- failed to get logger from context: %w
- invalid log level
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/e8ed6c2d95f2efe7.
Report an issue: GitHub.