mcp盾牌
🛡️ MCP客户端和服务器的实时PII编辑代理——Python 3.12+的零延迟隐私,可选Python 3.14子解释器加速。
mcp盾牌 是位于MCP客户端(例如Claude Desktop)和任何下游MCP服务器之间的拦截网关代理。它在个人身份信息到达LLM的上下文窗口之前实时检测和屏蔽个人身份信息,确保GDPR/HIPAA符合单一 pip install.
为什么是mcp盾皮?
当AI代理从MCP服务器请求数据时,原始有效载荷(可能包含SSN、医疗记录或信用卡)直接流入LLM。组织可能面临超过数亿美元的GDPR/HIPAA罚款。 mcp盾牌 消除了协议层的这种风险。
┌──────────────┐ ┌─────────────────┐ ┌──────────────────┐
│ Claude │────▶│ mcp-shield-pii │────▶│ Downstream MCP │
│ Desktop │◀────│ (PII Redaction) │◀────│ Server │
└──────────────┘ └─────────────────┘ └──────────────────┘
▲
PII masked before
reaching the LLM 安装
pip install mcp-shield-pii对于基于NLP的检测(姓名、组织、地址):
pip install mcp-shield-pii[nlp]
python -m spacy download en_core_web_sm快速开始
1.扫描文本以查找PII
# Simple scan
mcp-shield-pii scan "Contact john@example.com, SSN 123-45-6789"
# JSON output
mcp-shield-pii scan --json "Patient MRN-123456 at 192.168.1.1"
# Different masking strategies
mcp-shield-pii scan --strategy partial "Card: 4111-1111-1111-1111"
mcp-shield-pii scan --strategy hash "Email: secret@corp.com"
mcp-shield-pii scan --strategy pseudo "Call 555-123-4567"2.启动代理
# Basic proxy (stdio transport)
mcp-shield-pii proxy --downstream "npx -y @modelcontextprotocol/server-postgres postgresql://localhost/mydb"
# With config file
mcp-shield-pii proxy --downstream "python my_server.py" --config shield.toml
# Dry-run mode (log detections, don't modify payloads)
mcp-shield-pii proxy --downstream "npx my-mcp-server" --dry-run3.克劳德桌面集成
添加到您的 claude_desktop_config.json:
{
"mcpServers": {
"my-server-shielded": {
"command": "mcp-shield-pii",
"args": [
"proxy",
"--downstream", "npx -y @modelcontextprotocol/server-postgres postgresql://localhost/mydb",
"--config", "/path/to/shield.toml"
]
}
}
}4.生成配置文件
mcp-shield-pii generate-config --output shield.toml5.生成合规报告
mcp-shield-pii report --format markdown --output compliance_report.md6.启动仪表板
mcp-shield-pii dashboard --port 8765
# Open http://127.0.0.1:8765特性
v1.0--核心
| 特性 | 描述 |
|---|---|
| 标准代理 | 拦截客户端和下游服务器之间的MCP stdio传输 |
| 正则表达式引擎(18种) | 检测SSN、信用卡、电子邮件、电话、IBAN、API密钥、JWT等 |
| NLP引擎 | 可选spaCy NER用于人名、组织、地点、地址 |
| 掩蔽策略 | redact (`), partial (*--6789), hash (SHA256:a1b2...), pseudo` (一致的假货) |
| TOML配置 | 按实体规则、按工具允许/拒绝列表、置信阈值 |
| CallToolResult拦截 | 在传递非敏感RPC时,以JSON-RPC响应为目标 |
| 审计跟踪 | JSONL审计日志,包括时间戳、实体类型、置信度得分 |
| 命令行界面 | proxy, scan, report, dashboard, generate-config, version |
v1.1--硬化
| 特性 | 描述 |
|---|---|
| 情境感知评分 | 通过分析周围的文本减少误报 |
| 信心阈值 | 每个实体类型可配置的最小置信度 |
| 工具允许/拒绝列表 | 跳过受信任的工具,对敏感工具强制执行严格模式 |
| 干运行模式 | 记录在不修改有效载荷的情况下会被编辑的内容 |
| 热重新加载配置 | 更改规则而不重新启动代理 |
| 普罗米修斯指标 | /metrics 具有延迟百分位数和实体计数器的端点 |
v2.0--企业版
| 特性 | 描述 |
|---|---|
| 伪匿名化 | 保持语义含义的一致假数据映射 |
| 可逆重动作 | AES-256加密映射——授权密钥持有者可以恢复原始密钥 |
| 合规仪表板 | 带有实时事件表和严重性徽章的暗模式web UI |
| GDPR/HIPAA报告 | 自动生成的合规报告(文本、JSON、markdown) |
| Webhook警报 | 检测到高严重性PII时通知Slack/Teams |
| 副口译员人才库 | 通过以下方式进行无GIL并行检测 concurrent.interpreters (3.14+)或 ProcessPoolExecutor (3.12+) |
检测到的实体类型
基于正则表达式(18种类型)
| 实体 | 示例 | 验证 |
|---|---|---|
| 电子邮件 | user@example.com | 正则表达式 |
| 电话 | +1-555-123-4567 | 正则表达式 |
| SSN | 123-45-6789 | 正则表达式+格式验证 |
| 信用卡 | 4111-1111-1111-1111 | 正则表达式+Luhn校验和 |
伊班。 DE89370400440532013000 | 正则表达式+国家代码长度 | |
IPv4 的 192.168.1.1 | 正则表达式 | |
IPv6 的 2001:0db8::1 | 正则表达式 | |
| MAC地址 | 00:1A:2B:3C:4D:5E | 正则表达式 |
| AWS API密钥 | AKIA... | 正则表达式(前缀) |
| OpenAI密钥 | sk-... | 正则表达式(前缀) |
| 条纹键 | sk_live_... | 正则表达式(前缀) |
| GitHub代币 | ghp_... | 正则表达式(前缀) |
| 护照 | A12345678 | 正则表达式 |
出生日期: 1990-01-15 | 正则表达式 | |
| 医疗ID | MRN-123456 | 正则表达式 |
| 驾驶执照 | D123-4567-8901 | 正则表达式 |
| 带身份验证的URL | https://user:pass@host | 正则表达式 |
| JWT代币 | eyJhbG... | 正则表达式(前缀) |
基于NLP(5种类型,需要 [nlp] 额外)
| 实体 | 示例 |
|---|---|
| 人员姓名 | John Smith |
| 组织机构 | Acme Corp |
| 地址 | 123 Main St, Springfield |
| 地点 | New York City |
| 医疗状况 | Type 2 diabetes |
配置(shield.toml)
[shield]
default_masking_strategy = "redact"
default_confidence_threshold = 0.7
dry_run = false
[detection]
enable_regex = true
enable_nlp = false
enable_context_scoring = true
[entities.SSN]
masking_strategy = "redact"
confidence_threshold = 0.8
[entities.EMAIL]
masking_strategy = "pseudo"
confidence_threshold = 0.7
[tools.trusted_internal_tool]
action = "skip"
[tools.patient_records_api]
action = "strict"
masking_strategy = "redact"
[[webhooks]]
url = "https://hooks.slack.com/services/YOUR/WEBHOOK"
events = ["high_severity"]
[dashboard]
enabled = true
port = 8765
[metrics]
enabled = true
port = 9090程序化API
from mcp_shield_pii.detection.regex_engine import RegexDetectionEngine
from mcp_shield_pii.masking.strategies import get_strategy
from mcp_shield_pii.pipeline import ShieldPipeline
from mcp_shield_pii.config.loader import ShieldConfig
# Simple detection
engine = RegexDetectionEngine()
results = engine.detect("Email john@corp.com, SSN 123-45-6789")
for r in results:
print(f"{r.entity_type.value}: '{r.text}' (confidence: {r.confidence:.0%})")
# Full pipeline
config = ShieldConfig(default_masking_strategy="partial")
pipeline = ShieldPipeline(config)
masked, summary = pipeline.process_text("Contact admin@secret.org, card 4111-1111-1111-1111")
print(masked) # "Contact a***@***.org, card ****-****-****-1111"
pipeline.close()
# Pseudo-anonymization
config = ShieldConfig(default_masking_strategy="pseudo")
pipeline = ShieldPipeline(config)
masked, _ = pipeline.process_text("Email alice@corp.com then alice@corp.com again")
print(masked) # Same fake email both times (consistent mapping)
pipeline.close()建筑
src/mcp_shield_pii/
├── __init__.py # Public API exports
├── cli.py # Typer CLI (6 commands)
├── pipeline.py # Orchestration: detect → score → filter → mask → audit
├── compliance.py # GDPR/HIPAA report generator
├── webhooks.py # Async webhook alerts
├── detection/
│ ├── base.py # EntityType enum, DetectionResult, protocols
│ ├── regex_engine.py # 18 regex patterns + Luhn/IBAN validation
│ ├── nlp_engine.py # spaCy NER detection (optional)
│ └── context_scorer.py # Context-aware confidence adjustment
├── masking/
│ ├── strategies.py # Redact, partial, hash, pseudo-anonymization
│ └── reversible.py # AES-256 Fernet reversible redaction
├── config/
│ ├── loader.py # TOML config parser
│ └── watcher.py # Hot-reload file watcher
├── proxy/
│ ├── __init__.py # MCP JSON-RPC interceptor
│ └── stdio_proxy.py # Bidirectional stdio transport
├── concurrency/
│ └── __init__.py # Subinterpreter pool + ProcessPool fallback
├── metrics/
│ └── __init__.py # Prometheus metrics + HTTP server
├── audit/
│ └── __init__.py # JSONL audit logger
└── dashboard/
└── __init__.py # Web UI + REST API贡献
看 贡献.md
许可证
麻省理工学院——见 许可证 了解详情。
