googleapis/mcp-toolbox · error

error parsing template '%s': %w

Error message

error parsing template '%s': %w

What it means

PopulateTemplateWithFunc parses a Go text/template and returns this error when template.Parse fails, wrapping the underlying parse error and including the template name. It means the template string itself has invalid syntax (bad actions, unmatched {{ }}, unknown pipeline syntax).

Source

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

		"json": convertParamToJSON,
	})
}

// PopulateTemplate populate a Go template with no custom formatters
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

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Fix the template syntax at the indicated position in the wrapped parse error
  2. Escape literal braces where needed and ensure every {{if}}/{{range}} has {{end}}
  3. Validate the template locally with text/template before adding it to the config
  4. Check that referenced custom functions are registered in the funcMap before parsing

Example fix

// before
stmt := "SELECT * FROM t WHERE id = {{id" // unclosed action
// after
stmt := "SELECT * FROM t WHERE id = {{.id}}"
Defensive patterns

Strategy: validation

Validate before calling

func validateTemplate(tmplStr string) error {
    _, err := template.New("check").Parse(tmplStr)
    return err
}
// run before configuring the tool

Type guard

null

Try / catch

out, err := PopulateTemplate(name, tmplStr, data)
if err != nil {
    var pe *template.ParseError
    if errors.As(err, &pe) {
        return nil, fmt.Errorf("template syntax error at %s: %w", pe.Pos(), pe)
    }
    return nil, err
}

Prevention

When it happens

Trigger: Calling PopulateTemplate/PopulateTemplateWithFunc/PopulateTemplateWithJSON with a template containing syntax errors, e.g. an unclosed `{{` or malformed `{{range}}` without `{{end}}`, typically from a user-supplied tool `template` config.

Common situations: Users writing SQL statement templates in YAML configs with typos; template braces colliding with JSON literals in the statement; copying templates that reference missing `{{end}}` blocks.

Related errors


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