Tools 允许 LLM 在你的 codebase 中执行操作。OpenCode 自带了一组 built-in tools,同时你也可以通过 [custom tools] 或 [MCP servers] 来扩展能力。

默认情况下,所有 tools 都是 enabled(启用) 状态,并且不需要额外授权即可运行。你可以通过 [permissions] 来控制 tool 的行为。


Configure(配置)

使用 permission 字段来控制 tool 的行为。你可以为每个 tool 设置:allow、deny 或 ask(需要审批)。

```json title=”opencode.json” { “$schema”: “https://opencode.ai/config.json“, “permission”: { “edit”: “deny”, “bash”: “ask”, “webfetch”: “allow” } }

  1. 你也可以使用 wildcard(通配符)一次性控制多个 tools
  2. 例如,如果你希望对某个 MCP server 下的所有 tools 都要求审批:
  3. ```json title="opencode.json"
  4. {
  5. "$schema": "https://opencode.ai/config.json",
  6. "permission": {
  7. "mymcp_*": "ask"
  8. }
  9. }

可以在 [permissions] 文档中了解更多关于权限配置的细节。


Built-in(内置工具)

以下是 OpenCode 中所有可用的 built-in tools。


bash

在你的项目环境中执行 shell commands。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “bash”: “allow” } }

该 tool 允许 LLM 运行终端命令,例如 npm installgit status,或任何其他 shell command。


edit

通过精确的字符串替换来修改已有文件。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “edit”: “allow” } }

该 tool 通过替换精确匹配的文本来执行精细化修改,是 LLM 修改代码的主要方式。


write

创建新文件或覆盖已有文件。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “edit”: “allow” } }

该 tool 允许 LLM 创建新文件。如果文件已经存在,将会被直接覆盖。


read

从你的 codebase 中读取文件内容。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “read”: “allow” } }

该 tool 用于读取文件并返回其内容。对于大文件,支持按指定的 line range 读取。


grep

使用 regular expressions 搜索文件内容。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “grep”: “allow” } }

在整个 codebase 中进行高速内容搜索,支持完整的 regex 语法以及按文件 pattern 过滤。


glob

通过 pattern matching 查找文件。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “glob”: “allow” } }

使用 glob patterns(如 **/*.jssrc/**/*.ts)搜索文件,并按修改时间排序返回匹配的文件路径。


list

列出指定路径下的文件和目录。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “list”: “allow” } }

该 tool 用于列出目录内容,并支持使用 glob patterns 过滤结果。


lsp(experimental)

与已配置的 LSP servers 交互,获取代码智能能力,例如 definitions、references、hover 信息以及 call hierarchy。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “lsp”: “allow” } }

支持的操作包括: goToDefinitionfindReferenceshoverdocumentSymbolworkspaceSymbolgoToImplementationprepareCallHierarchyincomingCallsoutgoingCalls

如需配置项目可用的 LSP servers,请参考 [LSP Servers] 文档。


patch

将 patch 应用到文件。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “edit”: “allow” } }

该 tool 用于将 patch 文件应用到你的 codebase,适合应用来自不同来源的 diff 和 patch。


skill

加载一个 [skill](一个 SKILL.md 文件),并将其内容返回到当前对话中。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “skill”: “allow” } }


todowrite

在 coding session 中管理 todo lists。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “todowrite”: “allow” } }

用于创建和更新 task list,以便在复杂操作中跟踪进度。LLM 会利用它来组织 multi-step tasks。


todoread

读取已有的 todo lists。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “todoread”: “allow” } }

读取当前 todo list 的状态,用于帮助 LLM 跟踪哪些任务尚未完成或已经完成。


webfetch

获取 web 内容。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “webfetch”: “allow” } }

允许 LLM 获取并读取 web 页面,适用于查阅文档或在线资料调研。


question

在执行过程中向用户提问。

{ “$schema”: “https://opencode.ai/config.json“, “permission”: { “question”: “allow” } }

该 tool 允许 LLM 在任务执行过程中向用户提问,适用于:

  • 收集用户偏好或需求
  • 澄清含糊不清的指令
  • 获取实现方案上的决策
  • 提供可选方向供用户选择

每个问题都包含一个 header、问题正文以及一组选项。用户可以从提供的选项中选择,也可以输入自定义答案。当存在多个问题时,用户可以在提交所有答案前进行切换和检查。


Custom tools(自定义工具)

Custom tools 允许你定义自己的函数,供 LLM 调用。它们在 config file 中定义,并可以执行任意代码。

可以在 [custom tools] 文档中了解如何创建自定义工具。


MCP servers

MCP(Model Context Protocol)servers 允许你集成外部工具和服务,例如数据库访问、API 集成以及第三方服务。

可以在 [MCP servers] 文档中了解如何配置 MCP servers。


Internals(内部实现)

在内部实现上,grepgloblist 等 tools 底层使用了 [ripgrep]。 默认情况下,ripgrep 会遵循 .gitignore 的规则,这意味着 .gitignore 中列出的文件和目录会被排除在搜索和列表结果之外。


Ignore patterns(忽略规则)

如果你希望包含那些通常会被忽略的文件,可以在项目根目录创建一个 .ignore 文件,用来显式允许某些路径。

text title=".ignore" !node_modules/ !dist/ !build/

例如,上述 .ignore 文件允许 ripgrep 在 node_modules/dist/build/ 目录中执行搜索,即使它们已经被列在 .gitignore 中。