# Can Git Hooks Keep MCP Agents on Rails?
**TL;DR:** AI agents drift from your intended tool hierarchy the moment you stop watching — and AGENTS.md alone won't save you. Geoffrey Huntley's insight is that a pre-commit hook acts as a hard enforcement layer: it inspects the diff, detects bypasses, and re-prompts the agent in the same loop. At FlipFactory, we applied this pattern across three MCP servers and cut tool-drift incidents by roughly 80% within two weeks.
---
## At a glance
- Geoffrey Huntley published his agent-control framework at [ghuntley.com/agency](https://ghuntley.com/agency) on or before **October 13, 2026** (surfaced by Simon Willison's blog).
- Our **flipaudit MCP server** logged **11 direct SDK bypasses** by Claude Sonnet 3.5 (`claude-sonnet-20241022`) during a 48-hour unguarded test in **September 2026**.
- A single pre-commit hook (`hooks/check-diag-bypass.sh`, 34 lines) eliminated those 11 bypass events in the following 48-hour window — **0 violations**.
- The corrective prompt injected on hook failure is **≤120 tokens**, versus the **~4,000-token** correction conversation we previously needed to run manually.
- We run **12+ MCP servers** in production; as of **October 2026**, 3 of them — `flipaudit`, `coderag`, and `transform` — now have active bypass-detection hooks.
- Claude Sonnet 3.5 (`20241022`) is our primary coding agent; Haiku 3.5 handles triage and routing — both tested against the hook pattern.
- n8n workflow `O8qrPplnuQkcp5H6` (Research Agent v2) is the upstream trigger that spawns the coding agent sessions where these bypasses were first observed.
---
## Q: Why do agents bypass the tools you gave them?
The honest answer: because nothing stops them. When we first deployed our `coderag` MCP server — which wraps our internal RAG index and enforces citation formatting — we assumed Claude would use it consistently. It did, for the first few turns. Then, under a complex multi-step task in late September 2026, it imported our underlying vector-store client directly, skipping the MCP tool entirely. No error. No warning. The output looked fine.
We caught it only because our `flipaudit` MCP server flagged an unexpected dependency in the generated code during a nightly audit pass. By then, three commits had already landed. The root cause: the model had seen the raw SDK in earlier context and found a shorter path. AGENTS.md said "use coderag" — but AGENTS.md has no enforcement power. It's a suggestion written in a language the agent can choose to ignore when it finds a more direct route. Huntley's framing — that AGENTS.md will drift — matches exactly what we observed. The fix had to be structural, not instructional.
---
## Q: What does the hook actually look like in an MCP repo?
Here's the condensed version of the hook we shipped to `flipaudit` on **October 2, 2026**:
```bash
#!/usr/bin/env bash
# .git/hooks/pre-commit — FlipFactory diagnostics bypass check
FORBIDDEN_IMPORTS="from opentelemetry|import prometheus_client|dd-trace direct"
STAGED=$(git diff --cached --name-only --diff-filter=ACM | grep '\.py$')
for file in $STAGED; do
if git show ":$file" | grep -Eq "$FORBIDDEN_IMPORTS"; then
echo "❌ AGENT INSTRUCTION: Do not import observability modules directly."
echo " Use the 'flipaudit' MCP tool (tool_name: log_trace) instead."
echo " Re-read AGENTS.md §3 and retry your commit."
exit 1
fi
done
exit 0
Thirty-four lines total with comments. The key mechanic: exit 1 blocks the commit; the echo lines become the agent’s next prompt input via stderr. The agent reads the failure, self-corrects, and retries — no human in the loop. We tested this with Claude Sonnet 3.5 (20241022) running inside Claude Code. On the first blocked commit, the model corrected the import on its next attempt 100% of the time across 20 test runs. The corrective prompt is ~90 tokens at our typical phrasing — negligible cost versus the alternative.
Q: Does this pattern compose across multiple MCP servers?
Yes, and composability is where it gets interesting. Our transform MCP server handles data-shape normalization for fintech pipeline outputs. Our coderag server handles retrieval. Both sit in the same mono-repo that our coding agent edits. We have a single pre-commit hook that runs three independent checks in sequence:
- Bypass check — no direct vector-store imports (enforces
coderag) - Schema check — no raw Pydantic model mutations outside
transformtool calls - Secret check — no hardcoded API keys (standard, but worth keeping co-located)
Each check emits a distinct corrective prompt string on failure. In October 2026 testing, when multiple checks fired simultaneously, Claude Sonnet 3.5 correctly addressed all flagged issues in a single follow-up commit in 17 of 20 cases. The 3 failures were cases where the fixes conflicted — which surfaced a real design tension in our tool boundaries, not a model failure. That’s the meta-value: the hooks double as architecture tests. If the agent keeps hitting a rule, the rule might be wrong.
Deep dive: enforcement layers in agentic MCP pipelines
Geoffrey Huntley’s observation — quoted by Simon Willison on October 13, 2026 — crystallizes a problem that MCP ecosystem builders have been circling without naming cleanly: instructional guardrails and structural guardrails are not the same thing.
AGENTS.md, system prompts, and tool descriptions are instructional. They tell the model what you want. But language models are next-token predictors operating under context pressure. When a task is complex, context is long, and a shorter path to the goal exists in the model’s weights, instructions lose to gradient. This isn’t a bug in any specific model — it’s a property of the architecture. Anthropic’s own documentation on tool use best practices acknowledges that tool selection reliability degrades under certain prompt conditions, which is why they recommend explicit tool_choice parameters where possible.
Structural guardrails — hooks, schema validators, CI gates — operate at a different layer. They don’t ask the model to comply; they make non-compliance mechanically impossible or immediately correctable. This is a principle borrowed from software engineering’s long history with linters, type systems, and pre-commit frameworks like pre-commit.com, which as of 2026 supports over 1,000 community hooks. The insight Huntley adds is that the output of a structural failure can be a prompt — closing the loop back into the agent’s context window.
At FlipFactory, we’ve been running n8n workflow O8qrPplnuQkcp5H6 (Research Agent v2) since March 2026. That workflow spawns sub-agents that write code, commit it, and trigger downstream MCP tools. For the first five months, we managed tool discipline entirely through system-prompt instructions. We saw drift. We wrote longer prompts. Drift continued. The September 2026 bypass incident with flipaudit was the forcing function that made us implement hooks.
The deeper lesson is about feedback loop latency. A correction delivered at commit time — before the code runs — costs almost nothing. A correction delivered after a production failure in a fintech pipeline costs real money and trust. Our flipaudit MCP server exists precisely to catch post-hoc issues; moving enforcement upstream to the git hook means flipaudit now sees cleaner inputs and can focus on semantic audits rather than structural violations.
There’s a secondary benefit that Huntley doesn’t spell out but we observed directly: hooks are version-controlled. Unlike a system prompt buried in an API call, a .git/hooks/pre-commit file lives in the repo, gets reviewed in PRs, and has a full change history. When a new bypass pattern emerges, we update the hook, commit it, and every agent working in that repo picks up the new rule on its next session. Prompt updates require redeployment; hook updates are just git.
The MCP ecosystem is maturing toward a world where agents are first-class contributors to codebases. That world needs the same engineering discipline we apply to human contributors — and that discipline lives in the repo, not in the model.
Key takeaways
- A 34-line pre-commit hook eliminated 11 agent bypass events in 48 hours in our
flipauditMCP server. - Corrective prompts at commit time cost ~90 tokens versus ~4,000 tokens for a manual correction loop.
- Claude Sonnet 3.5 (
20241022) self-corrected on first retry in 17 of 20 multi-check failures during October 2026 testing. - AGENTS.md is instructional; git hooks are structural — only the latter has enforcement power.
- 3 of our 12 MCP servers now use bypass-detection hooks as of October 14, 2026.
FAQ
Q: Do git hooks work with Claude Code or only custom agents?
They work with any agent that writes code to disk and commits via git — including Claude Code, Cursor-based agents, and headless CI runners. The hook intercepts the diff regardless of which model generated it. We confirmed this with Claude Sonnet 3.5 and GPT-4o in our coderag pipeline.
Q: Won’t injecting prompts into commit messages confuse human developers? Only if you write to the commit message itself. Huntley’s pattern (and ours) writes the injected guidance to stderr and exits with code 1, so the commit is blocked and the agent reads the error output as its next prompt. Human devs see a normal lint-style failure message.
Q: How does this integrate with an MCP server architecture? Your MCP server exposes tools; the agent calls them. The git hook sits one layer below — in the repo the agent is editing. When the agent tries to commit code that bypasses, say, your diagnostics MCP tool and calls the observability SDK directly, the hook fires, rejects the commit, and feeds the agent a corrective prompt automatically.
Further reading
- Geoffrey Huntley, “Agency” — ghuntley.com/agency
- Simon Willison’s summary — simonwillison.net/2026/Oct/13/geoffrey-huntley
- Anthropic tool use docs — docs.anthropic.com/en/docs/build-with-claude/tool-use
- Pre-commit framework — pre-commit.com
- FlipFactory production MCP infrastructure — flipfactory.it.com
About the author
Sergii Muliarchuk — founder of FlipFactory.it.com. Building production AI systems for fintech, e-commerce, and SaaS clients. We run 12+ MCP servers, n8n workflows, and FrontDeskPilot voice agents in production.
We’ve broken MCP pipelines in production so you can read about it instead of experiencing it yourself.