Repository navigation
Expand file tree
/
Copy pathtool_hooks.py
More file actions
461 lines (393 loc) · 16.5 KB
/
Copy pathtool_hooks.py
File metadata and controls
461 lines (393 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# pytest: ollama, e2e
#
# Tool hook plugins — safety and security policies for tool invocation.
#
# This example demonstrates four enforcement / repair patterns using
# TOOL_PRE_INVOKE and TOOL_POST_INVOKE hooks, built on top of the @tool
# decorator examples:
#
# 1. Tool allow list — blocks any tool not on an explicit approved list
# 2. Argument validator — inspects args before invocation (e.g., blocks
# disallowed patterns in calculator expressions)
# 3. Tool audit logger — fire-and-forget logging of every tool call
# 4. Arg sanitizer — auto-fixes tool args before invocation instead of
# blocking (e.g., strips unsafe chars from calculator
# expressions and normalises location strings)
#
# Run:
# uv run python docs/examples/plugins/tool_hooks.py
import dataclasses
import logging
from mellea import start_session
from mellea.backends import ModelOption, tool
from mellea.plugins import (
HookType,
PluginMode,
PluginResult,
PluginSet,
PluginViolationError,
block,
hook,
)
from mellea.stdlib.functional import call_tools
from mellea.stdlib.requirements import uses_tool
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%dT%H:%M:%S",
)
logging.getLogger("httpx").setLevel(logging.ERROR)
logging.getLogger("mellea").setLevel(logging.ERROR)
log = logging.getLogger("tool_hooks")
# ---------------------------------------------------------------------------
# Tools (same as tool_decorator_example.py)
# ---------------------------------------------------------------------------
@tool
def get_weather(location: str, days: int = 1) -> dict:
"""Get weather forecast for a location.
Args:
location: City name
days: Number of days to forecast (default: 1)
"""
return {
"location": location,
"days": str(days),
"forecast": "sunny",
"temperature": "72",
}
@tool
def search_web(query: str, max_results: int = 5) -> list[str]:
"""Search the web for information.
Args:
query: Search query
max_results: Maximum number of results to return
"""
return [f"Result {i + 1} for '{query}'" for i in range(max_results)]
@tool(name="calculator")
def calculate(expression: str) -> str:
"""Evaluate a mathematical expression.
Args:
expression: Mathematical expression to evaluate (e.g., "2 + 2 * 3")
"""
# Only digits, whitespace, and basic arithmetic operators are permitted.
# This is enforced upstream by validate_tool_args, but the function
# applies its own check as a defence-in-depth measure.
allowed = set("0123456789 +-*/(). ")
if not set(expression).issubset(allowed):
return "Error: expression contains disallowed characters"
try:
# Safe: only reaches here when characters are in the allowed set
result = _safe_calc(expression)
return f"Result: {result}"
except Exception as e:
return f"Error: {e!s}"
def _safe_calc(expression: str) -> float:
"""Evaluate a restricted arithmetic expression (no builtins, no names)."""
import operator as op
import re
tokens = re.findall(r"[\d.]+|[+\-*/()]", expression)
# Build a simple recursive-descent expression for +, -, *, /, ()
pos = [0]
def parse_expr():
left = parse_term()
while pos[0] < len(tokens) and tokens[pos[0]] in ("+", "-"):
tok = tokens[pos[0]]
pos[0] += 1
right = parse_term()
left = op.add(left, right) if tok == "+" else op.sub(left, right)
return left
def parse_term():
left = parse_factor()
while pos[0] < len(tokens) and tokens[pos[0]] in ("*", "/"):
tok = tokens[pos[0]]
pos[0] += 1
right = parse_factor()
left = op.mul(left, right) if tok == "*" else op.truediv(left, right)
return left
def parse_factor():
tok = tokens[pos[0]]
if tok == "(":
pos[0] += 1
val = parse_expr()
pos[0] += 1 # consume ")"
return val
pos[0] += 1
return float(tok)
return parse_expr()
# ---------------------------------------------------------------------------
# Plugin 1 — Tool allow list (enforce)
#
# Only tools explicitly listed in ALLOWED_TOOLS may be called. Any tool call
# for an unlisted tool is blocked before it reaches the function.
# ---------------------------------------------------------------------------
ALLOWED_TOOLS: frozenset[str] = frozenset({"get_weather", "calculator"})
@hook(HookType.TOOL_PRE_INVOKE, mode=PluginMode.CONCURRENT, priority=5)
async def enforce_tool_allowlist(payload, _):
"""Block any tool not on the explicit allow list."""
if payload.is_control_flow:
return # framework control-flow tools (e.g. final_answer) are exempt
tool_name = payload.model_tool_call.name
if tool_name not in ALLOWED_TOOLS:
log.warning(
"[allowlist] BLOCKED tool=%r — not in allowed set %s",
tool_name,
sorted(ALLOWED_TOOLS),
)
return block(
f"Tool '{tool_name}' is not permitted",
code="TOOL_NOT_ALLOWED",
details={"tool": tool_name, "allowed": sorted(ALLOWED_TOOLS)},
)
log.info("[allowlist] permitted tool=%r", tool_name)
# ---------------------------------------------------------------------------
# Plugin 2 — Argument validator (enforce)
#
# Inspects the arguments before a tool is invoked. For the calculator,
# reject expressions that contain characters outside the safe set.
# This runs after the allow list so it only sees permitted tools.
# ---------------------------------------------------------------------------
_CALCULATOR_ALLOWED_CHARS: frozenset[str] = frozenset("0123456789 +-*/(). ")
@hook(HookType.TOOL_PRE_INVOKE, mode=PluginMode.CONCURRENT, priority=10)
async def validate_tool_args(payload, _):
"""Validate tool arguments before invocation."""
tool_name = payload.model_tool_call.name
tool_args = payload.model_tool_call.args or {}
if tool_name == "calculator":
expression = tool_args.get("expression", "")
disallowed = set(expression) - _CALCULATOR_ALLOWED_CHARS
if disallowed:
log.warning(
"[arg-validator] BLOCKED calculator expression=%r (disallowed chars: %s)",
expression,
disallowed,
)
return block(
f"Calculator expression contains disallowed characters: {disallowed}",
code="UNSAFE_EXPRESSION",
details={"expression": expression, "disallowed": sorted(disallowed)},
)
log.info("[arg-validator] calculator expression=%r is safe", expression)
else:
log.info("[arg-validator] no arg validation required for tool=%r", tool_name)
# ---------------------------------------------------------------------------
# Plugin 3 — Tool audit logger (fire-and-forget)
#
# Records every tool invocation outcome for audit purposes. Uses
# fire_and_forget so it never adds latency to the main execution path.
# ---------------------------------------------------------------------------
@hook(HookType.TOOL_POST_INVOKE, mode=PluginMode.FIRE_AND_FORGET)
async def audit_tool_calls(payload, _):
"""Log the result of every tool call for audit purposes."""
status = "OK" if payload.success else "ERROR"
tool_name = payload.model_tool_call.name
log.info(
"[audit] tool=%r status=%s latency=%dms args=%s",
tool_name,
status,
payload.execution_time_ms,
payload.model_tool_call.args,
)
if not payload.success and payload.error is not None:
log.error("[audit] tool=%r error=%r", tool_name, str(payload.error))
# ---------------------------------------------------------------------------
# Plugin 4 — Arg sanitizer (repair)
#
# Instead of blocking, this plugin auto-fixes tool arguments before
# invocation. Two repairs are applied:
#
# calculator — strips any character outside the safe arithmetic set so
# that the expression can still be evaluated. A warning is
# logged showing what was removed.
#
# get_weather — normalises the location string to title-case and strips
# leading/trailing whitespace (e.g. " NEW YORK " → "New York").
#
# The plugin returns a modified ModelToolCall via model_copy so that the
# corrected args are what actually reaches the tool function.
# ---------------------------------------------------------------------------
@hook(HookType.TOOL_PRE_INVOKE, mode=PluginMode.CONCURRENT, priority=15)
async def sanitize_tool_args(payload, _) -> PluginResult:
"""Auto-fix tool arguments rather than blocking on unsafe input."""
mtc = payload.model_tool_call
tool_name = mtc.name
args = dict(mtc.args or {})
updated: dict[str, object] = {}
if tool_name == "calculator":
raw_expr = str(args.get("expression", ""))
sanitized = "".join(c for c in raw_expr if c in _CALCULATOR_ALLOWED_CHARS)
if sanitized != raw_expr:
removed = set(raw_expr) - _CALCULATOR_ALLOWED_CHARS
log.warning(
"[sanitizer] calculator: stripped disallowed chars %s from expression=%r → %r",
sorted(removed),
raw_expr,
sanitized,
)
updated["expression"] = sanitized
elif tool_name == "get_weather":
raw_location = str(args.get("location", ""))
normalised = raw_location.strip().title()
if normalised != raw_location:
log.info(
"[sanitizer] get_weather: normalised location %r → %r",
raw_location,
normalised,
)
updated["location"] = normalised
if not updated:
return None # nothing changed — pass through as-is
new_args = {**args, **updated}
new_call = dataclasses.replace(mtc, args=new_args)
modified = payload.model_copy(update={"model_tool_call": new_call})
return PluginResult(continue_processing=True, modified_payload=modified)
# ---------------------------------------------------------------------------
# Compose into PluginSets for clean session-scoped registration
# ---------------------------------------------------------------------------
tool_security = PluginSet(
"tool-security", [enforce_tool_allowlist, validate_tool_args, audit_tool_calls]
)
tool_sanitizer = PluginSet("tool-sanitizer", [sanitize_tool_args, audit_tool_calls])
# ---------------------------------------------------------------------------
# Scenarios
# ---------------------------------------------------------------------------
def _run_scenario(name: str, fn) -> None:
"""Run a scenario function, logging any PluginViolationError without halting."""
log.info("=== %s ===", name)
try:
fn()
except PluginViolationError as e:
log.warning(
"Execution blocked on %s: [%s] %s (plugin=%s)",
e.hook_type,
e.code,
e.reason,
e.plugin_name,
)
log.info("")
def scenario_1_allowed_tool(all_tools):
"""Scenario 1: allowed tool call (get_weather)."""
with start_session(plugins=[tool_security]) as m:
result = m.instruct(
description="What is the weather in Boston for the next 3 days?",
requirements=[uses_tool("get_weather")],
model_options={ModelOption.TOOLS: all_tools},
tool_calls=True,
)
tool_outputs = call_tools(result, m.backend)
if tool_outputs:
log.info("Tool returned: %s", tool_outputs[0].content)
else:
log.error("Expected tool call but none were executed")
def scenario_2_blocked_tool(all_tools):
"""Scenario 2: blocked tool call (search_web not on allow list)."""
with start_session(plugins=[tool_security]) as m:
result = m.instruct(
description="Search the web for the latest Python news.",
requirements=[uses_tool("search_web")],
model_options={ModelOption.TOOLS: all_tools},
tool_calls=True,
)
tool_outputs = call_tools(result, m.backend)
if not tool_outputs:
log.info("Tool call was blocked — outputs list is empty, as expected")
else:
log.warning("Expected tool to be blocked but it executed: %s", tool_outputs)
def scenario_3_safe_calculator(all_tools):
"""Scenario 3: safe calculator expression goes through."""
with start_session(plugins=[tool_security]) as m:
result = m.instruct(
description="Use the calculator to compute 6 * 7.",
requirements=[uses_tool("calculator")],
model_options={ModelOption.TOOLS: all_tools},
tool_calls=True,
)
tool_outputs = call_tools(result, m.backend)
if tool_outputs:
log.info("Tool returned: %s", tool_outputs[0].content)
else:
log.error("Expected tool call but none were executed")
def scenario_4_blocked_calculator(all_tools):
"""Scenario 4: unsafe calculator expression is blocked."""
with start_session(plugins=[tool_security]) as m:
result = m.instruct(
description=(
"Use the calculator on this expression: "
"__builtins__['print']('injected')"
),
requirements=[uses_tool("calculator")],
model_options={ModelOption.TOOLS: all_tools},
tool_calls=True,
)
tool_outputs = call_tools(result, m.backend)
if not tool_outputs:
log.info("Tool call was blocked — outputs list is empty, as expected")
else:
log.warning("Expected tool to be blocked but it executed: %s", tool_outputs)
def scenario_5_sanitizer_calculator(all_tools):
"""Scenario 5: arg sanitizer auto-fixes an unsafe calculator expression."""
with start_session(plugins=[tool_sanitizer]) as m:
result = m.instruct(
description=(
"Use the calculator on this expression: "
"6 * 7 + __import__('os').getpid()"
),
requirements=[uses_tool("calculator")],
model_options={ModelOption.TOOLS: all_tools},
tool_calls=True,
)
tool_outputs = call_tools(result, m.backend)
if tool_outputs:
log.info(
"Sanitized expression evaluated — tool returned: %s",
tool_outputs[0].content,
)
else:
log.error("Expected sanitized tool call but none were executed")
def scenario_6_sanitizer_location(all_tools):
"""Scenario 6: arg sanitizer normalises a messy location string."""
with start_session(plugins=[tool_sanitizer]) as m:
result = m.instruct(
description="What is the weather in ' NEW YORK '?",
requirements=[uses_tool("get_weather")],
model_options={ModelOption.TOOLS: all_tools},
tool_calls=True,
)
tool_outputs = call_tools(result, m.backend)
if tool_outputs:
log.info(
"Weather fetched with normalised location — tool returned: %s",
tool_outputs[0].content,
)
else:
log.error("Expected tool call but none were executed")
# ---------------------------------------------------------------------------
# Main — six scenarios
# ---------------------------------------------------------------------------
if __name__ == "__main__":
log.info("--- Tool hook plugins example ---")
log.info("")
all_tools = [get_weather, search_web, calculate]
_run_scenario(
"Scenario 1: allowed tool — get_weather",
lambda: scenario_1_allowed_tool(all_tools),
)
_run_scenario(
"Scenario 2: blocked tool — search_web not on allow list",
lambda: scenario_2_blocked_tool(all_tools),
)
_run_scenario(
"Scenario 3: safe calculator expression",
lambda: scenario_3_safe_calculator(all_tools),
)
_run_scenario(
"Scenario 4: unsafe calculator expression blocked",
lambda: scenario_4_blocked_calculator(all_tools),
)
_run_scenario(
"Scenario 5: arg sanitizer auto-fixes calculator expression",
lambda: scenario_5_sanitizer_calculator(all_tools),
)
_run_scenario(
"Scenario 6: arg sanitizer normalises location in get_weather",
lambda: scenario_6_sanitizer_location(all_tools),
)