concept

Tool requests and argument validation

Local strict argument validation and scoped read-only dispatch. JSON Schema is not authorization.

Formal Syntax & Structure

artificial-intelligence syntaxartificial-intelligence
validate(name, raw); dispatch(name, args, allowed_items)

Working Example

Example Snippetartificial-intelligence
import json
def unique_object(pairs):    obj = {}    for key, value in pairs:        if key in obj:            raise ValueError("duplicate key")        obj[key] = value    return obj
def validate(name, raw):    if name not in ("stock", "add"):        return None    if not isinstance(raw, str) or len(raw) > 256:        return None    try:        args = json.loads(raw, object_pairs_hook=unique_object)    except (ValueError, RecursionError):        return None    if not isinstance(args, dict):        return None    if name == "stock" and set(args) == {"item"}:        if type(args["item"]) is str and args["item"] in ("A", "B"):            return args    if name == "add" and set(args) == {"a", "b"}:        if all(type(v) is int and -100 <= v <= 100 for v in args.values()):            return args    return None
print(validate("stock", '{"item":"A"}'))print(validate("stock", '{"item":"A","admin":true}'))print(validate("add", '{"a":true,"b":2}'))print(validate("add", '{"a":3,"b":2}'))