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 SQL Admin 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/cloudsqladmin/cloud_sql_admin.go:78
}
type Config struct {
Name string `yaml:"name" validate:"required"`
Type string `yaml:"type" validate:"required"`
DefaultProject string `yaml:"defaultProject"`
UseClientOAuth bool `yaml:"useClientOAuth"`
ReadOnly bool `yaml:"readOnly"`
}
func (r Config) SourceConfigType() string {
return SourceType
}
// Initialize initializes a CloudSQL Admin 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, sqladmin.SqlserviceAdminScope)
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 handling layer forwards the User-Agent header into the context before Initialize
- If embedding, set the User-Agent explicitly the same way the toolbox server does
Example fix
// before src, err := cfg.Initialize(ctx, tracer) // ctx missing 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
- Route requests through the toolbox server, which injects the UA
- Inject the UA into contexts in tests via the util helper
- Document the UA requirement when embedding sources directly
When it happens
Trigger: Calling Config.Initialize on a cloudsqladmin source without a User-Agent value present in the passed context (util.UserAgentFromContext returns an error).
Common situations: Embedding the toolbox source in a custom server without injecting the User-Agent into the context; unit tests constructing the source directly without the expected context key.
Related errors
- error in User Agent retrieval: %s
- failed to find default credentials: %w
- failed to create Cloud Logging Admin client for project %q:
- failed to create logadmin client for project %q: %w
- failed to find default credentials: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/f89f4d5e933df5df.
Report an issue: GitHub.