Documentation
¶
Overview ¶
Package script provides a Starlark-based script execution engine for orchestrating MCP tool calls. It allows agents to send scripts that call multiple tools and return aggregated results in a single round-trip.
Index ¶
Constants ¶
const ( // DefaultSandboxMemoryLimit is the memory limit for one sandboxed script. DefaultSandboxMemoryLimit uint64 = 128 << 20 // DefaultSandboxMaxConcurrent is how many sandboxed scripts may run at once. DefaultSandboxMaxConcurrent = 2 )
const DefaultStepLimit uint64 = 100_000
DefaultStepLimit is the default maximum number of Starlark execution steps.
const ExecuteToolScriptName = "execute_tool_script"
ExecuteToolScriptName is the name of the virtual tool exposed by the script middleware.
const MemoryLimitEnforced = sandbox.MemoryLimitEnforced
MemoryLimitEnforced reports whether the OS enforces a Sandbox's memory limit on this platform. It is true on Linux only, and false in race-detector builds there; elsewhere a sandboxed script still runs in its own process, so a crash does not take the server down, but its memory is not limited.
Variables ¶
var ErrMemoryLimitExceeded = sandbox.ErrMemoryLimitExceeded
ErrMemoryLimitExceeded is matched (with errors.Is) by the error Execute returns when the OS stopped a sandboxed script for using more memory than its Sandbox allows.
var ErrSandboxCrashed = sandbox.ErrCrashed
ErrSandboxCrashed is matched (with errors.Is) by the error Execute returns when a sandboxed script's process exited without a result for a reason other than ErrMemoryLimitExceeded. The Go runtime does not always report a refused memory request as running out of memory, so a script over its memory limit can end this way too.
var ErrStepLimitExceeded = core.ErrStepLimitExceeded
ErrStepLimitExceeded is matched (with errors.Is) by the error Execute returns when the script used up its step budget, which callables run under parallel() draw from too. The error message itself is the interpreter's.
Functions ¶
func GenerateToolDescription ¶
GenerateToolDescription creates a dynamic description for the execute_tool_script tool, listing all available tools and their calling conventions.
Types ¶
type Config ¶
type Config struct {
// StepLimit is the maximum number of Starlark execution steps per script,
// summed over the script and every callable it runs under parallel().
// Prevents infinite loops and runaway computation.
// Zero uses DefaultStepLimit (100,000).
StepLimit uint64
// ParallelMax is the maximum number of concurrent goroutines that
// parallel() can spawn. Zero means unlimited.
ParallelMax int
// OnStats, when non-nil, is called once per Execute with what the
// execution did, whether it succeeded or failed. It runs on the Execute
// goroutine just before Execute returns, so it must be cheap.
OnStats func(Stats)
// Sandbox, when non-nil, runs each script in a child process under its
// memory limit; see [Sandbox]. Nil runs scripts in this process, where
// nothing bounds the memory a script allocates.
Sandbox *Sandbox
}
Config holds script execution parameters. A nil Config passed to New uses sensible defaults for all fields.
type Executor ¶
type Executor interface {
// Execute runs a Starlark script with optional named data arguments
// injected as top-level variables. Tools are already bound from
// construction and available as callable functions within the script.
// When ctx is done, the script stops at its next execution step, also
// under parallel(), and the error matches ctx.Err() with errors.Is.
Execute(ctx context.Context, script string, data map[string]interface{}) (*mcp.CallToolResult, error)
// ToolDescription returns the dynamic description for the
// execute_tool_script virtual tool definition, listing all available
// tools and their calling conventions.
ToolDescription() string
}
Executor runs Starlark scripts and describes the virtual tool.
Scripts can call any tool bound at construction time, use loops and conditionals, fan out with parallel(), and return aggregated results. The returned CallToolResult is ready for direct serialization into a JSON-RPC response by the middleware layer.
type Sandbox ¶ added in v0.72.0
type Sandbox struct {
// contains filtered or unexported fields
}
Sandbox runs scripts in child processes, each with its own memory limit, and can bound how many run at once, so that scripts together use at most memoryLimit × maxConcurrent bytes besides the server's own memory.
Starlark checks its step limit only between steps, and one expression can allocate a gigabyte in a single step, so an in-process limit cannot bound a script's memory (GHSA-jfhm-5xjh-87wv). A sandboxed script runs in a child started from the same executable, which limits its own memory before it reads the script. The script's tool calls come back to this process and run here, through each Tool's Call, so the caller's identity, credentials and authorization never leave it. The child also gets an empty environment.
The memory limit is enforced only where MemoryLimitEnforced is true. Starting a child costs some milliseconds per script, more for a large executable. A Sandbox is safe for concurrent use; share one across the executors of a server so its concurrency bound holds for the whole process.
func NewSandbox ¶ added in v0.72.0
NewSandbox returns a Sandbox that limits each script to memoryLimit bytes and runs at most maxConcurrent scripts at once; a script waits for a free slot until its context is done. A maxConcurrent of zero means no cap, as Config.ParallelMax does: the total memory of concurrent scripts is then unbounded, so a caller that takes the value from configuration should default an unset value to a positive one.
type Stats ¶ added in v0.69.0
type Stats struct {
// Steps is the number of Starlark execution steps, summed over the
// script and every callable it ran under parallel().
Steps uint64
// ParallelCalls is the number of parallel() calls the script made.
ParallelCalls int
// ParallelTasks is the number of callables those parallel() calls
// started, summed over all of them.
ParallelTasks int
}
Stats describes what one Execute call did. It carries no script content.
type Tool ¶
type Tool struct {
// Name is the MCP tool name (e.g., "github-fetch-prs").
Name string
// Description is the human-readable tool description.
Description string
// Call invokes the tool with the given arguments and returns the MCP result.
// Arguments are always a string-keyed map. When scripts use positional
// arguments, they are converted to "arg0", "arg1", etc.
//
// The caller is responsible for enforcing per-call timeouts (e.g., by
// wrapping ctx with context.WithTimeout in the closure). The engine
// passes the context through but does not apply additional deadlines.
Call func(ctx context.Context, arguments map[string]interface{}) (*mcp.CallToolResult, error)
}
Tool bundles an MCP tool's metadata with a callback for invoking it.
The middleware layer constructs these with Call closures that route invocations through the middleware chain, ensuring authz and other policies are enforced on inner tool calls.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
builtins
Package builtins provides Starlark builtin functions for the script engine.
|
Package builtins provides Starlark builtin functions for the script engine. |
|
conversions
Package conversions provides bidirectional type conversion between Go values and Starlark values, MCP result parsing, and tool name sanitization.
|
Package conversions provides bidirectional type conversion between Go values and Starlark values, MCP result parsing, and tool name sanitization. |
|
core
Package core provides the Starlark script execution engine.
|
Package core provides the Starlark script execution engine. |
|
sandbox
Package sandbox runs a script in a child process under an OS memory limit and carries its tool calls back to the parent.
|
Package sandbox runs a script in a child process under an OS memory limit and carries its tool calls back to the parent. |