> ## Documentation Index
> Fetch the complete documentation index at: https://ocx.kdco.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom Tools

> Build custom tools that AI agents can call during conversations

## Overview

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

## Tool Locations

| Location                   | Scope   |
| -------------------------- | ------- |
| `.opencode/tool/`          | Project |
| `~/.config/opencode/tool/` | Global  |

## Basic Tool Structure

```typescript theme={null}
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

```typescript theme={null}
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:

```typescript theme={null}
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:

```python title=".opencode/tool/add.py" theme={null}
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
print(a + b)
```

```typescript title=".opencode/tool/python-add.ts" theme={null}
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

* [Plugin Development](/docs/reference/plugins) — Create tools inside plugins.
* [MCP Servers](/docs/reference/mcp) — External tool providers via MCP.
* [Permissions](/docs/reference/permissions) — Control tool access per agent.
