屏蔽MCP
](https://www.python.org/downloads/)  
一种用于模型上下文协议(MCP)服务器的安全中间件,在不修改官方SDK的情况下增强了安全性和监控能力。此软件包提供了用于保护和监视MCP工具调用的工具,遵循MCP文档中概述的最佳实践。在MCP开发中互动时抽象自己。
目录
特性
- 工具访问控制:基于白名单的MCP工具访问控制
- 结果消毒:可配置的工具输出净化
- 结构化日志记录:使用structlog进行全面的审计日志记录
- 速率限制:用于速率限制的令牌桶算法
- 错误处理:标准化的错误处理和格式化
- MCP检查器兼容:与MCP检查器工具无缝协作
需求
系统要求
- Python 3.8或更高版本
- pip(Python包安装程序)
- virtualenv(建议开发)
快速开始
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组件
装饰师(decorators.py)
主要 @secure_tool 协调所有安全功能的装饰器:
@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.py)
使用structlog进行结构化日志记录:
from shieldmcp import ToolAudit
audit = ToolAudit()
audit.logToolCallStart(
tool_name="search",
args={"query": "test"},
user_id="user123"
)访问控制(access.py)
工具访问验证:
from shieldmcp import ToolAccess
access = ToolAccess(allowed_tools={"tool1", "tool2"})
access.validateToolAccess("tool1") # Raises ValueError if not allowed消毒剂(sanitizers.py)
结果消毒实用程序:
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_limit.py)
令牌桶速率限制:
from shieldmcp import RateLimitConfig
# Configure rate limits
config = RateLimitConfig(
requests_per_minute=60,
burst_size=10
)最佳实践
工具访问控制
- 始终定义允许使用的工具的白名单
- 使用尽可能严格的工具集
- 定期审查和更新白名单
结果消毒
- 对所有文本输出进行消毒
- 定义敏感数据的模式
- 设定合理的长度限制
日志记录
- 包括用户和会话ID(如果可用)
- 记录成功和失败的操作
- 使用结构化日志进行更好的分析
速率限制
- 根据工具复杂性设置适当的限制
- 考虑突发大小以获得更好的用户体验
- 监控日志中的速率限制点击次数
发展
设置开发环境
# Clone the repository
git clone https://github.com/shieldmcp/shieldmcp.git
cd shieldmcp
# 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运行测试
pytest tests/路线图
计划的功能
- 支持文员MCP和Github MCP
- 扩展文档
- TypeScript支持
致谢
______________________________________________________________________
如有任何疑问,请随时提出。

