OpenClaw之所以能够轻松顺利地完成一些复杂任务,除了有合理且优秀的提示词外,还有另外一大功臣:丰富的工具箱。
一、工具总数
OpenClaw 共有 40+ 个工具,分为以下几个类别:
| | |
| 核心工具 | | read, write, edit, exec, process |
| 会话管理 | | sessions_list, sessions_history, sessions_send, sessions_spawn, sessions_yield, session_status, subagents |
| 网络工具 | | web_search, web_fetch, browser, tavily |
| OpenClaw 核心 | | gateway, agents_list, cron, nodes, canvas, tts, message 等 |
| 媒体工具 | | image, image_generate, pdf |
| 沙盒工具 | | sandboxed_read, sandboxed_write, sandboxed_edit |
| 其他 | | apply_patch, host_workspace_edit, host_workspace_write,memory_search,memory_get |
二、工具定义位置
外部依赖
import { codingTools, createReadTool, createWriteTool, createEditTool } from"@mariozechner/pi-coding-agent";
说明:基础工具(read, write, edit)来自外部库 @mariozechner/pi-coding-agent
三、工具定义结构
通用模式
functioncreateXxxTool(options) {return {name: "xxx", // 工具名称description: "...", // 工具描述parameters: { // 参数定义(JSON Schema)type: "object",properties: {...},required: [...] },execute: async (toolCallId, params, signal, onUpdate) => {// 执行逻辑return result; } };}
示例:createExecTool
functioncreateExecTool(defaults) {return {name: "exec",description: "Run shell commands (pty available for TTY-required CLIs)",parameters: {type: "object",properties: {command: { type: "string", description: "Command to execute" },background: { type: "boolean", description: "Run in background" },yieldMs: { type: "number", description: "Milliseconds to wait before backgrounding" },// ... },required: ["command"] },execute: async (toolCallId, params, signal, onUpdate) => {// 执行 shell 命令// 处理后台任务// 返回结果 } };}
四、完整工具列表
核心工具(5 个)
| | |
read | createReadTool | |
write | createWriteTool | |
edit | createEditTool | |
exec | createExecTool | |
process | createProcessTool | |
会话管理工具(7 个)
| | |
sessions_list | createSessionsListTool | |
sessions_history | createSessionsHistoryTool | |
sessions_send | createSessionsSendTool | |
sessions_spawn | createSessionsSpawnTool | |
sessions_yield | createSessionsYieldTool | |
session_status | createSessionStatusTool | |
subagents | createSubagentsTool | |
OpenClaw 核心工具(14 个)
| | |
browser | createBrowserTool | |
canvas | createCanvasTool | |
nodes | createNodesTool | |
cron | createCronTool | |
message | createMessageTool | |
tts | createTtsTool | |
gateway | createGatewayTool | |
agents_list | createAgentsListTool | |
image | createImageTool | |
image_generate | createImageGenerateTool | |
pdf | createPdfTool | |
web_search | createWebSearchTool | |
web_fetch | createWebFetchTool | |
apply_patch | createApplyPatchTool | |
沙盒工具(3 个)
| | |
sandboxed_read | createSandboxedReadTool | |
sandboxed_write | createSandboxedWriteTool | |
sandboxed_edit | createSandboxedEditTool | |
五、工具创建流程
createOpenClawCodingTools 流程
functioncreateOpenClawCodingTools(options) {// 1. 解析配置const sandbox = options?.sandbox;const policy = resolveEffectiveToolPolicy(options);// 2. 创建基础工具(read, write, edit)const base = codingTools.flatMap((tool) => {if (tool.name === 'read') {return [createOpenClawReadTool(createReadTool(workspaceRoot))]; }// ... });// 3. 创建 exec 和 process 工具const execTool = createExecTool({...defaults, cwd: workspaceRoot});const processTool = createProcessTool({scopeKey});// 4. 创建 OpenClaw 核心工具const openClawTools = createOpenClawTools({agentSessionKey: options?.sessionKey,workspaceDir: workspaceRoot,// ... });// 5. 合并所有工具const tools = [ ...base, execTool, processTool, ...openClawTools ];// 6. 应用策略管道const withHooks = applyToolPolicyPipeline({tools: tools,policy: policy,// ... });return withHooks;}
createOpenClawTools 流程
functioncreateOpenClawTools(options) {// 1. 创建媒体工具const imageTool = createImageTool({...});const imageGenerateTool = createImageGenerateTool({...});const pdfTool = createPdfTool({...});// 2. 创建网络工具const webSearchTool = createWebSearchTool({...});const webFetchTool = createWebFetchTool({...});// 3. 创建消息工具const messageTool = createMessageTool({...});// 4. 创建核心工具数组const tools = [createBrowserTool({...}),createCanvasTool({...}),createNodesTool({...}),createCronTool({...}), ...messageTool ? [messageTool] : [],createTtsTool({...}), ...imageGenerateTool ? [imageGenerateTool] : [],createGatewayTool({...}),createAgentsListTool({...}),createSessionsListTool({...}),createSessionsHistoryTool({...}),createSessionsSendTool({...}),createSessionsYieldTool({...}),createSessionsSpawnTool({...}),createSubagentsTool({...}),createSessionStatusTool({...}), ...webSearchTool ? [webSearchTool] : [], ...webFetchTool ? [webFetchTool] : [], ...imageTool ? [imageTool] : [], ...pdfTool ? [pdfTool] : [] ];// 5. 添加插件工具const pluginTools = resolvePluginTools({...});return [...tools, ...pluginTools];}
六、工具策略管道
applyToolPolicyPipeline
工具在返回前会经过策略管道处理:
const withHooks = applyToolPolicyPipeline({tools: tools, // 原始工具列表toolMeta: (tool) =>getPluginToolMeta(tool),warn: logWarn,steps: [ {policy: profilePolicy,label: "profile tools.allow" }, {policy: globalPolicy,label: "global tools.allow" }, {policy: sandboxToolPolicy,label: "sandbox tools.allow" },// ... ]});
策略类型
| |
profilePolicy | |
globalPolicy | |
agentPolicy | |
groupPolicy | |
sandboxToolPolicy | |
subagentPolicy | |
七、工具执行流程
1. before_tool_call Hook
工具执行前会经过 before_tool_call Hook:
asyncfunctionrunBeforeToolCallHook(args) {// 1. 检测工具调用循环const loopResult = detectToolCallLoop(sessionState, toolName, params);if (loopResult.stuck) {return { blocked: true, reason: "Loop detected" }; }// 2. 运行全局 Hookconst hookResult = await hookRunner.runBeforeToolCall({...});if (hookResult?.block) {return { blocked: true, reason: hookResult.blockReason }; }return { blocked: false, params };}
2. 工具执行
constexecute = async (toolCallId, params, signal, onUpdate) => {// 1. 运行 before_tool_call Hookconst outcome = awaitrunBeforeToolCallHook({toolName, params, toolCallId, ctx});if (outcome.blocked) thrownewError(outcome.reason);// 2. 执行工具const result = awaitexecute(toolCallId, outcome.params, signal, onUpdate);// 3. 记录结果awaitrecordLoopOutcome({...});return result;};
八、工具命名规范
命名模式
| | |
createXxxTool | createExecTool | |
xxx | exec | |
XxxTool | ExecTool | |
工具名称规则
- 2. 下划线分隔:
web_search, apply_patch
九、工具参数定义
JSON Schema 格式
parameters: {type: "object",properties: {command: {type: "string",description: "Command to execute" },background: {type: "boolean",description: "Run in background",default: false },timeout: {type: "number",description: "Timeout in seconds" } },required: ["command"]}
十、总结
工具架构特点
- 2. 可组合:通过
createOpenClawCodingTools 组合 - 5. 有监控:before_tool_call Hook 检测循环
工具数量统计