Skip to main content

Context Protector

PyPI version Python License CI

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

Metadata

Release files for context-protector 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for context-protector 1.0.0
File Size Uploaded
context_protector-1.0.0.tar.gz 28.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for context-protector 1.0.0
File Interpreter ABI Platform
context_protector-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 64.2 kB

Release files / context_protector-1.0.0.tar.gz

Download URL context_protector-1.0.0.tar.gz
Size 28.3 kB
Tags Source
SHA-256 checksum
How to use checksums
bf0a8b553105ebb97f67f134c0548e36af5da3f654c2ca55803b5d71096c3c2f
BLAKE2b-256 checksum
How to use checksums
6ecb2743bb3274ac2892f3e2eedf86d06a773d1dba9d40e43203ccfdcd2b158c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 1, 2026.

Transparency log

Release files / context_protector-1.0.0-py3-none-any.whl

Download URL context_protector-1.0.0-py3-none-any.whl
Size 35.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f982961d52a22a18d16e8eb24e95b4c01650687c0d1bc3e2bd32e69a32b2d1cd
BLAKE2b-256 checksum
How to use checksums
e72c0f55ead4702e02bb6130115dc6d2541285f3e4b670997a3515a840ddf8d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 1, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page