mcp_decrypt_text_pii
Decrypt personally identifiable information from encrypted text using a mapping of original values to encrypted values within the MCP PII Tools server.
Instructions
MCP Tool: 암호화된 텍스트에서 PII 복호화
Args:
encrypted_text (str): 암호화된 텍스트
encrypted_items (Dict[str, str]): 원본값 -> 암호화값 매핑
Returns:
Dict[str, Any]: 복호화 결과
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| encrypted_text | Yes | ||
| encrypted_items | Yes |
Implementation Reference
- mcp_pii_tools.py:724-794 (handler)The core handler function decorated with @mcp.tool() that implements the decryption logic for PII in encrypted text. It reverses the encryption using PIICrypto and mapping provided.@mcp.tool() def mcp_decrypt_text_pii(encrypted_text: str, encrypted_items: Dict[str, str]) -> Dict[str, Any]: """ MCP Tool: 암호화된 텍스트에서 PII 복호화 Args: encrypted_text (str): 암호화된 텍스트 encrypted_items (Dict[str, str]): 원본값 -> 암호화값 매핑 Returns: Dict[str, Any]: 복호화 결과 """ try: start_time = time.time() # 암호화값 -> 원본값 매핑 생성 decrypted_items = {} decrypted_text = encrypted_text crypto = PIICrypto() for original_value, encrypted_value in encrypted_items.items(): # PII 유형 추정 (암호화된 값을 기준으로) pii_type = _guess_pii_type(encrypted_value) # Base64 패턴인 경우 (결정론적 암호화) 여러 유형 시도 if re.match(r'^[A-Za-z0-9+/=]+$', encrypted_value) and len(encrypted_value) > 20: # 이름, 주소, 이메일 순서로 시도 decrypted_value = None for try_type in ["이름", "주소", "이메일"]: try: result = crypto.decrypt_pii_item(encrypted_value, try_type) if not (result.startswith("[복호화실패:") or result.startswith("[FPE복호화실패:")): decrypted_value = result pii_type = try_type break except: continue if decrypted_value is None: decrypted_value = f"[복호화실패:{encrypted_value[:20]}...]" else: # FPE 암호화 또는 다른 패턴 decrypted_value = crypto.decrypt_pii_item(encrypted_value, pii_type) decrypted_items[encrypted_value] = decrypted_value # 텍스트에서 복호화 decrypted_text = decrypted_text.replace(encrypted_value, decrypted_value) processing_time = time.time() - start_time return { "success": True, "encrypted_text": encrypted_text, "decrypted_text": decrypted_text, "decrypted_items": decrypted_items, "count": len(encrypted_items), "processing_time": processing_time } except Exception as e: return { "success": False, "error": str(e), "encrypted_text": encrypted_text, "decrypted_text": encrypted_text, "decrypted_items": {}, "count": 0, "processing_time": 0 }
- mcp_pii_tools.py:962-978 (schema)The JSON schema defining the input parameters and description for the decrypt_text_pii tool (function named mcp_decrypt_text_pii)."decrypt_text_pii": { "name": "decrypt_text_pii", "description": "암호화된 텍스트에서 PII를 복호화합니다.", "parameters": { "type": "object", "properties": { "encrypted_text": { "type": "string", "description": "암호화된 텍스트" }, "encrypted_items": { "type": "object", "description": "원본값 -> 암호화값 매핑 딕셔너리" } }, "required": ["encrypted_text", "encrypted_items"] }
- mcp_pii_tools.py:796-837 (helper)Helper function used by the handler to guess PII type from encrypted values to aid decryption.def _guess_pii_type(value: str) -> str: """PII 값으로부터 유형을 추정하는 헬퍼 함수 (암호화된 값도 고려)""" import re # 이메일 패턴 if re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', value): return "이메일" # 전화번호 패턴 (한국) - 하이픈 포함 if re.match(r'^\d{3}-\d{4}-\d{4}$', value) or re.match(r'^\d{2,3}-\d{3,4}-\d{4}$', value): return "전화번호" # 신용카드번호 패턴 - 하이픈 포함 if re.match(r'^\d{4}-\d{4}-\d{4}-\d{4}$', value): return "신용카드번호" # 여권번호 패턴 - 알파벳 + 숫자 (암호화된 경우도 고려) if re.match(r'^[A-Z]\d{8,9}$', value) or re.match(r'^[A-Z]\d{7,10}$', value): return "여권번호" # 주민등록번호 패턴 - 하이픈 포함 if re.match(r'^\d{6}-\d{7}$', value): return "주민등록번호" # 주소 패턴 (한국) - 한글 키워드 포함 if any(keyword in value for keyword in ["시", "구", "로", "동", "번지"]): return "주소" # Base64 패턴 (결정론적 암호화 결과) - 이름, 주소, 이메일만 사용 if re.match(r'^[A-Za-z0-9+/=]+$', value): # Base64는 결정론적 암호화 결과이므로 이름, 주소, 이메일 중 하나 # 길이로 대략적인 구분 (정확하지 않을 수 있음) if len(value) < 50: return "이름" # 짧은 Base64는 보통 이름 elif len(value) < 70: return "이메일" # 중간 길이 Base64는 보통 이메일 else: return "주소" # 긴 Base64는 보통 주소 # 기본적으로 이름으로 추정 return "이름"
- mcp_pii_example.py:14-17 (registration)Registration of the mcp_decrypt_text_pii tool in the MCP server tools list in the example file.mcp_encrypt_text_pii, mcp_decrypt_text_pii, MCP_TOOLS )