{"record":{"id":"ef56a1063fa0e0a5","repo":"googleapis/mcp-toolbox","slug":"error-parsing-template-s-w","errorCode":null,"errorMessage":"error parsing template '%s': %w","messagePattern":"error parsing template '(.+?)': %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/util/parameters/common.go","lineNumber":102,"sourceCode":"\t\t\"json\": convertParamToJSON,\n\t})\n}\n\n// PopulateTemplate populate a Go template with no custom formatters\nfunc PopulateTemplate(templateName, templateString string, data map[string]any) (string, error) {\n\treturn PopulateTemplateWithFunc(templateName, templateString, data, nil)\n}\n\n// PopulateTemplateWithFunc populate a Go template with provided functions\nfunc PopulateTemplateWithFunc(templateName, templateString string, data map[string]any, funcMap template.FuncMap) (string, error) {\n\ttmpl := template.New(templateName)\n\tif funcMap != nil {\n\t\ttmpl = tmpl.Funcs(funcMap)\n\t}\n\n\tparsedTmpl, err := tmpl.Parse(templateString)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"error parsing template '%s': %w\", templateName, err)\n\t}\n\n\tvar result bytes.Buffer\n\tif err := parsedTmpl.Execute(&result, data); err != nil {\n\t\treturn \"\", fmt.Errorf(\"error executing template '%s': %w\", templateName, err)\n\t}\n\treturn result.String(), nil\n}\n\n// CheckDuplicateParameters verify there are no duplicate parameter names\nfunc CheckDuplicateParameters(ps Parameters) error {\n\tseenNames := make(map[string]bool)\n\tfor _, p := range ps {\n\t\tpName := p.GetName()\n\t\tif _, exists := seenNames[pName]; exists {\n\t\t\treturn fmt.Errorf(\"parameter name must be unique across all parameter fields. Duplicate parameter: %s\", pName)\n\t\t}\n\t\tseenNames[pName] = true","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/util/parameters/common.go#L84-L120","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Fix the template syntax at the indicated position in the wrapped parse error","Escape literal braces where needed and ensure every {{if}}/{{range}} has {{end}}","Validate the template locally with text/template before adding it to the config","Check that referenced custom functions are registered in the funcMap before parsing"],"exampleFix":"// before\nstmt := \"SELECT * FROM t WHERE id = {{id\" // unclosed action\n// after\nstmt := \"SELECT * FROM t WHERE id = {{.id}}\"","handlingStrategy":"validation","validationCode":"func validateTemplate(tmplStr string) error {\n    _, err := template.New(\"check\").Parse(tmplStr)\n    return err\n}\n// run before configuring the tool","typeGuard":"null","tryCatchPattern":"out, err := PopulateTemplate(name, tmplStr, data)\nif err != nil {\n    var pe *template.ParseError\n    if errors.As(err, &pe) {\n        return nil, fmt.Errorf(\"template syntax error at %s: %w\", pe.Pos(), pe)\n    }\n    return nil, err\n}","preventionTips":["Lint templates with text/template.Parse before committing configs","Balance every {{if}}/{{range}} with {{end}}","Test templates with sample data in CI"],"tags":["templates","syntax","configuration"],"backgroundTag":"template-parse-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}