Skip to main content

Lesson 3 of 16

Security and Tool Permissions

Tool permissions link each tool call to a required permission. Before executing any tool, the system checks that the calling agent has the appropriate permission.

6 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Tool-Permission Mapping

Tool-Permission Mapping
# Map every tool to its required permission
TOOL_PERMISSIONS = {
    # Read tools
    "web_search":       "search_web",
    "get_order":        "read_orders",
    "list_orders":      "read_orders",
    "get_customer":     "read_customers",
    "get_product":      "read_products",

    # Write tools
    "update_order":     "update_order",
    "create_order":     "create_order",

    # Sensitive tools
    "issue_refund":     "issue_refund",
    "send_email":       "send_email",
    "delete_record":    "delete_record",
    "get_financials":   "access_finance",
}

class SecureToolRunner:
    def __init__(self, tools: dict, agent_permissions: set):
        self.tools = tools
        self.permissions = agent_permissions
        self.audit_log = []

    def execute(self, tool_name: str, agent_role: str, **kwargs) -> dict:
        # 1. Check tool exists
        if tool_name not in self.tools:
            return {"status": "error", "error": f"Tool '{tool_name}' not found"}

        # 2. Check permission
        required = TOOL_PERMISSIONS.get(tool_name)
        if required and required not in self.permissions:
            entry = {"tool": tool_name, "agent": agent_role,
                     "action": "DENIED", "reason": f"Missing permission: {required}"}
            self.audit_log.append(entry)
            return {"status": "error", "error_type": "permission",
                    "error": f"Permission denied: requires '{required}'"}

        # 3. Execute with audit logging
        try:
            result = self.tools[tool_name](**kwargs)
            self.audit_log.append({"tool": tool_name, "agent": agent_role,
                                   "action": "EXECUTED", "status": "success"})
            return result
        except Exception as e:
            return {"status": "error", "error": str(e)}

# Test
runner = SecureToolRunner(
    tools={"web_search": lambda query: {"results": ["r1"]},
           "issue_refund": lambda order_id, amount: {"refund_id": "R1"}},
    agent_permissions={"search_web"}  # Only search, not refund
)

print(runner.execute("web_search",   "research_agent", query="Python AI"))
print(runner.execute("issue_refund", "research_agent", order_id="O1", amount=50))

Finished this lesson?

0 of 16 in Module 10
Sign in to save progress

Topics in This Module

0/16 done