script

package
v0.21.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

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

View Source
const DefaultStepLimit uint64 = 100_000

DefaultStepLimit is the default maximum number of Starlark execution steps.

View Source
const ExecuteToolScriptName = "execute_tool_script"

ExecuteToolScriptName is the name of the virtual tool exposed by the script middleware.

Variables

This section is empty.

Functions

func GenerateToolDescription

func GenerateToolDescription(tools []Tool) string

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.
	// 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
}

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

func New

func New(tools []Tool, cfg *Config) Executor

New creates an Executor bound to the given tools and configuration. A nil cfg uses defaults (DefaultStepLimit, unlimited parallelism, no timeout).

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.

Jump to

Keyboard shortcuts

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