googleapis/mcp-toolbox · error

error in User Agent retrieval: %s

Error message

error in User Agent retrieval: %s

What it means

Thrown by the Dataproc source's Config.Initialize when util.UserAgentFromContext cannot extract a User-Agent string from the context. The library requires a User-Agent on every request for telemetry/identification, so initialization is aborted before any GCP client is created.

Source

Thrown at internal/sources/dataproc/dataproc.go:69

	}
	return actual, nil
}

type Config struct {
	Name    string `yaml:"name" validate:"required"`
	Type    string `yaml:"type" validate:"required"`
	Project string `yaml:"project" validate:"required"`
	Region  string `yaml:"region" validate:"required"`
}

func (r Config) SourceConfigType() string {
	return SourceType
}

func (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) {
	ua, err := util.UserAgentFromContext(ctx)
	if err != nil {
		return nil, fmt.Errorf("error in User Agent retrieval: %s", err)
	}
	endpoint := fmt.Sprintf("%s-dataproc.googleapis.com:443", r.Region)
	client, err := dataproc.NewClusterControllerClient(ctx, option.WithEndpoint(endpoint), option.WithUserAgent(ua))
	if err != nil {
		return nil, fmt.Errorf("failed to create dataproc client: %w", err)
	}
	opsClient, err := longrunning.NewOperationsClient(ctx, option.WithEndpoint(endpoint), option.WithUserAgent(ua))
	if err != nil {
		client.Close()
		return nil, fmt.Errorf("failed to create longrunning client: %w", err)
	}
	jobClient, err := dataproc.NewJobControllerClient(ctx, option.WithEndpoint(endpoint), option.WithUserAgent(ua))
	if err != nil {
		client.Close()
		opsClient.Close()
		return nil, fmt.Errorf("failed to create dataproc job client: %w", err)
	}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Invoke the source through the standard toolbox request path, which injects the User-Agent into the context.
  2. If calling Initialize directly (e.g., in tests), inject the User-Agent into the context using the same util helper the library uses.
  3. Check the version: if a recent refactor stopped populating the user agent in the context, update or report the calling framework code.

Example fix

// before
src, err := cfg.Initialize(ctx, tracer)

// after: ensure the user agent is present in the context
ctx = util.ContextWithUserAgent(ctx, "my-app/1.0")
src, err := cfg.Initialize(ctx, tracer)
Defensive patterns

Strategy: validation

Validate before calling

// ensure user agent is present before calling Initialize
ctx = util.ContextWithUserAgent(ctx, "my-client/1.0")
if ua, err := util.UserAgentFromContext(ctx); err != nil {
    return fmt.Errorf("context missing user agent: %w", err)
}

Type guard

null

Try / catch

src, err := cfg.Initialize(ctx, tracer)
if err != nil {
    if strings.Contains(err.Error(), "User Agent retrieval") {
        // fix context injection and retry
    }
    return err
}

Prevention

When it happens

Trigger: Initializing the dataproc source with a context that lacks the User-Agent value expected by util.UserAgentFromContext — i.e., the caller did not inject the user agent into the context before calling Initialize.

Common situations: Tests or custom harnesses that build a bare context.Context without the toolbox's user-agent injection; calling Initialize programmatically instead of through the standard server request path that attaches the User-Agent.

Related errors


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