Sponsored by Deepsite.site

Llm Guard Privacy Gateway

Created By
Amywith14 days ago
Content

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. 准备材料

2. 创建MCP插件

参考文档: https://alipaytbox.yuque.com/sxs0ba/huntb8/hwhtg4sp1mdppsib

3. 上架插件市场

参考文档: 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. 所有异常都被适当捕获,不会导致整个应用程序崩溃

这种设计确保了系统在网络受限或依赖库缺失的环境中仍能正常工作。

Recommend Servers
TraeBuild with Free GPT-4.1 & Claude 3.7. Fully MCP-Ready.
Jina AI MCP ToolsA Model Context Protocol (MCP) server that integrates with Jina AI Search Foundation APIs.
Serper MCP ServerA Serper MCP Server
EdgeOne Pages MCPAn MCP service designed for deploying HTML content to EdgeOne Pages and obtaining an accessible public URL.
DeepChatYour AI Partner on Desktop
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.
MiniMax MCPOfficial MiniMax Model Context Protocol (MCP) server that enables interaction with powerful Text to Speech, image generation and video generation APIs.
Visual Studio Code - Open Source ("Code - OSS")Visual Studio Code
Baidu Map百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
AiimagemultistyleA Model Context Protocol (MCP) server for image generation and manipulation using fal.ai's Stable Diffusion model.
Tavily Mcp
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.
CursorThe AI Code Editor
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.
Playwright McpPlaywright MCP server
MCP AdvisorMCP Advisor & Installation - Use the right MCP server for your needs
Context7Context7 MCP Server -- Up-to-date code documentation for LLMs and AI code editors
WindsurfThe new purpose-built IDE to harness magic
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"
ChatWiseThe second fastest AI chatbot™
Amap Maps高德地图官方 MCP Server