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:
- 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.
- 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.
- 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 ¶
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 ¶
Types ¶
type Config ¶
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).