Skip to content

Commit 66148df

Browse files
committed
docs: clarify custom tools can execute scripts in any language with Python example
1 parent 4611e08 commit 66148df

1 file changed

Lines changed: 58 additions & 20 deletions

File tree

packages/web/src/content/docs/custom-tools.mdx

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Custom tools are functions you create that the LLM can call during conversations
99

1010
## Creating a tool
1111

12-
Tools are defined as **TypeScript** or **JavaScript** files.
12+
Tools are defined as **TypeScript** or **JavaScript** files. However, the tool definition can invoke scripts written in **any language** — TypeScript or JavaScript is only used for the tool definition itself.
1313

1414
---
1515

@@ -45,6 +45,40 @@ The **filename** becomes the **tool name**. The above creates a `database` tool.
4545

4646
---
4747

48+
#### Multiple tools per file
49+
50+
You can also export multiple tools from a single file. Each export becomes **a separate tool** with the name **`<filename>_<exportname>`**:
51+
52+
```ts title=".opencode/tool/math.ts"
53+
import { tool } from "@opencode-ai/plugin"
54+
55+
export const add = tool({
56+
description: "Add two numbers",
57+
args: {
58+
a: tool.schema.number().describe("First number"),
59+
b: tool.schema.number().describe("Second number"),
60+
},
61+
async execute(args) {
62+
return args.a + args.b
63+
},
64+
})
65+
66+
export const multiply = tool({
67+
description: "Multiply two numbers",
68+
args: {
69+
a: tool.schema.number().describe("First number"),
70+
b: tool.schema.number().describe("Second number"),
71+
},
72+
async execute(args) {
73+
return args.a * args.b
74+
},
75+
})
76+
```
77+
78+
This creates two tools: `math_add` and `math_multiply`.
79+
80+
---
81+
4882
### Arguments
4983

5084
You can use `tool.schema`, which is just [Zod](https://zod.dev), to define argument types.
@@ -74,7 +108,7 @@ export default {
74108

75109
---
76110

77-
## Context
111+
### Context
78112

79113
Tools receive context about the current session:
80114

@@ -94,34 +128,38 @@ export default tool({
94128

95129
---
96130

97-
## Multiple tools per file
131+
## Examples
98132

99-
You can also export multiple tools from a single file. Each export becomes **a separate tool** with the name **`<filename>_<exportname>`**:
133+
### Write a tool in Python
100134

101-
```ts title=".opencode/tool/math.ts"
102-
import { tool } from "@opencode-ai/plugin"
135+
You can write your tools in any language you want. Here's an example that adds two numbers using Python.
103136

104-
export const add = tool({
105-
description: "Add two numbers",
106-
args: {
107-
a: tool.schema.number().describe("First number"),
108-
b: tool.schema.number().describe("Second number"),
109-
},
110-
async execute(args) {
111-
return args.a + args.b
112-
},
113-
})
137+
First, create the tool as a Python script:
114138

115-
export const multiply = tool({
116-
description: "Multiply two numbers",
139+
```python title=".opencode/tool/add.py"
140+
import sys
141+
142+
a = int(sys.argv[1])
143+
b = int(sys.argv[2])
144+
print(a + b)
145+
```
146+
147+
Then create the tool definition that invokes it:
148+
149+
```ts title=".opencode/tool/python-add.ts" {10}
150+
import { tool } from "@opencode-ai/plugin"
151+
152+
export default tool({
153+
description: "Add two numbers using Python",
117154
args: {
118155
a: tool.schema.number().describe("First number"),
119156
b: tool.schema.number().describe("Second number"),
120157
},
121158
async execute(args) {
122-
return args.a * args.b
159+
const result = await Bun.$`python3 .opencode/tool/add.py ${args.a} ${args.b}`.text()
160+
return result.trim()
123161
},
124162
})
125163
```
126164

127-
This creates two tools: `math_add` and `math_multiply`.
165+
Here we are using the [`Bun.$`](https://bun.com/docs/runtime/shell) utility to run the Python script.

0 commit comments

Comments
 (0)