Basic Tool Registration and Calling

Easy
Agents

Tool Calling Basics

AI agents extend their capabilities by calling external tools (APIs, functions, databases).

Task

Build a ToolRegistry that:

  1. Registers tools with a name, callable, and JSON schema.
  2. Invokes tools by name with keyword arguments.
  3. Lists available tools and their schemas.
  4. Raises ToolNotFoundError for unknown tools.

Constraints

  • Tool names must be unique.
  • Schema must be a dict with at least 'description' key.
  • Arguments must be validated against schema before calling.

Examples

Example 1:
Input: reg = ToolRegistry() reg.register('add', lambda a, b: a+b, {'description': 'Add two numbers'}) reg.call('add', a=2, b=3)
Output: 5
Explanation: Tool 'add' is found and executed with provided args.

Starter Code

from typing import Any, Dict, Callable

class ToolRegistry:
    def __init__(self):
        self.tools: Dict[str, Callable] = {}
        self.schemas: Dict[str, Dict] = {}

    def register(self, name: str, fn: Callable, schema: Dict) -> None:
        # TODO: Register tool with name, function, and JSON schema
        pass

    def call(self, name: str, **kwargs) -> Any:
        # TODO: Validate and call tool, raise ToolNotFoundError if missing
        pass

    def list_tools(self) -> Dict[str, Dict]:
        # TODO: Return all registered tool schemas
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews