custom-tools.mdx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ---
  2. title: Custom Tools
  3. description: Create tools the LLM can call in opencode.
  4. ---
  5. Custom tools are functions you create that the LLM can call during conversations. They work alongside opencode's [built-in tools](/docs/tools) like `read`, `write`, and `bash`.
  6. ---
  7. ## Creating a tool
  8. Tools are defined as **TypeScript** or **JavaScript** files.
  9. ---
  10. ### Location
  11. They can be defined:
  12. - Locally by placing them in the `.opencode/tool/` directory of your project.
  13. - Or globally, by placing them in `~/.config/opencode/tool/`.
  14. ---
  15. ### Structure
  16. The easiest way to create tools is using the `tool()` helper which provides type-safety and validation.
  17. ```ts title=".opencode/tool/database.ts" {1}
  18. import { tool } from "@opencode-ai/plugin"
  19. export default tool({
  20. description: "Query the project database",
  21. args: {
  22. query: tool.schema.string().describe("SQL query to execute"),
  23. },
  24. async execute(args) {
  25. // Your database logic here
  26. return `Executed query: ${args.query}`
  27. },
  28. })
  29. ```
  30. The **filename** becomes the **tool name**. The above creates a `database` tool.
  31. ---
  32. ### Arguments
  33. You can use `tool.schema`, which is just [Zod](https://zod.dev), to define argument types.
  34. ```ts "tool.schema"
  35. args: {
  36. query: tool.schema.string().describe("SQL query to execute")
  37. }
  38. ```
  39. You can also import [Zod](https://zod.dev) directly and return a plain object:
  40. ```ts {6}
  41. import { z } from "zod"
  42. export default {
  43. description: "Tool description",
  44. args: {
  45. param: z.string().describe("Parameter description"),
  46. },
  47. async execute(args, context) {
  48. // Tool implementation
  49. return "result"
  50. },
  51. }
  52. ```
  53. ---
  54. ## Context
  55. Tools receive context about the current session:
  56. ```ts title=".opencode/tool/project.ts" {8}
  57. import { tool } from "@opencode-ai/plugin"
  58. export default tool({
  59. description: "Get project information",
  60. args: {},
  61. async execute(args, context) {
  62. // Access context information
  63. const { agent, sessionID, messageID } = context
  64. return `Agent: ${agent}, Session: ${sessionID}, Message: ${messageID}`
  65. },
  66. })
  67. ```
  68. ---
  69. ## Multiple tools per file
  70. You can also export multiple tools from a single file. Each export becomes **a separate tool** with the name **`<filename>_<exportname>`**:
  71. ```ts title=".opencode/tool/math.ts"
  72. import { tool } from "@opencode-ai/plugin"
  73. export const add = tool({
  74. description: "Add two numbers",
  75. args: {
  76. a: tool.schema.number().describe("First number"),
  77. b: tool.schema.number().describe("Second number"),
  78. },
  79. async execute(args) {
  80. return args.a + args.b
  81. },
  82. })
  83. export const multiply = tool({
  84. description: "Multiply two numbers",
  85. args: {
  86. a: tool.schema.number().describe("First number"),
  87. b: tool.schema.number().describe("Second number"),
  88. },
  89. async execute(args) {
  90. return args.a * args.b
  91. },
  92. })
  93. ```
  94. This creates two tools: `math_add` and `math_multiply`.