Spaces:
Running
Running
File size: 9,118 Bytes
78b489c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | """
Example demonstrating the actor-aware HIPAAAccessPolicy.
"""
import asyncio
import json
from fastmcp import FastMCP
from fastmcp.policy.policies import HIPAAAccessPolicy
def create_hipaa_server():
"""Create a FastMCP server with HIPAA policy engine enabled."""
# Create server
server = FastMCP("HIPAA Compliance Server")
# Enable policy engine
policy_engine = server.enable_policy_engine()
policy_engine.register_policy(HIPAAAccessPolicy())
@server.tool
async def access_phi(
user_id: str,
user_roles: list[str],
action: str,
purpose: str,
patient_id: str,
data_elements: list[str],
is_clinical: bool = True,
recipient_id: str | None = None,
recipient_type: str | None = None
) -> dict:
"""Access PHI with HIPAA policy evaluation."""
# Create context for policy evaluation
context = {
"user": {"id": user_id, "roles": user_roles},
"action": action,
"purpose": purpose,
"resource": {
"is_phi": True,
"type": "phi",
"is_clinical": is_clinical,
"data_elements": data_elements
},
"patient": {"id": patient_id}
}
# Add recipient if provided (for disclosure scenarios)
if recipient_id and recipient_type:
context["recipient"] = {"id": recipient_id, "type": recipient_type}
# Evaluate HIPAA policy
decision = await policy_engine.evaluate(context)
return {
"access_granted": decision.allow,
"reason": decision.reason,
"obligations": decision.obligations,
"proof": decision.proof
}
@server.tool
async def access_billing_data(
user_id: str,
user_roles: list[str],
action: str,
purpose: str,
patient_id: str,
billing_elements: list[str]
) -> dict:
"""Access billing data with HIPAA policy evaluation."""
context = {
"user": {"id": user_id, "roles": user_roles},
"action": action,
"purpose": purpose,
"resource": {
"is_phi": True,
"is_clinical": False,
"data_elements": billing_elements
},
"patient": {"id": patient_id}
}
decision = await policy_engine.evaluate(context)
return {
"access_granted": decision.allow,
"reason": decision.reason,
"obligations": decision.obligations,
"proof": decision.proof
}
return server
async def demonstrate_actor_aware_hipaa_policy():
"""Demonstrate HIPAAPolicy with scenarios for each actor type."""
server = create_hipaa_server()
policy_engine = server.get_policy_engine()
assert policy_engine is not None
print("🔐 Actor-Aware HIPAA Policy Evaluation Example")
print("=" * 60)
# Test scenarios using the tools
scenarios = [
# --- Provider Scenarios ---
{
"name": "[Provider] Access clinical data for treatment (Allowed)",
"tool": "access_phi",
"params": {
"user_id": "dr_smith",
"user_roles": ["provider"],
"action": "read",
"purpose": "Treatment",
"patient_id": "patient_123",
"data_elements": ["full_record"],
"is_clinical": True
}
},
{
"name": "[Provider] Disclose PHI to another provider (Allowed with obligations)",
"tool": "access_phi",
"params": {
"user_id": "dr_jones",
"user_roles": ["provider"],
"action": "disclose",
"purpose": "Treatment",
"patient_id": "patient_123",
"data_elements": ["lab_results"],
"is_clinical": True,
"recipient_id": "specialist_clinic",
"recipient_type": "health_care_provider"
}
},
# --- Payee Scenarios ---
{
"name": "[Payee] Access billing information for payment (Allowed)",
"tool": "access_billing_data",
"params": {
"user_id": "bill_staff_01",
"user_roles": ["payee"],
"action": "read",
"purpose": "Payment",
"patient_id": "patient_456",
"billing_elements": ["billing_codes", "dates_of_service"]
}
},
{
"name": "[Payee] Attempt to access clinical notes (Denied by Minimum Necessary)",
"tool": "access_phi",
"params": {
"user_id": "bill_staff_01",
"user_roles": ["payee"],
"action": "read",
"purpose": "Payment",
"patient_id": "patient_456",
"data_elements": ["physician_notes"],
"is_clinical": True
}
},
{
"name": "[Payee] Attempt to modify clinical record (Denied by Integrity Rule)",
"tool": "access_phi",
"params": {
"user_id": "bill_staff_01",
"user_roles": ["payee"],
"action": "write",
"purpose": "Payment",
"patient_id": "patient_456",
"data_elements": ["diagnosis_code"],
"is_clinical": True
}
},
# --- Patient Scenarios ---
{
"name": "[Patient] Request own full medical record (Allowed, bypasses Min. Necessary)",
"tool": "access_phi",
"params": {
"user_id": "patient_789",
"user_roles": ["patient"],
"action": "read",
"purpose": "Self_Access",
"patient_id": "patient_789",
"data_elements": ["full_record"],
"is_clinical": True
}
},
{
"name": "[Patient] Request export of own data (Allowed with encryption obligation)",
"tool": "access_phi",
"params": {
"user_id": "patient_789",
"user_roles": ["patient"],
"action": "export",
"purpose": "Self_Access",
"patient_id": "patient_789",
"data_elements": ["full_record"],
"is_clinical": True
}
}
]
for scenario in scenarios:
print(f"\n📋 Scenario: {scenario['name']}")
print("-" * 40)
# Create context directly for policy evaluation
if scenario["tool"] == "access_phi":
context = {
"user": {
"id": scenario["params"]["user_id"],
"roles": scenario["params"]["user_roles"]
},
"action": scenario["params"]["action"],
"purpose": scenario["params"]["purpose"],
"resource": {
"is_phi": True,
"type": "phi",
"is_clinical": scenario["params"]["is_clinical"],
"data_elements": scenario["params"]["data_elements"]
},
"patient": {"id": scenario["params"]["patient_id"]}
}
# Add recipient if provided
if "recipient_id" in scenario["params"]:
context["recipient"] = {
"id": scenario["params"]["recipient_id"],
"type": scenario["params"]["recipient_type"]
}
elif scenario["tool"] == "access_billing_data":
context = {
"user": {
"id": scenario["params"]["user_id"],
"roles": scenario["params"]["user_roles"]
},
"action": scenario["params"]["action"],
"purpose": scenario["params"]["purpose"],
"resource": {
"is_phi": True,
"is_clinical": False,
"data_elements": scenario["params"]["billing_elements"]
},
"patient": {"id": scenario["params"]["patient_id"]}
}
decision = await policy_engine.evaluate(context)
print(f"Decision: {'✅ ALLOW' if decision.allow else '❌ DENY'}")
print(f"Reason: {decision.reason}")
if decision.obligations:
print("Obligations:")
for ob in decision.obligations:
print(f" - {ob['type']}: {ob['description']}")
if decision.proof:
print(f"Proof: {json.dumps(decision.proof, indent=2)}")
def main():
"""Run the HIPAA policy example."""
asyncio.run(demonstrate_actor_aware_hipaa_policy())
if __name__ == "__main__":
main()
|