InterviewPrepKit

Home / Coding / Agent Coding / Tools & Function Calling / A Tool Registry

A Tool Registry

easy 00:00
Solving tips
  • A registry is just a dict from tool name to callable; register writes to it and call reads from it.
  • An unknown tool is the common failure — return a structured error the agent can read, don't crash the loop.
  • Return the same shape on success and failure so the caller has one thing to parse.

Every agent needs a way to go from a tool name the model produced ("add", "search") to the actual Python function that runs it. That indirection layer is the tool registry. It is small, but it is where unknown-tool errors and tool crashes get turned into something the agent loop can handle instead of an exception that kills the run.

Task

Implement the ToolRegistry class:

  1. register(name, fn) stores the callable fn under the string name. Registering the same name again overwrites the previous function.
  2. call(name, args) looks up name and invokes fn(args), returning a structured dict:
    • success: {"ok": True, "tool": name, "result": <return value>}
    • unknown tool: {"ok": False, "tool": name, "error": "unknown tool: <name>"}
    • tool raised: {"ok": False, "tool": name, "error": "<exception message>"}
  3. list_tools() returns the registered names as a sorted list.

The point is dispatch plus clean error handling. Both an unknown tool and a tool that raises must come back as a structured ok: False result, never an uncaught exception.

Example

reg = ToolRegistry()
reg.register("add", lambda args: args["a"] + args["b"])
reg.register("div", lambda args: args["a"] / args["b"])

reg.call("add", {"a": 2, "b": 3})     # -> {"ok": True,  "tool": "add", "result": 5}
reg.call("mul", {"a": 2, "b": 3})     # -> {"ok": False, "tool": "mul", "error": "unknown tool: mul"}
reg.call("div", {"a": 1, "b": 0})     # -> {"ok": False, "tool": "div", "error": "division by zero"}
reg.list_tools()                       # -> ["add", "div"]

Constraints

  • Do not call any real LLM, network, or external service — tools are plain Python callables passed in.
  • A tool raising an exception must be caught and reported, not propagated.
  • list_tools() output is sorted so it is deterministic.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug