Documentation
¶
Overview ¶
Package conversions provides bidirectional type conversion between Go values and Starlark values, MCP result parsing, and tool name sanitization.
Index ¶
Constants ¶
const MaxBytes = sandbox.MaxMessageBytes
MaxBytes bounds the JSON a converted value encodes to, estimated as two bytes per value plus the length of each string. The estimate never exceeds the real size, so the limit refuses only values too large for one sandbox message.
const MaxNestingDepth = 10000 - 16
MaxNestingDepth is the deepest a value may nest lists, dicts and tuples for StarlarkToGo to convert it. encoding/json refuses to decode JSON nested more than 10000 levels deep, and a converted value is always sent inside an envelope: a sandbox protocol message to the parent, then a JSON-RPC request to the backend, each three levels deep. The limit leaves room for both, so a value that converts can also be decoded wherever it is sent, in the sandbox and in this process alike.
const MaxNodes = 1 << 18
MaxNodes is the most values (lists, dicts, tuples, dict keys and scalars, counting nested ones) StarlarkToGo converts. Converting costs up to about 150 bytes per value (measured for dicts, the costliest shape; flat lists cost 16 to 28), so a value at the limit stays far below the sandbox's 128 MiB memory limit and fails the same way in the sandbox and in this process. The limit also bounds a value that shares a list many times without containing it, such as a = [a, a] repeated 40 times, which would otherwise expand to 2^40 values.
Variables ¶
var ErrCyclicValue = errors.New("value contains itself")
ErrCyclicValue is returned by StarlarkToGo for a value that contains itself, such as a list appended to itself.
var ErrTooManyElements = fmt.Errorf("value has more than %d elements, counting nested ones", MaxNodes)
ErrTooManyElements is returned by StarlarkToGo for a value made of more than MaxNodes values.
var ErrValueTooDeep = fmt.Errorf("value is nested more than %d levels deep", MaxNestingDepth)
ErrValueTooDeep is returned by StarlarkToGo for a value nested deeper than MaxNestingDepth.
var ErrValueTooLarge = fmt.Errorf("value is larger than %d MiB as JSON", MaxBytes>>20)
ErrValueTooLarge is returned by StarlarkToGo for a value whose JSON would be larger than MaxBytes.
Functions ¶
func GoToStarlark ¶
GoToStarlark converts a Go value to a Starlark value.
func ParseToolResult ¶
func ParseToolResult(result *mcp.CallToolResult) (interface{}, error)
ParseToolResult converts an mcp.CallToolResult into a Go value suitable for Starlark consumption.
It handles multiple response formats:
- Structured content with the mcp-go SDK {"result": v} wrapper pattern
- Structured content without the wrapper
- Text content parsed as JSON
- Plain text returned as-is
- Multi-item responses (first text item used)
- Error results returned as an error
func SanitizeName ¶
SanitizeName converts an MCP tool name into a valid Starlark identifier. Characters that are not alphanumeric or underscore are replaced with underscores. Leading digits get a prefix underscore.
func StarlarkToGo ¶
StarlarkToGo converts a Starlark value to a Go value. A script can build a list or dict that contains itself, nest values arbitrarily deep, or share one value so often that it expands exponentially, so the conversion fails with ErrCyclicValue, ErrValueTooDeep, ErrTooManyElements or ErrValueTooLarge instead of running without bound.
Types ¶
type StarlarkConverter ¶ added in v0.73.0
type StarlarkConverter struct {
// contains filtered or unexported fields
}
StarlarkConverter converts Starlark values to Go values as StarlarkToGo does, with one MaxNodes and MaxBytes budget for every value it converts. A caller converting the parts of one message, such as the arguments of a tool call, uses one converter for all of them. The zero value is ready to use.
func (*StarlarkConverter) Convert ¶ added in v0.73.0
func (c *StarlarkConverter) Convert(v starlark.Value) (interface{}, error)
Convert converts v, as StarlarkToGo does. It first checks v without allocating anything per value, so a value that is cyclic, too deep or too large fails before any of it is converted.