核心概念:在 OpenClaw 中,插件不仅是功能的扩展,更是智能体 (Agent) 的手脚。通过注册 Agent Tools,插件可以将具体的函数能力(如查询数据库、执行工作流、调用外部 API)以标准化的 JSON Schema 形式暴露给大语言模型 (LLM),让 AI 能够自主规划并执行复杂任务。 适用场景:
在 OpenClaw 架构中,Agent Tool 是一个遵循特定规范的函数定义。它包含三个核心要素:
@sinclair/typebox 定义) 严格规定输入格式,确保 LLM 生成的参数合法。工具分为两类:
这是最常见的形式。插件加载后,该工具自动对拥有插件权限的 Agent 可用。
依赖准备:
你需要使用 @sinclair/typebox 来定义参数结构,这比手写 JSON Schema 更安全且具备类型提示。
import { Type } from "@sinclair/typebox";
// 插件入口函数
export default function (api) {
api.registerTool({
// 1. 工具名称:全局唯一,避免与核心工具冲突
name: "fetch_weather",
// 2. 描述:LLM 根据此描述决定是否调用
description: "Get real-time weather information for a specific city.",
// 3. 参数定义:使用 Typebox 构建 JSON Schema
parameters: Type.Object({
city: Type.String({ description: "The name of the city, e.g., 'Beijing'" }),
unit: Type.Optional(Type.Enum({ celsius: "celsius", fahrenheit: "fahrenheit" }, { default: "celsius" }))
}),
// 4. 执行逻辑:异步函数,接收调用 ID 和解析后的参数
async execute(_id, params) {
// 模拟 API 调用
const weatherData = await mockWeatherApi(params.city, params.unit);
// 返回标准格式响应
return {
content: [
{ type: "text", text: `🌤️ ${params.city} current weather: ${weatherData.condition}, ${weatherData.temp}°${params.unit}` }
]
};
},
});
}对于可能产生副作用(如删除文件、发送邮件、执行 Shell 命令)的工具,必须标记为 optional: true。这是一种安全机制,防止用户意外安装插件后,AI 自动执行危险操作。
export default function (api) {
api.registerTool(
{
name: "execute_workflow",
description: "Trigger a local automation workflow by name. Requires explicit permission.",
// 这里也可以直接使用原生 JSON Schema 对象,不一定非要用 Typebox
parameters: {
type: "object",
properties: {
pipeline: {
type: "string",
description: "The name of the workflow pipeline to run"
},
dryRun: {
type: "boolean",
description: "If true, only simulate the execution",
default: false
}
},
required: ["pipeline"],
},
async execute(_id, params) {
if (params.dryRun) {
return { content: [{ type: "text", text: `[Simulated] Would run pipeline: ${params.pipeline}` }] };
}
// 执行真实逻辑...
return { content: [{ type: "text", text: `✅ Pipeline ${params.pipeline} executed successfully.` }] };
},
},
// ⚠️ 关键配置:标记为可选
{ optional: true },
);
}可选工具不会自动生效。你需要在 openclaw.json (或环境变量对应的配置) 中通过 Allowlist (白名单) 机制显式授权。
工具权限可以在两个层级配置:
tools):影响所有 Agent。agents.list[].tools):仅影响特定 Agent,优先级更高。在配置文件中,allow 数组支持三种引用方式:
{
"agents": {
"list": [
{
"id": "admin-agent",
"tools": {
"allow": [
// 方式 A: 指定具体工具名 (最精确)
"execute_workflow",
// 方式 B: 指定插件 ID (启用该插件下所有工具,包括可选的)
"my-automation-plugin",
// 方式 C: 使用组别名 (启用所有插件提供的工具)
"group:plugins"
],
// 可选:拒绝列表,优先级高于 allow
"deny": [
"dangerous_delete_tool"
]
}
},
{
"id": "readonly-agent",
"tools": {
// 只允许核心工具,不允许任何插件的可选工具
"allow": [
"group:core"
]
}
}
]
}
}allow 列表中写入的是插件 ID(如 my-automation-plugin),系统会将其视为对该插件所有可选工具的授权。allow 列表中只包含了插件相关的条目(插件 ID 或 group:plugins),那么核心工具 (Core Tools) 将默认被禁用。allow: ["group:plugins", "group:core"]
除了基础的 allow/deny,OpenClaw 还支持更细粒度的控制:
配置项 | 位置 | 作用 |
|---|---|---|
tools.profile | 全局 / Agent | 定义一个基础白名单模板,可被复用。 |
tools.byProvider | 全局 / Agent | 针对不同 LLM 提供商 (如 OpenAI vs Anthropic) 设置不同的工具集(因为某些模型对工具调用支持不同)。 |
tools.sandbox.tools.* | 全局 | 当 Agent 运行在沙箱环境时,限制可用的底层系统工具。 |
{ optional: true }。description 字段来决定是否调用工具。Typebox 或 JSON Schema 的 required 和 format 字段进行强校验。execute 函数内部,再次验证关键参数(防御性编程),防止 Schema 绕过。github_create_issue 而不是 create_issue,以减少与未来核心工具或其他插件冲突的概率。问题现象 | 可能原因 | 解决方案 |
|---|---|---|
LLM 看不到我的工具 | 1. 工具标记为 optional 但未配置白名单。2. 插件未正确加载。 | 检查 agents.list[].tools.allow 是否包含工具名或插件 ID。查看启动日志确认插件加载成功。 |
核心工具消失了 | allow 列表中只写了插件相关项,未包含 group:core。 | 在 allow 数组中添加 "group:core" 或具体的核心工具名。 |
工具调用报错 "Unknown Tool" | 工具名在配置中拼写错误,或与核心工具冲突被跳过。 | 运行 openclaw plugins info <plugin-name> 查看已注册的工具列表,核对名称。 |
参数解析失败 | LLM 生成的参数不符合 Schema 定义。 | 优化 description 引导 LLM;检查 parameters 定义是否过于严格或缺少默认值。 |
OpenClaw 的插件工具系统设计哲学是 “灵活扩展,安全可控”。
optional: true 保护用户,提供精准的 Schema 和描述。allow 列表的威力,按需授权,构建安全的 Agent 工作流。通过这套机制,你可以将 OpenClaw 从一个简单的聊天机器人,进化为能够安全执行复杂业务逻辑的超级自动化平台。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。