|
| 1 | +--- |
| 2 | +title: Custom tools |
| 3 | +description: Create custom tools to extend opencode capabilities. |
| 4 | +--- |
| 5 | + |
| 6 | +Custom tools are functions you create that the LLM can call during conversations. They work alongside opencode's built-in tools like `read`, `write`, and `bash`. |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## Tool structure |
| 11 | + |
| 12 | +The easiest way to create tools is using the `tool()` helper which provides type safety and validation: |
| 13 | + |
| 14 | +```ts title=".opencode/tool/database.ts" |
| 15 | +import { tool } from "@opencode-ai/plugin" |
| 16 | + |
| 17 | +export default tool((z) => ({ |
| 18 | + description: "Query the project database", |
| 19 | + args: { |
| 20 | + query: z.string().describe("SQL query to execute"), |
| 21 | + }, |
| 22 | + async execute(args) { |
| 23 | + // Your database logic here |
| 24 | + return `Executed query: ${args.query}` |
| 25 | + }, |
| 26 | +})) |
| 27 | +``` |
| 28 | + |
| 29 | +You can also import Zod directly and return a plain object: |
| 30 | + |
| 31 | +```ts |
| 32 | +import z from "zod/v4" |
| 33 | + |
| 34 | +export default { |
| 35 | + description: "Tool description", |
| 36 | + args: { |
| 37 | + param: z.string().describe("Parameter description"), |
| 38 | + }, |
| 39 | + async execute(args, context) { |
| 40 | + // Tool implementation |
| 41 | + return "result" |
| 42 | + }, |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +The filename becomes the tool name. This creates a `database` tool. |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## Arguments |
| 51 | + |
| 52 | +Use the `z` parameter to define tool arguments with validation and descriptions: |
| 53 | + |
| 54 | +```ts title=".opencode/tool/calculator.ts" |
| 55 | +import { tool } from "@opencode-ai/plugin" |
| 56 | + |
| 57 | +export default tool((z) => ({ |
| 58 | + description: "Perform mathematical calculations", |
| 59 | + args: { |
| 60 | + expression: z.string().describe("Mathematical expression to evaluate"), |
| 61 | + precision: z.number().optional().describe("Decimal precision"), |
| 62 | + }, |
| 63 | + async execute(args) { |
| 64 | + // Your calculation logic here |
| 65 | + return `Result: ${eval(args.expression).toFixed(args.precision || 2)}` |
| 66 | + }, |
| 67 | +})) |
| 68 | +``` |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +## Context |
| 73 | + |
| 74 | +Tools receive context about the current session: |
| 75 | + |
| 76 | +```ts title=".opencode/tool/project.ts" |
| 77 | +import { tool } from "@opencode-ai/plugin" |
| 78 | + |
| 79 | +export default tool((z) => ({ |
| 80 | + description: "Get project information", |
| 81 | + args: {}, |
| 82 | + async execute(args, context) { |
| 83 | + // Access context information |
| 84 | + const { project, directory, worktree } = context |
| 85 | + return `Project: ${project.name}, Directory: ${directory}` |
| 86 | + }, |
| 87 | +})) |
| 88 | +``` |
| 89 | + |
| 90 | +--- |
| 91 | + |
| 92 | +## Tool locations |
| 93 | + |
| 94 | +Custom tools are loaded from: |
| 95 | + |
| 96 | +- Project: `.opencode/tool/` |
| 97 | +- Global: `~/.config/opencode/tool/` |
| 98 | + |
| 99 | +Files must use `.js` or `.ts` extensions. |
| 100 | + |
| 101 | +--- |
| 102 | + |
| 103 | +## Built-in tools |
| 104 | + |
| 105 | +opencode includes several built-in tools: `read`, `write`, `edit`, `bash`, `glob`, `grep`, `list`, `patch`, `todo`, and `task`. [Learn more](/docs/tui#tools). |