toolround

package
v1.261029.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MPL-2.0 Imports: 12 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 toolround is the Tool Round Stage: the single decision point for every tool call a model makes, on the Anthropic Beta protocol.

For each round the model produces, every tool_use is decided in a fixed order:

  1. Gate (Guardrails): a blocked call ends the turn. It is replaced by a text block carrying the block message; no other call of that round is executed or handed to the client.
  2. Ownership (MCP): calls the server owns are executed and their results, screened by the Gate, are fed back for another round. When a round mixes owned and client calls, the owned calls run now and their results are stored for the client's follow-up request.
  3. Everything else passes to the client.

Text and thinking stream to the client live; only tool_use blocks are held until their round is decided. A streamed answer that spans several rounds reaches the client as one message; a complete answer is the final round.

Index

Constants

View Source
const DefaultMaxRounds = 3

DefaultMaxRounds bounds how many rounds may execute owned tools in one request, as many as the existing tool loops run. Unlike them, the stage then asks the model once more so the turn ends with an answer built on those results (the existing loops ended it with an empty or failed answer), so a request makes at most DefaultMaxRounds+1 upstream calls.

Variables

This section is empty.

Functions

func New

func New(config Config) stage.Stage

New returns the Tool Round Stage.

Types

type Config

type Config struct {
	Owner     Owner
	Gate      Gate
	MaxRounds int
}

Config configures the stage. A nil Owner owns no tools; a nil Gate allows everything. With neither, the stage is a pass-through.

type Gate

type Gate interface {
	// Request screens the client's request before the first round.
	Request(ctx context.Context, request *anthropic.BetaMessageNewParams) error
	// ToolUse decides one tool call against the conversation so far.
	ToolUse(ctx context.Context, request *anthropic.BetaMessageNewParams, call ToolCall) Verdict
	// ToolResult screens an owned tool's result before the model sees it and
	// may rewrite it in place.
	ToolResult(ctx context.Context, request *anthropic.BetaMessageNewParams, call ToolCall, result *anthropic.BetaToolResultBlockParam)
	// Restore returns a tool input with alias tokens replaced by real values.
	Restore(input json.RawMessage) json.RawMessage
	// Response screens the final client-bound message of a complete answer and
	// may rewrite it in place.
	Response(ctx context.Context, request *anthropic.BetaMessageNewParams, message *anthropic.BetaMessage)
}

Gate screens a turn (Guardrails).

type Owner

type Owner interface {
	// Owns reports whether the server executes the named tool.
	Owns(name string) bool
	// Execute runs one owned call. Tool failures are reported in the result
	// (is_error), never as an aborted request. The returned context carries
	// state later executions in the same request depend on.
	Execute(ctx context.Context, call ToolCall, request *anthropic.BetaMessageNewParams) (context.Context, anthropic.BetaToolResultBlockParam)
	// Resume splices results stored by an earlier mixed round into request.
	Resume(ctx context.Context, request *anthropic.BetaMessageNewParams) *anthropic.BetaMessageNewParams
	// Suspend stores a mixed round (the assistant turn and the owned results)
	// for the client's follow-up request.
	Suspend(ctx context.Context, turn anthropic.BetaMessageParam, results []anthropic.BetaToolResultBlockParam)
}

Owner decides and runs server-owned tools (MCP).

type ToolCall

type ToolCall struct {
	ID    string
	Name  string
	Input json.RawMessage
}

ToolCall is one tool_use the model produced.

type Verdict

type Verdict struct {
	Block   bool
	Message string
}

Verdict is the Gate's decision on one tool call.

Jump to

Keyboard shortcuts

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