Skip to main content

Overview

Custom tools are functions the LLM can call during conversations. They extend what AI agents can do beyond built-in capabilities.

Tool Locations

LocationScope
.opencode/tool/Project
~/.config/opencode/tool/Global

Basic Tool Structure

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}`
  },
})
The filename becomes the tool name.

Multiple Tools Per File

import { tool } from "@opencode-ai/plugin"

export const add = tool({
  description: "Add two numbers",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second number"),
  },
  async execute(args) {
    return args.a + args.b
  },
})

export const multiply = tool({
  description: "Multiply two numbers",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second number"),
  },
  async execute(args) {
    return args.a * args.b
  },
})
Creates tools: math_add and math_multiply (filename prefix + export name).

Tool Context

Access session and agent information:
export default tool({
  description: "Get project information",
  args: {},
  async execute(args, context) {
    const { agent, sessionID, messageID } = context
    return `Agent: ${agent}, Session: ${sessionID}`
  },
})

Using External Languages

Wrap non-TypeScript tools with a TypeScript shell:
.opencode/tool/add.py
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
print(a + b)
.opencode/tool/python-add.ts
import { tool } from "@opencode-ai/plugin"

export default tool({
  description: "Add two numbers using Python",
  args: {
    a: tool.schema.number().describe("First number"),
    b: tool.schema.number().describe("Second number"),
  },
  async execute(args) {
    const result = await Bun.$`python3 .opencode/tool/add.py ${args.a} ${args.b}`.text()
    return result.trim()
  },
})

See Also