{"record":{"id":"f89f4d5e933df5df","repo":"googleapis/mcp-toolbox","slug":"error-in-user-agent-retrieval-s-f89f4d","errorCode":null,"errorMessage":"error in User Agent retrieval: %s","messagePattern":"error in User Agent retrieval: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudsqladmin/cloud_sql_admin.go","lineNumber":78,"sourceCode":"}\n\ntype Config struct {\n\tName           string `yaml:\"name\" validate:\"required\"`\n\tType           string `yaml:\"type\" validate:\"required\"`\n\tDefaultProject string `yaml:\"defaultProject\"`\n\tUseClientOAuth bool   `yaml:\"useClientOAuth\"`\n\tReadOnly       bool   `yaml:\"readOnly\"`\n}\n\nfunc (r Config) SourceConfigType() string {\n\treturn SourceType\n}\n\n// Initialize initializes a CloudSQL Admin Source instance.\nfunc (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) {\n\tua, err := util.UserAgentFromContext(ctx)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error in User Agent retrieval: %s\", err)\n\t}\n\n\tvar client *http.Client\n\tif r.UseClientOAuth {\n\t\tclient = &http.Client{\n\t\t\tTransport: util.NewUserAgentRoundTripper(ua, http.DefaultTransport),\n\t\t}\n\t} else {\n\t\t// Use Application Default Credentials\n\t\tcreds, err := google.FindDefaultCredentials(ctx, sqladmin.SqlserviceAdminScope)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to find default credentials: %w\", err)\n\t\t}\n\t\tbaseClient := oauth2.NewClient(ctx, creds.TokenSource)\n\t\tbaseClient.Transport = util.NewUserAgentRoundTripper(ua, baseClient.Transport)\n\t\tclient = baseClient\n\t}\n","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudsqladmin/cloud_sql_admin.go#L60-L96","documentation":"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.","triggerScenarios":"Calling Config.Initialize on a cloudsqladmin source without a User-Agent value present in the passed context (util.UserAgentFromContext returns an error).","commonSituations":"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.","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"],"exampleFix":"// before\nsrc, err := cfg.Initialize(ctx, tracer) // ctx missing UA\n// after\nctx = util.ContextWithUserAgent(ctx, \"my-app/1.0\")\nsrc, err := cfg.Initialize(ctx, tracer)","handlingStrategy":"validation","validationCode":"ua, err := util.UserAgentFromContext(ctx)\nif err != nil {\n    ctx = util.ContextWithUserAgent(ctx, \"my-app/1.0\")\n}","typeGuard":null,"tryCatchPattern":"if _, err := util.UserAgentFromContext(ctx); err != nil {\n    log.Printf(\"User-Agent missing from context: %v\", err)\n}","preventionTips":["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"],"tags":["gcp","cloud-sql","user-agent","go"],"backgroundTag":"missing-user-agent-context","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}