Using Built-in Helpers
The template rendering commands include a set of built-in helper functions to assist you with the rendering process.
Functions
include
This function allows you to invoke your own custom helper functions.
e.g.
This function can also be used to render a file.
e.g.
The path to template file to render is relative to the parent template file.
os_writefile
Write a file to the output directory
The destination path is relative to the output directory. ( ".." or absolute paths are not allowed)
This function outputs the destination file path.
e.g.
os_copyfile
Copies files to the output directory. This function outputs the destination file path as a string.
The destination path is relative to the output directory ( ".." or absolute paths are not allowed)
The source path is relative to the main template file directory ( ".." or absolute paths are not allowed)
e.g.
os_getenvs
Gets all environment variables as a dictionary
e.g.
os_getenv
Gets the value of the specified env var
e.g.
slug_make
Converts string to a slug
e.g.
The example above outputs "my-api-proxy"url_parse
Parse a URL into its parts.
This function outputs a net.URL struct.
e.g.
blank
Outputs empty string. This is useful to consume the output of another function.
e.g.
deref
Dereferences the input pointer.
fmt_printf
Write to stdout during the rendering process. This function is useful for so called "printf" debugging.
For example, you can use it to trace the template rendering as it runs. Or, can also use it to dump values to stdout in order to see the contents.
e.g.
oas3_to_mcp
Extracts MCP metadata from an OpenAPI 3.x description
The result contains tools_list
and tools_targets
sections.
- The
tools_list
is an array that can be used for MCP tools/list response. - The
tools_targets
is a map with information useful for transcoding MCP tool/calls to REST.
e.g.
The file path is relative to the main template file directory.{
// A list of tool definitions.
tools_list: [
{
name: "...", // The unique operationId
title: "...", // The operation summary.
description: "...", // The operation description.
inputSchema: { ... }, // JSON Schema with all query, header, path and the request body
outputSchema: { ... }, // JSON Schema for the successful response body.
},
...
],
// A map of tool targets
tools_targets: {
[tool_name]: { // The unique operationId
verb: "...", // The target API HTTP method (e.g., "GET", "POST").
pathSuffix: "...", // The target API path template (e.g., "/users/{userId}").
contentType: "...", // The Content-Type header to sent to target
accept: "...", // The Accept header to send to target
pathParams: ["...", ... ], // List of target path parameter names.
queryParams ["...", ... ], // List of target query parameter names.
headerParams: ["...", ... ], // List of target header parameter names.
payloadParam: "...", // Name of the payload parameter used for the request body.
payloadSchema: { ... }, // JSON Schema for the target request body
responseSchema: { ... } // JSON Schema for the target response body
}
}
}
Libraries
Sprig
This library contains a lot of useful functions for string manipulation, accessing maps, lists, encoding, and more.
Functions from Sprig library are available during rendering.
e.g.