第 14 章

内置工具全解:16个核心工具的能力边界与适用场景

第14章:内置工具全解——16个核心工具的能力边界与适用场景

概述

OpenClaw 内置 16 个原生工具,覆盖文件操作、命令执行、浏览器控制、网页搜索、定时调度、记忆管理和多 Agent 协调等所有核心能力。理解每个工具的边界和适用场景,是构建高效 Agent 工作流的前提。


14.1 工具总览

工具名 类别 核心能力
read 文件操作 读取文件内容
write 文件操作 创建或覆盖文件
edit 文件操作 精准修改文件内容
exec 命令执行 执行 Shell 命令(带沙箱)
bash 命令执行 执行 Bash 脚本块
process 进程管理 启动/停止/监控进程
browser 浏览器控制 自动化 Web 交互
web_search 网络搜索 集成搜索引擎查询
message 消息发送 向用户/系统发送消息
cron 定时调度 创建和管理定时任务
memory_search 记忆管理 语义搜索记忆库
memory_get 记忆管理 读取指定记忆文件
sessions_spawn Agent 协调 生成子 Agent
canvas_* 视觉工作区 Canvas 控件操作组
node_* 物理设备 IoT/硬件节点控制组

14.2 read — 文件读取

功能说明

读取本地文件系统中文件的内容,支持文本和二进制文件。

接口定义

{
  "tool": "read",
  "input": {
    "path": "/path/to/file",
    "encoding": "utf-8",
    "start_line": 1,
    "end_line": 100,
    "max_bytes": 102400
  }
}

参数说明

参数 类型 必填 说明
path string 文件绝对路径
encoding string 默认 utf-8,可选 base64
start_line int 起始行号(包含)
end_line int 结束行号(包含)
max_bytes int 最大读取字节数

适用场景

限制


14.3 write — 文件写入

功能说明

创建新文件或覆盖现有文件的全部内容。

接口定义

{
  "tool": "write",
  "input": {
    "path": "/path/to/output.txt",
    "content": "文件内容在此",
    "encoding": "utf-8",
    "create_dirs": true,
    "overwrite": true
  }
}

适用场景

限制


14.4 edit — 精准文件编辑

功能说明

对文件进行精准的局部内容修改,支持字符串替换、行号替换和正则替换。

接口定义

{
  "tool": "edit",
  "input": {
    "path": "/path/to/file.py",
    "operations": [
      {
        "type": "replace_string",
        "old": "def old_function():",
        "new": "def new_function():",
        "replace_all": false
      },
      {
        "type": "replace_lines",
        "start_line": 42,
        "end_line": 45,
        "new_content": "# 新的代码块\nprint('hello')"
      },
      {
        "type": "insert_after_line",
        "line": 10,
        "content": "# 插入的注释"
      }
    ]
  }
}

read vs edit vs write 的选择逻辑

需要读取文件内容?
    --> 使用 read

需要修改文件?
    --> 需要修改整个文件?
        --> 使用 write
    --> 只需要修改特定部分?
        --> 使用 edit
            --> 知道确切的字符串?使用 replace_string
            --> 知道行号范围?使用 replace_lines

14.5 exec — Shell 命令执行

功能说明

执行单个 Shell 命令,带沙箱控制和超时管理。

接口定义

{
  "tool": "exec",
  "input": {
    "command": "ls -la /workspace",
    "cwd": "/workspace",
    "env": {
      "MY_VAR": "value"
    },
    "timeout_seconds": 30,
    "capture_output": true
  }
}

输出格式

{
  "stdout": "文件列表内容...",
  "stderr": "",
  "exit_code": 0,
  "duration_ms": 142
}

14.6 bash — Bash 脚本块执行

功能说明

执行多行 Bash 脚本,支持管道、变量和复杂逻辑。

接口定义

{
  "tool": "bash",
  "input": {
    "script": "#!/bin/bash\nset -e\ncd /workspace\nfor f in *.py; do\n  echo \"Processing: $f\"\n  python -m py_compile \"$f\"\ndone",
    "timeout_seconds": 60,
    "cwd": "/workspace"
  }
}

exec vs bash 的差异

维度 exec bash
适用场景 单个命令 多步骤脚本
管道支持 有限 完整
变量赋值 不持久 跨行持久
错误处理 基础 完整(set -e/-o pipefail)
调试性 简单 可加 set -x
危险环境变量 执行前剥离 执行前剥离

重要:两者在执行前都会剥离危险环境变量,包括 DYLD_*LD_*NODE_OPTIONS 等。


14.7 process — 进程管理

功能说明

启动、停止、查询后台进程,适合需要长期运行的服务。

接口定义

{
  "tool": "process",
  "input": {
    "action": "start",
    "command": "python server.py",
    "cwd": "/workspace",
    "name": "my-server",
    "restart_on_crash": true,
    "log_file": "/var/log/my-server.log"
  }
}

操作类型

action 说明
start 启动新进程
stop 停止指定进程
restart 重启进程
status 查询进程状态
list 列出所有受管进程
log 读取进程日志

适用场景


14.8 browser — 浏览器控制

功能说明

通过 Playwright/Puppeteer 控制无头浏览器,实现 Web 自动化操作。

坐标点击

{
  "tool": "browser",
  "input": {
    "action": "click",
    "selector": "#submit-button",
    "x": 450,
    "y": 320,
    "click_type": "left"
  }
}

表单填写

{
  "tool": "browser",
  "input": {
    "action": "fill_form",
    "fields": [
      {"selector": "#username", "value": "admin"},
      {"selector": "#password", "value": "${SECRET_PASSWORD}"},
      {"selector": "#remember-me", "action": "check"}
    ]
  }
}

完整操作集

{
  "tool": "browser",
  "input": {
    "action": "navigate",
    "url": "https://example.com"
  }
}
action 说明
navigate 打开 URL
click 点击元素
fill 填写输入框
fill_form 批量填写表单
screenshot 截图
scroll 滚动页面
wait_for 等待元素出现
evaluate 执行 JavaScript
get_text 获取元素文本
get_html 获取页面 HTML
select 选择下拉项
hover 悬停在元素上

适用场景


14.9 web_search — 网页搜索

功能说明

集成多个搜索引擎和内容抓取服务,获取实时网络信息。

支持的后端

后端 特点 配置
Exa 语义搜索,高质量结果 EXAI_API_KEY
Tavily AI 优化搜索,含摘要 TAVILY_API_KEY
Firecrawl 完整页面内容抓取 FIRECRAWL_API_KEY
Serper Google 结果,高速 SERPER_API_KEY
Bing 微软必应搜索 BING_SEARCH_KEY

接口定义

{
  "tool": "web_search",
  "input": {
    "query": "OpenClaw Agent framework 2026",
    "backend": "tavily",
    "max_results": 5,
    "include_content": true,
    "time_range": "month"
  }
}

输出示例

{
  "results": [
    {
      "title": "OpenClaw v3.0 Release Notes",
      "url": "https://docs.openclaw.dev/changelog",
      "snippet": "OpenClaw 3.0 introduces...",
      "content": "Full page content...",
      "published_date": "2026-03-15"
    }
  ],
  "total_results": 5
}

14.10 message — 消息发送

功能说明

向用户、系统日志或外部 Webhook 发送消息,支持多种消息类型。

接口定义

{
  "tool": "message",
  "input": {
    "target": "user",
    "content": "任务已完成,请查看结果",
    "type": "info",
    "attachments": [
      {"type": "file", "path": "/output/report.pdf"}
    ]
  }
}

消息目标

target 说明
user 发送给当前用户
log 写入系统日志
webhook 发送到配置的 Webhook
slack 发送到 Slack 频道
email 发送电子邮件

消息类型

type 说明
info 普通信息
warning 警告消息
error 错误通知
success 成功通知
progress 进度更新

14.11 cron — 定时任务调度

功能说明

创建、管理和删除定时任务,支持标准 Cron 表达式。

Cron 表达式语法

┌─────────── 分钟 (0-59)
│ ┌─────────── 小时 (0-23)
│ │ ┌─────────── 日 (1-31)
│ │ │ ┌─────────── 月 (1-12)
│ │ │ │ ┌─────────── 星期 (0-6, 0=周日)
│ │ │ │ │
* * * * *

接口定义

{
  "tool": "cron",
  "input": {
    "action": "create",
    "name": "daily-report",
    "schedule": "0 9 * * 1-5",
    "task": {
      "type": "agent_run",
      "agent": "report-generator",
      "input": {"date": "${today}"}
    },
    "timezone": "Asia/Shanghai",
    "enabled": true
  }
}

常用 Cron 表达式示例

# 每天早上 9 点(工作日)
0 9 * * 1-5

# 每小时执行一次
0 * * * *

# 每 15 分钟执行一次
*/15 * * * *

# 每月 1 号零点执行
0 0 1 * *

# 每周日凌晨 2 点执行备份
0 2 * * 0

# 每天下午 3 点和 6 点
0 15,18 * * *

Cron 操作类型

action 说明
create 创建新定时任务
update 修改现有任务
delete 删除任务
enable 启用任务
disable 禁用任务
list 列出所有任务
run_now 立即执行一次

14.12 memory_search — 语义搜索记忆

功能说明

在 Agent 记忆库中进行语义相似度搜索,返回最相关的记忆片段。

接口定义

{
  "tool": "memory_search",
  "input": {
    "query": "用户偏好的输出格式设置",
    "top_k": 5,
    "threshold": 0.7,
    "namespace": "user_preferences",
    "filters": {
      "created_after": "2026-01-01",
      "tags": ["preferences", "format"]
    }
  }
}

输出示例

{
  "memories": [
    {
      "id": "mem_abc123",
      "content": "用户偏好 JSON 格式输出,缩进为 2 空格",
      "similarity": 0.92,
      "created_at": "2026-02-15T10:23:00Z",
      "tags": ["preferences", "format"]
    }
  ]
}

14.13 memory_get — 读取指定记忆文件

功能说明

通过 ID 或路径精准读取特定的记忆条目,不进行语义搜索。

接口定义

{
  "tool": "memory_get",
  "input": {
    "id": "mem_abc123",
    "path": "user_preferences/output_format.md",
    "namespace": "global"
  }
}

memory_search vs memory_get 的选择

你知道需要什么记忆?
    |
    +-- 知道确切 ID 或文件名 --> 使用 memory_get(精准、快速)
    |
    +-- 知道大概含义但不知道具体位置 --> 使用 memory_search(语义搜索)
    |
    +-- 需要批量扫描相关记忆 --> 先 memory_search,再对结果使用 memory_get

14.14 sessions_spawn — 生成子 Agent

功能说明

创建独立的子 Agent 会话,实现并行任务处理或专业化分工。

接口定义

{
  "tool": "sessions_spawn",
  "input": {
    "agent_config": {
      "name": "code-reviewer",
      "model": "anthropic/claude-opus-4-6",
      "system_prompt": "你是专业代码审查 Agent,专注于安全漏洞检测",
      "tools": ["read", "web_search"],
      "max_turns": 20
    },
    "initial_message": "请审查 /workspace/src 目录下的所有 Python 文件",
    "delivery_mode": "background",
    "on_complete_notify": true
  }
}

交付模式

模式 说明 适用场景
interactive 实时流式返回结果 需要实时监控子 Agent 进度
background 完成后 announce 结果 长时间任务,主 Agent 继续其他工作

使用模式

模式 A:并行任务分解

// 主 Agent 同时spawn多个子Agent处理不同模块
{
  "tool": "sessions_spawn",
  "input": {
    "agent_config": {"name": "frontend-agent"},
    "initial_message": "优化 React 组件性能"
  }
}
// 同时
{
  "tool": "sessions_spawn",
  "input": {
    "agent_config": {"name": "backend-agent"},
    "initial_message": "优化 API 接口性能"
  }
}

模式 B:专业化分工

{
  "tool": "sessions_spawn",
  "input": {
    "agent_config": {
      "name": "security-auditor",
      "system_prompt": "专注安全审计,只使用 read 工具",
      "tools": ["read"],
      "tool_policy": "allowlist"
    }
  }
}

14.15 Canvas 控制工具组

Canvas 工具用于操作 OpenClaw 视觉工作区,支持创建交互式数据看板和工作流可视化。

主要操作

{
  "tool": "canvas_create_node",
  "input": {
    "type": "text",
    "content": "数据分析结果",
    "x": 100,
    "y": 200,
    "width": 300,
    "height": 150
  }
}
工具名 说明
canvas_create_node 创建 Canvas 节点
canvas_update_node 更新节点内容
canvas_delete_node 删除节点
canvas_connect 连接两个节点
canvas_layout 自动布局

14.16 Node 设备控制工具组

Node 工具用于控制物理设备和 IoT 节点,适合边缘计算和工业自动化场景。

{
  "tool": "node_send_command",
  "input": {
    "node_id": "sensor-hub-01",
    "command": "read_temperature",
    "params": {"unit": "celsius"}
  }
}

14.17 工具组合使用模式

模式 A:代码生成与验证

web_search(查文档)
    --> read(读取现有代码)
    --> write/edit(生成新代码)
    --> exec(运行测试)
    --> message(报告结果)

模式 B:定时数据收集

cron(触发调度)
    --> web_search(搜索最新数据)
    --> write(保存原始数据)
    --> sessions_spawn(异步分析Agent)
    --> message(发送分析报告)

模式 C:自动化 Web 操作

browser(导航到目标页面)
    --> browser(填写表单)
    --> browser(截图确认)
    --> write(保存截图)
    --> message(通知完成)

本章小结

下一章 将深入讲解工具策略:Allow/Deny 规则、沙箱配置与 elevated 标志。

本章评分
4.6  / 5  (23 评分)

💬 留言讨论