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.
In a graph
Section titled “In a graph”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())?;Input forms
Section titled “Input forms”ToolNode accepts:
- a state object with the messages under
messages(or the key set withmessages_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.
Options
Section titled “Options”| 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() |
Errors
Section titled “Errors”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.
Approval interrupts
Section titled “Approval interrupts”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.
State, store and commands from tools
Section titled “State, store and commands from tools”- 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 aCommand.
Tool stream events
Section titled “Tool stream events”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
Section titled “ValidationNode”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.