- Shield MCP
Shield MCP
A shield for logging, deep debug and sanitization for MCP servers at development stage
Content
Shield MCP
A security middleware for Model Context Protocol (MCP) servers that enhances security and monitoring capabilities without modifying the official SDK. This package provides tools for securing and monitoring MCP tool calls, following the best practices outlined in the MCP documentation. Abstract yourself while interact at MCP development.
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Components
- Best Practices
- Development
- Roadmap
- Acknowledgments
Features
- Tool Access Control: Whitelist-based access control for MCP tools
- Result Sanitization: Configurable sanitization of tool outputs
- Structured Logging: Comprehensive audit logging using structlog
- Rate Limiting: Token bucket algorithm for rate limiting
- Error Handling: Standardized error handling and formatting
- MCP Inspector Compatible: Works seamlessly with the MCP Inspector tool
Requirements
System Requirements
- Python 3.8 or higher
- pip (Python package installer)
- virtualenv (recommended for development)
Quick Start
from shieldmcp import secure_tool
from shieldmcp.sanitizers import ToolSanitizer
from shieldmcp.rate_limit import RateLimitConfig
# Define allowed tools
ALLOWED_TOOLS = {"search", "read_file", "write_file"}
# Create a text sanitizer
text_sanitizer = ToolSanitizer.createTextSanitizer(
max_length=1000,
sensitive_patterns=[
r"\b\d{16}\b", # Credit card numbers
r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b" # Email addresses
]
)
# Configure rate limiting
rate_limit = RateLimitConfig(
requests_per_minute=60, # 1 request per second
burst_size=10 # Allow bursts of up to 10 requests
)
# Apply the decorator to your MCP tools
@secure_tool(
allowed_tools=ALLOWED_TOOLS,
sanitize_fn=text_sanitizer,
user_id="user123",
session_id="session456",
rate_limit=rate_limit
)
def search(query: str):
# Your tool implementation
return results
Components
Decorators (decorators.py)
The main @secure_tool decorator that orchestrates all security features:
@secure_tool(
allowed_tools={"tool1", "tool2"}, # Set of allowed tool names
sanitize_fn=your_sanitizer, # Optional result sanitization function
user_id="user123", # Optional user identifier
session_id="session456", # Optional session identifier
rate_limit=RateLimitConfig( # Optional rate limit configuration
requests_per_minute=60,
burst_size=10
)
)
def your_tool():
pass
Audit Logging (audit.py)
Structured logging using structlog:
from shieldmcp import ToolAudit
audit = ToolAudit()
audit.logToolCallStart(
tool_name="search",
args={"query": "test"},
user_id="user123"
)
Access Control (access.py)
Tool access validation:
from shieldmcp import ToolAccess
access = ToolAccess(allowed_tools={"tool1", "tool2"})
access.validateToolAccess("tool1") # Raises ValueError if not allowed
Sanitizers (sanitizers.py)
Result sanitization utilities:
from shieldmcp import ToolSanitizer
# Create a custom sanitizer
sanitizer = ToolSanitizer.createTextSanitizer(
max_length=1000,
sensitive_patterns=[r"\b\d{16}\b"]
)
# Use it directly
clean_text = sanitizer("Your text with sensitive data")
Rate Limiting (rate_limit.py)
Token bucket rate limiting:
from shieldmcp import RateLimitConfig
# Configure rate limits
config = RateLimitConfig(
requests_per_minute=60,
burst_size=10
)
Best Practices
Tool Access Control
- Always define a whitelist of allowed tools
- Use the most restrictive set of tools possible
- Regularly review and update the whitelist
Result Sanitization
- Sanitize all text output
- Define patterns for sensitive data
- Set reasonable length limits
Logging
- Include user and session IDs when available
- Log both successful and failed operations
- Use structured logging for better analysis
Rate Limiting
- Set appropriate limits based on tool complexity
- Consider burst sizes for better user experience
- Monitor rate limit hits in logs
Development
Setup Development Environment
# Clone the repository
git clone https://github.com/shieldmcp/shieldmcp.git
cd modelShield
# Create virtual environment
python -m venv venv
source venv/bin/activate # or `venv\Scripts\activate` on Windows
# Install development dependencies
pip install -r requirements.txt
Running Tests
pytest tests/
Roadmap
Planned Features
- Support for Clerk MCP and Github MCP
- Extended documentation
- TypeScript support
Acknowledgments
- Model Context Protocol for the protocol specification
- structlog for structured logging
Feel free to make any inquiries.
Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
WindsurfThe new purpose-built IDE to harness magic
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
BlenderBlenderMCP connects Blender to Claude AI through the Model Context Protocol (MCP), allowing Claude to directly interact with and control Blender. This integration enables prompt assisted 3D modeling, scene creation, and manipulation.
TimeA Model Context Protocol server that provides time and timezone conversion capabilities. This server enables LLMs to get current time information and perform timezone conversions using IANA timezone names, with automatic system timezone detection.
Tavily Mcp
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Serper MCP ServerA Serper MCP Server
Zhipu Web SearchZhipu Web Search MCP Server is a search engine specifically designed for large models. It integrates four search engines, allowing users to flexibly compare and switch between them. Building upon the web crawling and ranking capabilities of traditional search engines, it enhances intent recognition capabilities, returning results more suitable for large model processing (such as webpage titles, URLs, summaries, site names, site icons, etc.). This helps AI applications achieve "dynamic knowledge acquisition" and "precise scenario adaptation" capabilities.
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Amap Maps高德地图官方 MCP Server
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
ChatWiseThe second fastest AI chatbot™
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Playwright McpPlaywright MCP server
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
DeepChatYour AI Partner on Desktop
Howtocook Mcp基于Anduin2017 / HowToCook (程序员在家做饭指南)的mcp server,帮你推荐菜谱、规划膳食,解决“今天吃什么“的世纪难题;
Based on Anduin2017/HowToCook (Programmer's Guide to Cooking at Home), MCP Server helps you recommend recipes, plan meals, and solve the century old problem of "what to eat today"
CursorThe AI Code Editor
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code