You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/web/src/content/docs/custom-tools.mdx
+50-28Lines changed: 50 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,27 +9,30 @@ Custom tools are functions you create that the LLM can call during conversations
9
9
10
10
## Tool structure
11
11
12
-
The easiest way to create tools is using the `tool()` helper which provides type safety and validation:
12
+
Tools are defined as `.ts/.js` files in the `.opencode/tool/` directory. They
13
+
can also be defined globally in `~/.config/opencode/tool/`.
14
+
15
+
The easiest way to create tools is using the `tool()` helper which provides type safety and validation. Use `tool.schema` (which is just [Zod](https://zod.dev)) to define argument types:
13
16
14
17
```ts title=".opencode/tool/database.ts"
15
18
import { tool } from"@opencode-ai/plugin"
16
19
17
-
exportdefaulttool((z) => ({
20
+
exportdefaulttool({
18
21
description: "Query the project database",
19
22
args: {
20
-
query: z.string().describe("SQL query to execute"),
23
+
query: tool.schema.string().describe("SQL query to execute"),
21
24
},
22
25
async execute(args) {
23
26
// Your database logic here
24
27
return`Executed query: ${args.query}`
25
28
},
26
-
}))
29
+
})
27
30
```
28
31
29
-
You can also import Zod directly and return a plain object:
32
+
You can also import [Zod](https://zod.dev) directly and return a plain object:
30
33
31
34
```ts
32
-
importzfrom"zod/v4"
35
+
import{ z } from"zod"
33
36
34
37
exportdefault {
35
38
description: "Tool description",
@@ -42,29 +45,62 @@ export default {
42
45
},
43
46
}
44
47
```
45
-
46
48
The filename becomes the tool name. This creates a `database` tool.
47
49
48
50
---
49
51
52
+
## Multiple tools per file
53
+
54
+
You can export multiple tools from a single file. Each export becomes a separate tool with the name `<filename>_<exportname>`:
0 commit comments