← BACK TO FIELD NOTES
GUIDE11 MIN READ

Building a Claude Code Plugin for an Internal Library

How I turned an MCP server into a Claude Code plugin with skills, agents, and an enforcement hook, and the two 'correct code, wrong behavior' bugs that cost real debugging time.

PUBLISHED:November 1, 2026
TRACK:PLUGIN-DEVELOPMENT
TAGS:
#frontend#agentic-harness#mcp#architecture

Hero

I built a Claude Code plugin for our internal UI library, a set of roughly 30 Angular components. The goal: make AI coding assistants write correct code against that library, without every developer relearning its gotchas the hard way.

The first version was just an MCP server (the story of that is in How We Built an MCP Server for Our UI Library). Useful, but it’s only advice. Nothing stops a developer, or an AI agent, from ignoring the answer and writing wrong code anyway.

So the plugin wraps that server with the pieces that make the knowledge proactive: slash commands, skills that trigger on their own, specialist agents, and a hook that checks generated code against the library’s rules the moment it’s written. It ships as one installable unit.

In this post:

  • How a plugin is laid out and wired together
  • Skills, agents, and the hook that turns advice into enforcement
  • Two bugs where the code was correct and the behavior was still wrong: a stale MCP snapshot and a stale marketplace cache

Who this is for: you already use Claude Code and maintain a library or codebase with rules that aren’t obvious from the types alone. A CLAUDE.md file can carry some of those rules, but it can’t be versioned and shipped as a unit, can’t run checks against generated code, and can’t scope tool access per task. A plugin can.

Plugin setup

A Claude Code plugin lives in one folder. The manifest, .claude-plugin/plugin.json, holds its metadata, and every other component sits at the plugin root, not inside .claude-plugin/.

Here is a simplified layout. The real plugin also has the slash-command skills and vitest, but the shape is the same:

ui-plugin/
├── .claude-plugin/
│   └── plugin.json
├── skills/
│   ├── component-usage/
│   │   └── SKILL.md
│   ├── angular-patterns/
│   │   └── SKILL.md
│   └── code-governance/
│       └── SKILL.md
├── agents/
│   ├── ui-specialist.md
│   └── ui-migrator.md
├── hooks/
│   └── hooks.json
└── .mcp.json

Claude Code finds components in these default locations on its own. If you write a manifest, only name is required. The rest is metadata, plus userConfig and custom paths for components that live outside the default directories. Ours is minimal:

{
  "name": "ui-plugin",
  "version": "0.3.0",
  "description": "Tools, skills, and agents for working with the internal UI library"
}

The MCP server is its own npm package, installed globally with npm install -g @org/ui-mcp. The plugin declares it in .mcp.json at the plugin root, and Claude Code launches the installed binary:

{
  "mcpServers": {
    "ui-library-server": {
      "command": "ui-mcp",
      "args": [],
      "env": {}
    }
  }
}

Three things to know:

  • skills/, agents/ and hooks/hooks.json are discovered automatically. hooks.json has the same shape as hooks in settings.json.
  • .mcp.json lists MCP servers by name. Each entry has a command and args that Claude Code runs as a stdio process.
  • Every skill gets a name you can invoke as a slash command, namespaced by the plugin (/ui-plugin:ui-from-figma). Whether Claude can also load it on its own is a separate setting (next section).

At runtime, Claude Code prefixes the server’s tool IDs with the plugin name and server name, so plugin and server are two packages that Claude Code sees as one unit.

That’s the whole bundling model: one manifest as the source of truth, one version bump as the deployment signal. To load a local plugin during development, pass --plugin-dir ./path/to/plugin. For installed plugins, restart or run /reload-plugins to pick up changes.

Skills: auto-invoking and forced

Skills are SKILL.md files with frontmatter. Claude Code loads a skill’s body only when relevant, so long reference material costs almost nothing until it’s needed.

The frontmatter fields that mattered for us:

  • name and description: what Claude matches against the current task. Put the words developers actually use in the description (“component”, “props”, “API”).
  • disable-model-invocation: true: only the user can trigger the skill, by slash command. Without it, Claude can also load the skill on its own.
  • context: fork (with an optional agent): run the skill in an isolated subagent, away from the main conversation.

We split our skills by who should trigger them:

  • Slash-command only: /ui, /from-screenshot, /from-figma. They route to specific MCP tools on demand.
  • Auto-invoking: component-usage, angular-patterns, code-governance, vitest. Nobody has to remember to ask.

component-usage, for example, carries guidance on component props, events, and pitfalls. Claude evaluates available skills against the current task and loads the ones whose descriptions match.

When auto-invocation isn’t enough

Auto-invocation is unreliable. Claude skips skills when the task is complex or when the prompt uses different terminology than the description.

The workaround is a UserPromptSubmit hook that injects an instruction when the prompt matches a pattern. Ours looks for a Figma URL. Another installed Figma skill can win the match, so the hook names the skill to use:

{
  "hooks": {
    "UserPromptSubmit": [
      { "hooks": [{ "type": "command", "command": "ui-figma-route-hook" }] }
    ]
  }
}

The text it injects:

This workspace depends on the internal UI library. The prompt includes a Figma URL: before calling any Figma MCP tool, invoke the ui-plugin:ui-from-figma skill (even if another installed Figma skill also seems to apply). It wraps the Figma fetch with the required component and token mapping and the render-and-verify checklist that a generic Figma-to-code skill does not know to run.

The hook is strictly additive: it never blocks or errors a prompt, and it injects nothing when there’s no Figma URL. Because it’s gated on the URL, the token cost is paid only on prompts that need it.

Forking skills

Add context: fork and Claude Code runs the skill in a fresh subagent, with the skill content as its prompt. The subagent doesn’t see conversation history, so the skill’s instructions must be self-contained. Use it for skills that read many files or run tool-heavy analysis, not for short reference material like component-usage.

---
name: audit-component-usage
description: Scan the repo for deprecated library components and report every call site.
context: fork
---

Agents and their permissions

An agent is a markdown file in agents/ that Claude Code can dispatch to as a subagent. Agents run in isolated context windows and don’t see the main conversation, so their instructions have to stand on their own.

We ship two:

AgentJobTools (plus the MCP read tools)Never
ui-specialistBuild or review code, return it, don’t saveRead, Grep, GlobWrite, Edit, Bash
ui-migratorRun and verify library upgrade migrationsRead, Grep, Glob, Write, Edit, Bashcommit (prompt rule only)

The principle: an agent gets the smallest set of tools that lets it do its one job, and the grant is pinned by a test, not a docstring. We pin each agent’s tool access with a static contract test, so if someone quietly widens an agent’s access later, the test fails instead of the change shipping unnoticed.

The two agents show the two kinds of boundary. For ui-specialist, the harness draws the line: it has no Write, Edit or Bash, so there is nothing to talk it out of. For ui-migrator, the line is softer. It needs Bash to run the build and tests, so “never commit” lives in its instructions rather than in its tool list. No hook blocks git commit for it today. That’s a weaker guarantee, and a PreToolUse hook on Bash is the fix I’d reach for next.

Tool access is scoped with the tools field in the agent file’s frontmatter.

The hook: from advice to enforcement

This is the piece that moves the plugin from advice to enforcement. The MCP server can tell you the right API. It can’t stop you writing the wrong code.

Hooks run at points in Claude Code’s lifecycle (SessionStart, UserPromptSubmit, PreToolUse, PostToolUse, Stop, SubagentStart, SubagentStop, and more). A hook receives JSON about the event, can return a decision, and can feed extra context back to Claude.

The plugin wires four hooks: two PreToolUse (a component precheck on Write|Edit, and a version check on the MCP tools), the Figma UserPromptSubmit hook above, and the one that matters most here, a PostToolUse on Write|Edit. It checks the written code against the library’s rules and feeds violations back to the model before it moves on to the next tool. The model sees its own mistake before it ships.

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit",
        "hooks": [
          {
            "type": "command",
            "command": "ui-validate-hook"
          }
        ]
      }
    ]
  }
}

The matcher narrows which tool calls trigger the hook. With no matcher, it fires on every call of that event type. Our hook commands (ui-validate-hook, ui-figma-route-hook) are npm-installed executables.

The script reads the event JSON from stdin and returns output deliberately non-blocking. It never throws, and it never stops the model, it only informs it:

export function buildHookOutput(violations: GovernanceViolation[]): HookOutput {
  if (violations.length === 0) return { continue: true };

  return {
    continue: true,
    hookSpecificOutput: {
      hookEventName: "PostToolUse",
      additionalContext: violations.map(formatViolation).join("\n"),
    },
  };
}

additionalContext is passed into Claude’s context as a system reminder at the point where the hook fired. Claude reads it on the next model request; it doesn’t appear as a chat message. If several hooks return additionalContext for the same event, Claude Code merges them into one reminder.

What it looks like in practice

Real output from running the hook on a file containing a native <select>:

// 1. Code written:
<select (change)="pick($event)"><option>a</option></select>

// 2. Hook feedback the model sees:
@org/ui-components governance: 1 violation(s) in bad.component.html
- no-native-form-elements (line 1): Replace native form elements with ui-* equivalents (ui-button, ui-input, ui-select, ui-textarea).
// 3. Corrected edit (hook returns nothing on re-check):
<ui-select [items]="rows" labelKey="name" valueKey="id" (selectionChange)="pick($event)"></ui-select>

I watched it fire unprompted on real Write and Edit calls. That was the moment it stopped being a spec.

Bug 1: The stale MCP server

The MCP server serves a snapshot bundled at install time, not a live read of your checkout. Its output can be stale relative to the branch you’re working on.

The concrete case: ui-select is single-select only (ui-multi-select is a separate component), but the global server’s get_component() reported a different API. Code generated from that answer was wrong, while the source on the branch was right.

I only caught it by comparing the server’s answer against the component’s source and its prompt YAML. They disagreed, and the source was right.

Symptom: generated code uses an API that doesn’t match your branch. Root cause: the server answers from the snapshot taken at global-install time. A branch that’s been running for weeks can describe components from a month ago. Fix: operational, not architectural. Reinstall the server when your branch changes: npm install -g @org/ui-mcp.

The standing rule that came out of this: never treat live MCP output as ground truth for anything going into a deliverable. When precision counts, read the YAML or the source.

Bug 2: The marketplace cache

Claude Code doesn’t load the plugin from a live checkout. It loads from a cached snapshot of the marketplace the plugin was installed from.

So a version bump in plugin.json is invisible to a running session until all of these have happened:

  1. The change is pushed.
  2. It’s merged into the marketplace-tracked ref.
  3. You run an explicit plugin update (claude plugin update ui-plugin, or /plugin inside a session) and reload (/reload-plugins).

Symptom: I added hooks.json, bumped the plugin to 0.3.0, and the hook never fired in my session. Root cause: the session loads ~/.claude/plugins/cache/<marketplace>/ui-plugin/<version>/, not the worktree. For a local demo I patched the cached copy in place, which reverts on the next plugin update. Fix: the three steps above. Correct code, wrong behavior.

A related gotcha: the hook binary silently did nothing when run as an npm-linked command. Its “am I the entry point” guard compared paths without resolving symlinks, so it never ran main(). Resolve with realpathSync before comparing.

Same bug, different layers

Both bugs have the same shape: a gap between “the code is correct” and “what a user actually runs reflects that.” Testing the logic in isolation proves the first. It tells you nothing about the second.

Anything shipped through a packaged or cached distribution path needs its own answer to “how does this reach a running session,” as a verification step alongside the tests.

Flow diagram

Did it work?

I don’t have numbers yet. What I watched: the hook firing unprompted on real Write and Edit calls, and the model correcting itself before moving on. What I’d measure next:

  • Hook fires per week, split by rule, to see which rule catches the most.
  • Wrong-prop and native-element comments in code review before and after the plugin.
  • How often the model’s next edit clears the violation without a human prompt.

References & Sources