Spaces:
Sleeping
Sleeping
Create src/macg/tools/registry.py
Browse files- src/macg/tools/registry.py +28 -0
src/macg/tools/registry.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Callable, Any
|
| 4 |
+
|
| 5 |
+
@dataclass
|
| 6 |
+
class Tool:
|
| 7 |
+
name: str
|
| 8 |
+
description: str
|
| 9 |
+
fn: Callable[..., Any]
|
| 10 |
+
|
| 11 |
+
class ToolRegistry:
|
| 12 |
+
"""
|
| 13 |
+
Think of this as a tiny MCP-style “tool server”.
|
| 14 |
+
Agents don’t call random Python—they call named tools with structured inputs.
|
| 15 |
+
"""
|
| 16 |
+
def __init__(self) -> None:
|
| 17 |
+
self._tools: dict[str, Tool] = {}
|
| 18 |
+
|
| 19 |
+
def register(self, tool: Tool) -> None:
|
| 20 |
+
self._tools[tool.name] = tool
|
| 21 |
+
|
| 22 |
+
def list_tools(self) -> list[dict[str, str]]:
|
| 23 |
+
return [{"name": t.name, "description": t.description} for t in self._tools.values()]
|
| 24 |
+
|
| 25 |
+
def call(self, name: str, **kwargs) -> Any:
|
| 26 |
+
if name not in self._tools:
|
| 27 |
+
raise KeyError(f"Tool not found: {name}")
|
| 28 |
+
return self._tools[name].fn(**kwargs)
|