Original URL: https://getagenttools.com/blog/opencode-hooks/

# opencode hooks: tool.execute.before in a plugin, and the permission hook that never fires

1 Nov 2026Marek.

- opencode
- coding-agents
- guardrails
- agent-security

Yes, opencode has hooks. They do not live in a config file. You write a plugin, a JavaScript or TypeScript module. It returns an object of hook functions. The one that matches Claude Code's `PreToolUse` is `tool.execute.before`. It runs before a tool call. If it throws, the call stops. `tool.execute.after` plays the `PostToolUse` role.

 

```ts
// .opencode/plugins/block-push.ts
export const BlockPush = async () => ({
  "tool.execute.before": async (input, output) => {
    if (input.tool === "bash" && output.args.command.includes("git push")) {
      throw new Error("git push is blocked by a plugin")
    }
  },
})
```

 

This is opencode 1.x code. The [migration guide](https://opencode.ai/v2/docs/build/plugins/migrate-v1/) says "V1 plugin implementations do not run in V2". The match is a toy. `git push` or `git -C repo push` slips past a substring check.

  

- **Where hooks live.** In plugin files under `.opencode/plugins/` or `~/.config/opencode/plugins/`, or in npm packages listed under `plugin` in `opencode.json`.
- **How a hook says no.** It throws an error. The docs show this with a plugin that refuses to read `.env` files.
- **What it can change.** Fields of `output.args`, like `output.args.command`.
- **The gap.** `permission.ask` is in the 1.x plugin types. In the 1.x source nothing calls it. opencode v2 replaces it with a permission `evaluate` hook.
- **How I know.** I do not use opencode. I read the docs and the v1.19.4 source on 25 October 2026. The example above is mine and untested.

 

How the Claude Code event works is in my [PreToolUse](https://getagenttools.com/blog/pretooluse-hook/) post.

 

## Does opencode have hooks like Claude Code?

 

Same hook points, different mechanism. In Claude Code a hook is a command in a settings file. It reads JSON on stdin. Exit code 2 blocks the call. In opencode a hook is a function inside a plugin. It gets two objects, `input` and `output`. It blocks by throwing.

 

The [plugins page](https://opencode.ai/docs/plugins/) lists the events. Under "Tool Events" there are two, `tool.execute.before` and `tool.execute.after`.

 

| Claude Code | opencode 1.x | opencode v2 |
| --- | --- | --- |
| `PreToolUse` in a settings file | `tool.execute.before` returned by a plugin | `ctx.tool.hook("execute.before", ...)` |
| `PostToolUse` | `tool.execute.after` | `ctx.tool.hook("execute.after", ...)` |
| Exit 2 or `"permissionDecision": "deny"` | Throw an error | Throw an error |
| `updatedInput` rewrites the call | Assign `output.args.command` and other fields | Change `event.input` |
| `"ask"` sends the call to a prompt | No working hook for this | `ctx.permission.hook("evaluate", ...)` sets `effect` |

 

The v2 column comes from the [V1 migration guide](https://opencode.ai/v2/docs/build/plugins/migrate-v1/). npm still ships 1.x as `latest`, 1.19.4 on 25 October. The guide's v2 example looks like this:

 

```ts
import { Plugin } from "@opencode/plugin"

export default Plugin.define({
  id: "company.guard",
  async setup(ctx) {
    await ctx.tool.hook("execute.before", (event) => {
      const input = event.input as { filePath?: string }
      if (event.tool === "read" && input.filePath?.includes(".env")) {
        throw new Error("Do not read .env files")
      }
    })
  },
})
```

 

## How do I write an opencode hooks plugin?

 

Put a `.ts` or `.js` file in `.opencode/plugins/` for one project. Use `~/.config/opencode/plugins/` for all projects. The docs say files there "are automatically loaded at startup". The discovery code also accepts a folder named `plugin`, singular. It scans `{plugin,plugins}/*.{ts,js}` ([config/plugin.ts, line 21](https://github.com/anomalyco/opencode/blob/v1.19.4/packages/opencode/src/config/plugin.ts#L21)).

 

A plugin is an async function that returns the hooks object. For types, import `Plugin` from `@opencode-ai/plugin`. The full list of hook keys sits in its `Hooks` interface ([plugin/src/index.ts, line 222](https://github.com/anomalyco/opencode/blob/v1.19.4/packages/plugin/src/index.ts#L222)).

 

One trap in the loader. Every export of a plugin file counts as a plugin. An exported constant makes it throw "Plugin export is not a function" ([plugin/index.ts, line 107](https://github.com/anomalyco/opencode/blob/v1.19.4/packages/opencode/src/plugin/index.ts#L107)). An exported helper function gets called as a plugin. Keep helpers unexported.

 

## How do I block a command with tool.execute.before?

 

Check `input.tool` and the arguments, then throw. This is the docs example, trimmed:

 

```js
export const EnvProtection = async () => {
  return {
    "tool.execute.before": async (input, output) => {
      if (input.tool === "read" && output.args.filePath.includes(".env")) {
        throw new Error("Do not read .env files")
      }
    },
  }
}
```

 

For shell commands the tool ID is `bash`. A comment in the source says it stays "for compatibility with existing plugins, users, and saved permissions. Rename with opencode 2.0" ([tool/shell/id.ts, line 14](https://github.com/anomalyco/opencode/blob/v1.19.4/packages/opencode/src/tool/shell/id.ts#L14)). The command sits in `output.args.command`. Assign to that field. The tool runs with the same `args` object it passed to the hook.

 

In `session/tools.ts` the hook fires first ([line 106](https://github.com/anomalyco/opencode/blob/v1.19.4/packages/opencode/src/session/tools.ts#L106)). The tool itself runs after it ([line 111](https://github.com/anomalyco/opencode/blob/v1.19.4/packages/opencode/src/session/tools.ts#L111)). The permission check lives inside the tool. For MCP tools it sits just below the hook ([line 408](https://github.com/anomalyco/opencode/blob/v1.19.4/packages/opencode/src/session/tools.ts#L408)). So a throw stops the call before any prompt shows.

 

A rewrite also changes what your permission rules see. Issue [\#35882](https://github.com/anomalyco/opencode/issues/35882) reports that. A plugin wrapped `grep` in another command. The `"grep *": "allow"` rule stopped matching. Claude Code documents the same for `updatedInput`.

 

I have not run a throw myself. A comment on issue [\#12472](https://github.com/anomalyco/opencode/issues/12472) reports a test on 1.18.25. Throwing stopped the tool. The reason reached the model. A bot reply on [\#52837](https://github.com/anomalyco/opencode/issues/52837) says the v1 trigger swallowed hook errors. The v1 docs and that test say a throw blocks.

 

## Is there opencode hooks support for permission prompts?

 

Not in 1.x. The `Hooks` interface declares `permission.ask` with an output of `ask`, `deny` or `allow` ([line 261](https://github.com/anomalyco/opencode/blob/v1.19.4/packages/plugin/src/index.ts#L261)). I searched the v1.19.4 source for a call to it and found none. Issue [\#7006](https://github.com/anomalyco/opencode/issues/7006) says the same. It closed on 1 October 2026 with this note: "This is v2 only. The 1.x `permission.ask` hook still doesn't fire."

 

In 1.x a hook can deny by throwing. It cannot send a call to the prompt. Claude Code can, with `"ask"`.

 

v2 adds `ctx.permission.hook("evaluate", ...)`. The [v2 guide](https://opencode.ai/v2/docs/build/plugins/) says it runs "after configured rules are evaluated and before an action runs or a permission prompt is published". A hook may set `effect` to `allow`, `ask` or `deny`. "An explicit configured `deny` is final and does not invoke the hook."

 

## opencode hooks config and docs: where to look

 

There is no `hooks` block in `opencode.json`. I found no hooks key in the 1.x config code. The only config route is the `plugin` array of npm packages. v2 renames that key to `plugins` and accepts local folders in it. Docs: [v1 plugins](https://opencode.ai/docs/plugins/) and the [v2 plugin guide](https://opencode.ai/v2/docs/build/plugins/).

 

Can opencode run the hooks already in your Claude Code `settings.json`? Not natively. Issue [\#12472](https://github.com/anomalyco/opencode/issues/12472) asks for it and is still open. opencode does read `~/.claude/CLAUDE.md`. I found no code in v1.19.4 that reads Claude Code's `settings.json`. Commenters there link community bridge plugins I have not checked.

 

Codex reads hooks from `hooks.json` with Claude Code's field names. See my [Codex hooks](https://getagenttools.com/blog/codex-hooks/) post.

 

## When would I use an opencode hook?

 

For a check that rules cannot express. A deny rule matches a pattern. A hook reads the whole argument string and decides in code. It is only as good as that code. That is the job `PreToolUse` does in Claude Code, which I use every day.

 

I would keep it short and deny-only on 1.x. A bug that throws stops every tool call it touches. If you only need to block a few commands, deny rules are simpler. They survive `--auto`, as I covered in [opencode --dangerously-skip-permissions](https://getagenttools.com/blog/opencode-dangerously-skip-permissions/).

 

## Frequently asked questions

 

### Does opencode support Claude Code hooks?

 

No. opencode does not read hooks from Claude Code's `settings.json`. Its own hooks live in plugins.

 

### What is the opencode equivalent of PreToolUse?

 

`tool.execute.before`, returned from a plugin function. Throw an error to block the call. Assign `output.args.command` to rewrite a shell command.

 

### Did you test this?

 

No. I do not run opencode. This post reads its docs and the v1.19.4 source. The Claude Code side is my own daily use.

 

Sources, read on 25 October 2026: opencode [plugins](https://opencode.ai/docs/plugins/) page (v1), the v2 [plugin guide](https://opencode.ai/v2/docs/build/plugins/) and [V1 migration guide](https://opencode.ai/v2/docs/build/plugins/migrate-v1/), the [anomalyco/opencode](https://github.com/anomalyco/opencode) repository at tag v1.19.4, issues \#7006, \#12472 and \#35882, and the Claude Code [hooks reference](https://code.claude.com/docs/en/hooks).

Put this into practice

[See the tool list](https://getagenttools.com/tools/)[Train your team](https://getagenttools.com/#training)
