/get-detected-piis
Detects Personally Identifiable Information (PII) in text, including emails, phone numbers, SSNs, credit cards, and IP addresses. Returns identified PII types, values, and positions for data security and compliance.
Instructions
Analyzes text for Personally Identifiable Information (PII) using regex patterns.
This function scans the input text for common PII patterns including:
Email addresses
Phone numbers (US format: XXX-XXX-XXXX)
Social Security Numbers (XXX-XX-XXXX)
Credit card numbers (XXXX-XXXX-XXXX-XXXX)
IP addresses (IPv4)
Args: text (str): The text content to be analyzed for PII entities.
Returns: List[Dict]: A list of detected PII entities, sorted by their position in the text. Each dictionary contains: - type (str): Type of PII detected (EMAIL, PHONE, SSN, etc.) - value (str): The actual PII string found - start (int): Starting character position in the text - end (int): Ending character position in the text
Example: >>> text = "Contact john.doe@email.com or 123-456-7890" >>> get_detected_piis(text) [ { "type": "EMAIL", "value": "john.doe@email.com", "start": 8, "end": 25 }, { "type": "PHONE", "value": "123-456-7890", "start": 29, "end": 41 } ]
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes |
Implementation Reference
- main.py:12-73 (handler)The handler function that executes the /get-detected-piis tool logic. It uses regex patterns to detect PII such as emails, phones, SSNs, credit cards, and IP addresses in the input text, returning a sorted list of detections with positions.
def get_detected_piis(text: str) -> List[Dict]: """ Analyzes text for Personally Identifiable Information (PII) using regex patterns. This function scans the input text for common PII patterns including: - Email addresses - Phone numbers (US format: XXX-XXX-XXXX) - Social Security Numbers (XXX-XX-XXXX) - Credit card numbers (XXXX-XXXX-XXXX-XXXX) - IP addresses (IPv4) Args: text (str): The text content to be analyzed for PII entities. Returns: List[Dict]: A list of detected PII entities, sorted by their position in the text. Each dictionary contains: - type (str): Type of PII detected (EMAIL, PHONE, SSN, etc.) - value (str): The actual PII string found - start (int): Starting character position in the text - end (int): Ending character position in the text Example: >>> text = "Contact john.doe@email.com or 123-456-7890" >>> get_detected_piis(text) [ { "type": "EMAIL", "value": "john.doe@email.com", "start": 8, "end": 25 }, { "type": "PHONE", "value": "123-456-7890", "start": 29, "end": 41 } ] """ import re pii_patterns = { 'EMAIL': r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', 'PHONE': r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b', 'SSN': r'\b\d{3}[-]?\d{2}[-]?\d{4}\b', 'CREDIT_CARD': r'\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b', 'IP_ADDRESS': r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b' } results = [] for pii_type, pattern in pii_patterns.items(): matches = re.finditer(pattern, text) for match in matches: results.append({ 'type': pii_type, 'value': match.group(), 'start': match.start(), 'end': match.end() }) return sorted(results, key=lambda x: x['start']) - main.py:10-10 (registration)Registers the get_detected_piis function as an MCP tool using the FastMCP decorator with the tool name constant.
@mcp.tool(GET_DETECTED_PIIS) - main.py:12-12 (schema)Type signature defining input (text: str) and output (List[Dict]) for the tool.
def get_detected_piis(text: str) -> List[Dict]: - app/utils/constants.py:1-1 (helper)Constant defining the tool name string used in registration.
GET_DETECTED_PIIS = "/get-detected-piis" - main.py:54-59 (helper)Regex patterns dictionary used by the handler for detecting different types of PII.
pii_patterns = { 'EMAIL': r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b', 'PHONE': r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b', 'SSN': r'\b\d{3}[-]?\d{2}[-]?\d{4}\b', 'CREDIT_CARD': r'\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b', 'IP_ADDRESS': r'\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b'