-
Notifications
You must be signed in to change notification settings - Fork 404
Description
Checked other resources
- I added a very descriptive title to this issue.
- I searched the LangGraph.js documentation with the integrated search.
- I used the GitHub search to find a similar question and didn't find it.
- I am sure that this is a bug in LangGraph.js rather than my code.
- The bug is not resolved by updating to the latest stable version of LangGraph (or the specific integration package).
Example Code
Here is the test code
import { createAgent, createMiddleware, tool } from "langchain";
import * as z from "zod";
import {ChatOpenAI} from "@langchain/openai";
const model = new ChatOpenAI({
model: "my-model",
apiKey: "my-api-key",
streaming: true,
});
const getWeather = tool(
({ location }) => `Weather in ${location}: Sunny, 72°F`,
{
name: "get_weather",
description: "Get weather information for a location",
schema: z.object({
location: z.string().describe("The location to get weather for"),
}),
}
);
// A tool that will be added dynamically at runtime
const calculateTip = tool(
({ billAmount, tipPercentage = 20 }) => {
const tip = billAmount * (tipPercentage / 100);
return `Tip: $${tip.toFixed(2)}, Total: $${(billAmount + tip).toFixed(2)}`;
},
{
name: "calculate_tip",
description: "Calculate the tip amount for a bill",
schema: z.object({
billAmount: z.number().describe("The bill amount"),
tipPercentage: z.number().default(20).describe("Tip percentage"),
}),
}
);
const dynamicToolMiddleware = createMiddleware({
name: "DynamicToolMiddleware",
wrapModelCall: (request, handler) => {
// Add dynamic tool to the request
// This could be loaded from an MCP server, database, etc.
return handler({
...request,
tools: [...request.tools, calculateTip],
});
},
wrapToolCall: (request, handler) => {
// Handle execution of the dynamic tool
if (request.toolCall.name === "calculate_tip") {
return handler({ ...request, tool: calculateTip });
}
return handler(request);
},
});
const agent = createAgent({
model: model,
tools: [getWeather], // Only static tools registered here
middleware: [dynamicToolMiddleware],
});
// The agent can now use both getWeather AND calculateTip
const result = agent.invoke({
messages: [{ role: "user", content: "Calculate a 20% tip on $85" }],
});Error Message and Stack Trace (if applicable)
Uncaught Error: You have added a new tool in "wrapModelCall" hook of middleware "DynamicToolMiddleware": calculate_tip. This is not supported.
Description
I've just started learning langchain + langgraph. I'm currently using the TypeScript versions langchain 1.2.15 and langgraph 1.1.2. I wanted to follow this document to implement dynamic registration of tools: https://docs.langchain.com/oss/javascript/langchain/agents#runtime-tool-registration, but I get error.
I found that in langchain's source file AgentNode.js there is indeed logic that checks for newTools and invalidTools, which differs from what the documentation describes.
System Info
Node v20.18.0
packages
├── @langchain/core@1.1.17
├── @langchain/langgraph@1.1.2
├── @langchain/mcp-adapters@1.1.2
├── @langchain/openai@1.2.3