LLM Guard Privacy Gateway
A privacy protection gateway based on MCP protocol for detecting and filtering sensitive information in LLM applications.
Features
- PII desensitizationAutomatically recognize Chinese names, phone numbers, and email addresses in text and replace them with placeholders
- Key interceptionDetect common API Keys, Alibaba Cloud AccessKeys, and RSA private keys
- Business blockadeDefine a blacklist list, and if the text contains these words, return the intercept status directly
- MCP packagingEncapsulate the above logic into an MCP tool and return the processing result in JSON format
install
pip install -r requirements.txtRegarding the issue of downloading the spaCy model
If you encounter network issues while downloading the spaCy Chinese language model (such as a connection being closed by a remote host), you can try the following solutions:
Solution 1: Use domestic image sources
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple spacySolution 2: Manually download the model
visit https://github.com/explosion/spacy-models/releases Download the corresponding model file and install it locally:
pip install path/to/zh_core_web_sm-3.x.x.tar.gzSolution 3: Use other mirror sites
# 使用清华镜像
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/Instructions for use
Basic Usage
from llm_guard import sanitize_prompt_mcp
prompt = "你好,我是张三,我的电话是13812345678"
result = sanitize_prompt_mcp(prompt)
print(result)Used in MCP services
from llm_guard import LLMSanitizer
sanitizer = LLMSanitizer()
result = sanitizer.sanitize_prompt("你的提示文本")MCP Service Deployment
This project supports the MCP protocol and can run as an MCP tool server. In this architecture, tools are registered in the cloud, but data processing is entirely done locally to ensure data privacy.
Run MCP service
python mcp_server.pyDocker deployment
Build Docker image:
docker build -t llm-guard-mcp .Run container:
docker run -it --rm llm-guard-mcpMCP Tool Description
MCP services provide the following tools:
sanitize_prompt
Used for cleaning prompt text and removing sensitive information.
parameter
prompt(string, required): Prompt text that needs to be cleaned
Return value:
- The result in JSON format, including the original text, cleaned text, detected sensitive information, etc
Plugin Market Listing
This project supports publishing to the Ant Treasure Box plugin market. The specific steps are as follows:
1. Prepare materials
- \plugin.json\ - Plugin configuration file
- \PLUGIN.md\ - Plugin Detailed Introduction Document
- Source code file
2. Create MCP plugin
Reference document: https://alipaytbox.yuque.com/sxs0ba/huntb8/hwhtg4sp1mdppsib
3. Add plugins to the market
Reference document: https://alipaytbox.yuque.com/sxs0ba/huntb8/ohqgnh2gt1qhymqb
return format
The function will return a JSON object containing the following fields:
original_prompt: Original prompt textsanitized_promptProcessed prompt textpii_detectedList of detected PII informationapi_keys_detectedList of detected API keysblacklist_words_foundDiscovered blacklist vocabularystatus: Processing status ("allowed" or "blocked")message: Processing messages
Custom Configuration
You can modify \[llm_guard. py\]( file:///e:/llm -In guard/lm_guard. py LLMSanitizer Class to customize blacklist vocabulary or other rules.
Error handling and downgrade mechanism
This implementation has a comprehensive error handling and downgrade mechanism:
- Even if the Presidio library is not installed or cannot be initialized, the system will still use built-in regular expressions for detection
- When network issues prevent the spaCy model from downloading, the system can still run (although the accuracy may slightly decrease)
- All exceptions are properly captured and will not cause the entire application to crash
This design ensures that the system can still function properly in environments with network limitations or missing dependency libraries.
