Skip to content

API: Tooling

This section covers the ToolRegistry and its related classes for building production-grade, governable tools.

safeagent.tool_registry

AccessManager

A centralized class to manage Role-Based Access Control (RBAC).

This class can be initialized with a user role mapping, or it will use a default set of roles for demonstration purposes.

Source code in src/safeagent/tool_registry.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class AccessManager:
    """
    A centralized class to manage Role-Based Access Control (RBAC).

    This class can be initialized with a user role mapping, or it will
    use a default set of roles for demonstration purposes.
    """
    def __init__(self, role_config: Optional[Dict[str, List[str]]] = None):
        """
        Initializes the AccessManager.

        Args:
            role_config: A dictionary mapping user IDs to a list of their roles.
                         If None, a default demo configuration is used.
        """
        if role_config is not None:
            self._user_role_database = role_config
        else:
            self._user_role_database = {
                "billing_user_01": ["billing_agent", "support"],
                "weather_analyst_7": ["weather_forecaster"],
                "data_auditor_3": ["readonly_viewer", "guest_access"]
            }

    def check_access(self, user_id: str, required_role: str) -> bool:
        """
        Checks if a user has a required role by looking them up in the
        internal role database.
        """
        current_user_roles = self._user_role_database.get(user_id, [])
        return required_role in current_user_roles

__init__(role_config=None)

Initializes the AccessManager.

Parameters:

Name Type Description Default
role_config Optional[Dict[str, List[str]]]

A dictionary mapping user IDs to a list of their roles. If None, a default demo configuration is used.

None
Source code in src/safeagent/tool_registry.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def __init__(self, role_config: Optional[Dict[str, List[str]]] = None):
    """
    Initializes the AccessManager.

    Args:
        role_config: A dictionary mapping user IDs to a list of their roles.
                     If None, a default demo configuration is used.
    """
    if role_config is not None:
        self._user_role_database = role_config
    else:
        self._user_role_database = {
            "billing_user_01": ["billing_agent", "support"],
            "weather_analyst_7": ["weather_forecaster"],
            "data_auditor_3": ["readonly_viewer", "guest_access"]
        }

check_access(user_id, required_role)

Checks if a user has a required role by looking them up in the internal role database.

Source code in src/safeagent/tool_registry.py
40
41
42
43
44
45
46
def check_access(self, user_id: str, required_role: str) -> bool:
    """
    Checks if a user has a required role by looking them up in the
    internal role database.
    """
    current_user_roles = self._user_role_database.get(user_id, [])
    return required_role in current_user_roles

SimilarityMetric

Bases: Enum

Specifies the similarity metric for vector search.

Source code in src/safeagent/tool_registry.py
76
77
78
79
80
class SimilarityMetric(Enum):
    """Specifies the similarity metric for vector search."""
    L2 = "l2"
    COSINE = "cosine"
    DOT_PRODUCT = "dot_product"

ToolExecutionError

Bases: ToolRegistryError

Raised when a tool fails to execute after all retries.

Source code in src/safeagent/tool_registry.py
91
92
93
class ToolExecutionError(ToolRegistryError):
    """Raised when a tool fails to execute after all retries."""
    pass

ToolNotFoundError

Bases: ToolRegistryError

Raised when a tool is not found in the registry.

Source code in src/safeagent/tool_registry.py
87
88
89
class ToolNotFoundError(ToolRegistryError):
    """Raised when a tool is not found in the registry."""
    pass

ToolRegistry

A central, governed registry for tools that includes RBAC, automatic retries, circuit breakers, cost/latency tracking, caching, async support, output sinks, and dynamic schemas.

Source code in src/safeagent/tool_registry.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
class ToolRegistry:
    """
    A central, governed registry for tools that includes RBAC, automatic retries,
    circuit breakers, cost/latency tracking, caching, async support, output sinks,
    and dynamic schemas.
    """
    def __init__(
        self,
        governance_manager: GovernanceManager,
        access_manager: Optional[AccessManager] = None,
        embedding_config: Optional[Dict] = None,
        similarity_metric: SimilarityMetric = SimilarityMetric.L2,
        embedding_dimension: int = 768
    ):
        self._tools: Dict[str, Callable] = {}
        self._tool_metadata: Dict[str, Dict] = {}
        self.gov = governance_manager
        self.access_manager = access_manager or AccessManager()
        self.embedding_config = embedding_config or {}
        self.similarity_metric = similarity_metric
        self.embedding_dimension = embedding_dimension
        self._circuit_breaker_state: Dict[str, Dict] = {}
        self._cache: Dict[str, Dict] = {}  # In-memory cache

        self._tool_index = None
        self._index_to_tool_name: Dict[int, str] = {}
        if _EMBEDDINGS_ENABLED:
            self._initialize_faiss_index()

    def _initialize_faiss_index(self):
        """Initializes the correct FAISS index based on the chosen similarity metric."""
        if self.similarity_metric == SimilarityMetric.L2:
            self._tool_index = faiss.IndexFlatL2(self.embedding_dimension)
        elif self.similarity_metric in (SimilarityMetric.COSINE, SimilarityMetric.DOT_PRODUCT):
            self._tool_index = faiss.IndexFlatIP(self.embedding_dimension)
        else:
            raise ValueError("Unsupported similarity metric: {}".format(self.similarity_metric))

    def _index_tool(self, tool_name: str):
        """Embeds and indexes a tool's description for semantic search."""
        if not _EMBEDDINGS_ENABLED or self._tool_index is None: return
        metadata = self._tool_metadata.get(tool_name, {})
        description = "Tool: {}. Description: {}".format(tool_name, metadata.get("docstring", ""))
        api_key = self.embedding_config.get("api_key", "")
        vector = gemini_embed(text=description, api_key=api_key)
        if vector:
            vector_np = np.array([vector], dtype=np.float32)
            if self.similarity_metric == SimilarityMetric.COSINE:
                faiss.normalize_L2(vector_np)
            new_index_id = self._tool_index.ntotal
            self._tool_index.add(vector_np)
            self._index_to_tool_name[new_index_id] = tool_name

    def register(
        self,
        required_role: Optional[str] = None,
        retry_attempts: int = 0,
        retry_delay: float = 1.0,
        circuit_breaker_threshold: int = 0,
        cache_ttl_seconds: int = 0,
        cost_per_call: Optional[float] = None,
        cost_calculator: Optional[Callable[[Any], float]] = None,
        output_sinks: Optional[List[BaseOutputSink]] = None
    ) -> Callable:
        """A decorator to register a tool with advanced, governed execution policies."""
        def decorator(func: Callable) -> Callable:
            tool_name = func.__name__
            self._tools[tool_name] = func
            self._tool_metadata[tool_name] = {
                "docstring": inspect.getdoc(func),
                "signature": inspect.signature(func),
                "is_async": inspect.iscoroutinefunction(func),
                "policies": {
                    "role": required_role, "retry_attempts": retry_attempts,
                    "retry_delay": retry_delay, "circuit_breaker_threshold": circuit_breaker_threshold,
                    "cache_ttl_seconds": cache_ttl_seconds, "cost_per_call": cost_per_call,
                    "cost_calculator": cost_calculator, "output_sinks": output_sinks or []
                }
            }
            self._circuit_breaker_state[tool_name] = {'failure_count': 0, 'is_open': False, 'opened_at': 0}
            self._index_tool(tool_name)
            return func
        return decorator

    def _create_cache_key(self, tool_name: str, **kwargs) -> str:
        """Creates a stable cache key from the tool name and arguments."""
        hasher = hashlib.md5()
        encoded = json.dumps(kwargs, sort_keys=True).encode('utf-8')
        hasher.update(encoded)
        return "{}:{}".format(tool_name, hasher.hexdigest())

    def _check_pre_execution_policies(self, name: str, user_id: str, policies: Dict, **kwargs) -> Optional[Any]:
        """Handles caching, circuit breaker, and RBAC checks. Returns cached result if hit."""
        # Caching
        if policies["cache_ttl_seconds"] > 0:
            cache_key = self._create_cache_key(name, **kwargs)
            if cache_key in self._cache:
                cached_item = self._cache[cache_key]
                if time.time() - cached_item["timestamp"] < policies["cache_ttl_seconds"]:
                    self.gov.audit(user_id, "tool_cache_hit", name, {"args": kwargs})
                    return cached_item["result"]

        # Circuit Breaker
        cb_state = self._circuit_breaker_state[name]
        if cb_state['is_open']:
            if time.time() - cb_state['opened_at'] > 60:  # 1-minute cooldown
                cb_state['is_open'] = False
            else:
                msg = "Circuit breaker for tool '{}' is open.".format(name)
                self.gov.audit(user_id, "tool_circuit_breaker_open", name, {"error": msg})
                raise ToolExecutionError(msg)

        # RBAC
        if policies["role"] and not self.access_manager.check_access(user_id, policies["role"]):
            msg = "User '{}' lacks required role '{}' for tool '{}'.".format(user_id, policies["role"], name)
            self.gov.audit(user_id, "tool_access_denied", name, {"required_role": policies["role"]})
            raise RBACError(msg)

        return None

    def _handle_post_execution(self, name: str, user_id: str, policies: Dict, result: Any, latency_ms: float, **kwargs):
        """Handles auditing, cost calculation, caching, and output sinks after successful execution."""
        cost = policies["cost_per_call"]
        if policies["cost_calculator"]:
            cost = policies["cost_calculator"](result)

        audit_metadata = {"result_type": type(result).__name__, "latency_ms": round(latency_ms), "cost": cost}
        self.gov.audit(user_id, "tool_call_end", name, audit_metadata)

        if policies["cache_ttl_seconds"] > 0:
            cache_key = self._create_cache_key(name, **kwargs)
            self._cache[cache_key] = {"timestamp": time.time(), "result": result}

        run_id = self.gov.get_current_run_id()
        for sink in policies["output_sinks"]:
            try:
                sink_metadata = sink.handle(name, result, run_id, **kwargs)
                self.gov.audit(user_id, "output_sink_success", str(sink), {"tool_name": name, **sink_metadata})
            except Exception as e:
                self.gov.audit(user_id, "output_sink_failure", str(sink), {"tool_name": name, "error": str(e)})

    def _handle_execution_error(self, name: str, user_id: str, policies: Dict, e: Exception, attempt: int):
        """Handles failures, including retry logic and circuit breaker trips."""
        self.gov.audit(user_id, "tool_call_error", name, {"error": str(e), "attempt": attempt + 1})
        if attempt >= policies["retry_attempts"]:
            cb_state = self._circuit_breaker_state[name]
            cb_state['failure_count'] += 1
            if policies["circuit_breaker_threshold"] > 0 and cb_state['failure_count'] >= policies["circuit_breaker_threshold"]:
                cb_state['is_open'] = True
                cb_state['opened_at'] = time.time()
                self.gov.audit(user_id, "tool_circuit_breaker_tripped", name)
            raise ToolExecutionError("Tool '{}' failed after all retry attempts.".format(name)) from e

    def _get_governed_sync_tool(self, name: str, user_id: str, original_func: Callable, policies: Dict) -> Callable:
        """Returns the fully governed wrapper for a synchronous tool."""
        def sync_wrapper(**kwargs):
            cached_result = self._check_pre_execution_policies(name, user_id, policies, **kwargs)
            if cached_result is not None: return cached_result

            for attempt in range(policies["retry_attempts"] + 1):
                start_time = time.monotonic()
                try:
                    self.gov.audit(user_id, "tool_call_start", name, {"args": kwargs, "attempt": attempt + 1})
                    result = original_func(**kwargs)
                    latency_ms = (time.monotonic() - start_time) * 1000
                    self._handle_post_execution(name, user_id, policies, result, latency_ms, **kwargs)
                    return result
                except Exception as e:
                    self._handle_execution_error(name, user_id, policies, e, attempt)
                    time.sleep(policies["retry_delay"] * (2 ** attempt))
            # This line should be logically unreachable if retry_attempts >= 0
            raise ToolExecutionError("Tool '{}' execution logic failed unexpectedly.".format(name))
        return sync_wrapper

    def _get_governed_async_tool(self, name: str, user_id: str, original_func: Callable, policies: Dict) -> Callable:
        """Returns the fully governed wrapper for an asynchronous tool."""
        async def async_wrapper(**kwargs):
            cached_result = self._check_pre_execution_policies(name, user_id, policies, **kwargs)
            if cached_result is not None: return cached_result

            for attempt in range(policies["retry_attempts"] + 1):
                start_time = time.monotonic()
                try:
                    self.gov.audit(user_id, "tool_call_start", name, {"args": kwargs, "attempt": attempt + 1})
                    result = await original_func(**kwargs)
                    latency_ms = (time.monotonic() - start_time) * 1000
                    self._handle_post_execution(name, user_id, policies, result, latency_ms, **kwargs)
                    return result
                except Exception as e:
                    self._handle_execution_error(name, user_id, policies, e, attempt)
                    await asyncio.sleep(policies["retry_delay"] * (2 ** attempt))
            # This line should be logically unreachable if retry_attempts >= 0
            raise ToolExecutionError("Tool '{}' execution logic failed unexpectedly.".format(name))
        return async_wrapper

    def get_governed_tool(self, name: str, user_id: str) -> Callable:
        """
        Retrieves a tool by name and wraps it in all registered governance policies.
        This method correctly handles both synchronous and asynchronous tools.
        """
        if name not in self._tools:
            raise ToolNotFoundError("Tool '{}' not found in registry.".format(name))

        metadata = self._tool_metadata[name]
        original_func = self._tools[name]
        policies = metadata["policies"]

        if metadata["is_async"]:
            return self._get_governed_async_tool(name, user_id, original_func, policies)
        else:
            return self._get_governed_sync_tool(name, user_id, original_func, policies)

    def generate_tool_schema(self, tool_names: List[str]) -> List[Dict[str, Any]]:
        """Generates a JSON Schema-like description for a list of tools."""
        schema = []
        for name in tool_names:
            if name in self._tool_metadata:
                metadata = self._tool_metadata[name]
                sig = metadata["signature"]
                properties = {}
                for param in sig.parameters.values():
                    if param.name != 'self':
                        type_map = {str: 'string', int: 'number', float: 'number', bool: 'boolean'}
                        param_type = type_map.get(param.annotation, 'string')
                        properties[param.name] = {'type': param_type, 'description': ''}
                schema.append({
                    "name": name,
                    "description": metadata["docstring"],
                    "parameters": {
                        "type": "object",
                        "properties": properties,
                        "required": [p.name for p in sig.parameters.values() if p.default == inspect.Parameter.empty and p.name != 'self']
                    }
                })
        return schema

    def get_relevant_tools(self, query: str, top_k: int = 3) -> List[str]:
        """Finds the most semantically relevant tools for a given query using a vector index."""
        if not _EMBEDDINGS_ENABLED or self._tool_index is None or self._tool_index.ntotal == 0:
            return []
        api_key = self.embedding_config.get("api_key", "")
        query_vector = gemini_embed(text=query, api_key=api_key)
        if not query_vector:
            return []
        query_np = np.array([query_vector], dtype=np.float32)
        if self.similarity_metric == SimilarityMetric.COSINE:
            faiss.normalize_L2(query_np)
        distances, indices = self._tool_index.search(query_np, min(top_k, self._tool_index.ntotal))
        return [self._index_to_tool_name[i] for i in indices[0]]

generate_tool_schema(tool_names)

Generates a JSON Schema-like description for a list of tools.

Source code in src/safeagent/tool_registry.py
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
def generate_tool_schema(self, tool_names: List[str]) -> List[Dict[str, Any]]:
    """Generates a JSON Schema-like description for a list of tools."""
    schema = []
    for name in tool_names:
        if name in self._tool_metadata:
            metadata = self._tool_metadata[name]
            sig = metadata["signature"]
            properties = {}
            for param in sig.parameters.values():
                if param.name != 'self':
                    type_map = {str: 'string', int: 'number', float: 'number', bool: 'boolean'}
                    param_type = type_map.get(param.annotation, 'string')
                    properties[param.name] = {'type': param_type, 'description': ''}
            schema.append({
                "name": name,
                "description": metadata["docstring"],
                "parameters": {
                    "type": "object",
                    "properties": properties,
                    "required": [p.name for p in sig.parameters.values() if p.default == inspect.Parameter.empty and p.name != 'self']
                }
            })
    return schema

get_governed_tool(name, user_id)

Retrieves a tool by name and wraps it in all registered governance policies. This method correctly handles both synchronous and asynchronous tools.

Source code in src/safeagent/tool_registry.py
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
def get_governed_tool(self, name: str, user_id: str) -> Callable:
    """
    Retrieves a tool by name and wraps it in all registered governance policies.
    This method correctly handles both synchronous and asynchronous tools.
    """
    if name not in self._tools:
        raise ToolNotFoundError("Tool '{}' not found in registry.".format(name))

    metadata = self._tool_metadata[name]
    original_func = self._tools[name]
    policies = metadata["policies"]

    if metadata["is_async"]:
        return self._get_governed_async_tool(name, user_id, original_func, policies)
    else:
        return self._get_governed_sync_tool(name, user_id, original_func, policies)

get_relevant_tools(query, top_k=3)

Finds the most semantically relevant tools for a given query using a vector index.

Source code in src/safeagent/tool_registry.py
332
333
334
335
336
337
338
339
340
341
342
343
344
def get_relevant_tools(self, query: str, top_k: int = 3) -> List[str]:
    """Finds the most semantically relevant tools for a given query using a vector index."""
    if not _EMBEDDINGS_ENABLED or self._tool_index is None or self._tool_index.ntotal == 0:
        return []
    api_key = self.embedding_config.get("api_key", "")
    query_vector = gemini_embed(text=query, api_key=api_key)
    if not query_vector:
        return []
    query_np = np.array([query_vector], dtype=np.float32)
    if self.similarity_metric == SimilarityMetric.COSINE:
        faiss.normalize_L2(query_np)
    distances, indices = self._tool_index.search(query_np, min(top_k, self._tool_index.ntotal))
    return [self._index_to_tool_name[i] for i in indices[0]]

register(required_role=None, retry_attempts=0, retry_delay=1.0, circuit_breaker_threshold=0, cache_ttl_seconds=0, cost_per_call=None, cost_calculator=None, output_sinks=None)

A decorator to register a tool with advanced, governed execution policies.

Source code in src/safeagent/tool_registry.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
def register(
    self,
    required_role: Optional[str] = None,
    retry_attempts: int = 0,
    retry_delay: float = 1.0,
    circuit_breaker_threshold: int = 0,
    cache_ttl_seconds: int = 0,
    cost_per_call: Optional[float] = None,
    cost_calculator: Optional[Callable[[Any], float]] = None,
    output_sinks: Optional[List[BaseOutputSink]] = None
) -> Callable:
    """A decorator to register a tool with advanced, governed execution policies."""
    def decorator(func: Callable) -> Callable:
        tool_name = func.__name__
        self._tools[tool_name] = func
        self._tool_metadata[tool_name] = {
            "docstring": inspect.getdoc(func),
            "signature": inspect.signature(func),
            "is_async": inspect.iscoroutinefunction(func),
            "policies": {
                "role": required_role, "retry_attempts": retry_attempts,
                "retry_delay": retry_delay, "circuit_breaker_threshold": circuit_breaker_threshold,
                "cache_ttl_seconds": cache_ttl_seconds, "cost_per_call": cost_per_call,
                "cost_calculator": cost_calculator, "output_sinks": output_sinks or []
            }
        }
        self._circuit_breaker_state[tool_name] = {'failure_count': 0, 'is_open': False, 'opened_at': 0}
        self._index_tool(tool_name)
        return func
    return decorator

ToolRegistryError

Bases: Exception

Base class for tool registry exceptions.

Source code in src/safeagent/tool_registry.py
83
84
85
class ToolRegistryError(Exception):
    """Base class for tool registry exceptions."""
    pass