conversions

package
v0.73.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package conversions provides bidirectional type conversion between Go values and Starlark values, MCP result parsing, and tool name sanitization.

Index

Constants

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.

View Source
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.

View Source
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

View Source
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.

View Source
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.

View Source
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.

View Source
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

func GoToStarlark(v interface{}) (starlark.Value, error)

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

func SanitizeName(name string) string

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

func StarlarkToGo(v starlark.Value) (interface{}, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL