Skip to main content

Security middleware for Model Context Protocol (MCP) that detects and blocks malicious tool calls

Project description

Kavach - MCP Security Middleware

Security middleware for Model Context Protocol (MCP) that detects and blocks malicious tool calls using pattern-based rule scanning.

Built by Shivam Namdeo | PyPI Package

Quick Start

# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate

# Run the example
cd example
python3 app.py

Architecture

Core Components:

  • middleware.py - KavachMiddleware: Main entry point. Processes tool calls and returns allow/block decisions.
  • engine.py - DetectionEngine: Scans text against rules and collects violations.
  • rules.py - KAVACH_RULES: Rule definitions for detecting prompt injection, PII, API keys, etc.
  • types.py - Rule: Data class defining rule structure (id, name, severity, patterns).

How It Works

from kavach import KavachMiddleware

middleware = KavachMiddleware()

# Process any tool call
result = middleware.process({
    "tool": "aws.s3",
    "access_key": "AKIAIOSFODNN7EXAMPLE"
})

# Returns: {"allowed": False, "violations": [...]}

Flow:

  1. Tool call is converted to string
  2. DetectionEngine scans text against all rule patterns (regex)
  3. If violations found and strict mode enabled → blocked
  4. Otherwise → allowed

Rules

Detects:

  • Prompt Injection - "ignore previous instructions", "override instructions"
  • Secret Leakage - AWS keys (AKIA...), OpenAI keys (sk-...)
  • PII - 10/16 digit sequences

Add custom rules in rules.py:

Rule(
    id="custom-rule",
    name="Rule Name",
    severity="high",
    patterns=[re.compile(r"pattern")]
)

Usage

# Allow all (strict=False)
middleware = KavachMiddleware(strict=False)

# Use custom rules
custom_rules = [Rule(...)]
middleware = KavachMiddleware(rules=custom_rules)

Project Structure

kavach-mcp-middleware/
├── kavach/
│   ├── __init__.py       # Package exports
│   ├── middleware.py     # Main middleware class
│   ├── engine.py         # Detection logic
│   ├── rules.py          # Security rules
│   └── types.py          # Data classes
└── example/
    └── app.py            # Example usage

Contributing

We'd love to get more features and improvements! Please feel free to:

  • Add new detection rules in kavach/rules.py
  • Improve the detection engine in kavach/engine.py
  • Submit bug fixes and enhancements via pull requests
  • Suggest new security patterns to detect

All contributions are welcome! 🚀

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kavach_mcp-0.1.4.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kavach_mcp-0.1.4-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file kavach_mcp-0.1.4.tar.gz.

File metadata

  • Download URL: kavach_mcp-0.1.4.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for kavach_mcp-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a3ebf251334bf13031d0e98dcb5ac4c3e80d9801a5bf886f8e255795cb87743a
MD5 5130899bc729ce10c8773baa77f0bd3f
BLAKE2b-256 31b75e2ffbcded2c401972e6e4dafa6d263b5786de149a27edb7e416073a88f6

See more details on using hashes here.

File details

Details for the file kavach_mcp-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: kavach_mcp-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for kavach_mcp-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4fad3da710d6e466f359ee2da5011bcbd229936638265cea31fe683803f6c2ed
MD5 dd71a16c2285bd8a1867aa54b6344c0e
BLAKE2b-256 d3b98f1bcb2baeec329ca92ead1affa80d69e1e9da132da7e503c89d5113fd33

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page