tools.mdx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. ---
  2. title: Tools
  3. description: Manage the tools an LLM can use.
  4. ---
  5. Tools allow the LLM to perform actions in your codebase. OpenCode comes with a set of built-in tools, but you can extend it with [custom tools](/docs/custom-tools) or [MCP servers](/docs/mcp-servers).
  6. By default, all tools are **enabled** and don't need permission to run. You can control tool behavior through [permissions](/docs/permissions).
  7. ---
  8. ## Configure
  9. Use the `permission` field to control tool behavior. You can allow, deny, or require approval for each tool.
  10. ```json title="opencode.json"
  11. {
  12. "$schema": "https://opencode.ai/config.json",
  13. "permission": {
  14. "edit": "deny",
  15. "bash": "ask",
  16. "webfetch": "allow"
  17. }
  18. }
  19. ```
  20. You can also use wildcards to control multiple tools at once. For example, to require approval for all tools from an MCP server:
  21. ```json title="opencode.json"
  22. {
  23. "$schema": "https://opencode.ai/config.json",
  24. "permission": {
  25. "mymcp_*": "ask"
  26. }
  27. }
  28. ```
  29. [Learn more](/docs/permissions) about configuring permissions.
  30. ---
  31. ## Built-in
  32. Here are all the built-in tools available in OpenCode.
  33. ---
  34. ### bash
  35. Execute shell commands in your project environment.
  36. ```json title="opencode.json" {4}
  37. {
  38. "$schema": "https://opencode.ai/config.json",
  39. "permission": {
  40. "bash": "allow"
  41. }
  42. }
  43. ```
  44. This tool allows the LLM to run terminal commands like `npm install`, `git status`, or any other shell command.
  45. ---
  46. ### edit
  47. Modify existing files using exact string replacements.
  48. ```json title="opencode.json" {4}
  49. {
  50. "$schema": "https://opencode.ai/config.json",
  51. "permission": {
  52. "edit": "allow"
  53. }
  54. }
  55. ```
  56. This tool performs precise edits to files by replacing exact text matches. It's the primary way the LLM modifies code.
  57. ---
  58. ### write
  59. Create new files or overwrite existing ones.
  60. ```json title="opencode.json" {4}
  61. {
  62. "$schema": "https://opencode.ai/config.json",
  63. "permission": {
  64. "edit": "allow"
  65. }
  66. }
  67. ```
  68. Use this to allow the LLM to create new files. It will overwrite existing files if they already exist.
  69. :::note
  70. The `write` tool is controlled by the `edit` permission, which covers all file modifications (`edit`, `write`, `patch`, `multiedit`).
  71. :::
  72. ---
  73. ### read
  74. Read file contents from your codebase.
  75. ```json title="opencode.json" {4}
  76. {
  77. "$schema": "https://opencode.ai/config.json",
  78. "permission": {
  79. "read": "allow"
  80. }
  81. }
  82. ```
  83. This tool reads files and returns their contents. It supports reading specific line ranges for large files.
  84. ---
  85. ### grep
  86. Search file contents using regular expressions.
  87. ```json title="opencode.json" {4}
  88. {
  89. "$schema": "https://opencode.ai/config.json",
  90. "permission": {
  91. "grep": "allow"
  92. }
  93. }
  94. ```
  95. Fast content search across your codebase. Supports full regex syntax and file pattern filtering.
  96. ---
  97. ### glob
  98. Find files by pattern matching.
  99. ```json title="opencode.json" {4}
  100. {
  101. "$schema": "https://opencode.ai/config.json",
  102. "permission": {
  103. "glob": "allow"
  104. }
  105. }
  106. ```
  107. Search for files using glob patterns like `**/*.js` or `src/**/*.ts`. Returns matching file paths sorted by modification time.
  108. ---
  109. ### list
  110. List files and directories in a given path.
  111. ```json title="opencode.json" {4}
  112. {
  113. "$schema": "https://opencode.ai/config.json",
  114. "permission": {
  115. "list": "allow"
  116. }
  117. }
  118. ```
  119. This tool lists directory contents. It accepts glob patterns to filter results.
  120. ---
  121. ### lsp (experimental)
  122. Interact with your configured LSP servers to get code intelligence features like definitions, references, hover info, and call hierarchy.
  123. :::note
  124. This tool is only available when `OPENCODE_EXPERIMENTAL_LSP_TOOL=true` (or `OPENCODE_EXPERIMENTAL=true`).
  125. :::
  126. ```json title="opencode.json" {4}
  127. {
  128. "$schema": "https://opencode.ai/config.json",
  129. "permission": {
  130. "lsp": "allow"
  131. }
  132. }
  133. ```
  134. Supported operations include `goToDefinition`, `findReferences`, `hover`, `documentSymbol`, `workspaceSymbol`, `goToImplementation`, `prepareCallHierarchy`, `incomingCalls`, and `outgoingCalls`.
  135. To configure which LSP servers are available for your project, see [LSP Servers](/docs/lsp).
  136. ---
  137. ### patch
  138. Apply patches to files.
  139. ```json title="opencode.json" {4}
  140. {
  141. "$schema": "https://opencode.ai/config.json",
  142. "permission": {
  143. "edit": "allow"
  144. }
  145. }
  146. ```
  147. This tool applies patch files to your codebase. Useful for applying diffs and patches from various sources.
  148. :::note
  149. The `patch` tool is controlled by the `edit` permission, which covers all file modifications (`edit`, `write`, `patch`, `multiedit`).
  150. :::
  151. ---
  152. ### skill
  153. Load a [skill](/docs/skills) (a `SKILL.md` file) and return its content in the conversation.
  154. ```json title="opencode.json" {4}
  155. {
  156. "$schema": "https://opencode.ai/config.json",
  157. "permission": {
  158. "skill": "allow"
  159. }
  160. }
  161. ```
  162. ---
  163. ### todowrite
  164. Manage todo lists during coding sessions.
  165. ```json title="opencode.json" {4}
  166. {
  167. "$schema": "https://opencode.ai/config.json",
  168. "permission": {
  169. "todowrite": "allow"
  170. }
  171. }
  172. ```
  173. Creates and updates task lists to track progress during complex operations. The LLM uses this to organize multi-step tasks.
  174. :::note
  175. This tool is disabled for subagents by default, but you can enable it manually. [Learn more](/docs/agents/#permissions)
  176. :::
  177. ---
  178. ### todoread
  179. Read existing todo lists.
  180. ```json title="opencode.json" {4}
  181. {
  182. "$schema": "https://opencode.ai/config.json",
  183. "permission": {
  184. "todoread": "allow"
  185. }
  186. }
  187. ```
  188. Reads the current todo list state. Used by the LLM to track what tasks are pending or completed.
  189. :::note
  190. This tool is disabled for subagents by default, but you can enable it manually. [Learn more](/docs/agents/#permissions)
  191. :::
  192. ---
  193. ### webfetch
  194. Fetch web content.
  195. ```json title="opencode.json" {4}
  196. {
  197. "$schema": "https://opencode.ai/config.json",
  198. "permission": {
  199. "webfetch": "allow"
  200. }
  201. }
  202. ```
  203. Allows the LLM to fetch and read web pages. Useful for looking up documentation or researching online resources.
  204. ---
  205. ### question
  206. Ask the user questions during execution.
  207. ```json title="opencode.json" {4}
  208. {
  209. "$schema": "https://opencode.ai/config.json",
  210. "permission": {
  211. "question": "allow"
  212. }
  213. }
  214. ```
  215. This tool allows the LLM to ask the user questions during a task. It's useful for:
  216. - Gathering user preferences or requirements
  217. - Clarifying ambiguous instructions
  218. - Getting decisions on implementation choices
  219. - Offering choices about what direction to take
  220. Each question includes a header, the question text, and a list of options. Users can select from the provided options or type a custom answer. When there are multiple questions, users can navigate between them before submitting all answers.
  221. ---
  222. ## Custom tools
  223. Custom tools let you define your own functions that the LLM can call. These are defined in your config file and can execute arbitrary code.
  224. [Learn more](/docs/custom-tools) about creating custom tools.
  225. ---
  226. ## MCP servers
  227. MCP (Model Context Protocol) servers allow you to integrate external tools and services. This includes database access, API integrations, and third-party services.
  228. [Learn more](/docs/mcp-servers) about configuring MCP servers.
  229. ---
  230. ## Internals
  231. Internally, tools like `grep`, `glob`, and `list` use [ripgrep](https://github.com/BurntSushi/ripgrep) under the hood. By default, ripgrep respects `.gitignore` patterns, which means files and directories listed in your `.gitignore` will be excluded from searches and listings.
  232. ---
  233. ### Ignore patterns
  234. To include files that would normally be ignored, create a `.ignore` file in your project root. This file can explicitly allow certain paths.
  235. ```text title=".ignore"
  236. !node_modules/
  237. !dist/
  238. !build/
  239. ```
  240. For example, this `.ignore` file allows ripgrep to search within `node_modules/`, `dist/`, and `build/` directories even if they're listed in `.gitignore`.