Skip to content

ToolNode

warpra_prebuilt::ToolNode runs the tool calls of the last assistant message and appends one tool message per call. It is the tools node of the ReAct agent, and you can use it in your own graphs.

use std::sync::Arc;
use nexura_core::{FnTool, Tool, ToolOutput, ToolSpec};
use warpra::*;
use warpra_prebuilt::ToolNode;
use serde_json::json;
let spec = ToolSpec::new("echo", "Echo text", json!({"type": "object", "properties": {"text": {"type": "string"}}}));
let echo: Arc<dyn Tool> = Arc::new(FnTool::new(spec, |args, _ctx| async move {
Ok(ToolOutput::text(format!("echoed:{}", args["text"].as_str().unwrap_or(""))))
}));
let mut g = StateGraph::new().messages("messages");
g.add_node_fn("caller", |_s: serde_json::Value, _c: NodeCtx| async move {
Ok::<_, NodeError>(json!({"messages": [{"role": "assistant", "content": "", "tool_calls": [
{"id": "tc1", "name": "echo", "arguments": {"text": "hi"}}
]}]}))
})?;
g.add_node("tools", ToolNode::new(vec![echo]))?;
g.add_edge(START, "caller")?.add_edge("caller", "tools")?.add_edge("tools", END)?;
let app = g.compile(CompileOpts::new())?;

ToolNode accepts:

  • a state object with the messages under messages (or the key set with messages_key(..)),
  • a messages list,
  • a list of {"type": "tool_call", ..} objects,
  • the V2 payload {"__type": "tool_call_with_context", "tool_call": .., "state": ..} that the agent sends per call.

Calls run concurrently. Results keep call order.

Method Default Effect
messages_key(key) messages State key holding the messages
handle_errors(HandleToolErrors) Default Error handling mode, see below
validate_args(bool) true Validate arguments against the tool’s JSON schema first. Failures are ToolValidation errors.
approval(bool) true Interrupt before tools with requires_approval()

An unknown tool produces an error message: Error: x is not a valid tool, try one of [a, b]. Other errors follow HandleToolErrors: Default, All, Message, Handler, Codes or Raise.

For a tool whose requires_approval() is true, the node interrupts with {"type": "tool_approval", "tool_call": {..}}. Resume with true, {"approved": true} or {"type": "accept"} to approve. Anything else rejects.

  • The current state reaches the tool as ToolContext::state (injected state).
  • warpra_prebuilt::injected_store() returns the graph’s store inside a tool (injected store).
  • A tool can return command_output(content, &command) to update state or route with a Command.

With StreamMode::Tools requested, ToolNode emits tool-started (with the call input), tool-finished (with the tool message) and tool-error. Inside a tool body, emit_output_delta(..) sends tool-output-delta. Interrupted tools and unknown tools emit no terminal event.

ValidationNode validates tool calls against schemas without running them, and returns error tool messages for invalid calls. Build it with ValidationNode::new(schemas) or ValidationNode::from_tools(&tools). format_error(..) sets a custom formatter.

Warpra is open source under Apache-2.0. Made by Liceron.