Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCode Custom Tools

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:


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:

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:

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:


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:


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:


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:


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:


IX. Best practices