Skip to main content

Secure MCP runtime โ€” policy enforcement, SSRF blocking, audit logging

Project description

mcp-shield ๐Ÿ›ก๏ธ

The security runtime for MCP servers. Every tool call inspected. Every attack blocked. Every decision logged.

Python PyPI Tests Status


What is MCP?

Model Context Protocol (MCP) is an open standard that lets AI assistants (Claude, Cursor, Copilot) connect to external tools and services โ€” file systems, APIs, databases, browsers โ€” through MCP servers.

Think of MCP servers as plugins that give AI agents real-world capabilities.


The Problem

MCP servers run as trusted processes on your machine with broad access:

Access Risk
๐Ÿ—‚๏ธ Filesystem Read /etc/passwd, steal SSH keys
๐ŸŒ Network SSRF to 169.254.169.254 (AWS metadata endpoint)
๐Ÿ”‘ Environment variables Steal API keys, tokens, secrets
โš™๏ธ Shell Execute arbitrary commands

A malicious or compromised MCP server can silently exfiltrate your secrets, pivot to internal infrastructure, or execute code โ€” and you'd never know.

This is not theoretical. A real SSRF vulnerability was found in an MCP OAuth HTTP transport implementation that allowed exactly this class of attack.


How mcp-shield Fixes This

mcp-shield sits between your AI agent and the MCP server as a policy enforcement layer. Before any tool executes, mcp-shield evaluates it. If it's not explicitly allowed โ€” it's blocked.

AI Agent
   โ”‚
   โ–ผ
mcp-shield /inspect
   โ”‚
   โ”œโ”€โ”€ Tool allowlist check      โ†’  "read_secrets" not in allowlist  โ†’ ๐Ÿšซ BLOCK
   โ”œโ”€โ”€ Blocked pattern check     โ†’  "ssrf_fetch" is dangerous        โ†’ ๐Ÿšซ BLOCK
   โ”œโ”€โ”€ Argument scanning         โ†’  "169.254.169.254" in args        โ†’ ๐Ÿšซ BLOCK
   โ”‚   (recursive, nested dicts)
   โ””โ”€โ”€ Passed all checks         โ†’  โœ… ALLOW โ†’ MCP Server executes
                                          โ”‚
                                          โ–ผ
                                     Audit Log (SQLite)
                              timestamp | server | tool | decision | reason

Install

pip install mcpshield-runtime

Or clone and run locally:

git clone https://github.com/srisowmya2000/mcp-shield
cd mcp-shield
python3 -m venv .venv && source .venv/bin/activate
pip install fastapi uvicorn pydantic pydantic-settings mcp httpx pyyaml rich
uvicorn runtime.api.main:app --reload

Open:


Live Demo

# Start mcp-shield
uvicorn runtime.api.main:app --reload

# ๐Ÿšซ Attempt secret theft โ†’ BLOCKED
curl -X POST http://localhost:8000/inspect \
  -H "Content-Type: application/json" \
  -d '{"server_name":"evil","policy":"default","tool_call":{"tool_name":"read_secrets","arguments":{}}}'
# โ†’ {"decision":"BLOCK","reason":"Tool 'read_secrets' is not in the allowed_tools list","blocked":true}

# ๐Ÿšซ Attempt SSRF to AWS metadata endpoint โ†’ BLOCKED
curl -X POST http://localhost:8000/inspect \
  -H "Content-Type: application/json" \
  -d '{"server_name":"evil","policy":"default","tool_call":{"tool_name":"ssrf_fetch","arguments":{"url":"http://169.254.169.254/latest/meta-data/"}}}'
# โ†’ {"decision":"BLOCK","reason":"Argument contains blocked pattern: '169.254.169.254'","blocked":true}

# โœ… Safe tool โ†’ ALLOWED
curl -X POST http://localhost:8000/inspect \
  -H "Content-Type: application/json" \
  -d '{"server_name":"safe","policy":"default","tool_call":{"tool_name":"safe_tool","arguments":{"name":"Sri"}}}'
# โ†’ {"decision":"ALLOW","reason":"Passed all policy checks","blocked":false}

CLI

# Inspect a tool call
python3 -m runtime.cli inspect read_secrets
# โ†’ ๐Ÿšซ BLOCKED โ€” Tool 'read_secrets' is not in the allowed_tools list

python3 -m runtime.cli inspect safe_tool
# โ†’ โœ… ALLOWED โ€” Passed all policy checks

# Score a server's risk level
python3 -m runtime.cli risk "read_secrets,ssrf_fetch,safe_tool"
# โ†’ ๐Ÿ”ด HIGH RISK (score: 80)
# โ†’ High-risk tools: ['read_secrets', 'ssrf_fetch']
# โ†’ Do not run without strict policy. Use isolated network.

# View live audit log
python3 -m runtime.cli audit

# View stats
python3 -m runtime.cli stats
# โ†’ Total: 6 | โœ… Allowed: 2 | ๐Ÿšซ Blocked: 4 (67% block rate)

Policies

Drop a YAML file in policies/ and reference it by name in any /inspect call.

# policies/default.yaml
allowed_tools:
  - safe_tool
  - list_files
  - get_time

block_network: true
block_env_access: true

blocked_arg_patterns:
  - "169.254.169.254"   # AWS metadata SSRF
  - "169.254.170.2"     # ECS metadata SSRF
  - "localhost"
  - "127.0.0.1"
  - "/etc/passwd"
  - "/etc/shadow"
  - "file://"
  - "gopher://"

max_memory_mb: 256
execution_timeout_seconds: 30

Switch policy per server:

POST /inspect  โ†’  { "policy": "strict", ... }

Two policies included: default and strict (zero-trust).


Features

Feature Description
๐Ÿ”’ Policy Engine YAML allowlists + blocked patterns, per-server policies
๐Ÿ” Argument Scanning Recursively scans nested args for SSRF, path traversal, dangerous patterns
๐Ÿ“‹ Audit Logger Every decision logged to SQLite โ€” timestamp, server, tool, reason
๐Ÿณ Docker Sandbox Hardened containers: --cap-drop=ALL, --network=none, --read-only
๐Ÿ”ฅ Firecracker Backend microVM isolation โ€” each server gets its own Linux kernel (Linux/KVM only)
๐Ÿ“Š Risk Scorer Scores MCP servers LOW / MEDIUM / HIGH based on tool capabilities
๐Ÿ–ฅ๏ธ Live Dashboard Real-time web UI at /dashboard โ€” live block/allow feed, flash animations
โšก CLI mcpshield inspect, audit, stats, risk with rich colored output

API Reference

Endpoint Method Description
/health GET Service health check
/inspect POST Evaluate tool call โ†’ ALLOW / BLOCK
/audit GET Recent audit log entries
/audit/stats GET Total / allowed / blocked counts
/risk/score POST Score server risk by tool list
/sandbox/launch POST Launch MCP server in hardened Docker container
/sandbox/stop/{name} POST Stop a running sandbox
/sandbox/list GET List running sandboxes
/dashboard GET Live real-time decision dashboard
/docs GET Interactive Swagger API docs

Docker Sandbox

Every MCP server launched via mcp-shield runs with:

--cap-drop=ALL          no Linux capabilities
--no-new-privileges     no privilege escalation
--read-only             immutable filesystem
--network=none          no network access
--memory=256m           memory limit
--cpus=0.5              CPU limit
--pids-limit=64         process limit
--tmpfs=/tmp            ephemeral tmp only

Firecracker microVM Backend

For stronger isolation, mcp-shield supports Firecracker microVMs โ€” each MCP server gets its own Linux kernel. A kernel exploit inside the VM cannot reach the host.

Docker:       shared kernel โ†’ kernel exploit = host at risk
Firecracker:  own kernel    โ†’ kernel exploit = contained in VM

Requires Linux with KVM. See docs/firecracker-setup.md.


Architecture

mcp-shield/
โ”œโ”€โ”€ runtime/
โ”‚   โ”œโ”€โ”€ api/
โ”‚   โ”‚   โ””โ”€โ”€ main.py              # FastAPI โ€” all endpoints
โ”‚   โ”œโ”€โ”€ policy_engine.py         # YAML policy loader + evaluator
โ”‚   โ”œโ”€โ”€ audit_logger.py          # SQLite decision log
โ”‚   โ”œโ”€โ”€ risk_scorer.py           # LOW/MEDIUM/HIGH risk scoring
โ”‚   โ”œโ”€โ”€ cli.py                   # Typer CLI
โ”‚   โ”œโ”€โ”€ models.py                # Pydantic schemas
โ”‚   โ””โ”€โ”€ sandbox/
โ”‚       โ”œโ”€โ”€ base.py              # Abstract backend interface
โ”‚       โ”œโ”€โ”€ docker_backend.py    # Hardened Docker sandbox
โ”‚       โ””โ”€โ”€ firecracker_backend.py  # microVM backend (Linux/KVM)
โ”œโ”€โ”€ policies/
โ”‚   โ”œโ”€โ”€ default.yaml
โ”‚   โ””โ”€โ”€ strict.yaml
โ”œโ”€โ”€ examples/
โ”‚   โ”œโ”€โ”€ malicious_mcp_server/    # Demo attacker (SSRF + secret theft + exec)
โ”‚   โ””โ”€โ”€ safe_mcp_server/         # Demo benign server
โ”œโ”€โ”€ docs/
โ”‚   โ”œโ”€โ”€ threat-model.md          # Attack scenarios + limitations
โ”‚   โ””โ”€โ”€ firecracker-setup.md     # Firecracker setup guide
โ””โ”€โ”€ tests/                       # 12 tests โ€” all passing

Tests

pip install pytest
pytest tests/ -v
# 12 passed in 0.11s

Covers: tool allowlist blocking, SSRF argument detection, nested arg scanning, strict policy enforcement, edge cases, unknown policy handling.


Threat Model

See docs/threat-model.md for:

  • Attack scenarios (SSRF, secret theft, command execution, path traversal)
  • What mcp-shield blocks vs what it doesn't
  • Defense in depth recommendations

Roadmap

  • Policy engine (allowlist + pattern scanning)
  • Audit logger (SQLite)
  • FastAPI REST surface
  • Docker sandbox backend (hardened)
  • Demo malicious MCP server
  • Risk scorer (LOW / MEDIUM / HIGH)
  • CLI (mcpshield inspect, audit, stats, risk)
  • Real-time dashboard
  • Firecracker microVM backend
  • PyPI package (pip install mcpshield-runtime)
  • Threat model documentation
  • Prompt injection detection
  • Per-tool argument schema validation
  • Webhook alerts on BLOCK events

License

MIT โ€” see LICENSE


Author

Sri Sowmya Nemani โ€” Security researcher & engineer. Bug bounty | MCP security | AI agent security

GitHub ยท PyPI

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

mcpshield_runtime-0.1.2.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

mcpshield_runtime-0.1.2-py3-none-any.whl (15.8 kB view details)

Uploaded Python 3

File details

Details for the file mcpshield_runtime-0.1.2.tar.gz.

File metadata

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

File hashes

Hashes for mcpshield_runtime-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5d19a04b71e46f045dea65d1c3260df21eb53d4c136d931489106bf7d398463a
MD5 ea18f9d415db4295fdc049cdea6c2189
BLAKE2b-256 2cf966f04c0a4b3a242ed13d317c7754e991dcd83f2a3c94ef9a05b889f45935

See more details on using hashes here.

File details

Details for the file mcpshield_runtime-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mcpshield_runtime-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 13150e3b3e0191e0b5b871fb20bae126e49be426067b5dd54f2cb99b79415fd4
MD5 608a8a1cd3453b739ee8bebbc36856a5
BLAKE2b-256 04bae75cb788d31d40345bac89d6fd9a9f5c3be7d39cfc9cc023237ff2334d84

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