用 Enforra 为 AI Agent 建立工具调用安全门控——从支持退款到编码安全
场景:你的 AI 支持 Agent 可以退款
假设你部署了一个 AI 支持 Agent,它有一条工具叫 stripe.refund。你在系统提示词里写了「只退款金额不超过 50 美元」。但系统提示词不是安全边界——它只是一个建议。如果 Agent 被 prompt injection 绕过、或者模型幻觉导致它决定给客户退款 1000 美元,你的业务规则就被绕过了。
Enforra 就是来解决这个问题的:它在工具回调执行之前设置一个策略检查层,返回 allow(允许)、block(阻止)、require_approval(需要审批)或 log_only(仅记录)四种决策之一。
Enforra 的核心设计
Enforra(Apache-2.0, 9⭐)是一个开源的动作治理 SDK。它的核心理念很简单:在执行工具回调之前,先跑一遍本地策略文件。
Agent 请求 → Enforra 策略评估 → 决策 → 仅当 allow 或 log_only 时执行回调
关键区别:它不是 MCP 代理、不是身份提供商、不是模型防火墙——它只做一件事:在你应用的工具回调执行之前,根据 YAML 策略做出允许/阻止/审批/记录的决策。全部在本地运行,不需要云服务。
快速上手:Node.js SDK
安装只需一行:
npm install @enforra/sdk-node
然后在你的 Agent 工具调用中包裹 Enforra:
import { createEnforraClient } from "@enforra/sdk-node";
const enforra = await createEnforraClient({
policyPath: "./policies/support-agent.yaml",
auditPath: ".enforra/audit.jsonl"
});
const result = await enforra.enforceToolCall({
agent: "support-agent",
tool: "stripe.refund",
args: { customerId: "cus_123", amount: 250 },
context: { environment: "production" },
execute: async () => {
return { refundId: "ref_123", status: "created" };
}
});
console.log(result.decision); // "require_approval"
注意 execute 回调:只有策略返回 allow 或 log_only 时才会执行。如果策略决定 block 或 require_approval,回调根本不会触发。
编写你的第一条策略
策略文件是 YAML 格式,放在项目中:
version: 1
defaults:
decision: block
policies:
- id: allow-small-refunds
match:
agent: support-agent
tool: stripe.refund
conditions:
- field: args.amount
operator: lte
value: 50
decision: allow
- id: approve-medium-refunds
match:
agent: support-agent
tool: stripe.refund
conditions:
- field: args.amount
operator: gt
value: 50
- field: args.amount
operator: lte
value: 500
decision: require_approval
三行规则覆盖了零风险小额退款(自动允许)、中风险退款(人工审批)和大额退款(直接阻止)。
Coding Agent 的安全策略
对于编码 Agent 的场景,你可以用策略来控制 Agent 能访问什么文件、能运行什么命令:
version: 1
defaults:
decision: block
policies:
- id: block-env-file-access
match:
tool: filesystem.read
conditions:
- field: args.path
operator: contains
value: ".env"
decision: block
- id: approve-package-install
match:
tool: terminal.run
conditions:
- field: args.command
operator: contains
value: "npm install"
decision: require_approval
这意味着即使用户让 Agent「帮我看看配置文件」,.env 文件也被严格保护。而任何 npm install 命令都会被标记为需要人工审批——防止 Agent 在 CI 或生产环境中安装意外依赖。
观察者模式:先看看再执行
你不想在生产环境中断已有流程?用 Observe Mode(观察者模式):
version: 1
mode: observe
defaults:
decision: block
policies:
- id: block-large-refunds
match:
tool: stripe.refund
conditions:
- field: args.amount
operator: gt
value: 500
decision: block
在观察模式下,Enforra 会按策略评估每次工具调用并记录决策,但从不阻止执行。审计日志中会标记 enforcement_mode: "observe",让你看到「如果启用这条策略,会阻止哪些操作」,而不会影响现有流程。
这是逐步推行的关键功能——先在观察模式下跑几天,确认策略不会误杀合法请求,再切换到强制执行模式。
审计日志:自动记录每次决策
Enforra 自动在 .enforra/audit.jsonl 中写入结构化的审计日志:
{"agent":"support-agent","tool":"stripe.refund","decision":"require_approval","reason":"matched policy approve-medium-refunds","timestamp":"2026-06-03T15:00:00Z"}
{"agent":"support-agent","tool":"stripe.refund","decision":"allow","reason":"matched policy allow-small-refunds","timestamp":"2026-06-03T15:01:00Z"}
{"agent":"support-agent","tool":"stripe.refund","decision":"block","reason":"matched policy block-large-refunds","timestamp":"2026-06-03T15:02:00Z"}
参数会在写入前被递归脱敏(不会记录 args.apiKey 等字段)。可选开启 hash-chain 完整性校验,检测日志是否被篡改或删除。
多框架集成
Enforra 提供官方集成示例,覆盖主流框架:
- LangGraph(Python):在 LangGraph 的 tool node 前包裹 Enforra
- OpenAI Agents SDK(Python):在 Agent 的 tool 回调中集成
- Vercel AI SDK(Node.js):用
@enforra/mcp保护 MCP 工具处理函数 - CrewAI(Python):通过 tool wrapper 模式插入策略检查
- AutoGen(Python):同样的 wrapper 模式
现实场景:热更新策略
策略文件的另一个好处是可以热更新——你不需要重启 Agent 或重新部署。修改 YAML 文件后,下一次工具调用就会应用新规则。这在安全事件响应中非常有用:
echo ' - id: block-s3-bucket-list
match:
tool: aws.s3.listBuckets
decision: block' >> policies/coding-agent.yaml
Enforra 的局限
- 不是模型防火墙:它不能检测 prompt injection 或模型输出安全,只控制工具调用
- 不是 MCP 代理:不管理 MCP 客户端-服务端的传输或认证
- 需要手动集成:它不自动注入到所有 Agent 调用中,你需要在自己代码中包裹需要保护的工具
- 决定是同步的:高并发场景下需要考虑策略评估的性能(内置 benchmark 供参考)
总结
Enforra 填补了一个被忽视的空白:AI Agent 的工具调用缺乏运行时安全边界。它在代码层面提供了一个轻量级的策略执行层——不是依赖 LLM 去「记住规则」,而是在工具实际执行前做一次确定性的、可审计的策略检查。
对于生产环境的 AI Agent 应用,特别是涉及支付、数据访问、文件系统操作的场景,Enforra 是一个值得加入的工具链组件。最吸引人的是策略文件的声明式管理和观察者模式——让你可以逐步、安全地把策略推行到生产环境中。
项目地址:github.com/enforra/enforra 许可证:Apache-2.0 | 语言:TypeScript(Node.js + Python SDK)