phi-guard-mcp 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- phi_guard_mcp/__init__.py +32 -0
- phi_guard_mcp/benchmark.py +139 -0
- phi_guard_mcp/cli.py +124 -0
- phi_guard_mcp/engine.py +275 -0
- phi_guard_mcp/gate.py +110 -0
- phi_guard_mcp/models.py +140 -0
- phi_guard_mcp/server.py +144 -0
- phi_guard_mcp-0.1.2.dist-info/METADATA +206 -0
- phi_guard_mcp-0.1.2.dist-info/RECORD +12 -0
- phi_guard_mcp-0.1.2.dist-info/WHEEL +4 -0
- phi_guard_mcp-0.1.2.dist-info/entry_points.txt +3 -0
- phi_guard_mcp-0.1.2.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Rule-based PHI detection, redaction, audit, and validation helpers."""
|
|
2
|
+
|
|
3
|
+
from .benchmark import evaluate_benchmark
|
|
4
|
+
from .engine import audit_text, redact_text, scan_text, validate_no_phi
|
|
5
|
+
from .gate import run_gate
|
|
6
|
+
from .models import (
|
|
7
|
+
AuditReport,
|
|
8
|
+
BenchmarkReport,
|
|
9
|
+
Finding,
|
|
10
|
+
GateReport,
|
|
11
|
+
RedactionResult,
|
|
12
|
+
ScanResult,
|
|
13
|
+
ValidationResult,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"AuditReport",
|
|
18
|
+
"BenchmarkReport",
|
|
19
|
+
"Finding",
|
|
20
|
+
"GateReport",
|
|
21
|
+
"RedactionResult",
|
|
22
|
+
"ScanResult",
|
|
23
|
+
"ValidationResult",
|
|
24
|
+
"audit_text",
|
|
25
|
+
"evaluate_benchmark",
|
|
26
|
+
"redact_text",
|
|
27
|
+
"run_gate",
|
|
28
|
+
"scan_text",
|
|
29
|
+
"validate_no_phi",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
__version__ = "0.1.2"
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"""Synthetic benchmark evaluation for PHI-like identifier detection."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from collections import Counter, defaultdict
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from .engine import scan_text
|
|
10
|
+
from .models import (
|
|
11
|
+
BenchmarkCase,
|
|
12
|
+
BenchmarkCaseResult,
|
|
13
|
+
BenchmarkCategoryMetrics,
|
|
14
|
+
BenchmarkReport,
|
|
15
|
+
ExpectedFinding,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
FindingKey = tuple[str, str]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def evaluate_benchmark(cases_dir: str | Path) -> BenchmarkReport:
|
|
22
|
+
"""Evaluate detector output against synthetic benchmark cases."""
|
|
23
|
+
|
|
24
|
+
root = Path(cases_dir)
|
|
25
|
+
case_paths = sorted(root.glob("*.json"))
|
|
26
|
+
if not case_paths:
|
|
27
|
+
raise ValueError(f"No benchmark case JSON files found in {root}")
|
|
28
|
+
|
|
29
|
+
total_tp = 0
|
|
30
|
+
total_fp = 0
|
|
31
|
+
total_fn = 0
|
|
32
|
+
case_results: list[BenchmarkCaseResult] = []
|
|
33
|
+
category_counts: dict[str, Counter[str]] = defaultdict(Counter)
|
|
34
|
+
|
|
35
|
+
for case_path in case_paths:
|
|
36
|
+
case = BenchmarkCase.model_validate(json.loads(case_path.read_text(encoding="utf-8")))
|
|
37
|
+
scan = scan_text(case.text)
|
|
38
|
+
|
|
39
|
+
expected = Counter((item.category, item.text) for item in case.expected_findings)
|
|
40
|
+
detected = Counter((item.category, item.text) for item in scan.findings)
|
|
41
|
+
matched = expected & detected
|
|
42
|
+
false_positive = detected - matched
|
|
43
|
+
false_negative = expected - matched
|
|
44
|
+
|
|
45
|
+
tp = sum(matched.values())
|
|
46
|
+
fp = sum(false_positive.values())
|
|
47
|
+
fn = sum(false_negative.values())
|
|
48
|
+
total_tp += tp
|
|
49
|
+
total_fp += fp
|
|
50
|
+
total_fn += fn
|
|
51
|
+
|
|
52
|
+
_count_categories(category_counts, "expected", expected)
|
|
53
|
+
_count_categories(category_counts, "detected", detected)
|
|
54
|
+
_count_categories(category_counts, "true_positive", matched)
|
|
55
|
+
_count_categories(category_counts, "false_positive", false_positive)
|
|
56
|
+
_count_categories(category_counts, "false_negative", false_negative)
|
|
57
|
+
|
|
58
|
+
case_results.append(
|
|
59
|
+
BenchmarkCaseResult(
|
|
60
|
+
id=case.id,
|
|
61
|
+
true_positive=tp,
|
|
62
|
+
false_positive=fp,
|
|
63
|
+
false_negative=fn,
|
|
64
|
+
precision=_precision(tp, fp),
|
|
65
|
+
recall=_recall(tp, fn),
|
|
66
|
+
f1=_f1(tp, fp, fn),
|
|
67
|
+
missing=_keys_to_findings(false_negative),
|
|
68
|
+
unexpected=_keys_to_findings(false_positive),
|
|
69
|
+
)
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
return BenchmarkReport(
|
|
73
|
+
cases_dir=str(root),
|
|
74
|
+
total_cases=len(case_results),
|
|
75
|
+
true_positive=total_tp,
|
|
76
|
+
false_positive=total_fp,
|
|
77
|
+
false_negative=total_fn,
|
|
78
|
+
precision=_precision(total_tp, total_fp),
|
|
79
|
+
recall=_recall(total_tp, total_fn),
|
|
80
|
+
f1=_f1(total_tp, total_fp, total_fn),
|
|
81
|
+
per_category=_category_metrics(category_counts),
|
|
82
|
+
cases=case_results,
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def _count_categories(
|
|
87
|
+
category_counts: dict[str, Counter[str]],
|
|
88
|
+
metric: str,
|
|
89
|
+
values: Counter[FindingKey],
|
|
90
|
+
) -> None:
|
|
91
|
+
for (category, _text), count in values.items():
|
|
92
|
+
category_counts[category][metric] += count
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _category_metrics(category_counts: dict[str, Counter[str]]) -> dict[str, BenchmarkCategoryMetrics]:
|
|
96
|
+
metrics: dict[str, BenchmarkCategoryMetrics] = {}
|
|
97
|
+
for category in sorted(category_counts):
|
|
98
|
+
counts = category_counts[category]
|
|
99
|
+
tp = counts["true_positive"]
|
|
100
|
+
fp = counts["false_positive"]
|
|
101
|
+
fn = counts["false_negative"]
|
|
102
|
+
metrics[category] = BenchmarkCategoryMetrics(
|
|
103
|
+
expected=counts["expected"],
|
|
104
|
+
detected=counts["detected"],
|
|
105
|
+
true_positive=tp,
|
|
106
|
+
false_positive=fp,
|
|
107
|
+
false_negative=fn,
|
|
108
|
+
precision=_precision(tp, fp),
|
|
109
|
+
recall=_recall(tp, fn),
|
|
110
|
+
f1=_f1(tp, fp, fn),
|
|
111
|
+
)
|
|
112
|
+
return metrics
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _keys_to_findings(values: Counter[FindingKey]) -> list[ExpectedFinding]:
|
|
116
|
+
findings: list[ExpectedFinding] = []
|
|
117
|
+
for category, text in sorted(values):
|
|
118
|
+
findings.extend(
|
|
119
|
+
ExpectedFinding(category=category, text=text)
|
|
120
|
+
for _ in range(values[(category, text)])
|
|
121
|
+
)
|
|
122
|
+
return findings
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _precision(true_positive: int, false_positive: int) -> float:
|
|
126
|
+
denominator = true_positive + false_positive
|
|
127
|
+
return round(true_positive / denominator, 6) if denominator else 1.0
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _recall(true_positive: int, false_negative: int) -> float:
|
|
131
|
+
denominator = true_positive + false_negative
|
|
132
|
+
return round(true_positive / denominator, 6) if denominator else 1.0
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _f1(true_positive: int, false_positive: int, false_negative: int) -> float:
|
|
136
|
+
precision = _precision(true_positive, false_positive)
|
|
137
|
+
recall = _recall(true_positive, false_negative)
|
|
138
|
+
denominator = precision + recall
|
|
139
|
+
return round(2 * precision * recall / denominator, 6) if denominator else 0.0
|
phi_guard_mcp/cli.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"""Command line interface for phi-guard."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import json
|
|
7
|
+
import sys
|
|
8
|
+
from collections.abc import Sequence
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
from . import __version__
|
|
12
|
+
from .benchmark import evaluate_benchmark
|
|
13
|
+
from .engine import audit_text, redact_text, scan_text, validate_no_phi
|
|
14
|
+
from .gate import run_gate
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
18
|
+
argv = tuple(sys.argv[1:] if argv is None else argv)
|
|
19
|
+
parser = _build_parser()
|
|
20
|
+
|
|
21
|
+
if argv in (("--help",), ("-h",)):
|
|
22
|
+
parser.print_help()
|
|
23
|
+
return 0
|
|
24
|
+
if argv == ("--version",):
|
|
25
|
+
print(f"phi-guard {__version__}")
|
|
26
|
+
return 0
|
|
27
|
+
|
|
28
|
+
args = parser.parse_args(argv)
|
|
29
|
+
|
|
30
|
+
if args.command is None:
|
|
31
|
+
parser.print_help()
|
|
32
|
+
return 0
|
|
33
|
+
|
|
34
|
+
if args.command == "benchmark":
|
|
35
|
+
result = evaluate_benchmark(args.cases_dir)
|
|
36
|
+
payload = result.model_dump()
|
|
37
|
+
if args.out:
|
|
38
|
+
_write_json(args.out, payload)
|
|
39
|
+
_print_json(payload)
|
|
40
|
+
return 0
|
|
41
|
+
|
|
42
|
+
if args.command == "gate":
|
|
43
|
+
result = run_gate(args.path, config_path=args.config)
|
|
44
|
+
_print_json(result.model_dump())
|
|
45
|
+
return 0 if result.ok else 1
|
|
46
|
+
|
|
47
|
+
text = _read_text(args.file)
|
|
48
|
+
|
|
49
|
+
if args.command == "scan":
|
|
50
|
+
_print_json(scan_text(text).model_dump())
|
|
51
|
+
return 0
|
|
52
|
+
|
|
53
|
+
if args.command == "redact":
|
|
54
|
+
result = redact_text(text, mode=args.mode)
|
|
55
|
+
out_path = Path(args.out)
|
|
56
|
+
out_path.write_text(result.redacted_text, encoding="utf-8")
|
|
57
|
+
_print_json(result.model_dump())
|
|
58
|
+
return 0
|
|
59
|
+
|
|
60
|
+
if args.command == "audit":
|
|
61
|
+
_print_json(audit_text(text).model_dump())
|
|
62
|
+
return 0
|
|
63
|
+
|
|
64
|
+
if args.command == "validate":
|
|
65
|
+
result = validate_no_phi(text)
|
|
66
|
+
_print_json(result.model_dump())
|
|
67
|
+
return 0 if result.ok else 1
|
|
68
|
+
|
|
69
|
+
parser.error(f"Unknown command: {args.command}")
|
|
70
|
+
return 2
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
74
|
+
parser = argparse.ArgumentParser(
|
|
75
|
+
prog="phi-guard",
|
|
76
|
+
description="Detect, redact, and audit PHI-like identifiers before text reaches AI agents.",
|
|
77
|
+
)
|
|
78
|
+
parser.add_argument("--version", action="version", version=f"phi-guard {__version__}")
|
|
79
|
+
subparsers = parser.add_subparsers(dest="command")
|
|
80
|
+
|
|
81
|
+
scan_parser = subparsers.add_parser("scan", help="Scan a text file and output JSON findings.")
|
|
82
|
+
scan_parser.add_argument("file", help="Path to .txt, .md, .json, or '-' for stdin.")
|
|
83
|
+
|
|
84
|
+
redact_parser = subparsers.add_parser("redact", help="Redact a text file and write the result.")
|
|
85
|
+
redact_parser.add_argument("file", help="Path to .txt, .md, .json, or '-' for stdin.")
|
|
86
|
+
redact_parser.add_argument("--out", required=True, help="Path for the redacted output file.")
|
|
87
|
+
redact_parser.add_argument("--mode", choices=["placeholder"], default="placeholder")
|
|
88
|
+
|
|
89
|
+
audit_parser = subparsers.add_parser("audit", help="Output an audit JSON report.")
|
|
90
|
+
audit_parser.add_argument("file", help="Path to .txt, .md, .json, or '-' for stdin.")
|
|
91
|
+
|
|
92
|
+
validate_parser = subparsers.add_parser(
|
|
93
|
+
"validate",
|
|
94
|
+
help="Validate that no PHI-like identifiers are detected.",
|
|
95
|
+
)
|
|
96
|
+
validate_parser.add_argument("file", help="Path to .txt, .md, .json, or '-' for stdin.")
|
|
97
|
+
|
|
98
|
+
benchmark_parser = subparsers.add_parser("benchmark", help="Run a synthetic benchmark.")
|
|
99
|
+
benchmark_parser.add_argument("cases_dir", help="Directory containing synthetic benchmark JSON cases.")
|
|
100
|
+
benchmark_parser.add_argument("--out", help="Optional path for the benchmark JSON report.")
|
|
101
|
+
|
|
102
|
+
gate_parser = subparsers.add_parser("gate", help="Run the repository privacy gate.")
|
|
103
|
+
gate_parser.add_argument("path", nargs="?", default=".", help="Directory or file to scan.")
|
|
104
|
+
gate_parser.add_argument("--config", help="Path to .phi-guard.toml.")
|
|
105
|
+
|
|
106
|
+
return parser
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _read_text(file_arg: str) -> str:
|
|
110
|
+
if file_arg == "-":
|
|
111
|
+
return sys.stdin.read()
|
|
112
|
+
return Path(file_arg).read_text(encoding="utf-8")
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _print_json(payload: object) -> None:
|
|
116
|
+
print(json.dumps(payload, indent=2, ensure_ascii=False))
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _write_json(path: str, payload: object) -> None:
|
|
120
|
+
Path(path).write_text(json.dumps(payload, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
if __name__ == "__main__":
|
|
124
|
+
raise SystemExit(main())
|
phi_guard_mcp/engine.py
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
"""Transparent rule-based PHI-like identifier detection."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from collections import Counter
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from re import Pattern
|
|
9
|
+
|
|
10
|
+
from .models import (
|
|
11
|
+
SAFE_HARBOR_IDENTIFIERS,
|
|
12
|
+
AuditReport,
|
|
13
|
+
Finding,
|
|
14
|
+
PHICategory,
|
|
15
|
+
RedactionMode,
|
|
16
|
+
RedactionResult,
|
|
17
|
+
ScanResult,
|
|
18
|
+
ValidationResult,
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass(frozen=True)
|
|
23
|
+
class Rule:
|
|
24
|
+
rule_id: str
|
|
25
|
+
category: PHICategory
|
|
26
|
+
pattern: Pattern[str]
|
|
27
|
+
confidence: float
|
|
28
|
+
group: str | None = None
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
MONTH_PATTERN = (
|
|
32
|
+
r"Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|"
|
|
33
|
+
r"Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?"
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
RULES: tuple[Rule, ...] = (
|
|
38
|
+
Rule(
|
|
39
|
+
rule_id="email.basic",
|
|
40
|
+
category="EMAIL",
|
|
41
|
+
pattern=re.compile(r"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b", re.IGNORECASE),
|
|
42
|
+
confidence=0.99,
|
|
43
|
+
),
|
|
44
|
+
Rule(
|
|
45
|
+
rule_id="ssn.us",
|
|
46
|
+
category="SSN",
|
|
47
|
+
pattern=re.compile(r"\b\d{3}-\d{2}-\d{4}\b"),
|
|
48
|
+
confidence=0.98,
|
|
49
|
+
),
|
|
50
|
+
Rule(
|
|
51
|
+
rule_id="phone.us",
|
|
52
|
+
category="PHONE",
|
|
53
|
+
pattern=re.compile(r"(?<!\d)(?:\+?1[-.\s]?)?(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}(?!\d)"),
|
|
54
|
+
confidence=0.92,
|
|
55
|
+
),
|
|
56
|
+
Rule(
|
|
57
|
+
rule_id="url.http",
|
|
58
|
+
category="URL",
|
|
59
|
+
pattern=re.compile(r"\bhttps?://[^\s)>\"\]`']+", re.IGNORECASE),
|
|
60
|
+
confidence=0.97,
|
|
61
|
+
),
|
|
62
|
+
Rule(
|
|
63
|
+
rule_id="ip.v4",
|
|
64
|
+
category="IP_ADDRESS",
|
|
65
|
+
pattern=re.compile(r"\b(?:\d{1,3}\.){3}\d{1,3}\b"),
|
|
66
|
+
confidence=0.90,
|
|
67
|
+
),
|
|
68
|
+
Rule(
|
|
69
|
+
rule_id="mrn.labeled",
|
|
70
|
+
category="MRN",
|
|
71
|
+
pattern=re.compile(
|
|
72
|
+
r"\b(?:MRN|Medical[ \t]+Record(?:[ \t]+Number)?|Record[ \t]+No\.?)"
|
|
73
|
+
r"[:# \t-]*(?P<phi>(?=[A-Z0-9-]*\d)[A-Z0-9][A-Z0-9-]{3,})\b",
|
|
74
|
+
re.IGNORECASE,
|
|
75
|
+
),
|
|
76
|
+
confidence=0.96,
|
|
77
|
+
group="phi",
|
|
78
|
+
),
|
|
79
|
+
Rule(
|
|
80
|
+
rule_id="account.labeled",
|
|
81
|
+
category="ACCOUNT_ID",
|
|
82
|
+
pattern=re.compile(
|
|
83
|
+
r"\b(?:Account|Acct|Patient[ \t]+ID|Member[ \t]+ID|Policy)"
|
|
84
|
+
r"[:# \t-]*(?P<phi>(?=[A-Z0-9-]*\d)[A-Z0-9][A-Z0-9-]{3,})\b",
|
|
85
|
+
re.IGNORECASE,
|
|
86
|
+
),
|
|
87
|
+
confidence=0.90,
|
|
88
|
+
group="phi",
|
|
89
|
+
),
|
|
90
|
+
Rule(
|
|
91
|
+
rule_id="date.iso_or_us",
|
|
92
|
+
category="DATE",
|
|
93
|
+
pattern=re.compile(r"\b(?:\d{4}[/-]\d{1,2}[/-]\d{1,2}|\d{1,2}[/-]\d{1,2}[/-]\d{2,4})\b"),
|
|
94
|
+
confidence=0.90,
|
|
95
|
+
),
|
|
96
|
+
Rule(
|
|
97
|
+
rule_id="date.month_name",
|
|
98
|
+
category="DATE",
|
|
99
|
+
pattern=re.compile(rf"\b(?:{MONTH_PATTERN})\s+\d{{1,2}},\s+\d{{4}}\b", re.IGNORECASE),
|
|
100
|
+
confidence=0.90,
|
|
101
|
+
),
|
|
102
|
+
Rule(
|
|
103
|
+
rule_id="address.street",
|
|
104
|
+
category="ADDRESS",
|
|
105
|
+
pattern=re.compile(
|
|
106
|
+
r"\b\d{1,6}\s+[A-Z][A-Za-z0-9.'-]*"
|
|
107
|
+
r"(?:\s+[A-Z][A-Za-z0-9.'-]*){0,4}\s+"
|
|
108
|
+
r"(?:St|Street|Ave|Avenue|Rd|Road|Blvd|Boulevard|Dr|Drive|Lane|Ln|Way|Court|Ct)\b",
|
|
109
|
+
re.IGNORECASE,
|
|
110
|
+
),
|
|
111
|
+
confidence=0.82,
|
|
112
|
+
),
|
|
113
|
+
Rule(
|
|
114
|
+
rule_id="facility.named",
|
|
115
|
+
category="MEDICAL_FACILITY",
|
|
116
|
+
pattern=re.compile(
|
|
117
|
+
r"\b[A-Z][A-Za-z&.'-]*(?:[ \t]+[A-Z][A-Za-z&.'-]*){0,4}[ \t]+"
|
|
118
|
+
r"(?:Hospital|Clinic|Medical[ \t]+Center|Health[ \t]+System|Urgent[ \t]+Care)\b"
|
|
119
|
+
),
|
|
120
|
+
confidence=0.82,
|
|
121
|
+
),
|
|
122
|
+
Rule(
|
|
123
|
+
rule_id="name.clinical_label",
|
|
124
|
+
category="NAME",
|
|
125
|
+
pattern=re.compile(
|
|
126
|
+
r"\b(?:Patient[ \t]+Name|Name|Patient|Provider|Physician)[:# \t-]+"
|
|
127
|
+
r"(?P<phi>[A-Z][a-z]+(?:[ \t]+[A-Z][a-z]+){1,2})\b"
|
|
128
|
+
),
|
|
129
|
+
confidence=0.84,
|
|
130
|
+
group="phi",
|
|
131
|
+
),
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
PLACEHOLDERS: dict[PHICategory, str] = {
|
|
135
|
+
"NAME": "[NAME]",
|
|
136
|
+
"DATE": "[DATE]",
|
|
137
|
+
"PHONE": "[PHONE]",
|
|
138
|
+
"EMAIL": "[EMAIL]",
|
|
139
|
+
"ADDRESS": "[ADDRESS]",
|
|
140
|
+
"MRN": "[MRN]",
|
|
141
|
+
"SSN": "[SSN]",
|
|
142
|
+
"URL": "[URL]",
|
|
143
|
+
"IP_ADDRESS": "[IP_ADDRESS]",
|
|
144
|
+
"MEDICAL_FACILITY": "[MEDICAL_FACILITY]",
|
|
145
|
+
"ACCOUNT_ID": "[ACCOUNT_ID]",
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def scan_text(text: str) -> ScanResult:
|
|
150
|
+
"""Scan plain text for PHI-like identifiers."""
|
|
151
|
+
|
|
152
|
+
findings = _deduplicate_findings(_collect_findings(text))
|
|
153
|
+
return ScanResult(
|
|
154
|
+
text_length=len(text),
|
|
155
|
+
findings=findings,
|
|
156
|
+
summary=_summarize(findings),
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def redact_text(text: str, mode: RedactionMode = "placeholder") -> RedactionResult:
|
|
161
|
+
"""Redact detected PHI-like identifiers with stable placeholders."""
|
|
162
|
+
|
|
163
|
+
if mode != "placeholder":
|
|
164
|
+
raise ValueError("Only placeholder redaction is supported in the current release.")
|
|
165
|
+
|
|
166
|
+
scan = scan_text(text)
|
|
167
|
+
redacted = text
|
|
168
|
+
for finding in sorted(scan.findings, key=lambda item: item.start, reverse=True):
|
|
169
|
+
redacted = redacted[: finding.start] + PLACEHOLDERS[finding.category] + redacted[finding.end :]
|
|
170
|
+
|
|
171
|
+
return RedactionResult(
|
|
172
|
+
mode=mode,
|
|
173
|
+
redacted_text=redacted,
|
|
174
|
+
findings=scan.findings,
|
|
175
|
+
summary=scan.summary,
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def audit_text(text: str) -> AuditReport:
|
|
180
|
+
"""Return an audit-oriented summary of PHI-like identifiers."""
|
|
181
|
+
|
|
182
|
+
scan = scan_text(text)
|
|
183
|
+
return AuditReport(
|
|
184
|
+
text_length=scan.text_length,
|
|
185
|
+
total_findings=len(scan.findings),
|
|
186
|
+
categories=scan.summary,
|
|
187
|
+
findings=scan.findings,
|
|
188
|
+
safe_harbor_notes=[
|
|
189
|
+
"This report can support common identifier review, but it is not a full "
|
|
190
|
+
"Safe Harbor determination.",
|
|
191
|
+
"Manual review is required before using output in a regulated environment.",
|
|
192
|
+
],
|
|
193
|
+
limitations=[
|
|
194
|
+
"Rule-based matching can miss identifiers and can produce false positives.",
|
|
195
|
+
"This package does not provide diagnosis, treatment, triage, CDS, or HIPAA compliance.",
|
|
196
|
+
"Use synthetic or properly authorized text only.",
|
|
197
|
+
],
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def validate_no_phi(text: str) -> ValidationResult:
|
|
202
|
+
"""Validate whether text contains detected PHI-like identifiers."""
|
|
203
|
+
|
|
204
|
+
scan = scan_text(text)
|
|
205
|
+
has_phi = bool(scan.findings)
|
|
206
|
+
return ValidationResult(
|
|
207
|
+
ok=not has_phi,
|
|
208
|
+
has_phi=has_phi,
|
|
209
|
+
message="No PHI-like identifiers detected." if not has_phi else "PHI-like identifiers detected.",
|
|
210
|
+
findings=scan.findings,
|
|
211
|
+
summary=scan.summary,
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _collect_findings(text: str) -> list[Finding]:
|
|
216
|
+
findings: list[Finding] = []
|
|
217
|
+
for rule in RULES:
|
|
218
|
+
for match in rule.pattern.finditer(text):
|
|
219
|
+
if rule.category == "IP_ADDRESS" and not _is_valid_ipv4(match.group(0)):
|
|
220
|
+
continue
|
|
221
|
+
|
|
222
|
+
start, end = match.span(rule.group) if rule.group else match.span()
|
|
223
|
+
value = text[start:end]
|
|
224
|
+
if not value.strip():
|
|
225
|
+
continue
|
|
226
|
+
|
|
227
|
+
findings.append(
|
|
228
|
+
Finding(
|
|
229
|
+
category=rule.category,
|
|
230
|
+
text=value,
|
|
231
|
+
start=start,
|
|
232
|
+
end=end,
|
|
233
|
+
confidence=rule.confidence,
|
|
234
|
+
rule_id=rule.rule_id,
|
|
235
|
+
safe_harbor_identifier=SAFE_HARBOR_IDENTIFIERS[rule.category],
|
|
236
|
+
)
|
|
237
|
+
)
|
|
238
|
+
return findings
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _deduplicate_findings(findings: list[Finding]) -> list[Finding]:
|
|
242
|
+
ordered = sorted(
|
|
243
|
+
findings,
|
|
244
|
+
key=lambda item: (
|
|
245
|
+
item.start,
|
|
246
|
+
-(item.end - item.start),
|
|
247
|
+
-item.confidence,
|
|
248
|
+
item.category,
|
|
249
|
+
item.rule_id,
|
|
250
|
+
),
|
|
251
|
+
)
|
|
252
|
+
selected: list[Finding] = []
|
|
253
|
+
occupied: list[tuple[int, int]] = []
|
|
254
|
+
|
|
255
|
+
for finding in ordered:
|
|
256
|
+
if any(_overlaps((finding.start, finding.end), existing) for existing in occupied):
|
|
257
|
+
continue
|
|
258
|
+
selected.append(finding)
|
|
259
|
+
occupied.append((finding.start, finding.end))
|
|
260
|
+
|
|
261
|
+
return sorted(selected, key=lambda item: (item.start, item.end, item.category, item.rule_id))
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _overlaps(left: tuple[int, int], right: tuple[int, int]) -> bool:
|
|
265
|
+
return left[0] < right[1] and right[0] < left[1]
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _summarize(findings: list[Finding]) -> dict[str, int]:
|
|
269
|
+
counts = Counter(finding.category for finding in findings)
|
|
270
|
+
return dict(sorted(counts.items()))
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def _is_valid_ipv4(value: str) -> bool:
|
|
274
|
+
parts = value.split(".")
|
|
275
|
+
return len(parts) == 4 and all(part.isdigit() and 0 <= int(part) <= 255 for part in parts)
|
phi_guard_mcp/gate.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"""Repository privacy gate for PHI-like identifiers."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import fnmatch
|
|
6
|
+
import tomllib
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from .engine import scan_text
|
|
10
|
+
from .models import Finding, GateFileReport, GateReport
|
|
11
|
+
|
|
12
|
+
DEFAULT_INCLUDE = [
|
|
13
|
+
"**/*.txt",
|
|
14
|
+
"**/*.md",
|
|
15
|
+
"**/*.py",
|
|
16
|
+
"**/*.json",
|
|
17
|
+
"pyproject.toml",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
DEFAULT_EXCLUDE = [
|
|
21
|
+
".git/**",
|
|
22
|
+
".venv/**",
|
|
23
|
+
"dist/**",
|
|
24
|
+
"build/**",
|
|
25
|
+
"examples/**",
|
|
26
|
+
"tests/**",
|
|
27
|
+
"benchmarks/**",
|
|
28
|
+
"__pycache__/**",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def run_gate(root: str | Path = ".", config_path: str | Path | None = None) -> GateReport:
|
|
33
|
+
"""Scan configured repository paths and fail when PHI-like identifiers are found."""
|
|
34
|
+
|
|
35
|
+
root_path = Path(root).resolve()
|
|
36
|
+
include, exclude, allow_text = _load_config(root_path, config_path)
|
|
37
|
+
files = _discover_files(root_path, include, exclude)
|
|
38
|
+
|
|
39
|
+
flagged: list[GateFileReport] = []
|
|
40
|
+
for path in files:
|
|
41
|
+
text = path.read_text(encoding="utf-8", errors="replace")
|
|
42
|
+
scan = scan_text(text)
|
|
43
|
+
findings = [finding for finding in scan.findings if finding.text not in allow_text]
|
|
44
|
+
if findings:
|
|
45
|
+
flagged.append(
|
|
46
|
+
GateFileReport(
|
|
47
|
+
path=path.relative_to(root_path).as_posix(),
|
|
48
|
+
findings=findings,
|
|
49
|
+
summary=_summarize_findings(findings),
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
return GateReport(
|
|
54
|
+
ok=not flagged,
|
|
55
|
+
root=str(root_path),
|
|
56
|
+
scanned_files=len(files),
|
|
57
|
+
flagged_files=len(flagged),
|
|
58
|
+
findings=flagged,
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _load_config(root: Path, config_path: str | Path | None) -> tuple[list[str], list[str], set[str]]:
|
|
63
|
+
resolved_config = _resolve_config_path(root, config_path)
|
|
64
|
+
if resolved_config is None:
|
|
65
|
+
return DEFAULT_INCLUDE, DEFAULT_EXCLUDE, set()
|
|
66
|
+
|
|
67
|
+
data = tomllib.loads(resolved_config.read_text(encoding="utf-8"))
|
|
68
|
+
gate_config = data.get("gate", {})
|
|
69
|
+
return (
|
|
70
|
+
list(gate_config.get("include", DEFAULT_INCLUDE)),
|
|
71
|
+
list(gate_config.get("exclude", DEFAULT_EXCLUDE)),
|
|
72
|
+
set(gate_config.get("allow_text", [])),
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _resolve_config_path(root: Path, config_path: str | Path | None) -> Path | None:
|
|
77
|
+
if config_path is not None:
|
|
78
|
+
candidate = Path(config_path)
|
|
79
|
+
if not candidate.is_absolute():
|
|
80
|
+
candidate = root / candidate
|
|
81
|
+
if not candidate.exists():
|
|
82
|
+
raise FileNotFoundError(candidate)
|
|
83
|
+
return candidate
|
|
84
|
+
|
|
85
|
+
default_config = root / ".phi-guard.toml"
|
|
86
|
+
return default_config if default_config.exists() else None
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _discover_files(root: Path, include: list[str], exclude: list[str]) -> list[Path]:
|
|
90
|
+
if root.is_file():
|
|
91
|
+
return [root]
|
|
92
|
+
|
|
93
|
+
paths: set[Path] = set()
|
|
94
|
+
for pattern in include:
|
|
95
|
+
for candidate in root.glob(pattern):
|
|
96
|
+
if candidate.is_file() and not _is_excluded(candidate.relative_to(root), exclude):
|
|
97
|
+
paths.add(candidate)
|
|
98
|
+
return sorted(paths)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _is_excluded(relative_path: Path, exclude: list[str]) -> bool:
|
|
102
|
+
path = relative_path.as_posix()
|
|
103
|
+
return any(fnmatch.fnmatch(path, pattern) for pattern in exclude)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _summarize_findings(findings: list[Finding]) -> dict[str, int]:
|
|
107
|
+
summary: dict[str, int] = {}
|
|
108
|
+
for finding in findings:
|
|
109
|
+
summary[finding.category] = summary.get(finding.category, 0) + 1
|
|
110
|
+
return dict(sorted(summary.items()))
|
phi_guard_mcp/models.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"""Stable result models shared by the Python API, CLI, and MCP server."""
|
|
2
|
+
|
|
3
|
+
from typing import Literal
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
|
|
7
|
+
PHICategory = Literal[
|
|
8
|
+
"NAME",
|
|
9
|
+
"DATE",
|
|
10
|
+
"PHONE",
|
|
11
|
+
"EMAIL",
|
|
12
|
+
"ADDRESS",
|
|
13
|
+
"MRN",
|
|
14
|
+
"SSN",
|
|
15
|
+
"URL",
|
|
16
|
+
"IP_ADDRESS",
|
|
17
|
+
"MEDICAL_FACILITY",
|
|
18
|
+
"ACCOUNT_ID",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
RedactionMode = Literal["placeholder"]
|
|
22
|
+
|
|
23
|
+
SAFE_HARBOR_IDENTIFIERS: dict[PHICategory, str] = {
|
|
24
|
+
"NAME": "Names",
|
|
25
|
+
"DATE": "All elements of dates except year",
|
|
26
|
+
"PHONE": "Telephone numbers",
|
|
27
|
+
"EMAIL": "Email addresses",
|
|
28
|
+
"ADDRESS": "Geographic subdivisions smaller than a state",
|
|
29
|
+
"MRN": "Medical record numbers",
|
|
30
|
+
"SSN": "Social Security numbers",
|
|
31
|
+
"URL": "Web Universal Resource Locators",
|
|
32
|
+
"IP_ADDRESS": "Internet Protocol addresses",
|
|
33
|
+
"ACCOUNT_ID": "Account numbers",
|
|
34
|
+
"MEDICAL_FACILITY": "Other unique identifying characteristic or code",
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class Finding(BaseModel):
|
|
39
|
+
category: PHICategory
|
|
40
|
+
text: str
|
|
41
|
+
start: int = Field(ge=0)
|
|
42
|
+
end: int = Field(ge=0)
|
|
43
|
+
confidence: float = Field(ge=0.0, le=1.0)
|
|
44
|
+
rule_id: str
|
|
45
|
+
safe_harbor_identifier: str
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ScanResult(BaseModel):
|
|
49
|
+
ok: bool = True
|
|
50
|
+
text_length: int
|
|
51
|
+
findings: list[Finding]
|
|
52
|
+
summary: dict[str, int]
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class RedactionResult(BaseModel):
|
|
56
|
+
ok: bool = True
|
|
57
|
+
mode: RedactionMode
|
|
58
|
+
redacted_text: str
|
|
59
|
+
findings: list[Finding]
|
|
60
|
+
summary: dict[str, int]
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class AuditReport(BaseModel):
|
|
64
|
+
ok: bool = True
|
|
65
|
+
text_length: int
|
|
66
|
+
total_findings: int
|
|
67
|
+
categories: dict[str, int]
|
|
68
|
+
findings: list[Finding]
|
|
69
|
+
safe_harbor_notes: list[str]
|
|
70
|
+
limitations: list[str]
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class ValidationResult(BaseModel):
|
|
74
|
+
ok: bool
|
|
75
|
+
has_phi: bool
|
|
76
|
+
message: str
|
|
77
|
+
findings: list[Finding]
|
|
78
|
+
summary: dict[str, int]
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ExpectedFinding(BaseModel):
|
|
82
|
+
category: PHICategory
|
|
83
|
+
text: str
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class BenchmarkCase(BaseModel):
|
|
87
|
+
id: str
|
|
88
|
+
text: str
|
|
89
|
+
expected_findings: list[ExpectedFinding]
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class BenchmarkCaseResult(BaseModel):
|
|
93
|
+
id: str
|
|
94
|
+
true_positive: int = Field(ge=0)
|
|
95
|
+
false_positive: int = Field(ge=0)
|
|
96
|
+
false_negative: int = Field(ge=0)
|
|
97
|
+
precision: float = Field(ge=0.0, le=1.0)
|
|
98
|
+
recall: float = Field(ge=0.0, le=1.0)
|
|
99
|
+
f1: float = Field(ge=0.0, le=1.0)
|
|
100
|
+
missing: list[ExpectedFinding]
|
|
101
|
+
unexpected: list[ExpectedFinding]
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class BenchmarkCategoryMetrics(BaseModel):
|
|
105
|
+
expected: int = Field(ge=0)
|
|
106
|
+
detected: int = Field(ge=0)
|
|
107
|
+
true_positive: int = Field(ge=0)
|
|
108
|
+
false_positive: int = Field(ge=0)
|
|
109
|
+
false_negative: int = Field(ge=0)
|
|
110
|
+
precision: float = Field(ge=0.0, le=1.0)
|
|
111
|
+
recall: float = Field(ge=0.0, le=1.0)
|
|
112
|
+
f1: float = Field(ge=0.0, le=1.0)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
class BenchmarkReport(BaseModel):
|
|
116
|
+
ok: bool = True
|
|
117
|
+
cases_dir: str
|
|
118
|
+
total_cases: int = Field(ge=0)
|
|
119
|
+
true_positive: int = Field(ge=0)
|
|
120
|
+
false_positive: int = Field(ge=0)
|
|
121
|
+
false_negative: int = Field(ge=0)
|
|
122
|
+
precision: float = Field(ge=0.0, le=1.0)
|
|
123
|
+
recall: float = Field(ge=0.0, le=1.0)
|
|
124
|
+
f1: float = Field(ge=0.0, le=1.0)
|
|
125
|
+
per_category: dict[str, BenchmarkCategoryMetrics]
|
|
126
|
+
cases: list[BenchmarkCaseResult]
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class GateFileReport(BaseModel):
|
|
130
|
+
path: str
|
|
131
|
+
findings: list[Finding]
|
|
132
|
+
summary: dict[str, int]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
class GateReport(BaseModel):
|
|
136
|
+
ok: bool
|
|
137
|
+
root: str
|
|
138
|
+
scanned_files: int = Field(ge=0)
|
|
139
|
+
flagged_files: int = Field(ge=0)
|
|
140
|
+
findings: list[GateFileReport]
|
phi_guard_mcp/server.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""MCP stdio server for PHI guard tools."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
from importlib import metadata
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
import anyio
|
|
11
|
+
from mcp.server import Server
|
|
12
|
+
from mcp.server.stdio import stdio_server
|
|
13
|
+
from mcp.types import TextContent, Tool
|
|
14
|
+
from pydantic import BaseModel, Field
|
|
15
|
+
|
|
16
|
+
from . import __version__
|
|
17
|
+
from .engine import audit_text, redact_text, scan_text, validate_no_phi
|
|
18
|
+
from .models import RedactionMode
|
|
19
|
+
|
|
20
|
+
SERVER_NAME = "phi-guard-mcp"
|
|
21
|
+
|
|
22
|
+
_HELP = """phi-guard-mcp
|
|
23
|
+
|
|
24
|
+
MCP stdio server for detecting, redacting, and auditing PHI-like identifiers
|
|
25
|
+
before medical text is sent to AI agents.
|
|
26
|
+
|
|
27
|
+
Usage:
|
|
28
|
+
phi-guard-mcp Start the MCP stdio server
|
|
29
|
+
phi-guard-mcp --help Show this help
|
|
30
|
+
phi-guard-mcp --version Show package version
|
|
31
|
+
|
|
32
|
+
CLI:
|
|
33
|
+
phi-guard scan <file>
|
|
34
|
+
phi-guard redact <file> --out <file>
|
|
35
|
+
phi-guard audit <file>
|
|
36
|
+
phi-guard validate <file>
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class TextArgs(BaseModel):
|
|
41
|
+
text: str = Field(min_length=0, description="Plain text to inspect.")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class RedactArgs(TextArgs):
|
|
45
|
+
mode: RedactionMode = Field(default="placeholder", description="Redaction mode.")
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
async def _scan_phi(args: TextArgs) -> dict[str, Any]:
|
|
49
|
+
return scan_text(args.text).model_dump()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
async def _redact_phi(args: RedactArgs) -> dict[str, Any]:
|
|
53
|
+
return redact_text(args.text, mode=args.mode).model_dump()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
async def _audit_deidentification(args: TextArgs) -> dict[str, Any]:
|
|
57
|
+
return audit_text(args.text).model_dump()
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
async def _validate_no_phi(args: TextArgs) -> dict[str, Any]:
|
|
61
|
+
return validate_no_phi(args.text).model_dump()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
TOOL_REGISTRY: dict[str, tuple[type[BaseModel], Any, str]] = {
|
|
65
|
+
"scan_phi": (
|
|
66
|
+
TextArgs,
|
|
67
|
+
_scan_phi,
|
|
68
|
+
"Detect PHI-like identifiers and return structured findings.",
|
|
69
|
+
),
|
|
70
|
+
"redact_phi": (
|
|
71
|
+
RedactArgs,
|
|
72
|
+
_redact_phi,
|
|
73
|
+
"Detect and redact PHI-like identifiers with placeholders.",
|
|
74
|
+
),
|
|
75
|
+
"audit_deidentification": (
|
|
76
|
+
TextArgs,
|
|
77
|
+
_audit_deidentification,
|
|
78
|
+
"Return an audit-oriented summary of PHI-like identifiers and limitations.",
|
|
79
|
+
),
|
|
80
|
+
"validate_no_phi": (
|
|
81
|
+
TextArgs,
|
|
82
|
+
_validate_no_phi,
|
|
83
|
+
"Validate whether text has no detected PHI-like identifiers.",
|
|
84
|
+
),
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
server = Server(SERVER_NAME)
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@server.list_tools()
|
|
91
|
+
async def _list_tools() -> list[Tool]:
|
|
92
|
+
return [
|
|
93
|
+
Tool(
|
|
94
|
+
name=name,
|
|
95
|
+
description=description,
|
|
96
|
+
inputSchema=schema_cls.model_json_schema(),
|
|
97
|
+
)
|
|
98
|
+
for name, (schema_cls, _handler, description) in TOOL_REGISTRY.items()
|
|
99
|
+
]
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@server.call_tool()
|
|
103
|
+
async def _call_tool(name: str, arguments: dict[str, Any] | None) -> list[TextContent]:
|
|
104
|
+
if name not in TOOL_REGISTRY:
|
|
105
|
+
raise ValueError(f"Unknown tool: {name}")
|
|
106
|
+
|
|
107
|
+
schema_cls, handler, _description = TOOL_REGISTRY[name]
|
|
108
|
+
parsed_args = schema_cls.model_validate(arguments or {})
|
|
109
|
+
result = await handler(parsed_args)
|
|
110
|
+
return [TextContent(type="text", text=json.dumps(result, indent=2, ensure_ascii=False))]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
async def _run_stdio_server() -> None:
|
|
114
|
+
async with stdio_server() as (read_stream, write_stream):
|
|
115
|
+
await server.run(
|
|
116
|
+
read_stream,
|
|
117
|
+
write_stream,
|
|
118
|
+
server.create_initialization_options(),
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _package_version() -> str:
|
|
123
|
+
try:
|
|
124
|
+
return metadata.version("phi-guard-mcp")
|
|
125
|
+
except metadata.PackageNotFoundError:
|
|
126
|
+
return __version__
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def main(argv: list[str] | None = None) -> None:
|
|
130
|
+
args = sys.argv[1:] if argv is None else argv
|
|
131
|
+
if args in (["--help"], ["-h"]):
|
|
132
|
+
print(_HELP)
|
|
133
|
+
return
|
|
134
|
+
if args == ["--version"]:
|
|
135
|
+
print(f"phi-guard-mcp {_package_version()}")
|
|
136
|
+
return
|
|
137
|
+
if args:
|
|
138
|
+
print(_HELP, file=sys.stderr)
|
|
139
|
+
raise SystemExit(2)
|
|
140
|
+
anyio.run(_run_stdio_server)
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
if __name__ == "__main__":
|
|
144
|
+
main()
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: phi-guard-mcp
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: MCP server and CLI for detecting, redacting, and auditing PHI before medical text is sent to AI agents.
|
|
5
|
+
Project-URL: Homepage, https://github.com/charlesree826/phi-guard-mcp
|
|
6
|
+
Project-URL: Repository, https://github.com/charlesree826/phi-guard-mcp
|
|
7
|
+
Project-URL: Issues, https://github.com/charlesree826/phi-guard-mcp/issues
|
|
8
|
+
Author: Charles Ree
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: de-identification,hipaa,mcp,medical-ai,phi,privacy
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Healthcare Industry
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Topic :: Security
|
|
19
|
+
Classifier: Topic :: Text Processing
|
|
20
|
+
Requires-Python: >=3.12
|
|
21
|
+
Requires-Dist: anyio>=4.3.0
|
|
22
|
+
Requires-Dist: mcp>=1.2.0
|
|
23
|
+
Requires-Dist: pydantic>=2.6.0
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: build>=1.2.2; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest>=8.3.0; extra == 'dev'
|
|
27
|
+
Requires-Dist: ruff>=0.8.0; extra == 'dev'
|
|
28
|
+
Requires-Dist: twine>=5.1.0; extra == 'dev'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# phi-guard-mcp
|
|
32
|
+
|
|
33
|
+
[](https://github.com/charlesree826/phi-guard-mcp/actions/workflows/ci.yml)
|
|
34
|
+
[](https://github.com/charlesree826/phi-guard-mcp/releases)
|
|
35
|
+
[](pyproject.toml)
|
|
36
|
+
[](LICENSE)
|
|
37
|
+
|
|
38
|
+
MCP server and CLI for detecting, redacting, and auditing PHI before medical text is sent to AI
|
|
39
|
+
agents.
|
|
40
|
+
|
|
41
|
+
`phi-guard-mcp` is healthcare AI safety infrastructure, not a clinical product. It is a local,
|
|
42
|
+
rule-based guardrail that helps developers identify PHI-like identifiers in plain text, redact them
|
|
43
|
+
with stable placeholders, and produce audit-friendly JSON before content reaches an AI agent or MCP
|
|
44
|
+
workflow.
|
|
45
|
+
|
|
46
|
+
Proof points for maintainers:
|
|
47
|
+
|
|
48
|
+
- Synthetic benchmark with exact-match PHI finding evaluation.
|
|
49
|
+
- Safe Harbor mapping audit fields for review workflows.
|
|
50
|
+
- CI privacy gate that blocks PHI-like identifiers in maintained source and docs.
|
|
51
|
+
- CLI, Python API, and MCP stdio tools sharing one stable JSON result model.
|
|
52
|
+
|
|
53
|
+
Important scope limits:
|
|
54
|
+
|
|
55
|
+
- Not for diagnosis, treatment, triage, medical advice, or medication recommendations.
|
|
56
|
+
- Not a HIPAA compliance guarantee and not a substitute for legal, privacy, or security review.
|
|
57
|
+
- Not an FDA-regulated clinical decision support or device software function.
|
|
58
|
+
- Do not test with real patient records. The examples in this repo are synthetic.
|
|
59
|
+
|
|
60
|
+
The project aligns its documentation vocabulary with HHS HIPAA de-identification concepts such as
|
|
61
|
+
Safe Harbor and Expert Determination, while intentionally avoiding clinical decision support scope.
|
|
62
|
+
See [HHS de-identification guidance](https://www.hhs.gov/hipaa/for-professionals/special-topics/de-identification/index.html),
|
|
63
|
+
[FDA CDS guidance](https://www.fda.gov/regulatory-information/search-fda-guidance-documents/clinical-decision-support-software),
|
|
64
|
+
and [FDA device software functions](https://www.fda.gov/medical-devices/digital-health-center-excellence/device-software-functions-including-mobile-medical-applications).
|
|
65
|
+
|
|
66
|
+
## Install
|
|
67
|
+
|
|
68
|
+
Install from the current GitHub release wheel:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
python -m pip install https://github.com/charlesree826/phi-guard-mcp/releases/download/v0.1.2/phi_guard_mcp-0.1.2-py3-none-any.whl
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
For local development:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
python -m pip install -e ".[dev]"
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
PyPI publishing is configured through GitHub Actions trusted publishing and will be enabled after
|
|
81
|
+
the PyPI package owner creates the matching pending publisher entry for this repository.
|
|
82
|
+
|
|
83
|
+
## Quickstart
|
|
84
|
+
|
|
85
|
+
Scan a synthetic note:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
phi-guard scan examples/synthetic_clinical_note.txt
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Redact PHI-like identifiers:
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
phi-guard redact examples/synthetic_clinical_note.txt --out /tmp/synthetic_redacted.txt
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Audit a note:
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
phi-guard audit examples/synthetic_clinical_note.txt
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Validate text before it enters an AI agent:
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
phi-guard validate examples/synthetic_clean_note.txt
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
Run the synthetic benchmark:
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
phi-guard benchmark benchmarks/synthetic/cases --out benchmarks/synthetic-report.json
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Run the repository privacy gate:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
phi-guard gate --config .phi-guard.toml
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
All CLI commands output stable JSON for automation.
|
|
122
|
+
|
|
123
|
+
See [docs/demo.md](docs/demo.md) for a complete CLI and MCP transcript.
|
|
124
|
+
|
|
125
|
+
## MCP Server
|
|
126
|
+
|
|
127
|
+
Run the stdio MCP server:
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
phi-guard-mcp
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Available tools:
|
|
134
|
+
|
|
135
|
+
- `scan_phi(text)`
|
|
136
|
+
- `redact_phi(text, mode="placeholder")`
|
|
137
|
+
- `audit_deidentification(text)`
|
|
138
|
+
- `validate_no_phi(text)`
|
|
139
|
+
|
|
140
|
+
MCP tools return the same finding schema as the CLI, including `safe_harbor_identifier`.
|
|
141
|
+
|
|
142
|
+
Example MCP client config:
|
|
143
|
+
|
|
144
|
+
```json
|
|
145
|
+
{
|
|
146
|
+
"mcpServers": {
|
|
147
|
+
"phi-guard": {
|
|
148
|
+
"command": "phi-guard-mcp"
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Python API
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from phi_guard_mcp import audit_text, evaluate_benchmark, redact_text, scan_text, validate_no_phi
|
|
158
|
+
|
|
159
|
+
result = scan_text("Patient Name: Jordan Rivera, MRN: MRN-48291")
|
|
160
|
+
redacted = redact_text("Patient Name: Jordan Rivera, MRN: MRN-48291")
|
|
161
|
+
audit = audit_text("Patient Name: Jordan Rivera, MRN: MRN-48291")
|
|
162
|
+
validation = validate_no_phi("No identifiers are present in this synthetic note.")
|
|
163
|
+
benchmark = evaluate_benchmark("benchmarks/synthetic/cases")
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
## What It Detects
|
|
167
|
+
|
|
168
|
+
The first release focuses on plain text and common PHI-like identifiers:
|
|
169
|
+
|
|
170
|
+
- Names in clinical label contexts
|
|
171
|
+
- Dates
|
|
172
|
+
- Phone numbers
|
|
173
|
+
- Email addresses
|
|
174
|
+
- Address-like fragments
|
|
175
|
+
- Medical record numbers
|
|
176
|
+
- Social Security numbers
|
|
177
|
+
- URLs and IP addresses
|
|
178
|
+
- Medical facility names
|
|
179
|
+
- Account, member, policy, and patient ID tokens
|
|
180
|
+
|
|
181
|
+
This is a deterministic heuristic engine. It favors transparent behavior and repeatable JSON over
|
|
182
|
+
opaque model judgment.
|
|
183
|
+
|
|
184
|
+
Safe Harbor mapping is included as a review aid only. It does not make output HIPAA compliant and
|
|
185
|
+
does not replace Expert Determination or legal review.
|
|
186
|
+
|
|
187
|
+
## Project Docs
|
|
188
|
+
|
|
189
|
+
- [Demo](docs/demo.md)
|
|
190
|
+
- [Synthetic benchmark](docs/benchmark.md)
|
|
191
|
+
- [Privacy gate](docs/privacy-gate.md)
|
|
192
|
+
- [Safety scope](docs/safety-scope.md)
|
|
193
|
+
- [Roadmap](docs/roadmap.md)
|
|
194
|
+
- [Contributing](CONTRIBUTING.md)
|
|
195
|
+
- [Security policy](SECURITY.md)
|
|
196
|
+
|
|
197
|
+
## Development
|
|
198
|
+
|
|
199
|
+
```bash
|
|
200
|
+
python -m compileall -q src tests
|
|
201
|
+
python -m pytest -q
|
|
202
|
+
ruff check .
|
|
203
|
+
phi-guard gate --config .phi-guard.toml
|
|
204
|
+
python -m build
|
|
205
|
+
twine check dist/*
|
|
206
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
phi_guard_mcp/__init__.py,sha256=g-FNIBx8ut_0T1Xbc8Uxrn3LVHev9_gawNAR21XwCF8,663
|
|
2
|
+
phi_guard_mcp/benchmark.py,sha256=VtY-l-1Y-isJP6Q6aRI4AOa_wG5ODIJj8FOqdONJS3M,4742
|
|
3
|
+
phi_guard_mcp/cli.py,sha256=vFNbzTLb603hissVGK9HHBjSisBBv_ctmw65OTFRn_k,4278
|
|
4
|
+
phi_guard_mcp/engine.py,sha256=pO8drfyOLMKeNypznL9x4EsoIPgMRvnHX1QlUWdAM8k,8278
|
|
5
|
+
phi_guard_mcp/gate.py,sha256=0lxrMS-ZAynqjk8x6bYAEHNc4aSZdj0m1JK5ZEUYi0g,3381
|
|
6
|
+
phi_guard_mcp/models.py,sha256=R6bh50iyKd0ShYjdECWmoN9gA8mPYt7edyAiNFLOodU,3366
|
|
7
|
+
phi_guard_mcp/server.py,sha256=DSGf8JxB3MoiFacAWOoXpK8cu-15pp33dNt3ZHV6VUA,3851
|
|
8
|
+
phi_guard_mcp-0.1.2.dist-info/METADATA,sha256=BK9dOmRrMhTPPtFAwAI-cIjtfjXCMgRO7NsgZYcIyYo,6614
|
|
9
|
+
phi_guard_mcp-0.1.2.dist-info/WHEEL,sha256=mffPy8wBnZQn2VnJUU5jE99KsxaSfiyMHV9Yt0aLVxs,87
|
|
10
|
+
phi_guard_mcp-0.1.2.dist-info/entry_points.txt,sha256=iq-dkI1N3QNpSU8Kzkd1y7VkbPSCEsP86fRpMnYGZGg,95
|
|
11
|
+
phi_guard_mcp-0.1.2.dist-info/licenses/LICENSE,sha256=cQfRJm4R05GtH02Mrl7RktEwwbhFqof8LFWfBBBMHOM,1068
|
|
12
|
+
phi_guard_mcp-0.1.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Charles Ree
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|