In this tutorial, we build a governed AI-agent workflow using Microsoft’s Agent Governance Toolkit as the reference point. We create a Colab-ready implementation where agents do not directly execute tools; instead, every action first passes through a governance layer that checks the agent’s identity, trust score, risk tier, requested tool, action type, sensitivity level, and policy rules. We define a YAML-based policy that controls destructive database operations, external email sending, shell execution, access to sensitive data, and financial transfers. We then wrap each tool with governance logic so that actions can be allowed, denied, sandboxed, or routed through an approval step before execution. We also generate tamper-evident audit records, run policy tests, activate a kill switch, summarize governance decisions, and visualize the relationships between agents, tools, rules, and outcomes as a graph.
Copy CodeCopiedUse a different Browser
import os import sys import json import time import uuid import hmac import yaml import hashlib import random import shutil import subprocess from dataclasses import dataclass, asdict from datetime import datetime, timezone from typing import Any, Dict, List, Callable, Optional def pip_install(*packages): subprocess.run( [sys.executable, "-m", "pip", "install", "-q", *packages], check=False ) pip_install("pyyaml", "pandas", "networkx", "matplotlib", "rich") pip_install("agent-governance-toolkit[full]") from rich.console import Console from rich.table import Table from rich.panel import Panel from rich import box import pandas as pd import networkx as nx import matplotlib.pyplot as plt console = Console() REPO_URL = "https://github.com/microsoft/agent-governance-toolkit" REPO_DIR = "/content/agent-governance-toolkit" if not os.path.exists(REPO_DIR): subprocess.run(["git", "clone", "--depth", "1", REPO_URL, REPO_DIR], check=False) official_govern = None official_import_error = None try: from agentmesh.governance import govern as official_govern except Exception as e: official_import_error = repr(e)
We set up the Colab environment by installing the required libraries and importing everything needed for policy handling, auditing, visualization, and data analysis. We also clone the Microsoft Agent Governance Toolkit repository to keep the notebook connected to the original project. We then try to import the official governance function, while keeping the tutorial runnable even if the preview package changes.
In this tutorial, we build a governed AI-agent workflow using Microsoft’s Agent Governance Toolkit as the reference point. We create a Colab-ready implementation where agents do not directly execute tools; instead, every action first passes through a governance layer that checks the agent’s identity, trust score, risk tier, requested tool, action type, sensitivity level, and policy rules. We define a YAML-based policy that controls destructive database operations, external email sending, shell execution, access to sensitive data, and financial transfers. We then wrap each tool with governance logic so that actions can be allowed, denied, sandboxed, or routed through an approval step before execution. We also generate tamper-evident audit records, run policy tests, activate a kill switch, summarize governance decisions, and visualize the relationships between agents, tools, rules, and outcomes as a graph.
Copy CodeCopiedUse a different Browser
POLICY_PATH = "/content/advanced_agent_policy.yaml" policy_yaml = """ apiVersion: governance.toolkit/v1 name: advanced-colab-governance-policy default_action: allow metadata: owner: ai-platform-team environment: tutorial description: > Demonstrates deterministic governance controls for AI agent tool calls. rules: - name: block-destructive-database-actions description: "Agents must not perform destructive database operations." condition: "action.type in ['drop_table', 'delete_table', 'truncate_table']" action: deny severity: critical owasp_risk: "Tool misuse / Excessive agency" - name: require-human-approval-for-email description: "External email requires approval before execution." condition: "action.type == 'send_email' and action.recipient_domain != 'internal.local'" action: require_approval approvers: ["security-team", "business-owner"] severity: high owasp_risk: "Goal hijacking / Unauthorized action" - name: sandbox-shell-execution description: "Shell commands must run in a sandbox with blocked dangerous commands." condition: "action.type == 'shell_exec'" action: sandbox sandbox: blocked_terms: ["rm -rf", "curl http", "wget http", "chmod 777", "sudo"] max_runtime_seconds: 2 severity: high owasp_risk: "Tool misuse / Unsafe execution" - name: deny-low-trust-agent-sensitive-data description: "Low-trust agents cannot access sensitive data." condition: "identity.trust_score 1000" action: require_approval approvers: ["finance-controller"] severity: critical owasp_risk: "Excessive agency / Business process compromise" - name: rate-limit-high-risk-agent description: "High-risk agents are blocked from repeated autonomous actions." condition: "identity.risk_tier == 'high' and action.autonomous == True" action: deny severity: medium owasp_risk: "Rogue agent / Cascading failure" """ with open(POLICY_PATH, "w") as f: f.write(policy_yaml) with open(POLICY_PATH, "r") as f: policy = yaml.safe_load(f)
We create a YAML governance policy that defines how agent actions should be handled before execution. We add rules to block destructive database actions, require approval for external emails and financial transfers, sandbox shell commands, and restrict low-trust agents from sensitive data. We then save and reload this policy so the rest of the tutorial can use it as the main governance configuration.
We define the core data structures for representing agent identities, governance decisions, and governance-related exceptions. We also create a small dot-access dictionary helper so that policy conditions can read values such as action.type and identity.trust_score. We then build a safe condition evaluator that checks whether each policy rule matches the current agent action.
We implement a tamper-evident audit log that records every governance decision made by the system. We use chained hashes, so each new record depends on the previous record, making changes easier to detect. We also add methods to verify the audit chain and convert the records into a dataframe for later analysis.
We build the main governance engine that compares each agent action against the YAML policy rules. We handle different outcomes such as deny, approval required, sandbox mode, and default allow. We also include a kill switch that immediately blocks all actions when needed.
We define sample tools that represent real agent capabilities, including database access, email sending, shell execution, and money transfer. We then create a governed tool wrapper that ensures every tool call passes through the governance engine first. We ensure denied actions stop immediately, that approval-based actions require a simulated approval, and that only approved or allowed actions reach the actual tool.
We run a set of test scenarios that show how the governed system handles safe actions, risky actions, approval flows, and blocked operations. We display the audit log, run policy tests, activate and deactivate the kill switch, and summarize governance decisions with tables and charts. We also create a governance graph and export the audit logs, policy file, and test results as reusable artifacts.
In conclusion, we have a fully governed-agent workflow that covers both policy enforcement and observability. We simulated multiple agents with varying trust levels. We showed how the same system responds differently depending on the agent’s identity, the action’s sensitivity, and the rules defined in the policy file. Safe actions, such as simple database reads, are executed. In contrast, risky actions, such as destructive database changes, unsafe shell commands, low-trust sensitive access, and large financial transfers, are blocked or sent for approval. We also recorded every decision in an audit log, verified the audit chain, ran policy tests, exported governance artifacts, and created visual summaries that make the system’s behavior easier to review.
Check out the Full Codes here. Also, feel free to follow us on Twitter and don’t forget to join our 150k+ ML SubReddit and Subscribe to our Newsletter. Wait! are you on telegram? now you can join us on telegram as well.
Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.? Connect with us
The post An Implementation of the Microsoft Agent Governance Toolkit for Safe AI Agent Tool Use with Policies, Approvals, Audit Logs, and Risk Controls appeared first on MarkTechPost.
import os import sys import json import time import uuid import hmac import yaml import hashlib import random import shutil import subprocess from dataclasses import dataclass, asdict from datetime import datetime, timezone from typing import Any, Dict, List, Callable, Optional def pip_install(*packages): subprocess.run( [sys.executable, "-m", "pip", "install", "-q", *packages], check=False ) pip_install("pyyaml", "pandas", "networkx", "matplotlib", "rich") pip_install("agent-governance-toolkit[full]") from rich.console import Console from rich.table import Table from rich.panel import Panel from rich import box import pandas as pd import networkx as nx import matplotlib.pyplot as plt console = Console() REPO_URL = "https://github.com/microsoft/agent-governance-toolkit" REPO_DIR = "/content/agent-governance-toolkit" if not os.path.exists(REPO_DIR): subprocess.run(["git", "clone", "--depth", "1", REPO_URL, REPO_DIR], check=False) official_govern = None official_import_error = None try: from agentmesh.governance import govern as official_govern except Exception as e: official_import_error = repr(e)
We set up the Colab environment by installing the required libraries and importing everything needed for policy handling, auditing, visualization, and data analysis. We also clone the Microsoft Agent Governance Toolkit repository to keep the notebook connected to the original project. We then try to import the official governance function, while keeping the tutorial runnable even if the preview package changes.
Copy CodeCopiedUse a different Browser
POLICY_PATH = "/content/advanced_agent_policy.yaml" policy_yaml = """ apiVersion: governance.toolkit/v1 name: advanced-colab-governance-policy default_action: allow metadata: owner: ai-platform-team environment: tutorial description: > Demonstrates deterministic governance controls for AI agent tool calls. rules: - name: block-destructive-database-actions description: "Agents must not perform destructive database operations." condition: "action.type in ['drop_table', 'delete_table', 'truncate_table']" action: deny severity: critical owasp_risk: "Tool misuse / Excessive agency" - name: require-human-approval-for-email description: "External email requires approval before execution." condition: "action.type == 'send_email' and action.recipient_domain != 'internal.local'" action: require_approval approvers: ["security-team", "business-owner"] severity: high owasp_risk: "Goal hijacking / Unauthorized action" - name: sandbox-shell-execution description: "Shell commands must run in a sandbox with blocked dangerous commands." condition: "action.type == 'shell_exec'" action: sandbox sandbox: blocked_terms: ["rm -rf", "curl http", "wget http", "chmod 777", "sudo"] max_runtime_seconds: 2 severity: high owasp_risk: "Tool misuse / Unsafe execution" - name: deny-low-trust-agent-sensitive-data description: "Low-trust agents cannot access sensitive data." condition: "identity.trust_score 1000" action: require_approval approvers: ["finance-controller"] severity: critical owasp_risk: "Excessive agency / Business process compromise" - name: rate-limit-high-risk-agent description: "High-risk agents are blocked from repeated autonomous actions." condition: "identity.risk_tier == 'high' and action.autonomous == True" action: deny severity: medium owasp_risk: "Rogue agent / Cascading failure" """ with open(POLICY_PATH, "w") as f: f.write(policy_yaml) with open(POLICY_PATH, "r") as f: policy = yaml.safe_load(f)
We create a YAML governance policy that defines how agent actions should be handled before execution. We add rules to block destructive database actions, require approval for external emails and financial transfers, sandbox shell commands, and restrict low-trust agents from sensitive data. We then save and reload this policy so the rest of the tutorial can use it as the main governance configuration.
We define the core data structures for representing agent identities, governance decisions, and governance-related exceptions. We also create a small dot-access dictionary helper so that policy conditions can read values such as action.type and identity.trust_score. We then build a safe condition evaluator that checks whether each policy rule matches the current agent action.
We implement a tamper-evident audit log that records every governance decision made by the system. We use chained hashes, so each new record depends on the previous record, making changes easier to detect. We also add methods to verify the audit chain and convert the records into a dataframe for later analysis.
We build the main governance engine that compares each agent action against the YAML policy rules. We handle different outcomes such as deny, approval required, sandbox mode, and default allow. We also include a kill switch that immediately blocks all actions when needed.
We define sample tools that represent real agent capabilities, including database access, email sending, shell execution, and money transfer. We then create a governed tool wrapper that ensures every tool call passes through the governance engine first. We ensure denied actions stop immediately, that approval-based actions require a simulated approval, and that only approved or allowed actions reach the actual tool.
We run a set of test scenarios that show how the governed system handles safe actions, risky actions, approval flows, and blocked operations. We display the audit log, run policy tests, activate and deactivate the kill switch, and summarize governance decisions with tables and charts. We also create a governance graph and export the audit logs, policy file, and test results as reusable artifacts.
In conclusion, we have a fully governed-agent workflow that covers both policy enforcement and observability. We simulated multiple agents with varying trust levels. We showed how the same system responds differently depending on the agent’s identity, the action’s sensitivity, and the rules defined in the policy file. Safe actions, such as simple database reads, are executed. In contrast, risky actions, such as destructive database changes, unsafe shell commands, low-trust sensitive access, and large financial transfers, are blocked or sent for approval. We also recorded every decision in an audit log, verified the audit chain, ran policy tests, exported governance artifacts, and created visual summaries that make the system’s behavior easier to review.
Check out the Full Codes here. Also, feel free to follow us on Twitter and don’t forget to join our 150k+ ML SubReddit and Subscribe to our Newsletter. Wait! are you on telegram? now you can join us on telegram as well.
Need to partner with us for promoting your GitHub Repo OR Hugging Face Page OR Product Release OR Webinar etc.? Connect with us
The post An Implementation of the Microsoft Agent Governance Toolkit for Safe AI Agent Tool Use with Policies, Approvals, Audit Logs, and Risk Controls appeared first on MarkTechPost.