registerTool
Problem
When building a tool-using agent, you need a central place to register tools so the agent can find them by name at runtime. Without a registry, you pass tool arrays around everywhere — and one mismatch means the agent silently skips tool calls.
Solution
registerTool adds your tool to a global in-memory registry. Once registered, the agent can look up and execute that tool by name. Registration is validated — duplicate names throw immediately so you catch mistakes at startup, not during a live agent run.
Feature & Use-Case
Use registerTool when:
- You want to define tools once at app startup and reference them throughout your agent
- You are building a LangChain Node.js app where multiple agents share common tools (e.g. a search tool, a database query tool)
- You want name-collision protection — registering the same name twice throws a clear error
Import
import { registerTool } from "llm-layer-engine";Function Signature
function registerTool(tool: Tool): voidTool Type
type Tool = {
name: string;
execute: (input: Record<string, unknown>) => Promise<Record<string, unknown> | string | number | boolean>;
};Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
tool.name | string | ✅ | Unique name the agent uses to call this tool |
tool.execute | Function | ✅ | Async function that receives the tool’s input and returns a result |
Example — Register Tools at App Startup
import { registerTool } from "llm-layer-engine";
// Register a weather tool
registerTool({
name: "get_weather",
execute: async (input) => {
const city = input.city as string;
// Call your weather API here
return { city, temperature: "28°C", condition: "Sunny" };
},
});
// Register a search tool
registerTool({
name: "web_search",
execute: async (input) => {
const query = input.query as string;
// Call your search API here
return { results: [`Result for: ${query}`] };
},
});
console.log("Tools registered ✓");Error: Duplicate Tool Name
registerTool({ name: "get_weather", execute: async () => ({}) });
registerTool({ name: "get_weather", execute: async () => ({}) });
// ❌ Throws: "Tool with name get_weather already registered"Register each tool name exactly once. If you need to replace a tool during testing, use clearTools first.
Registering Tools for a Specific Agent Run
You can also pass tools directly into reActLoop without using the global registry:
import { reActLoop } from "llm-layer-engine";
const loop = reActLoop({
config,
provider,
messages,
tools: [
{
name: "get_weather",
execute: async (input) => ({ city: input.city, temp: "30°C" }),
},
],
});Both approaches work. Use the registry for shared tools, pass directly for agent-specific tools.
Conclusion
registerTool is how you wire tools into the global registry. Call it once per tool at app startup. Keep tool names lowercase and snake_case — they must match exactly what your LLM returns in its tool-call responses.