Skip to content

ToolContext

Context available during the tool hook (wraps one tool execution).

Extends TurnContext with tool-specific data and control flow methods: deny() for soft-blocking, skipCall() for mocking, modifyArgs() for argument transformation.

agent: object

Agent definition: name, model, instructions.

instructions: string

model: string

name: string

TurnContext.agent


args: Record<string, unknown>

Arguments from the LLM. Middleware can modify via modifyArgs().


callId: string

Tool call ID from the model response.


callIndex: number

Which tool call within this model response (0-based).


config: Record<string, unknown>

Middleware-specific configuration from the agent definition.

TurnContext.config


history: Message[]

Canonical conversation history (append-only).

TurnContext.history


input: Message[]

Input messages for this turn.

TurnContext.input


output: string | null

Assistant’s final text output for this turn. null until the turn completes.

TurnContext.output


sessionId: string

Unique session identifier.

TurnContext.sessionId


startedAt: number

Timestamp when this turn started.

TurnContext.startedAt


state: Record<string, unknown>

Session state — typed fields with optional reducers, shared across all turns.

TurnContext.state


tool: object

Tool definition (name, description, schema, approval flag).

description: string

jsonSchema: Record<string, unknown>

name: string

optional requireApproval?: boolean | ((args) => boolean | Promise<boolean>)


turnId: string

Unique turn identifier.

TurnContext.turnId


turnIndex: number

Turn number within this session (0-based).

TurnContext.turnIndex

abort(reason): never

Hard-stop the turn. Throws AbortError that unwinds the entire onion stack.

string

never

TurnContext.abort


approve(): void

Explicitly approve the tool call (reserved for future HITL flows).

void


deny(reason): void

Deny the tool call. Returns an error message to the LLM so it can adapt. Does NOT throw — this is a soft failure.

string

void


emit(event): void

Emit a stream event to the consumer.

StreamEvent

void

TurnContext.emit


modifyArgs(newArgs): void

Replace or merge tool call arguments.

Record<string, unknown>

void


registerTool(tool): void

Register a tool on the agent. Call in the agent hook before next().

Tool

void

TurnContext.registerTool


skipCall(result): void

Skip tool execution and return a synthetic result (for mocking/testing).

ToolResult

void