googleapis/mcp-toolbox · warning

failed to marshal context map: %w

Error message

failed to marshal context map: %w

What it means

After parsing YAML into map[string]any, UnmarshalYAML re-marshals it to JSON so protojson can populate the QueryDataContext proto. A failure here means the generic map cannot be serialized to JSON — extremely rare, since yaml.Unmarshal into map[string]any only yields JSON-serializable values (maps, slices, scalars).

Source

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

type compatibleSource interface {
	GetProjectID() string
	UseClientAuthorization() bool
	RunQuery(context.Context, string, *geminidataanalyticspb.QueryDataRequest) (*geminidataanalyticspb.QueryDataResponse, error)
}

// 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)
	}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Inspect the wrapped error and the raw map values for non-JSON-serializable types from custom yaml.Unmarshaler hooks.
  2. Update the MCP Toolbox version in case of a library bug.
  3. File an issue with the exact YAML input if reproducible.
Defensive patterns

Strategy: try-catch

Try / catch

if err := yaml.Unmarshal(b, &qd); err != nil {
    if strings.Contains(err.Error(), "failed to marshal context map") {
        // library-level failure; report with the raw YAML for an upstream issue
        return fmt.Errorf("context map -> JSON failed; report input: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: json.Marshal(raw) returning an error after yaml parsing succeeded; practically only via unusual custom YAML decoders or corrupted intermediate state.

Common situations: Almost never hit in real configs; would indicate a library-level or decoder-hook (e.g. custom yaml funcmap) issue rather than user error.

Related errors


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