googleapis/mcp-toolbox · error

error executing template '%s': %w

Error message

error executing template '%s': %w

What it means

After successfully parsing, PopulateTemplateWithFunc executes the template against the provided data map. This error wraps template.Execute failures: nil pointer dereferences on missing data, calling methods on wrong types, index out of range on pipelines, or custom funcs returning errors (like convertParamToJSON failures).

Source

Thrown at internal/util/parameters/common.go:107

func PopulateTemplate(templateName, templateString string, data map[string]any) (string, error) {
	return PopulateTemplateWithFunc(templateName, templateString, data, nil)
}

// PopulateTemplateWithFunc populate a Go template with provided functions
func PopulateTemplateWithFunc(templateName, templateString string, data map[string]any, funcMap template.FuncMap) (string, error) {
	tmpl := template.New(templateName)
	if funcMap != nil {
		tmpl = tmpl.Funcs(funcMap)
	}

	parsedTmpl, err := tmpl.Parse(templateString)
	if err != nil {
		return "", fmt.Errorf("error parsing template '%s': %w", templateName, err)
	}

	var result bytes.Buffer
	if err := parsedTmpl.Execute(&result, data); err != nil {
		return "", fmt.Errorf("error executing template '%s': %w", templateName, err)
	}
	return result.String(), nil
}

// CheckDuplicateParameters verify there are no duplicate parameter names
func CheckDuplicateParameters(ps Parameters) error {
	seenNames := make(map[string]bool)
	for _, p := range ps {
		pName := p.GetName()
		if _, exists := seenNames[pName]; exists {
			return fmt.Errorf("parameter name must be unique across all parameter fields. Duplicate parameter: %s", pName)
		}
		seenNames[pName] = true
	}
	return nil
}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Inspect the wrapped Execute error to find the failing action and data field
  2. Ensure all params referenced in the template are present and non-nil in the data map
  3. Fix custom function usage (e.g. json helper) so it receives marshalable values
  4. Test the template with representative data to reproduce the failure locally

Example fix

// before
tmpl := "{{.missing.field}}" // data has no "missing"
// after
tmpl := "{{if .missing}}{{.missing.field}}{{end}}"
Defensive patterns

Strategy: try-catch

Validate before calling

func hasRequiredFields(tmplStr string, data map[string]any) error {
    missing := []string{}
    for _, f := range extractFieldRefs(tmplStr) {
        if _, ok := data[f]; !ok {
            missing = append(missing, f)
        }
    }
    if len(missing) > 0 {
        return fmt.Errorf("missing template data: %v", missing)
    }
    return nil
}

Type guard

null

Try / catch

out, err := PopulateTemplate(name, tmplStr, data)
if err != nil {
    if strings.Contains(err.Error(), "executing template") {
        return nil, fmt.Errorf("template failed against supplied data: %w", err)
    }
    return nil, err
}

Prevention

When it happens

Trigger: Executing a syntactically valid template whose actions reference fields/functions incompatible with the supplied data — e.g. `{{param 'x'}}` where the json helper fails, or indexing a nil map entry.

Common situations: Runtime data shape differing from what the template expects; tool invoked without required params so data map keys are nil; custom template funcs returning errors for malformed params.

Related errors


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