import * as z from "zod"import { tool } from "langchain"const searchDatabase = tool( ({ query, limit }) => `Found ${limit} results for '${query}'`, { name: "search_database", description: "Search the customer database for records matching the query.", schema: z.object({ query: z.string().describe("Search terms to look for"), limit: z.number().describe("Maximum number of results to return"), }), });
import * as z from "zod";import { tool, ToolRuntime } from "langchain";const getWeather = tool( ({ city }, config: ToolRuntime) => { const writer = config.writer; // Stream custom updates as the tool executes if (writer) { writer(`Looking up data for city: ${city}`); writer(`Acquired data for city: ${city}`); } return `It's always sunny in ${city}!`; }, { name: "get_weather", description: "Get weather for a given city.", schema: z.object({ city: z.string(), }), });
import { tool } from "langchain";import * as z from "zod";const getWeather = tool(({ city }) => `It is currently sunny in ${city}.`, { name: "get_weather", description: "Get weather for a city.", schema: z.object({ city: z.string() }),});
import { tool } from "langchain";import * as z from "zod";const getWeatherData = tool( ({ city }) => ({ city, temperature_c: 22, conditions: "sunny", }), { name: "get_weather_data", description: "Get structured weather data for a city.", schema: z.object({ city: z.string() }), },);
import { createAgent, createMiddleware, tool } from "langchain";import * as z from "zod";// A tool that will be added dynamically at runtimeconst 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: "gpt-4o", tools: [getWeather], // Only static tools registered here middleware: [dynamicToolMiddleware],});// The agent can now use both getWeather AND calculateTipconst result = await agent.invoke({ messages: [{ role: "user", content: "Calculate a 20% tip on $85" }],});