Skip to main content
Glama
czangyeob

MCP PII Tools

by czangyeob

mcp_detect_pii

Detect personally identifiable information (PII) in text to identify sensitive data that requires protection.

Instructions

MCP Tool: 텍스트에서 PII 탐지

Args:
    text (str): 분석할 텍스트
    
Returns:
    Dict[str, Any]: 탐지 결과

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
resultYes

Implementation Reference

  • The handler function for the 'mcp_detect_pii' MCP tool. Decorated with @mcp.tool() for automatic registration and execution. Delegates to the global detector's detect_pii method.
    @mcp.tool()
    def mcp_detect_pii(text: str) -> Dict[str, Any]:
        """
        MCP Tool: 텍스트에서 PII 탐지
        
        Args:
            text (str): 분석할 텍스트
            
        Returns:
            Dict[str, Any]: 탐지 결과
        """
        detector = get_detector()
        return detector.detect_pii(text)
  • Core helper method in MCPPIIDetector class that performs the actual PII detection using langextract library, handles positions, confidence, and formats the output.
    def detect_pii(self, text: str) -> Dict[str, Any]:
        """
        텍스트에서 PII를 탐지 (MCP Tool용)
        
        Args:
            text (str): 분석할 텍스트
            
        Returns:
            Dict[str, Any]: MCP Tool 응답 형식
        """
        try:
            start_time = time.time()
            
            # Provider에 따른 langextract 호출
            if self.provider_type == "vllm":
                # vLLM Provider 사용
                result = lx.extract(
                    text_or_documents=text,
                    prompt_description=self.prompt,
                    examples=self.examples,
                    model=self.provider,  # 커스텀 Provider 인스턴스 사용
                    use_schema_constraints=False
                )
            else:
                # OpenAI Provider 사용 (기본)
                os.environ["OPENAI_BASE_URL"] = "https://api.openai.com/v1"
                result = lx.extract(
                    text_or_documents=text,
                    prompt_description=self.prompt,
                    examples=self.examples,
                    model_id=self.model_id,
                    api_key=self.api_key,
                    fence_output=True
                )
            
            # 결과를 PIIItem 리스트로 변환
            pii_items = []
            logger.info(f"탐지된 extraction 수: {len(result.extractions)}")
            
            for i, extraction in enumerate(result.extractions):
                logger.info(f"Extraction {i+1}: class='{extraction.extraction_class}', text='{extraction.extraction_text}'")
                
                # char_interval이 없으면 텍스트에서 직접 위치 찾기
                start_pos = 0
                end_pos = 0
                
                if extraction.char_interval:
                    start_pos = extraction.char_interval.start_pos
                    end_pos = extraction.char_interval.end_pos
                    logger.info(f"  char_interval 사용: {start_pos}-{end_pos}")
                else:
                    # 텍스트에서 직접 위치 찾기 (대소문자 구분 없이)
                    search_text = extraction.extraction_text
                    start_pos = text.find(search_text)
                    
                    # 대소문자 구분 없이 찾기
                    if start_pos == -1:
                        start_pos = text.lower().find(search_text.lower())
                    
                    if start_pos != -1:
                        end_pos = start_pos + len(search_text)
                        logger.info(f"  텍스트에서 직접 찾음: {start_pos}-{end_pos}")
                        
                        # 실제 찾은 텍스트와 원본이 일치하는지 확인
                        actual_found = text[start_pos:end_pos]
                        if actual_found != search_text:
                            logger.warning(f"  대소문자 차이: 찾은='{actual_found}', 원본='{search_text}'")
                    else:
                        start_pos = -1
                        end_pos = -1
                        logger.warning(f"  텍스트에서 찾을 수 없음: '{extraction.extraction_text}'")
                
                mapped_type = self._map_extraction_class(extraction.extraction_class)
                logger.info(f"  매핑된 타입: '{extraction.extraction_class}' -> '{mapped_type}'")
                
                pii_items.append(PIIItem(
                    type=mapped_type,
                    value=extraction.extraction_text,
                    confidence=0.9,  # langextract는 confidence를 제공하지 않으므로 기본값
                    start_pos=start_pos,
                    end_pos=end_pos
                ))
            
            processing_time = time.time() - start_time
            
            return {
                "success": True,
                "pii_items": [asdict(item) for item in pii_items],
                "count": len(pii_items),
                "processing_time": processing_time,
                "summary": self._get_pii_summary(pii_items)
            }
            
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "pii_items": [],
                "count": 0,
                "processing_time": 0,
                "summary": {}
            }
  • Dataclass schema defining the structure of individual PII detections returned by the tool.
    @dataclass
    class PIIItem:
        """PII 항목을 나타내는 데이터 클래스"""
        type: str           # PII 유형 (이름, 전화번호, 이메일 등)
        value: str          # 추출된 값
        confidence: float   # 신뢰도 (0.0-1.0)
        start_pos: int      # 텍스트 내 시작 위치
        end_pos: int        # 텍스트 내 끝 위치
  • Helper function providing singleton access to the global MCPPIIDetector instance used by the tool.
    def get_detector():
        """전역 PII 탐지기 인스턴스를 반환합니다."""
        global _global_detector
        if _global_detector is None:
            _global_detector = MCPPIIDetector()
        return _global_detector
  • Explicit JSON schema for the tool's input parameters (text: string), used for documentation and validation.
    "detect_pii": {
        "name": "detect_pii",
        "description": "텍스트에서 PII(개인정보)를 탐지합니다. 이름, 이메일, 전화번호, 여권번호, 주소 등을 찾습니다.",
        "parameters": {
            "type": "object",
            "properties": {
                "text": {
                    "type": "string",
                    "description": "분석할 텍스트"
                }
            },
            "required": ["text"]
        }
    },
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool detects PII but doesn't specify what types of PII are detected (e.g., names, emails, SSNs), the detection method (e.g., regex, ML models), or any limitations (e.g., accuracy, language support). This leaves significant gaps in understanding how the tool behaves beyond its basic function.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is brief and structured with clear sections for Args and Returns, making it easy to parse. However, the title is null, and the content could be more front-loaded with the core purpose before parameter details. It avoids redundancy but misses opportunities for efficiency in explaining behavior.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (PII detection can involve nuanced logic), lack of annotations, and presence of an output schema, the description is minimally adequate. It covers the basic function and parameters but fails to address key behavioral aspects like detection scope or error handling. The output schema likely details the return structure, reducing the need for return value explanation, but overall completeness is limited.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The description adds minimal value beyond the input schema. It documents the single parameter 'text' as '분석할 텍스트' (text to analyze), which aligns with the schema's 'Text' title but doesn't provide additional context like format expectations (e.g., plain text vs. structured data) or constraints (e.g., length limits). With 0% schema description coverage, this is inadequate compensation, but the single parameter keeps it from being lower.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: '텍스트에서 PII 탐지' (Detect PII in text). It specifies the verb (detect) and resource (PII in text), making the function unambiguous. However, it doesn't explicitly differentiate from sibling tools like mcp_anonymize_text or mcp_process_text, which likely perform related but distinct operations.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With siblings like mcp_anonymize_text (which might anonymize PII after detection) and mcp_process_text (a more general tool), the agent lacks explicit direction on selection criteria, such as 'use this for detection only' or 'combine with mcp_anonymize_text for full anonymization.'

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/czangyeob/mcp-pii-tools'

If you have feedback or need assistance with the MCP directory API, please join our Discord server