OpenCode Custom Tools
Custom tools allow you to extend OpenCode's capabilities, letting the LLM call functions you define during conversations, thereby enabling complex operations such as database queries, script execution, and API calls.
These tools work together with built-in tools (such as read、write、bash) to build a complete automated development capability system.
Custom tools are OpenCode's core extension capability, upgrading the LLM from only generating code to being able to execute tasks.
By designing tools properly, you can build:
- Automated development pipelines
- Intelligent code assistants
- AI operations systems
- Enterprise internal AI tool platforms
I. What are custom tools
A custom tool is essentially a function interface that the LLM can actively call during execution and obtain results from.
Typical application scenarios:
- Query databases (SQL / NoSQL)
- Call internal APIs
- Execute scripts (Python / Shell)
- Read system status or environment information
- Encapsulate complex business logic
You can understand it as:a plugin mechanism that gives AI execution capabilities。
II. Tool storage locations
OpenCode automatically loads tools from the following directories:
- Project-level:
.opencode/tools/ - Global level:
~/.config/opencode/tools/
The file name is the tool name (very important).
III. Basic tool definition (recommended method)
Use tool() Helper functions provide type safety and parameter validation.
import { tool } from "@opencode-ai/plugin"
export default tool({
description: "Query the project database",
args: {
query: tool.schema.string().describe("SQL query to execute"),
},
async execute(args) {
return `Executed query: ${args.query}`
},
})
Explanation:
description: tool description, used by the LLM to decide whether to callargs: parameter definition (based on Zod)execute: actual execution logic
IV. Multiple tools in a single file
A file can export multiple tools, each automatically named:
<文件名>_<导出名>
import { tool } from "@opencode-ai/plugin"
export const add = tool({
description: "Add two numbers",
args: {
a: tool.schema.number(),
b: tool.schema.number(),
},
async execute(args) {
return args.a + args.b
},
})
export const multiply = tool({
description: "Multiply two numbers",
args: {
a: tool.schema.number(),
b: tool.schema.number(),
},
async execute(args) {
return args.a * args.b
},
})
Finally, generated tools:
math_addmath_multiply
V. Conflicts with built-in tools
If your tool has the same name as a built-in tool, it willoverride the built-in tool。
// 覆盖内置 bash 工具
export default tool({
description: "Restricted bash wrapper",
args: {
command: tool.schema.string(),
},
async execute(args) {
return `blocked: ${args.command}`
},
})
Note:
- Avoid naming conflicts
- If you only want to limit capabilities, prefer using
permissionConfigure
VI. Parameter definition (Zod)
Parameters are defined using Zod Schema, supporting type validation and descriptions:
args: {
query: tool.schema.string().describe("SQL query"),
limit: tool.schema.number().optional(),
}
You can also use Zod directly:
import { z } from "zod"
export default {
description: "Example tool",
args: {
name: z.string(),
},
async execute(args) {
return `Hello ${args.name}`
},
}
VII. Context
Context information is automatically injected when a tool executes:
export default tool({
description: "Get project info",
args: {},
async execute(args, context) {
const { agent, sessionID, directory, worktree } = context
return `Agent: ${agent}, Dir: ${directory}`
},
})<
Available fields:
agent: current agentsessionID: session IDmessageID: message IDdirectory: Current working directoryworktree: Git root directory
VIII. Calling external languages (Python example)
You can implement tool logic in any language, such as Python.
1. Python script
# .opencode/tools/add.py
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
print(a + b)
2. Tool wrapper
import { tool } from "@opencode-ai/plugin"
import path from "path"
export default tool({
description: "Add two numbers using Python",
args: {
a: tool.schema.number(),
b: tool.schema.number(),
},
async execute(args, context) {
const script = path.join(context.worktree, ".opencode/tools/add.py")
const result = await Bun.$`python3 ${script} ${args.a} ${args.b}`.text()
return result.trim()
},
})
This approach is suitable for:
- Existing Python toolchain
- Calling data analysis / AI models
- Executing complex computational tasks
IX. Best practices
- Tool descriptions should be clear to help the LLM choose correctly
- Design parameters to be as simple and clear as possible
- Avoid side effects (unless explicitly needed)
- Prefer controlling risky operations through permission
- Tools should focus on a single responsibility