Skip to main content

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 | Use Cases

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

Sync: Content-based Blocking

from kavach import KavachMiddleware

middleware = KavachMiddleware()

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

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

Async: FastMCP Middleware Integration

from fastmcp import FastMCP
from kavach import KavachMiddleware

mcp = FastMCP("my-server")
mcp.add_middleware(
    KavachMiddleware(
        sensitive_tools=[
            "filesystem.delete",
            "aws.*",           # wildcard patterns
            "database.execute"
        ]
    )
)

Flow:

  1. Tool call intercepted by on_call_tool() middleware hook
  2. Tool name matched against sensitive_tools patterns
  3. If matched, DetectionEngine scans arguments against rules
  4. If violations found and strict mode enabled → raises SecurityException
  5. Otherwise → chain to next middleware

Default Security Rules

🛡️ Default Security Rules (Kavach)

  • Prompt Injection → Blocks attempts to override instructions (e.g., "ignore previous instructions", "act as admin")
  • Data Exfiltration → Blocks leaking system data (e.g., "send system prompt", "dump secrets")
  • PII Detection → Blocks personal data (e.g., phone numbers, credit cards, Aadhaar)
  • Secret Leakage → Blocks API keys/tokens (e.g., AWS key, sk-xxxx, private keys)
  • Code Execution → Blocks unsafe execution (e.g., eval(), exec(), os.system)
  • SQL Injection → Blocks DB attacks (e.g., DROP TABLE, UNION SELECT, ' OR '1'='1)
  • Path Traversal → Blocks file escape (e.g., ../../etc/passwd)
  • Filesystem Destruction → Blocks dangerous deletes (e.g., rm -rf /, delete /root)
  • Cloud Destruction → Blocks cloud deletes (e.g., delete S3 bucket, terminate instance)
  • Shell Injection → Blocks command chaining (e.g., || rm -rf, && drop database)
  • Tool Abuse → Blocks unsafe MCP tools (e.g., filesystem.delete, aws.s3.delete_bucket)

🚀 Custom Rules

You can easily add your own rules:

Rule(
    id="custom-rule",
    name="Custom Rule",
    severity="high",
    patterns=[re.compile(r"dangerous\s+action")]
)

Usage

Option 1: Default Rules Only

middleware = KavachMiddleware()

Option 2: Extend Defaults with Custom Rules

from kavach.types import Rule
import re

custom_rules = [
    Rule(
        id="custom-ban",
        name="Custom Ban",
        severity="high",
        description="Ban specific phrases",
        patterns=[re.compile(r"dangerous\s+action", re.I)]
    )
]

middleware = KavachMiddleware(
    rules=custom_rules,
    extend_rules=True  # Merge with KAVACH_RULES (default)
)

Option 3: Replace Defaults with Custom Rules

middleware = KavachMiddleware(
    rules=custom_rules,
    extend_rules=False  # Use ONLY custom rules
)

Option 4: Control Tool Access

# Allow violations in non-sensitive tools
middleware = KavachMiddleware(strict=False)

# Protect specific tools
middleware = KavachMiddleware(
    sensitive_tools=["filesystem.delete", "aws.s3.delete_bucket"]
)

Custom Logger | Maksing Logging

Kavach logger is fully pluggable. You can replace output layer without modifying detection engine or middleware.

from kavach.logger.setup import enable_logging, enable_masking
from kavach.logger.logger_manager import LoggerManager
from kavach.logger.base_logger import BaseLogger

class CustomLogger(BaseLogger):

    def info(self, msg, **kwargs):
        print(f"[MY SYSTEM INFO] {msg}")

    def error(self, msg, **kwargs):
        print(f"[MY SYSTEM ERROR] {msg}")

    def debug(self, msg, **kwargs):
        print(f"[MY SYSTEM DEBUG] {msg}")


# Step 1: Enable system logging -> turns logging ON/OFF
enable_logging(True)

# Step 2: Enable masking -> masks secrets in logs
enable_masking(True)

# Step 3: Replace logger -> replaces default logger completely
LoggerManager().set_logger(CustomLogger())

🔁 Custom Logger & Masking Override — Useful For?

This feature helps you control how Kavach handles logging in real-world production systems.

  • Custom Logger → Send logs to your own system (Datadog, Kafka, ELK, CloudWatch, files)
  • Masking → Hide sensitive data like API keys, tokens, and passwords in logs
  • Override Logger → Replace default logging without modifying core Kavach code
  • Production Debugging → Debug safely without exposing secrets
  • Compliance Ready Logs → Helps meet SOC2 / enterprise security standards

API Reference

KavachMiddleware.__init__()

Parameter Type Default Description
rules List[Rule] KAVACH_RULES Custom detection rules
strict bool True Raise exception (True) or return blocked result (False)
sensitive_tools List[str] [] Tools to protect (exact match or wildcard patterns)
extend_rules bool True Merge custom rules with defaults (True) or replace (False)

Methods

  • process(tool_call: dict) - Sync content scanning. Returns {"allowed": bool, ...}
  • async on_call_tool(context, call_next) - FastMCP async middleware hook
  • register_tool(tool_name: str) - Add tool to sensitive_tools at runtime

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! 🚀

Metadata

Release files for kavach-mcp 0.1.11

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

Source distribution (sdist)

Source distribution for kavach-mcp 0.1.11
File Size Uploaded Provenance
kavach_mcp-0.1.11.tar.gz 10.8 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for kavach-mcp 0.1.11
File Interpreter ABI Platform
kavach_mcp-0.1.11-py3-none-any.whl Python 3 none any Details

Total release size: 20.4 kB

Release files / kavach_mcp-0.1.11.tar.gz

Download URL kavach_mcp-0.1.11.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
94f427ad3cd98c1ea4fd14d05d44e7ade62df56de771f0b07c83e46404d3f73d
BLAKE2b-256 checksum
How to use checksums
0e67b2c498dad6fcc698194fcb7cf9c83d1456ed11b0fea7338e049ab5e49428
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 6, 2026.

Transparency log

Release files / kavach_mcp-0.1.11-py3-none-any.whl

Download URL kavach_mcp-0.1.11-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
efd6b6e87e7816436b49da6b177b3f9cb665aa4622642d3dd717c6d1d88e3090
BLAKE2b-256 checksum
How to use checksums
1131444f943cdf73f35bf76b66323812c4c067c0e0a2376ea5d5b09a75f4b906
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

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 Jun 6, 2026.

Transparency log

Security for kavach-mcp 0.1.11

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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for kavach_mcp-0.1.11.tar.gz

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 Jun 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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