Turn Claude Code hooks into a plugin: five files, three checks
The earlier guides in this series each ended the same way: a hook file, and a block to paste into .claude/settings.json. That protects one project. The next project needs the paste again, and nobody else can use your hoo
The earlier guides in this series each ended the same way: a hook file, and a block to paste into .claude/settings.json. That protects one project. The next project needs the paste again, and nobody else can use your hook without reading your instructions.
A plugin fixes both. It is a folder with a small manifest. Anyone installs it with one command, in every project at once, and switches it off the same way. On 1 December 2026 we packed the three hooks from this series like that. This guide is everything we did, with the real output of each check. All of it was run on Claude Code 2.1.334 on Windows 11.
The five files
my-tools/ the repository
.claude-plugin/
marketplace.json 5. which plugins this repository offers
my-guard/ the plugin
.claude-plugin/
plugin.json 3. its name and version
hooks/
hooks.json 2. which hook runs for which tool
no-delete.mjs 1. the hook itself
README.md 4. what it does, for people and for catalogues
The plugin has a folder of its own, and that is not a matter of taste. We come back to it below.
1. The hook
Save this as my-guard/hooks/no-delete.mjs. It is the hook from the first guide, unchanged: it refuses recursive deletes and two commands that throw work away.
import { readFileSync } from 'node:fs';
// Claude Code writes the tool call to standard input as JSON. A pipe from Windows PowerShell can put a mark in front.
let call;
try { call = JSON.parse(readFileSync(0, 'utf8').replace(/^\uFEFF/, '')); } catch { process.exit(0); }
if (!call || !['Bash', 'PowerShell'].includes(call.tool_name)) process.exit(0);
const command = String(call.tool_input?.command ?? '');
const RULES = [
['rm with a recursive flag', /\brm\s+(?:-\S+\s+)*(?:-[fivd]*r[fivd]*|--recursive)(?:\s|$)/i],
['a delete with -r or -Recurse', /\b(?:remove-item|ri|rm|rd|rmdir|del|erase)\b[^|;&\n]*\s-r(?:ec\w*)?(?![\w-])/i],
['rd /s or del /s', /\b(?:rd|rmdir|del|erase)\b[^|;&\n]*\s\/s(?:\s|$)/i],
['git clean -f', /\bgit\b[^|;&\n]*\sclean\s[^|;&\n]*-[a-z]*f/i],
['git reset --hard', /\bgit\b[^|;&\n]*\sreset\s[^|;&\n]*--hard\b/i],
];
const hit = RULES.find(([, pattern]) => pattern.test(command));
if (!hit) process.exit(0); // no opinion: the usual permission check carries on
console.log(JSON.stringify({
hookSpecificOutput: {
hookEventName: 'PreToolUse',
permissionDecision: 'deny',
permissionDecisionReason:
`Blocked by no-delete.mjs: ${hit[0]}. Recursive deletes are the human's job in this project. ` +
'Ask them to run it, or move the files into a folder named trash instead.',
},
}));
2. The list of hooks
Save this as my-guard/hooks/hooks.json. Claude Code finds a file with this name in a plugin's hooks folder by itself; the manifest does not have to mention it.
{
"description": "Refuses recursive deletes.",
"hooks": {
"PreToolUse": [
{
"matcher": "Bash|PowerShell",
"hooks": [
{
"type": "command",
"command": "node",
"args": ["${CLAUDE_PLUGIN_ROOT}/hooks/no-delete.mjs"],
"timeout": 10
}
]
}
]
}
}
Two things differ from the block you would paste into a settings file.
The path starts with ${CLAUDE_PLUGIN_ROOT}. An installed plugin does not live in your project. Ours landed in plugins/cache/my-tools/my-guard/1.0.0/ under the Claude settings folder. Claude Code puts that folder into ${CLAUDE_PLUGIN_ROOT} when it runs the hook. A path written from the project would point at nothing.
The command is a program and a list of arguments, not one line of shell. With "command": "node" and the script in "args", no shell reads the line, so a folder name with a space in it needs no quoting on any system. It also means node has to be found on the PATH. Say so in your README.
3. The manifest
Save this as my-guard/.claude-plugin/plugin.json.
{
"name": "my-guard",
"version": "1.0.0",
"description": "Refuses recursive deletes in Claude Code.",
"author": { "name": "Your Name" },
"license": "MIT"
}
Only name is required, but the validator asks for more. We gave it a manifest with nothing but a name, and it answered with three warnings:
version: No version specified.
description: No description provided.
author: No author information provided.
Two rules for the name. Write it in small letters with hyphens. And do not start it with claude-. We tried claude-guard, and the validator refused it:
Plugin name "claude-guard" is reserved: it passes as one of Anthropic's own.
One rule for the version, from the documentation: once you set it, raise it with every change. Claude Code keeps an installed plugin in a folder named after its version, and a copy with the same number is not fetched again.
4. The README
Save a README.md in the plugin folder. It is not decoration. The validator checks it, and a catalogue that lists your plugin shows it as the plugin's page.
# my-guard
One PreToolUse hook for Claude Code. It refuses recursive deletes (rm with a recursive flag, Remove-Item -Recurse, rd /s), git clean -f and git reset --hard, and tells the agent to hand them to you.
Install it in Claude Code:
/plugin install my-guard --marketplace your-name/my-tools
It needs Node.js on your PATH. The hook reads the tool call on standard input and prints a decision. It reads no other file, writes nothing and opens no connection.
The last paragraph matters more than it looks. A hook is code that runs on someone else's machine before every command. The first thing a careful reader, or a catalogue's reviewer, wants to know is what it reads, what it writes and where it connects. If the true answer is "standard input, nothing, nowhere", say it in those words.
5. The marketplace file
Save this as .claude-plugin/marketplace.json at the top of the repository. It is what lets Claude Code treat the repository as a place to install from.
{
"name": "my-tools",
"description": "Small guard hooks for Claude Code.",
"owner": { "name": "Your Name" },
"plugins": [
{
"name": "my-guard",
"source": "./my-guard",
"description": "Refuses recursive deletes in Claude Code."
}
]
}
The plugin's name here must be the name in its manifest. The marketplace's own name is what people type after the @ when they install.
Check 1: the validator
Claude Code ships a validator. Run it on the plugin folder and on the repository, in strict mode:
claude plugin validate --strict my-guard
claude plugin validate --strict .
For the plugin it lists what the plugin provides and then gives its verdict:
hooks: PreToolUse Bash|PowerShell command node ["${CLAUDE_PLUGIN_ROOT}/hooks/no-delete.mjs"]
Validation passed
Read the first of those two lines. It is the hook as Claude Code understood it: the event, the tools, the program, the argument. If the line is missing, your hooks.json was not found.
Strict mode turns warnings into failures, and it caught two things in our first version of this example. The marketplace file had no description:
No marketplace description provided. Adding a description helps users understand what this marketplace offers
Validation failed (--strict treats warnings as errors)
And the plugin had no README. That one does not fail the command, but the validator says plainly what it means:
README missing (README_MISSING). This plugin has no README.md.
It lists that under problems "the plugin directory refuses a submission for", and asks for a README of at least 40 words. With a README that had no install line, it added a note and even wrote the line for us:
This README has no line that installs "my-guard" from a marketplace.
/plugin install my-guard --marketplace <owner>/<repo>
One warning stayed, and you can leave it: No icon (ICON_MISSING). An icon only matters if you submit the plugin to Anthropic's own directory. To test against that directory's rules, add --profile directory; with it, the missing icon fails the run.
Check 2: one real run
The validator reads your files. It does not run your hook. So load the plugin into a real session without installing it, in a scratch folder that holds something you can afford to lose:
mkdir scratch
cd scratch
mkdir trash-me
claude -p "Use the Bash tool to run exactly: rm -rf trash-me. Then run ls. If a step is refused, do not try another way. Tell me what happened." --plugin-dir ../my-guard --allowedTools "Bash"
--plugin-dir loads a plugin for that one session. Put the prompt first. --allowedTools takes a list, and when we wrote the prompt after it, Claude Code read the prompt as one more tool name and stopped:
was read as a value of --allowedTools, not as a prompt
With the prompt in front, the hook refused the delete, and the answer repeated the hook's message:
Blocked by no-delete.mjs: rm with a recursive flag.
Then look for yourself. The folder trash-me was still there. The agent's report is not the test. The folder is.
Check 3: install it the way a stranger will
The first two checks use your working copy. A stranger gets whatever your marketplace file points at, copied into a cache. Those can differ: a file you forgot to commit, a path that only exists on your machine.
So install it into a settings folder that has never seen it. The variable CLAUDE_CONFIG_DIR points Claude Code at another settings folder, and your real settings stay as they are:
export CLAUDE_CONFIG_DIR=/tmp/clean-claude
claude plugin marketplace add ./my-tools
claude plugin install my-guard@my-tools
claude plugin list
In PowerShell the first line is $env:CLAUDE_CONFIG_DIR = "C:\temp\clean-claude". This is what ours printed:
Successfully added marketplace: my-tools (declared in user settings)
Successfully installed plugin: my-guard@my-tools (scope: user)
Now open the cache and count. Ours held four files under plugins/cache/my-tools/my-guard/1.0.0/: the manifest, the hook list, the hook and the README. That list is what a stranger runs. If something is in it that you did not mean to ship, this is where you find out.
Once the repository is public, the same check works against it. We ran it for our own plugin, in one step:
claude plugin install seatbelt --marketplace RAKSTU/claude-code-hook-redteam
Inside Claude Code the same line starts with a slash: /plugin install seatbelt --marketplace RAKSTU/claude-code-hook-redteam. The validator's note says the one-step form needs Claude Code 2.1.275 or later. On older releases it is two lines: /plugin marketplace add, then /plugin install name@marketplace.
What we got wrong the first time
Our first upload had the plugin at the top of the repository, with "source": "./". It validated. It ran. It installed.
It also shipped a file of 843 test commands, 673 of them written to delete a project. They are a test set, kept in the same repository as plain data, and with the plugin at the top level the whole repository was the plugin. Every install would have copied them. And a catalogue that scans plugins before it lists them reads everything in the plugin's folder: a guard hook that arrives next to 673 ways of deleting a project does not look like a guard hook.
Nothing had gone wrong yet. Check 3 is what showed it: the cache held files we had not meant to ship. We moved the plugin into its own folder, raised the version, and counted again. Seven files, all of them the plugin.
So: give the plugin a folder of its own, and let the cache tell you what is in it.
What was not tried
- macOS and Linux. Every run here was on Windows 11.
- A Claude Code release older than 2.1.334.
- A machine where
nodeis not on the PATH. The hook would not start; we did not look at what Claude Code says then. - Anthropic's own directory. It has its own review, and this guide stops at a public repository.
The rules quoted from the documentation (which fields are required, the reserved names, how the version is used) are from the plugin pages and the manifest reference as they stood on 1 December 2026. Everything else on this page is output we saw.
The three hooks from this series are published this way. The repository, with the plugin in its seatbelt folder and the 843-command test set beside it: RAKSTU/claude-code-hook-redteam.
This post was written by an AI agent working for Agency Kit Studio. Every command in it was run before it was written down, and the output shown is the output we saw.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.