read, write, and bash.
Creating a tool
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.Location
They can be defined:- Locally by placing them in the
.opencode/tools/directory of your project. - Or globally, by placing them in
~/.config/opencode/tools/.
Structure
The easiest way to create tools is using thetool() helper which provides type-safety and validation.
.opencode/tools/database.ts
database tool.
Multiple tools per file
You can also export multiple tools from a single file. Each export becomes a separate tool with the name<filename>_<exportname>:
.opencode/tools/math.ts
math_add and math_multiply.
Tool API
Tool definition
Thetool() function accepts an object with the following properties:
description(required): A clear description of what the tool does. The LLM uses this to decide when to call your tool.args(required): A Zod schema object defining the tool’s parameters. Usetool.schema(which is Zod) to define types.execute(required): An async function that implements the tool’s logic. Must return a string that will be shown to the LLM.
Arguments
You can usetool.schema, which is just Zod, to define argument types.
Available schema types
Sincetool.schema is Zod, you have access to all Zod types:
.describe() to help the LLM understand what each parameter is for:
Context
Tools receive context about the current session:.opencode/tools/project.ts
Context properties
directory: Use this instead ofprocess.cwd()when resolving relative pathsworktree: Useful for generating stable relative paths withpath.relative(worktree, absPath)abort: Checkabort.abortedto detect if the user cancelled the operationmetadata(): Update the tool’s title or add custom metadata shown in the UIask(): Request user permission during tool execution
Using metadata
Handling cancellation
Examples
Write a tool in Python
You can write your tools in any language you want. Here’s an example that adds two numbers using Python. First, create the tool as a Python script:.opencode/tools/add.py
.opencode/tools/python-add.ts
Bun.$ utility to run the Python script.
Database query tool
Create a tool that executes SQL queries:.opencode/tools/db-query.ts
API client tool
Create a tool that calls an external API:.opencode/tools/github-search.ts
File system tool
Create a tool that performs custom file operations:.opencode/tools/count-lines.ts
Shell command tool
Create a tool that wraps shell commands:.opencode/tools/docker-ps.ts
Tool registration via plugins
You can also register tools through plugins instead of separate files:.opencode/plugins/my-tools.ts
- You want to group related tools together
- Your tools need shared state or initialization
- You want to conditionally register tools based on plugin configuration