Llm Guard Privacy Gateway
Description
# LLM-Guard 隐私网关 一个基于MCP协议的隐私保护网关,用于在LLM应用中检测和过滤敏感信息。 ## 功能特性 1. **PII脱敏**:自动识别文本中的中文人名、手机号、邮箱,并将其替换为占位符 2. **密钥拦截**:检测常见的API Key、阿里云AccessKey以及RSA私钥 3. **商业阻断**:定义黑名单列表,如果文本包含这些词,直接返回拦截状态 4. **MCP封装**:将上述逻辑封装为MCP工具,返回JSON格式的处理结果 ## 安装 ```bash pip install -r requirements.txt ```…
About
# LLM-Guard 隐私网关 一个基于MCP协议的隐私保护网关,用于在LLM应用中检测和过滤敏感信息。 ## 功能特性 1. **PII脱敏**:自动识别文本中的中文人名、手机号、邮箱,并将其替换为占位符 2. **密钥拦截**:检测常见的API Key、阿里云AccessKey以及RSA私钥 3. **商业阻断**:定义黑名单列表,如果文本包含这些词,直接返回拦截状态 4. **MCP封装**:将上述逻辑封装为MCP工具,返回JSON格式的处理结果 ## 安装 ```bash pip install -r requirements.txt ``` ### 关于spaCy模型下载问题…
Details
- Author
- Amywith
- Downloads
- 223
- Categories
- AI, Security
Jump to
- PII masking: identifies Chinese names, phone numbers, and emails.
- Key interception: detects API keys, Alibaba Cloud AccessKeys, and RSA private keys.
- Commercial blocking: predefined blacklist words trigger an intercepted status.
- MCP encapsulation: logic exposed as MCP tools returning structured JSON.
Setting up with Highlight
This MCP is not yet compatible with Highlight’s one-click setup. However, you can still use it with Highlight by following these steps:
- Download and install Highlight from highlightai.com/download
- Navigate to the plugins tab and select "Add Custom Plugin"
-
Configure the plugin with the settings below
Plugin Name
Llm Guard Privacy GatewayCommand (node, npx, python, etc.)Please refer to the README for specific instructions on how to obtain API keys or other required environment variables.
- Enable "Start Automatically" if you want the plugin to start when Highlight launches
From the repository
Install dependencies with pip install -r requirements.txt. Use the Python function sanitize_prompt_mcp or the LLMSanitizer class. To run as an MCP server, execute python mcp_server.py or use the provided Docker image.
Claude Desktop / Cursor
Paste into your MCP client config file to install this server.
{
"mcpServers": {
"llm guard privacy gateway": {
"llm-guard-mcp": {
"command": "python",
"args": [
"-m",
"spacy",
"download",
"zh_core_web_sm",
"--index-url",
"https://pypi.tuna.tsinghua.edu.cn/simple/"
]
}
}
}
}
McpServers
{
"llm-guard-mcp": {
"command": "python",
"args": [
"-m",
"spacy",
"download",
"zh_core_web_sm",
"--index-url",
"https://pypi.tuna.tsinghua.edu.cn/simple/"
]
}
}
LLM-Guard 隐私网关
一个基于MCP协议的隐私保护网关,用于在LLM应用中检测和过滤敏感信息。
功能特性
1. PII脱敏:自动识别文本中的中文人名、手机号、邮箱,并将其替换为占位符
2. 密钥拦截:检测常见的API Key、阿里云AccessKey以及RSA私钥
3. 商业阻断:定义黑名单列表,如果文本包含这些词,直接返回拦截状态
4. MCP封装:将上述逻辑封装为MCP工具,返回JSON格式的处理结果
安装
pip install -r requirements.txt
关于spaCy模型下载问题
如果您在下载spaCy中文语言模型时遇到网络问题(如连接被远程主机关闭),可以尝试以下解决方案:
解决方案1:使用国内镜像源
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple spacy
解决方案2:手动下载模型
访问 https://github.com/explosion/spacy-models/releases 下载对应的模型文件,然后本地安装:pip install path/to/zh_core_web_sm-3.x.x.tar.gz
解决方案3:使用其他镜像站点
# 使用清华镜像
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ spacy
python -m spacy download zh_core_web_sm --index-url https://pypi.tuna.tsinghua.edu.cn/simple/
使用方法
基本用法
from llm_guard import sanitize_prompt_mcp
prompt = "你好,我是张三,我的电话是13812345678"
result = sanitize_prompt_mcp(prompt)
print(result)
在MCP服务中使用
from llm_guard import LLMSanitizer
sanitizer = LLMSanitizer()
result = sanitizer.sanitize_prompt("你的提示文本")
MCP服务部署
本项目支持MCP协议,可以作为MCP工具服务器运行。在这种架构下,工具注册在云端,但数据处理完全在本地进行,确保数据隐私。
运行MCP服务
python mcp_server.py
Docker部署
构建Docker镜像:
docker build -t llm-guard-mcp .
运行容器:
docker run -it --rm llm-guard-mcp
MCP工具说明
MCP服务提供以下工具:
sanitize_prompt
用于清洗提示文本,移除敏感信息。
参数:
- prompt (string, required): 需要清洗的提示文本
返回值:
- JSON格式的结果,包含原始文本、清洗后的文本、检测到的敏感信息等
插件市场上架
本项目支持发布到蚂蚁百宝箱插件市场,具体步骤如下:
1. 准备材料
- plugin.json - 插件配置文件 - PLUGIN.md - 插件详细介绍文档 - 源代码文件2. 创建MCP插件
参考文档: https://alipaytbox.yuque.com/sxs0ba/huntb8/hwhtg4sp1mdppsib3. 上架插件市场
参考文档: https://alipaytbox.yuque.com/sxs0ba/huntb8/ohqgnh2gt1qhymqb返回格式
函数会返回一个JSON对象,包含以下字段:
- original_prompt: 原始提示文本
- sanitized_prompt: 处理后的提示文本
- pii_detected: 检测到的PII信息列表
- api_keys_detected: 检测到的API密钥列表
- blacklist_words_found: 发现的黑名单词汇
- status: 处理状态 ("allowed" 或 "blocked")
- message: 处理消息
自定义配置
可以通过修改 llm_guard.py 中的 LLMSanitizer 类来自定义黑名单词汇或其他规则。
错误处理与降级机制
本实现具备完善的错误处理和降级机制:
1. 即使presidio库未安装或无法初始化,系统也会使用内置的正则表达式进行检测
2. 当网络问题导致spaCy模型无法下载时,系统仍然能够运行(虽然精度可能略有下降)
3. 所有异常都被适当捕获,不会导致整个应用程序崩溃
这种设计确保了系统在网络受限或依赖库缺失的环境中仍能正常工作。
Sign in to leave a review
Use Google, GitHub, or an email account so ratings stay tied to real people.
No reviews posted yet.
