langchain.js
    Preparing search index...

    Class ToolNode<StateSchema, ContextSchema>

    ToolNode is a built-in LangGraph component that handles tool calls within an agent's workflow. It works seamlessly with createAgent, offering advanced tool execution control, built in parallelism, and error handling.

    import { ToolNode, tool, AIMessage } from "langchain";
    import { z } from "zod/v3";

    const getWeather = tool((input) => {
    if (["sf", "san francisco"].includes(input.location.toLowerCase())) {
    return "It's 60 degrees and foggy.";
    } else {
    return "It's 90 degrees and sunny.";
    }
    }, {
    name: "get_weather",
    description: "Call to get the current weather.",
    schema: z.object({
    location: z.string().describe("Location to get the weather for."),
    }),
    });

    const tools = [getWeather];
    const toolNode = new ToolNode(tools);

    const messageWithSingleToolCall = new AIMessage({
    content: "",
    tool_calls: [
    {
    name: "get_weather",
    args: { location: "sf" },
    id: "tool_call_id",
    type: "tool_call",
    }
    ]
    })

    await toolNode.invoke({ messages: [messageWithSingleToolCall] });
    // Returns tool invocation responses as:
    // { messages: ToolMessage[] }

    Type Parameters

    • StateSchema extends AnyAnnotationRoot | BaseMessage = any
    • ContextSchema extends AnyAnnotationRoot | BaseMessage = any

    Hierarchy

    Index

    Constructors

    • Type Parameters

      • StateSchema extends any = any
      • ContextSchema extends any = any

      Parameters

      • tools: any[]
      • Optionaloptions: ToolNodeOptions

      Returns ToolNode<StateSchema, ContextSchema>

    Properties

    config?: any
    func: (...args: StateSchema[]) => ContextSchema | Promise<ContextSchema>
    handleToolErrors: boolean | ((error: unknown, toolCall: ToolCall) => any) = true
    lc_namespace: string[] = ...
    options?: ToolNodeOptions
    recurse: boolean = true
    signal?: AbortSignal
    tags?: string[]
    tools: any[]
    trace: boolean = false

    Methods

    • Returns Awaited<ContextSchema>

    • Parameters

      Returns Promise<ContextSchema>

    • Parameters

      • state: ToAnnotationRoot<StateSchema>["State"] & StateType<
            {
                llmInputMessages: BinaryOperatorAggregate<BaseMessage[], any>;
                messages: BinaryOperatorAggregate<BaseMessage[], any>;
            },
        >
      • config: RunnableConfig

      Returns Promise<ContextSchema>

    • Parameters

      • call: ToolCall
      • config: RunnableConfig

      Returns Promise<any>