Blog/Learn Agentic AI Development/The Tool Registry: Names Become Callable Functions

Chapter 04 / 10 min read

The Tool Registry: Names Become Callable Functions

Imports, function objects, dictionary dispatch, unknown tool handling and the security boundary.

The tool registry: names become callable Python functions

The problem

The model can return a tool name such as multiply_numbers. Python still needs to find the function object that implements that operation.

A registry is the bridge between the model-facing name and the actual Python implementation. It is not the tool itself, and it does not execute anything by magic. It is a controlled lookup table.

1. Import the functions

The calculator functions live in one module. We can import only the functions we need:

Code
from tools.calculator import (
    calculate_addition,
    multiply_numbers,
    divide_numbers,
)

Other valid import styles include:

Code
from tools.calculator import calculate_addition
from tools.calculator import *
from tools import calculator

# With module import:
calculator.multiply_numbers(10, 20)

Explicit imports are easier to read in this project because they show exactly which functions the file depends on. Wildcard imports hide that information.

2. Build the registry

Code
TOOL_REGISTRY = {
    "calculate_addition": calculate_addition,
    "multiply_numbers": multiply_numbers,
    "divide_numbers": divide_numbers,
}

The keys are strings. The values are function objects—not strings containing Python source code.

This distinction is important:

Code
tool_name = "calculate_addition"       # A string: the function's name
function = TOOL_REGISTRY[tool_name]    # A function object
result = function(5, 3)                # Actually invokes the function

The original exercise confirmed the lookup:

Code
result = TOOL_REGISTRY.get("calculate_addition")(5, 3)
print(result)

Observed result:

Code
{'success': True, 'operation': 'addition',
 'inputs': {'a': 5, 'b': 3}, 'result': 8}

3. Dispatch a model-selected tool

The registry lets the application handle the name dynamically:

Code
tool = "calculate_addition"
tool_args = {
    "a": 5,
    "b": 3,
}

if tool in TOOL_REGISTRY:
    result = TOOL_REGISTRY[tool](**tool_args)
else:
    result = f"Error: Tool '{tool}' not recognized."

print(result)

The **tool_args syntax unpacks dictionary entries into keyword arguments. The call is equivalent to:

Code
calculate_addition(a=5, b=3)

The source exercise deliberately changed the requested name to substract_numbers, which was not in the registry. The application printed:

Code
Error: Tool 'substract_numbers' not recognized.

That is a useful failure: the model cannot make an arbitrary Python function callable just by inventing its name.

4. The registry and tool definitions solve different problems

A key design decision in the conversation was to keep the registry and the LLM-facing definitions separate.

ComponentAudienceResponsibility
TOOL_REGISTRYPython applicationMap a name to an executable function
Tool definitionLLMExplain the tool and describe its input parameters
Pydantic modelPython application and schema generatorDefine and validate the argument contract

The registry answers: “Which Python function should run?”

The tool definition answers: “Which operation is available, what does it do, and what arguments should the model provide?”

Pydantic answers: “Do the received arguments satisfy the declared contract?”

Keeping these responsibilities separate improves readability and maintainability. It also lets the model-facing description evolve without changing the function lookup mechanism.

5. A critical security boundary

Never use a model-generated string with eval, exec, or arbitrary dynamic imports to execute code. The safe dispatch pattern is to look up a name in an allow-listed registry, validate its arguments, and only then invoke the registered function.

At this point in the project, the registry worked. The next issue was that a registry alone does not prove the arguments are correct. We need schemas and runtime validation.