googleapis/mcp-toolbox · error

failed to unmarshal context to proto: %w

Error message

failed to unmarshal context to proto: %w

What it means

The final step of QueryDataContext.UnmarshalYAML feeds the JSON bytes to protojson.Unmarshal to build geminidataanalyticspb.QueryDataContext. protojson rejects fields that don't match the proto schema (unknown fields, wrong types, badly formatted values), so schema-mismatched context YAML produces this wrapped error.

Source

Thrown at internal/tools/cloudgda/cloudgda.go:85

}

// QueryDataContext wraps geminidataanalyticspb.QueryDataContext to support YAML decoding via protojson.
type QueryDataContext struct {
	*geminidataanalyticspb.QueryDataContext
}

func (q *QueryDataContext) UnmarshalYAML(b []byte) error {
	var raw map[string]any
	if err := yaml.Unmarshal(b, &raw); err != nil {
		return fmt.Errorf("failed to unmarshal context from yaml: %w", err)
	}
	jsonBytes, err := json.Marshal(raw)
	if err != nil {
		return fmt.Errorf("failed to marshal context map: %w", err)
	}
	q.QueryDataContext = &geminidataanalyticspb.QueryDataContext{}
	if err := protojson.Unmarshal(jsonBytes, q.QueryDataContext); err != nil {
		return fmt.Errorf("failed to unmarshal context to proto: %w", err)
	}
	return nil
}

// GenerationOptions wraps geminidataanalyticspb.GenerationOptions to support YAML decoding via protojson.
type GenerationOptions struct {
	*geminidataanalyticspb.GenerationOptions
}

func (g *GenerationOptions) UnmarshalYAML(b []byte) error {
	var raw map[string]any
	if err := yaml.Unmarshal(b, &raw); err != nil {
		return fmt.Errorf("failed to unmarshal generation options from yaml: %w", err)
	}
	jsonBytes, err := json.Marshal(raw)
	if err != nil {
		return fmt.Errorf("failed to marshal generation options map: %w", err)
	}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Check the wrapped protojson error for the offending field and correct the key name/type in the YAML.
  2. Compare your context block with the QueryDataContext proto fields (tableReferences, etc.) and the official cloud-gda docs.
  3. Ensure tableReferences entries are objects like `- tableName: my_table`.
  4. Remove unknown fields if protojson strict mode rejects them.

Example fix

// before
context:
  tableRefs:
    - name: orders
// after
context:
  tableReferences:
    - tableName: orders
Defensive patterns

Strategy: validation

Validate before calling

// Validate context keys against the proto schema before unmarshaling
func checkContextKeys(m map[string]any) error {
    allowed := map[string]bool{"tableReferences": true /*, other QueryDataContext fields */}
    for k := range m {
        if !allowed[k] {
            return fmt.Errorf("unknown context field %q", k)
        }
    }
    return nil
}

Try / catch

if err := yaml.Unmarshal(b, &qd); err != nil {
    if strings.Contains(err.Error(), "failed to unmarshal context to proto") {
        return fmt.Errorf("context fields must match QueryDataContext proto (e.g. tableReferences[].tableName): %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: A context block whose keys don't match QueryDataContext proto fields (e.g. misspelled `tableReferences`, unexpected nested field), wrong value types (string where a message/list is expected), or invalid nested structures.

Common situations: Following outdated documentation for the context schema; guessing field names instead of checking the proto definition; providing a scalar where `tableReferences` expects a repeated message with `tableName`.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/f599400c4a1c926c. Report an issue: GitHub.