隐私保护网关

一个基于MCP协议的隐私保护网关,用于在LLM应用中检测和过滤敏感信息。

  • by Amywith
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Amywith/llm-guard-mcp

使用说明

LLM Guard Privacy Gateway

A privacy protection gateway based on MCP protocol for detecting and filtering sensitive information in LLM applications.

Features

  1. PII desensitizationAutomatically recognize Chinese names, phone numbers, and email addresses in text and replace them with placeholders
  2. Key interceptionDetect common API Keys, Alibaba Cloud AccessKeys, and RSA private keys
  3. Business blockadeDefine a blacklist list, and if the text contains these words, return the intercept status directly
  4. MCP packagingEncapsulate the above logic into an MCP tool and return the processing result in JSON format

install

pip install -r requirements.txt

Regarding 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 spacy

Solution 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.gz

Solution 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.py

Docker deployment

Build Docker image:

docker build -t llm-guard-mcp .

Run container:

docker run -it --rm llm-guard-mcp

MCP 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]( file:///e:/llm -Guard/plugin. json) - Plugin configuration file
  • [PLUGIN.md]( file:///e:/llm -Guard/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 text
  • sanitized_promptProcessed prompt text
  • pii_detectedList of detected PII information
  • api_keys_detectedList of detected API keys
  • blacklist_words_foundDiscovered blacklist vocabulary
  • status: 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:

  1. Even if the Presidio library is not installed or cannot be initialized, the system will still use built-in regular expressions for detection
  2. When network issues prevent the spaCy model from downloading, the system can still run (although the accuracy may slightly decrease)
  3. 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.