JavaScript or TypeScript modules that extend OpenCode by hooking into events.
A plugin is a standalone code module that adds functionality to OpenCode without modifying its core. Plugins subscribe to events (via hooks) and can add custom tools, inject environment variables, or run side effects.
A plugin is a JavaScript or TypeScript file that exports a default function. That function receives context about the project and returns an object containing hooks and optionally custom tools. OpenCode loads plugins from local directories or from npm at startup.
The plugin function is called with a set of parameters:
export default function({ project, client, $, directory, worktree }) {
// project: project metadata
// client: OpenCode API client
// $: shell helper (for running commands)
// directory: current working directory
// worktree: git worktree path
return {
hooks: { /* event handlers */ },
tools: [ /* custom tool definitions */ ],
};
}
A plugin file has three key parts:
tool() helperimport { tool } from "@opencode-ai/plugin";
export default function({ directory }) {
return {
hooks: {
"file.edited": async (event) => {
console.log("Edited:", event.path);
},
},
tools: [
tool({
name: "my-custom-tool",
description: "Does something specific",
parameters: { type: "object", properties: {} },
execute: async (args) => {
return "result from custom tool";
},
}),
],
};
}
Plugins are loaded from four locations, in this order:
~/.config/opencode/opencode.json under "plugin"opencode.json under "plugin"~/.config/opencode/plugins/.opencode/plugins/For npm plugins, add a package.json to .opencode/ with your dependencies. OpenCode auto-installs them via Bun at startup.
Local plugin (simplest):
# Create the plugins directory
mkdir -p .opencode/plugins
# Create your plugin file
cat > .opencode/plugins/my-plugin.ts <<'EOF'
export default function({ directory }) {
return {
hooks: {
"session.created": async () => {
console.log("Session started in", directory);
},
},
};
}
EOF
npm plugin:
# Add a package.json to .opencode/
cat > .opencode/package.json <<'EOF'
{
"dependencies": {
"@opencode-ai/plugin": "latest"
}
}
EOF
# Reference the npm package in opencode.json
cat > opencode.json <<'EOF'
{
"plugin": ["my-opencode-plugin"]
}
EOF
OpenCode installs npm dependencies via Bun automatically on startup.
A plugin that prints a message when a session starts and adds a custom tool:
// .opencode/plugins/greet.ts
import { tool } from "@opencode-ai/plugin";
export default function({ directory }) {
return {
hooks: {
"session.created": async () => {
console.log("Welcome! Session active in:", directory);
},
},
tools: [
tool({
name: "greet",
description: "Returns a greeting",
parameters: {
type: "object",
properties: {
name: { type: "string", description: "Who to greet" },
},
required: ["name"],
},
execute: async ({ name }) => {
return `Hello, ${name}!`;
},
}),
],
};
}
@opencode-ai/plugin, but it is optional.