googleapis/mcp-toolbox · error
error in User Agent retrieval: %s
Error message
error in User Agent retrieval: %s
What it means
This error is returned from the Cloud Monitoring source's Initialize when util.UserAgentFromContext(ctx) cannot extract a User-Agent from the context. The library requires callers to supply a User-Agent through the context, and initialization aborts without one.
Source
Thrown at internal/sources/cloudmonitoring/cloud_monitoring.go:65
}
return actual, nil
}
type Config struct {
Name string `yaml:"name" validate:"required"`
Type string `yaml:"type" validate:"required"`
UseClientOAuth bool `yaml:"useClientOAuth"`
}
func (r Config) SourceConfigType() string {
return SourceType
}
// Initialize initializes a Cloud Monitoring Source instance.
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)
}
var client *http.Client
if r.UseClientOAuth {
client = &http.Client{
Transport: util.NewUserAgentRoundTripper(ua, http.DefaultTransport),
}
} else {
// Use Application Default Credentials
creds, err := google.FindDefaultCredentials(ctx, monitoring.MonitoringScope)
if err != nil {
return nil, fmt.Errorf("failed to find default credentials: %w", err)
}
baseClient := oauth2.NewClient(ctx, creds.TokenSource)
baseClient.Transport = util.NewUserAgentRoundTripper(ua, baseClient.Transport)
client = baseClient
}
View on GitHub (pinned to 8cc6e09de2)
Solutions
- Pass a context containing the User-Agent (use the library's util helpers to inject it)
- Ensure the HTTP request handling layer forwards the User-Agent header into the context before Initialize
- If embedding, set the User-Agent explicitly via the same mechanism the toolbox server uses
Example fix
// before src, err := cfg.Initialize(ctx, tracer) // ctx has no UA // after ctx = util.ContextWithUserAgent(ctx, "my-app/1.0") src, err := cfg.Initialize(ctx, tracer)
Defensive patterns
Strategy: validation
Validate before calling
ua, err := util.UserAgentFromContext(ctx)
if err != nil {
ctx = util.ContextWithUserAgent(ctx, "my-app/1.0")
} Try / catch
if _, err := util.UserAgentFromContext(ctx); err != nil {
log.Printf("User-Agent missing from context: %v", err)
} Prevention
- Always derive request contexts through the toolbox's server handlers, which inject the UA
- Inject the UA into contexts in tests via the util helper
- Document the UA requirement when embedding sources in custom services
When it happens
Trigger: Calling Config.Initialize on a cloudmonitoring source without a User-Agent value present in the passed context (i.e. util.UserAgentFromContext returns an error).
Common situations: Embedding the toolbox source in a custom server without injecting the User-Agent into the request context; constructing sources directly in tests without the expected context key.
Related errors
- failed to find default credentials: %w
- error in User Agent retrieval: %s
- failed to create Cloud Logging Admin client for project %q:
- failed to create logadmin client for project %q: %w
- client-side OAuth is enabled but no access token was provide
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/a1fb5ab7a55d39e3.
Report an issue: GitHub.