googleapis/mcp-toolbox · error
failed to create per-request DataChatClient: %w
Error message
failed to create per-request DataChatClient: %w
What it means
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.
Source
Thrown at internal/sources/cloudgda/cloud_gda.go:141
func (s *Source) UseClientAuthorization() bool {
return s.UseClientOAuth
}
func (s *Source) GetClient(ctx context.Context, tokenStr string) (*geminidataanalytics.DataChatClient, func(), error) {
if s.UseClientOAuth {
if tokenStr == "" {
return nil, nil, fmt.Errorf("client-side OAuth is enabled but no access token was provided")
}
token := &oauth2.Token{AccessToken: tokenStr}
opts := []option.ClientOption{
option.WithUserAgent(s.userAgent),
option.WithTokenSource(oauth2.StaticTokenSource(token)),
}
client, err := NewDataChatClient(ctx, opts...)
if err != nil {
return nil, nil, fmt.Errorf("failed to create per-request DataChatClient: %w", err)
}
return client, func() { client.Close() }, nil
}
return s.Client, func() {}, nil
}
func (s *Source) RunQuery(ctx context.Context, tokenStr string, req *geminidataanalyticspb.QueryDataRequest) (*geminidataanalyticspb.QueryDataResponse, error) {
client, cleanup, err := s.GetClient(ctx, tokenStr)
if err != nil {
return nil, err
}
defer cleanup()
return client.QueryData(ctx, req)
}
View on GitHub (pinned to 8cc6e09de2)
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
Defensive patterns
Strategy: retry
Validate before calling
// pre-validate the token shape before calling
tok := strings.TrimSpace(token)
if useClientOAuth && (tok == "" || !strings.HasPrefix(tok, "ya29.") && !strings.Contains(tok, ".")) {
return errors.New("supplied access token does not look like a Google OAuth token")
} Try / catch
// Go: transient-friendly retry around RunQuery
for attempt := 0; attempt < 2; attempt++ {
res, err := src.RunQuery(ctx, prompt, token)
if err == nil { return res, nil }
if strings.Contains(err.Error(), "per-request DataChatClient") && attempt == 0 {
time.Sleep(time.Second)
continue
}
return err
} Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- failed to create DataChatClient: %w
- error creating client from OAuth access token: %w
- failed to find default credentials: %w
- client-side OAuth is enabled but no access token was provide
- error constructing service creator: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/feaa2ee323707683.
Report an issue: GitHub.