Documentation
¶
Overview ¶
Package codemode adapts the vMCP-agnostic Starlark engine in pkg/script into a core.VMCP decorator. The decorator advertises the execute_tool_script virtual tool alongside the backend tools and, when that tool is called, runs the submitted Starlark script. Inner tool calls made by the script route back through the inner core's CallTool, so the core admission seam authorizes each one by its real name — the script can only reach tools the identity is already admitted to.
The decorator is the seam where vMCP domain types (vmcp.Tool, vmcp.ToolCallResult) meet the engine's mcp-go types. That conversion is contained here so it does not leak across the core boundary (vmcp anti-pattern #5).
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrReservedToolName = errors.New("codemode: backend tool collides with reserved virtual tool name")
ErrReservedToolName is returned when a backend advertises a tool whose name collides with the reserved execute_tool_script virtual tool. It is fail-loud on purpose: a silent shadow would let the backend tool skip its own Cedar admission (LookupTool / ListTools would resolve the virtual definition instead), so the decorator refuses to serve rather than mask the backend tool (#5845).
Functions ¶
func NewDecorator ¶
NewDecorator wraps inner with vMCP code mode. The returned VMCP advertises execute_tool_script in addition to inner's tools and executes submitted Starlark scripts when that tool is called. A nil cfg uses defaults.
inner must be non-nil; a nil inner is a composition-root wiring bug and panics rather than deferring the failure to the first promoted method call.
Types ¶
type Config ¶
type Config struct {
// StepLimit is the maximum number of Starlark execution steps per script.
// Zero resolves to [script.DefaultStepLimit].
StepLimit uint64
// ParallelMax is the maximum number of concurrent goroutines that parallel()
// may spawn within a script. Zero (or negative) resolves to defaultParallelMax.
ParallelMax int
// ToolCallTimeout bounds each individual inner tool call made from a script.
// Zero (or negative) resolves to defaultToolCallTimeout.
ToolCallTimeout time.Duration
// OnScript, when non-nil, is called once per execute_tool_script call with
// the script's outcome, duration and the number of tool calls it started,
// including failed and denied ones (zero when the call fails before the
// script runs). Nil (the default) leaves behavior unchanged. See
// [ScriptObserver] for the contract.
OnScript ScriptObserver
// Telemetry, when non-nil, records OTel metrics and spans for every
// execute_tool_script call. Nil (the default) records none. Build it with
// [NewTelemetry].
Telemetry *Telemetry
}
Config holds the code mode tuning parameters. Presence of a non-nil *Config at the composition root is itself the opt-in toggle; a nil *Config (the default) leaves the core undecorated and execute_tool_script absent from tools/list.
A nil *Config passed to NewDecorator, or any field left zero, resolves to the safe defaults in resolve — never to the engine's unbounded zero-value behavior.
func FromConfig ¶
func FromConfig(c *config.CodeModeConfig) *Config
FromConfig translates the serialized vMCP code mode config into the runtime decorator Config. It returns nil when c is nil or disabled, so the caller can hand the result straight to the server config (a nil value leaves the core undecorated). It performs no defaulting: unset fields stay zero and are resolved to defaults by NewDecorator (see resolve), so the operator/CRD, YAML, and direct-construction paths share one source of defaults.
type ScriptObserver ¶ added in v0.69.0
type ScriptObserver func(ctx context.Context, stats ScriptStats)
ScriptObserver receives the stats of every execute_tool_script call. It runs synchronously on the request path after the script finishes, so it must be cheap and must not block; hand slow work to another goroutine. A panic in the observer is recovered and logged, and never changes the tool result.
type ScriptOutcome ¶ added in v0.69.0
type ScriptOutcome string
ScriptOutcome is how one execute_tool_script call ended. It is a small, fixed set, so it is safe to use as a metric attribute.
const ( // OutcomeSuccess means the script ran and returned a result. OutcomeSuccess ScriptOutcome = "success" // OutcomeError means the call failed for any reason not covered below: // invalid arguments, a runtime error in the script, or a failed tool call. OutcomeError ScriptOutcome = "error" // OutcomeTimeout means the script or one of its tool calls hit a deadline. OutcomeTimeout ScriptOutcome = "timeout" // OutcomeStepLimit means the script used up its execution step budget. OutcomeStepLimit ScriptOutcome = "step_limit" )
type ScriptStats ¶ added in v0.69.0
type ScriptStats struct {
// ToolCalls is the number of tool calls the script started, including
// calls that failed or were denied. Calls made under parallel() count
// individually. It is zero when the call failed before the script ran.
ToolCalls int
// Outcome is how the call ended.
Outcome ScriptOutcome
// Duration is the wall-clock time of the whole call.
Duration time.Duration
// Steps is the number of Starlark execution steps the script used,
// including those of callables run 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.
ParallelTasks int
}
ScriptStats describes one execute_tool_script call. It carries no script body, arguments or tool names, so an observer cannot leak them by accident.
type Telemetry ¶ added in v0.69.0
type Telemetry struct {
// contains filtered or unexported fields
}
Telemetry holds the OTel instruments for code mode. Build it once with NewTelemetry and set it on Config.Telemetry. A nil *Telemetry records nothing and leaves the request context untouched.
func NewTelemetry ¶ added in v0.69.0
func NewTelemetry(mp metric.MeterProvider, tp trace.TracerProvider) (*Telemetry, error)
NewTelemetry builds the code mode instruments from mp and tp under the vMCP instrumentation scope:
- toolhive_vmcp_script_executions: counter, attribute status.
- toolhive_vmcp_script_duration: histogram in seconds, attribute status.
- toolhive_vmcp_script_tool_calls: histogram of inner tool calls started per script, attribute status.
- toolhive_vmcp_script_parallel_tasks: counter of callables started by parallel(), attribute status.
status holds one of the four ScriptOutcome values. It also creates a codemode.ExecuteToolScript span per script and a codemode.CallTool child span per inner tool call. A nil provider is replaced by a no-op one.