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.Location

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Wrap the context before Initialize: ctx = util.NewLoggerContext(ctx, slog.Default())
  2. Use the toolbox's normal config-loading/server startup path, which injects the logger
  3. In tests, add a logger to the context fixture
  4. 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

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


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