{"record":{"id":"feaa2ee323707683","repo":"googleapis/mcp-toolbox","slug":"failed-to-create-per-request-datachatclient-w","errorCode":null,"errorMessage":"failed to create per-request DataChatClient: %w","messagePattern":"failed to create per-request DataChatClient: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/sources/cloudgda/cloud_gda.go","lineNumber":141,"sourceCode":"\nfunc (s *Source) UseClientAuthorization() bool {\n\treturn s.UseClientOAuth\n}\n\nfunc (s *Source) GetClient(ctx context.Context, tokenStr string) (*geminidataanalytics.DataChatClient, func(), error) {\n\tif s.UseClientOAuth {\n\t\tif tokenStr == \"\" {\n\t\t\treturn nil, nil, fmt.Errorf(\"client-side OAuth is enabled but no access token was provided\")\n\t\t}\n\t\ttoken := &oauth2.Token{AccessToken: tokenStr}\n\t\topts := []option.ClientOption{\n\t\t\toption.WithUserAgent(s.userAgent),\n\t\t\toption.WithTokenSource(oauth2.StaticTokenSource(token)),\n\t\t}\n\n\t\tclient, err := NewDataChatClient(ctx, opts...)\n\t\tif err != nil {\n\t\t\treturn nil, nil, fmt.Errorf(\"failed to create per-request DataChatClient: %w\", err)\n\t\t}\n\t\treturn client, func() { client.Close() }, nil\n\t}\n\treturn s.Client, func() {}, nil\n}\n\nfunc (s *Source) RunQuery(ctx context.Context, tokenStr string, req *geminidataanalyticspb.QueryDataRequest) (*geminidataanalyticspb.QueryDataResponse, error) {\n\tclient, cleanup, err := s.GetClient(ctx, tokenStr)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tdefer cleanup()\n\n\treturn client.QueryData(ctx, req)\n}\n","sourceCodeStart":123,"sourceCodeEnd":157,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/sources/cloudgda/cloud_gda.go#L123-L157","documentation":"When client-side OAuth is active and a valid token was supplied, GetClient constructs a fresh DataChatClient per request using a static token source; if NewDataChatClient fails, this wrapped error is returned instead of the client. This differs from 534 in that it happens per call, after a token was accepted.","triggerScenarios":"RunQuery -> GetClient with UseClientOAuth true and a non-empty token, where NewDataChatClient errors — most often because the token is syntactically present but the client construction still fails (API endpoint issues, invalid client options, or network failure reaching the Gemini Data Analytics endpoint).","commonSituations":"Transient Google API outages, region-restricted networks blocking googleapis.com, or a malformed/expired token passed to option.WithTokenSource that the client construction path rejects.","solutions":["Retry the request; per-request client creation is sensitive to transient network/API issues","Verify the access token is a valid, unexpired Google OAuth token with the Cloud Platform scope","Ensure the environment can reach the Gemini Data Analytics API endpoint (check proxies/firewalls)","Check the wrapped error for the concrete client-creation failure"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// pre-validate the token shape before calling\ntok := strings.TrimSpace(token)\nif useClientOAuth && (tok == \"\" || !strings.HasPrefix(tok, \"ya29.\") && !strings.Contains(tok, \".\")) {\n    return errors.New(\"supplied access token does not look like a Google OAuth token\")\n}","typeGuard":null,"tryCatchPattern":"// Go: transient-friendly retry around RunQuery\nfor attempt := 0; attempt < 2; attempt++ {\n    res, err := src.RunQuery(ctx, prompt, token)\n    if err == nil { return res, nil }\n    if strings.Contains(err.Error(), \"per-request DataChatClient\") && attempt == 0 {\n        time.Sleep(time.Second)\n        continue\n    }\n    return err\n}","preventionTips":["Refresh OAuth tokens before expiry","Verify outbound network access to googleapis.com endpoints","Log the wrapped cause to distinguish network vs option errors","Fall back to server ADC mode if per-request creation keeps failing"],"tags":["cloudgda","google-cloud","oauth","client-initialization"],"backgroundTag":"api-client-creation-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}