The Kavach Detection Engine

Regex-powered rule scanning that intercepts every LLM tool call and decides allow or block — before your infrastructure ever sees it. This is the core of the open-source kavach-mcp middleware, now scaled to platform level.

Pipeline

Every tool call runs the same guard

Intercept → match → decide → audit. Strict mode raises, advisory mode chains, and masking keeps secrets out of your logs.

Interceptor — on_call_tool()

Sync scanKavachMiddleware.process()
Async hookFastMCP middleware
Wildcardsaws.*, filesystem.*, db.*

Decision — DetectionEngine.scan()

RulesKAVACH_RULES + custom
Severitycritical / high / medium
Maskingsecrets redacted in logs

Action — allow / block / warn

Strictraise SecurityException
Advisoryreturn blocked result
Auditcompliance-ready trail
Default rules

Eleven rule families, out of the box

Severity-tagged patterns shipped in kavach/rules.py. Merge with your own or replace entirely.

Prompt Injection

Critical

System override and jailbreak attempts.

ignore\s+(all|previous)\s+instructions · you are now admin · DAN

Data Exfiltration

Critical

Attempts to leak or dump system data and prompts.

(send|upload|export|dump).*(secret|env|system)

PII Detection

High

Aadhaar, credit cards, phone and SSN-like values.

\b\d{10}\b · \b\d{12}\b · \b\d{16}\b

Secret Leakage

Critical

AWS, OpenAI and Google keys, PEM keys, api_key assignments.

AKIA[0-9A-Z]{16} · sk-[A-Za-z0-9]{20,} · AIza…

Dangerous Execution

Critical

eval, exec, compile, os.system and subprocess.

eval( · exec( · os\.system · subprocess

SQL Injection

High

DDL / DML attacks and comment-based tricks.

DROP TABLE · UNION SELECT · ' OR '1'='1

Path Traversal

High

Directory traversal and file escape.

(\.\./|\.\.\\)+

Filesystem Destruction

Critical

rm/del/wipe against system paths.

rm -rf / · del /s /q · delete /etc/passwd

Cloud Destruction

Critical

Delete bucket, terminate instance, drop database.

delete\s+(bucket|s3|instance|database) · drop database

Shell Injection

High

Command chaining to destructive commands.

(\|\||&&|\|)\s*(rm|del|drop|format)

Tool Abuse

Critical

Unsafe MCP tool usage regardless of arguments.

filesystem\.delete · aws\.s3\.delete_bucket · db\.drop

Custom Rules

Extend

Add any regex; merge with defaults or replace them.

Rule(id="custom", severity="high", patterns=[re.compile(r"…")])
Integration

Plug into FastMCP in three lines

Runtime protection as a drop-in middleware hook.

middleware integration
# examples/app.py
from fastmcp import FastMCP
from kavach import KavachMiddleware
 
mcp = FastMCP("my-server")
mcp.add_middleware(
  KavachMiddleware(
    sensitive_tools=["filesystem.delete", "aws.*"]
  )
)
 
✓ tool call intercepted · scanned · allow/block decided
Open source

Build on the rule engine teams already trust.

kavach-mcp is free and portable — extend it with your own patterns, shadow-mode test it, then come to the platform for live inventory and orchestration.