{"record":{"id":"a1fb5ab7a55d39e3","repo":"googleapis/mcp-toolbox","slug":"error-in-user-agent-retrieval-s-a1fb5a","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/cloudmonitoring/cloud_monitoring.go","lineNumber":65,"sourceCode":"\t}\n\treturn actual, nil\n}\n\ntype Config struct {\n\tName           string `yaml:\"name\" validate:\"required\"`\n\tType           string `yaml:\"type\" validate:\"required\"`\n\tUseClientOAuth bool   `yaml:\"useClientOAuth\"`\n}\n\nfunc (r Config) SourceConfigType() string {\n\treturn SourceType\n}\n\n// Initialize initializes a Cloud Monitoring 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, monitoring.MonitoringScope)\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":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudmonitoring/cloud_monitoring.go#L47-L83","documentation":"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.","triggerScenarios":"Calling Config.Initialize on a cloudmonitoring source without a User-Agent value present in the passed context (i.e. util.UserAgentFromContext returns an error).","commonSituations":"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.","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"],"exampleFix":"// before\nsrc, err := cfg.Initialize(ctx, tracer) // ctx has no 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":["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"],"tags":["gcp","cloud-monitoring","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"}