Vendor-neutral guardrails for AI coding agents. Protects against prompt injection attacks.
Project description
Context Protector
Protect AI coding agents from prompt injection attacks. Works with Claude Code and other AI coding tools.
Features
- Prompt Injection Detection - Block malicious inputs before tool execution
- Output Scanning - Detect threats in tool outputs (file reads, API responses)
- Multiple Backends - LlamaFirewall (default), NeMo Guardrails, GCP Model Armor
- Fully Local - No cloud dependencies required (optional Ollama support)
Installation
# Using uv (recommended)
uv tool install context-protector
# Using pip
pip install context-protector
# Using pipx
pipx install context-protector
Quick Start (Claude Code)
1. Install and initialize:
context-protector init
2. Add to Claude Code settings (~/.claude/settings.json):
{
"hooks": {
"PreToolUse": [
{
"matcher": "*",
"hooks": [{"type": "command", "command": "context-protector"}]
}
],
"PostToolUse": [
{
"matcher": "*",
"hooks": [{"type": "command", "command": "context-protector"}]
}
]
}
}
3. Done! Context Protector will now scan all tool inputs and outputs.
How It Works
┌─────────────────────────────────────────────────────────────┐
│ Claude Code │
│ │
│ Tool Request ──► PreToolUse Hook ──► context-protector │
│ │ │ │
│ [ALLOW/BLOCK] Scan Input │
│ │ │ │
│ Tool Response ◄── PostToolUse Hook ◄── context-protector │
│ │ │ │
│ [WARN/BLOCK] Scan Output │
└─────────────────────────────────────────────────────────────┘
Configuration
Config file: ~/.config/context-protector/config.yaml
general:
response_mode: warn # warn (default) or block
llama_firewall:
scanner_mode: auto # auto, basic, or full
Run context-protector init to create a config file with all options.
Environment Variables
All settings can be overridden with environment variables (prefix: CONTEXT_PROTECTOR_):
export CONTEXT_PROTECTOR_RESPONSE_MODE=block
export CONTEXT_PROTECTOR_SCANNER_MODE=basic
Providers
LlamaFirewall (Default)
Meta's LlamaFirewall for ML-based prompt injection detection.
| Mode | Description |
|---|---|
auto |
Tries ML detection, falls back to pattern-based if auth fails |
basic |
Pattern-based only (no HuggingFace auth required) |
full |
Full ML detection (requires HuggingFace auth) |
NeMo Guardrails
NVIDIA's guardrails toolkit with multiple detection modes.
| Mode | Description |
|---|---|
all |
Heuristics + injection detection (default) |
heuristics |
Perplexity-based jailbreak detection |
injection |
YARA-based SQL/XSS/code injection |
local |
LLM-based via Ollama (fully local) |
nemo_guardrails:
mode: local
ollama_model: mistral:7b
GCP Model Armor
Enterprise-grade content safety via Google Cloud.
gcp_model_armor:
enabled: true
project_id: your-project
location: us-central1
template_id: your-template
Response Modes
| Mode | Behavior |
|---|---|
warn |
Log threats, inject warnings (default) |
block |
Block malicious content entirely |
CLI Reference
context-protector # Run as Claude Code hook (reads stdin)
context-protector init # Create config file
context-protector --check # Check content from stdin JSON
context-protector --help # Show help
context-protector --version # Show version
Standalone Check Mode
For integration with other tools:
echo '{"content": "test input", "type": "tool_input"}' | context-protector --check
Output:
{"safe": true, "alert": null}
Development
git clone https://github.com/ottosulin/context-protector.git
cd context-protector
uv sync --all-groups
uv run pytest
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_protector-1.0.0.tar.gz.
File metadata
- Download URL: context_protector-1.0.0.tar.gz
- Upload date:
- Size: 28.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf0a8b553105ebb97f67f134c0548e36af5da3f654c2ca55803b5d71096c3c2f
|
|
| MD5 |
8fbb43d365024e71f8a9ec68c4bd522c
|
|
| BLAKE2b-256 |
6ecb2743bb3274ac2892f3e2eedf86d06a773d1dba9d40e43203ccfdcd2b158c
|
Provenance
The following attestation bundles were made for context_protector-1.0.0.tar.gz:
Publisher:
release.yml on ottosulin/context-protector
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
context_protector-1.0.0.tar.gz -
Subject digest:
bf0a8b553105ebb97f67f134c0548e36af5da3f654c2ca55803b5d71096c3c2f - Sigstore transparency entry: 891464276
- Sigstore integration time:
-
Permalink:
ottosulin/context-protector@360a7cd1c0c84d3e7f9a2903d9415411700e7aac -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/ottosulin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@360a7cd1c0c84d3e7f9a2903d9415411700e7aac -
Trigger Event:
release
-
Statement type:
File details
Details for the file context_protector-1.0.0-py3-none-any.whl.
File metadata
- Download URL: context_protector-1.0.0-py3-none-any.whl
- Upload date:
- Size: 35.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f982961d52a22a18d16e8eb24e95b4c01650687c0d1bc3e2bd32e69a32b2d1cd
|
|
| MD5 |
69befefc17bbf2e61b89ea8bf8f5dd7a
|
|
| BLAKE2b-256 |
e72c0f55ead4702e02bb6130115dc6d2541285f3e4b670997a3515a840ddf8d0
|
Provenance
The following attestation bundles were made for context_protector-1.0.0-py3-none-any.whl:
Publisher:
release.yml on ottosulin/context-protector
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
context_protector-1.0.0-py3-none-any.whl -
Subject digest:
f982961d52a22a18d16e8eb24e95b4c01650687c0d1bc3e2bd32e69a32b2d1cd - Sigstore transparency entry: 891464337
- Sigstore integration time:
-
Permalink:
ottosulin/context-protector@360a7cd1c0c84d3e7f9a2903d9415411700e7aac -
Branch / Tag:
refs/tags/v1.0.1 - Owner: https://github.com/ottosulin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@360a7cd1c0c84d3e7f9a2903d9415411700e7aac -
Trigger Event:
release
-
Statement type: