MCP数据脱敏应用开发

一、概述

数据脱敏(Data Masking),又称数据漂白、数据去隐私化或数据变形。

定义

指对某些敏感信息通过脱敏规则进行数据的变形,实现敏感隐私数据的可靠保护。在涉及客户安全数据或者一些商业性敏感数据的情况下,在不违反系统规则条件下,对真实数据进行改造并提供测试使用,如身份证号、手机号、卡号、客户号等个人信息都需要进行数据脱敏。

脱敏规则

可以包括但不限于:

替换:例如将身份证号的部分数字替换为"*"或随机数。

随机化:例如生成与真实数据格式相符但与原始数据无关的新数据。

固定模式脱敏:例如保留电话号码的区号和最后几位,其余部分替换。

加密:对数据进行不可逆或可逆加密,仅在必要时解密。

假名化:用虚构的名字替换真实姓名。

二、数据脱敏开发

这里直接使用python代码开发,使用fastmcp框架。

server.py

复制代码
from fastmcp import FastMCP
import re

mcp = FastMCP("desensitize", port=9000)


class DataMasker:
    def __init__(self):
        pass

    def mask_phone_number(self, phone_number):
        """
        对手机号码进行脱敏处理,将中间四位替换为 *
        """
        if len(phone_number) == 11:
            return phone_number[:3] + "****" + phone_number[7:]
        return phone_number

    def mask_email(self, email):
        """
        对邮箱地址进行脱敏处理,只显示邮箱名前两位和域名
        """
        if "@" in email:
            username, domain = email.split("@")
            return username[:2] + "****@" + domain
        return email

    def mask_id_card(self, id_card):
        """
        对身份证号码进行脱敏处理,只显示前四位和后四位
        """
        if len(id_card) == 18:
            return id_card[:4] + "**********" + id_card[14:]
        return id_card

    def mask_address(self, address):
        """
        对地址进行脱敏处理,模糊化门牌号和房间号
        例如,将 "1栋" 替换为 "**栋","101室" 替换为 "***室"
        """
        # 使用正则表达式
        desensitized_address = re.sub(r"(\d+)栋", r"**栋", address)
        desensitized_address = re.sub(r"(\d+)室", r"***室", desensitized_address)
        return desensitized_address


@mcp.tool()
def desensitize_text(text: str) -> str:
    """
    脱敏文本信息
    """
    masker = DataMasker()
    # 匹配手机号
    phone_pattern = r"\d{11}"
    phones = re.findall(phone_pattern, text)
    for phone in phones:
        masked_phone = masker.mask_phone_number(phone)
        text = text.replace(phone, masked_phone)

    # 匹配邮箱
    email_pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
    emails = re.findall(email_pattern, text)
    for email in emails:
        masked_email = masker.mask_email(email)
        text = text.replace(email, masked_email)

    # 匹配身份证号
    id_card_pattern = r"\d{18}"
    id_cards = re.findall(id_card_pattern, text)
    for id_card in id_cards:
        masked_id_card = masker.mask_id_card(id_card)
        text = text.replace(id_card, masked_id_card)

    # 匹配地址
    address_pattern = r"([\u4e00-\u9fa5]+省)?([\u4e00-\u9fa5]+市)?([\u4e00-\u9fa5]+区)?([\u4e00-\u9fa5]+街道)?(\d+[\u4e00-\u9fa5]+)?(\d+[\u4e00-\u9fa5]+)?"
    addresss = re.findall(address_pattern, text)

    # 如果没有找到地址,返回原始文本
    if not addresss:
        return text

    # 对每个匹配的地址进行脱敏处理
    for address_parts in addresss:
        # 将匹配的地址部分组合成完整的地址
        address = "".join([part for part in address_parts if part])
        if address:
            # print("address",address)
            masked_address = masker.mask_address(address)
            text = text.replace(address, masked_address)

    return text


if __name__ == "__main__":
    mcp.run(transport="sse")

运行代码

复制代码
python3 server.py

三、数据脱敏测试

打开Cherry Studio客户端,添加MCP服务器

添加智能体

将智能体添加到助手

开启MCP

聊天窗口,输入用户信息:

我的手机号是13812345678,我的邮箱是test@example.com,我的身份证号是123456789012345678,我的地址是北京市海淀区中关村大街1栋101室

可以看到返回的json,已经做了脱敏处理。

但是客户端返回的信息,有点不全。这是因为AI模型做了处理。因为AI模型认为这些敏感信息返回不安全,做了2次加工。

不过不影响,最终结果还是做了数据脱敏处理即可。

相关推荐
Just_Paranoid3 天前
华为云Flexus+DeepSeek征文|基于Dify构建音视频内容转录工作流
华为云·音视频·dify·maas·deepseek·flexusx
cooldream20094 天前
华为云Flexus+DeepSeek征文|利用华为云一键部署 Dify 平台并接入 DeepSeek 大模型,构建长篇文章生成助手
大模型·华为云·dify
Just_Paranoid14 天前
华为云Flexus+DeepSeek征文|基于Dify构建抓取金融新闻并发送邮箱工作流
华为云·dify·maas·新闻资讯·deepseek·flexusx
Just_Paranoid15 天前
华为云Flexus+DeepSeek征文|基于Dify构建解析网页写入Notion笔记工作流
华为云·notion·dify·deepseek·firecrawl·flexusx
Just_Paranoid15 天前
华为云Flexus+DeepSeek征文|基于Dify构建智能票据信息识别助手
华为云·ocr·dify·maas·deepseek·flexusx
cooldream200916 天前
华为云Flexus+DeepSeek征文|基于华为云一键部署Dify LLM 应用构建 PPT 生成助手的开发与实践
华为云·powerpoint·dify
Allen2000017 天前
Dify动手实践课3
dify
Jayin_chan17 天前
dify本地部署及添加ollama模型(ubuntu24.04)
ubuntu·ai大模型·dify·rag·本地部署
yumuing blog17 天前
实战指南:部署MinerU多模态文档解析API与Dify深度集成(实现解析PDF/JPG/PNG)
docker·pdf·ocr·markdown·dify·parse·mineru
cooldream200919 天前
华为云Flexus+DeepSeek征文|一键部署华为云CCE容器高可用Dify平台的实践经验与思考
华为云·高可用·dify