Skip to Content
JavaScriptUse ToolsUsing Tools

Using Tools

OpenAPITools provides a flexible way to work with tools across different AI providers. You can specify which tools you want to use and even target specific versions of those tools.

Specifying Tools

When working with any adapter (Anthropic, OpenAI, or LangChain), you can specify which tools you want to use in several ways:

Basic Usage - All Tools

By default, if you don’t specify any tool names, all available tools will be used:

// Get all available tools const tools = await adapter.getAnthropicTools(); // For Anthropic // OR const tools = await adapter.getOpenAITools(); // For OpenAI // OR const tools = await adapter.getLangChainTools(); // For LangChain

Specifying Tool Names

You can specify which tools you want to use by passing a list of tool names:

// Get specific tools by name const toolNames = ["otp_generator", "weather_lookup"]; const tools = await adapter.getAnthropicTools(toolNames);

Specifying Tool Versions

For more control, you can specify both the tool name and version:

// Get specific tools with specific versions const toolList = [ "simple_calculator", // Use latest version { name: "otp_generator", version: "v2" }, // Use specific version { name: "weather_lookup" }, // No version specified uses latest ]; const tools = await adapter.getOpenAITools(toolList);

Creating Tool Handlers

When creating tool handlers, you can also specify which tools should be handled:

// Create a handler for specific tools const toolHandler = await adapter.createAnthropicToolHandler(toolNames); // OR for OpenAI const toolHandler = await adapter.createOpenAIToolHandler(toolList);

Executing Tools

Anthropic

const response = await anthropic.messages.create(apiOptions); // loop through the response content for (const content of response.content) { if (content.type === "tool_use") { const toolResult = await toolHandler({ id: content.id, name: content.name, input: content.input, }); if (toolResult.error) { console.log("\nTool Error:", toolResult.error); messages.push({ role: "user", content: [ { type: "tool_result", tool_use_id: content.id, content: JSON.stringify({ error: toolResult.error }), }, ], }); } else { console.log("\nTool Result:", toolResult.output); messages.push({ role: "user", content: [ { type: "tool_result", tool_use_id: content.id, content: JSON.stringify({ output: toolResult.output }), }, ], }); } } }

OpenAI

const response = await openai.chat.completions.create(apiOptions); const message = response.choices[0].message; for (const toolCall of message.tool_calls) { console.log("\nAssistant is using tool:", toolCall.function.name); try { const args = JSON.parse(toolCall.function.arguments); console.log("Tool input:", JSON.stringify(args, null, 2)); } catch { console.log("Tool input:", toolCall.function.arguments); } // Execute the tool try { const toolResult = await toolHandler({ id: toolCall.id, function: { name: toolCall.function.name, arguments: toolCall.function.arguments, }, }); // Add tool response to messages if (toolResult.error) { console.log("\nTool Error:", toolResult.error); messages.push({ tool_call_id: toolCall.id, role: "tool", name: toolCall.function.name, content: JSON.stringify({ error: toolResult.error }), }); } else { console.log("\nTool Result:", toolResult.output); messages.push({ tool_call_id: toolCall.id, role: "tool", name: toolCall.function.name, content: JSON.stringify({ output: toolResult.output }), }); } } catch (error) { console.error("Tool execution error:", error); } }

LangChain

import { ChatAnthropic } from "@langchain/anthropic"; import { createToolCallingAgent } from "langchain/agents"; import { AgentExecutor } from "langchain/agents"; import { LangChainAdapter } from "@reacter/openapitools"; // Initialize the language model const llm = new ChatAnthropic({ model: "claude-3-7-sonnet-20250219", apiKey: "your-anthropic-api-key", }); // Initialize the adapter const adapter = new LangChainAdapter( "your OpenAPI Tools Apikey - https://openapitools.com/dashboard/settings" ); // Get tools in LangChain format const tools = await adapter.getLangChainTools(); // Create a LangChain agent with tools const agent = createToolCallingAgent({ llm, tools, prompt, }); const agentExecutor = new AgentExecutor({ agent, tools, }); // Use the agent const response = await agentExecutor.invoke({ input: "Can you generate an OTP for me?", }); console.log(response);
Last updated on