-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_enforcement.py
More file actions
490 lines (419 loc) Β· 17.1 KB
/
Copy pathmcp_enforcement.py
File metadata and controls
490 lines (419 loc) Β· 17.1 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
"""
MCP (Model Context Protocol) Enforcement Example
β
CURRENT STATUS:
- β
Passport MCP configuration is supported (passport.mcp.servers, passport.mcp.tools)
- β
Policy verification validates MCP against passport allowlist
- β
MCP context can be passed in policy verification requests
- β
Supports multiple MCP servers/tools per request (arrays)
This example demonstrates:
1. Extracting MCP headers from requests (application-level)
2. Passing MCP context to policy verification
3. Using arrays for multiple MCP servers/tools
ACTUAL SDK LOCATION: /sdk/python
ACTUAL MIDDLEWARE LOCATION: /middleware/fastapi
"""
import os
from typing import Optional, Dict, Any
from fastapi import FastAPI, Request, HTTPException, Header
from fastapi.responses import JSONResponse
from pydantic import BaseModel
# Import APort SDK (actual location: /sdk/python)
# In production: pip install aporthq-sdk-python
from aporthq_sdk_python import APortClient, APortClientOptions, AportError
app = FastAPI(title="MCP Enforcement Example", version="1.0.0")
# Initialize APort client
client = APortClient(
APortClientOptions(
base_url=os.getenv("APORT_API_BASE_URL", "https://api.aport.io"),
api_key=os.getenv("APORT_API_KEY"),
timeout_ms=5000,
)
)
# Request models
class RefundRequest(BaseModel):
amount: int # Amount in cents
currency: str
customer_id: str
order_id: str
reason_code: str = "customer_request"
region: str = "US"
idempotency_key: Optional[str] = None
class ExportRequest(BaseModel):
table_name: str
row_limit: int = 1000
include_pii: bool = False
def extract_mcp_headers(request: Request) -> Dict[str, Any]:
"""
Extract MCP headers from request.
Supports both single values and arrays for multiple MCP servers/tools.
Arrays are preferred for multiple MCP usage.
"""
# Support array format (preferred): X-MCP-Servers, X-MCP-Tools
servers_header = request.headers.get("X-MCP-Servers") or request.headers.get("X-MCP-Server")
tools_header = request.headers.get("X-MCP-Tools") or request.headers.get("X-MCP-Tool")
# Parse arrays if comma-separated, otherwise treat as single value
servers = []
if servers_header:
servers = [s.strip() for s in servers_header.split(",")] if "," in servers_header else [servers_header]
tools = []
if tools_header:
tools = [t.strip() for t in tools_header.split(",")] if "," in tools_header else [tools_header]
return {
"servers": servers,
"tools": tools,
"session": request.headers.get("X-MCP-Session"),
# Backward compatibility: single values
"server": servers[0] if servers else None,
"tool": tools[0] if tools else None,
}
async def validate_mcp_against_passport(
agent_id: str, mcp_headers: Dict[str, Any]
) -> bool:
"""
Validate MCP headers against passport allowlist.
Validates ALL servers/tools in the request against passport allowlist.
The verify endpoint also validates MCP context, but this provides
early validation before making the API call.
"""
try:
# Get passport to check MCP allowlist
passport = await client.get_passport_view(agent_id)
allowed_servers = passport.get("mcp", {}).get("servers", [])
allowed_tools = passport.get("mcp", {}).get("tools", [])
# Validate all servers (array support)
if mcp_headers.get("servers"):
unauthorized_servers = [
server for server in mcp_headers["servers"]
if server not in allowed_servers
]
if unauthorized_servers:
raise HTTPException(
status_code=403,
detail={
"error": "mcp_server_not_allowed",
"message": f"MCP server(s) {', '.join(unauthorized_servers)} not in passport allowlist. "
f"Allowed servers: {', '.join(allowed_servers)}",
},
)
# Validate all tools (array support)
if mcp_headers.get("tools"):
unauthorized_tools = [
tool for tool in mcp_headers["tools"]
if tool not in allowed_tools
]
if unauthorized_tools:
raise HTTPException(
status_code=403,
detail={
"error": "mcp_tool_not_allowed",
"message": f"MCP tool(s) {', '.join(unauthorized_tools)} not in passport allowlist. "
f"Allowed tools: {', '.join(allowed_tools)}",
},
)
return True
except AportError as e:
raise HTTPException(
status_code=e.status if hasattr(e, "status") else 500,
detail={"error": "passport_fetch_failed", "message": str(e)},
)
# Example 1: Basic endpoint that logs MCP headers
@app.post("/api/basic-mcp")
async def basic_mcp_endpoint(
request: Request,
x_agent_passport_id: Optional[str] = Header(None, alias="X-Agent-Passport-Id"),
):
"""Basic endpoint that demonstrates MCP header extraction."""
if not x_agent_passport_id:
raise HTTPException(
status_code=401,
detail={"error": "missing_agent_id", "message": "Agent ID is required"},
)
mcp_headers = extract_mcp_headers(request)
# Validate MCP headers (application-level)
# The verify endpoint also validates MCP, but this provides early validation
if mcp_headers.get("servers") or mcp_headers.get("tools"):
await validate_mcp_against_passport(x_agent_passport_id, mcp_headers)
return {
"success": True,
"agent_id": x_agent_passport_id,
"mcp_context": mcp_headers,
}
# Example 2: Refund with MCP enforcement + policy verification
@app.post("/api/refunds/create")
async def create_refund(
request: Request,
refund_data: RefundRequest,
x_agent_passport_id: Optional[str] = Header(None, alias="X-Agent-Passport-Id"),
):
"""
Create a refund with policy and MCP enforcement.
This endpoint is protected by:
1. Agent passport verification (via policy verification)
2. MCP allowlist checks (application-level, if headers present)
3. finance.payment.refund.v1 policy requirements
"""
if not x_agent_passport_id:
raise HTTPException(
status_code=401,
detail={"error": "missing_agent_id", "message": "Agent ID is required"},
)
# Extract and validate MCP headers (application-level)
mcp_headers = extract_mcp_headers(request)
if mcp_headers.get("servers") or mcp_headers.get("tools"):
await validate_mcp_against_passport(x_agent_passport_id, mcp_headers)
try:
# Policy verification (validates MCP against passport allowlist)
# Use new API endpoint: /api/verify/policy/{pack_id}
context = {
"amount": refund_data.amount,
"currency": refund_data.currency,
"customer_id": refund_data.customer_id,
"order_id": refund_data.order_id,
"reason_code": refund_data.reason_code,
"region": refund_data.region,
"idempotency_key": refund_data.idempotency_key
or f"refund_{request.headers.get('X-Request-ID', 'unknown')}",
}
# Include MCP context (arrays preferred, single values supported)
if mcp_headers.get("servers"):
context["mcp_servers"] = mcp_headers["servers"]
if mcp_headers.get("tools"):
context["mcp_tools"] = mcp_headers["tools"]
# Backward compatibility: single values
if mcp_headers.get("server"):
context["mcp_server"] = mcp_headers["server"]
if mcp_headers.get("tool"):
context["mcp_tool"] = mcp_headers["tool"]
if mcp_headers.get("session"):
context["mcp_session"] = mcp_headers["session"]
decision = await client.verify_policy(
x_agent_passport_id,
"finance.payment.refund.v1",
context,
)
if not decision.allow:
raise HTTPException(
status_code=403,
detail={
"error": "policy_violation",
"message": "Policy verification denied",
"decision_id": decision.decision_id,
"reasons": decision.reasons,
},
)
print(
f"Processing refund: ${refund_data.amount/100:.2f} {refund_data.currency} for {refund_data.customer_id}"
)
print(f"MCP Context: {mcp_headers}")
# Process refund (your business logic here)
import random
import string
refund_id = f"ref_{''.join(random.choices(string.ascii_lowercase + string.digits, k=9))}"
return {
"success": True,
"refund_id": refund_id,
"amount": refund_data.amount,
"currency": refund_data.currency,
"customer_id": refund_data.customer_id,
"order_id": refund_data.order_id,
"decision_id": decision.decision_id,
"processed_via_mcp": bool(mcp_headers.get("servers") or mcp_headers.get("tools")),
"mcp_servers": mcp_headers.get("servers", []),
"mcp_tools": mcp_headers.get("tools", []),
"mcp_session": mcp_headers.get("session"),
}
except AportError as e:
raise HTTPException(
status_code=e.status if hasattr(e, "status") else 500,
detail={
"error": "policy_verification_failed",
"message": str(e),
"decision_id": getattr(e, "decision_id", None),
},
)
# Example 3: Data export with MCP enforcement + policy verification
@app.post("/api/export/csv")
async def export_csv(
request: Request,
export_data: ExportRequest,
x_agent_passport_id: Optional[str] = Header(None, alias="X-Agent-Passport-Id"),
):
"""Export data to CSV with policy and MCP enforcement."""
if not x_agent_passport_id:
raise HTTPException(
status_code=401,
detail={"error": "missing_agent_id", "message": "Agent ID is required"},
)
# Extract and validate MCP headers (application-level)
mcp_headers = extract_mcp_headers(request)
if mcp_headers.get("servers") or mcp_headers.get("tools"):
await validate_mcp_against_passport(x_agent_passport_id, mcp_headers)
try:
# Policy verification (validates MCP against passport allowlist)
context = {
"table_name": export_data.table_name,
"row_limit": export_data.row_limit,
"include_pii": export_data.include_pii,
}
# Include MCP context (arrays preferred)
if mcp_headers.get("servers"):
context["mcp_servers"] = mcp_headers["servers"]
if mcp_headers.get("tools"):
context["mcp_tools"] = mcp_headers["tools"]
if mcp_headers.get("server"):
context["mcp_server"] = mcp_headers["server"]
if mcp_headers.get("tool"):
context["mcp_tool"] = mcp_headers["tool"]
if mcp_headers.get("session"):
context["mcp_session"] = mcp_headers["session"]
decision = await client.verify_policy(
x_agent_passport_id,
"data.export.create.v1",
context,
)
if not decision.allow:
raise HTTPException(
status_code=403,
detail={
"error": "policy_violation",
"message": "Policy verification denied",
"decision_id": decision.decision_id,
"reasons": decision.reasons,
},
)
print(f"Exporting {export_data.table_name} with limit: {export_data.row_limit}")
print(f"MCP Context: {mcp_headers}")
# Simulate CSV export
email_value = "john@example.com" if export_data.include_pii else "[REDACTED]"
csv_data = f"id,name,email\n1,John Doe,{email_value}\n"
import random
import string
export_id = f"exp_{''.join(random.choices(string.ascii_lowercase + string.digits, k=9))}"
return {
"success": True,
"export_id": export_id,
"format": "csv",
"rows": 1,
"mcp_servers": mcp_headers.get("servers", []),
"mcp_tools": mcp_headers.get("tools", []),
"decision_id": decision.decision_id,
"data": csv_data,
}
except AportError as e:
raise HTTPException(
status_code=e.status if hasattr(e, "status") else 500,
detail={
"error": "policy_verification_failed",
"message": str(e),
},
)
# Example 4: Health check (no auth required)
@app.get("/health")
async def health_check():
"""Health check endpoint (bypasses all middleware)."""
return {
"status": "healthy",
"timestamp": __import__("datetime").datetime.now().isoformat(),
"mcp_enforcement": "enabled (validates MCP context in policy verification)",
}
# Global exception handler
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
"""Handle all exceptions with proper error responses."""
print(f"Error: {exc}")
if "MCP" in str(exc):
return JSONResponse(
status_code=403,
content={"error": "mcp_enforcement_failed", "message": str(exc)},
)
if "Agent Passport" in str(exc) or "agent" in str(exc).lower():
return JSONResponse(
status_code=401,
content={"error": "authentication_failed", "message": str(exc)},
)
return JSONResponse(
status_code=500,
content={"error": "internal_server_error", "message": "An unexpected error occurred"},
)
if __name__ == "__main__":
import uvicorn
print("π MCP-enabled FastAPI server starting...")
print("π Try these endpoints:")
print(" POST /api/basic-mcp - Basic MCP header logging")
print(" POST /api/refunds/create - Refunds with policy + MCP")
print(" POST /api/export/csv - Data export with policy + MCP")
print(" GET /health - Health check (no auth required)")
print("")
print("π¦ Required headers:")
print(" X-Agent-Passport-Id: your-agent-id")
print(" X-MCP-Servers: https://mcp.stripe.com,https://mcp.notion.com (optional, comma-separated)")
print(" X-MCP-Tools: stripe.refunds.create,notion.pages.export (optional, comma-separated)")
print(" X-MCP-Session: session-id (optional)")
print("")
print(" Alternative (single values, backward compatible):")
print(" X-MCP-Server: https://mcp.stripe.com (optional)")
print(" X-MCP-Tool: stripe.refunds.create (optional)")
print("")
print("β
MCP validation: Policy verification validates MCP context against passport allowlist")
print(" See MCP_PRIMER.md and MCP_MULTIPLE_SERVERS.md for details.")
print("")
print("π API docs available at: http://localhost:8000/docs")
uvicorn.run(app, host="0.0.0.0", port=8000)
"""
Example curl commands:
# Basic MCP test (single server/tool)
curl -X POST http://localhost:8000/api/basic-mcp \
-H "Content-Type: application/json" \
-H "X-Agent-Passport-Id: ap_your_agent_id" \
-H "X-MCP-Server: https://mcp.stripe.com" \
-H "X-MCP-Tool: stripe.refunds.create" \
-H "X-MCP-Session: session_123" \
-d '{"test": true}'
# Basic MCP test (multiple servers/tools - preferred)
curl -X POST http://localhost:8000/api/basic-mcp \
-H "Content-Type: application/json" \
-H "X-Agent-Passport-Id: ap_your_agent_id" \
-H "X-MCP-Servers: https://mcp.stripe.com,https://mcp.notion.com" \
-H "X-MCP-Tools: stripe.refunds.create,notion.pages.export" \
-H "X-MCP-Session: session_123" \
-d '{"test": true}'
# Refund with MCP (single server/tool)
curl -X POST http://localhost:8000/api/refunds/create \
-H "Content-Type: application/json" \
-H "X-Agent-Passport-Id: ap_your_agent_id" \
-H "X-MCP-Server: https://mcp.stripe.com" \
-H "X-MCP-Tool: stripe.refunds.create" \
-d '{
"amount": 5000,
"currency": "USD",
"customer_id": "cust_123",
"order_id": "order_456",
"reason_code": "customer_request"
}'
# Refund with MCP (multiple servers/tools)
curl -X POST http://localhost:8000/api/refunds/create \
-H "Content-Type: application/json" \
-H "X-Agent-Passport-Id: ap_your_agent_id" \
-H "X-MCP-Servers: https://mcp.stripe.com,https://mcp.notion.com" \
-H "X-MCP-Tools: stripe.refunds.create,notion.pages.export" \
-d '{
"amount": 5000,
"currency": "USD",
"customer_id": "cust_123",
"order_id": "order_456",
"reason_code": "customer_request"
}'
# Export with MCP (multiple servers/tools)
curl -X POST http://localhost:8000/api/export/csv \
-H "Content-Type: application/json" \
-H "X-Agent-Passport-Id: ap_your_agent_id" \
-H "X-MCP-Servers: https://mcp.notion.com,https://mcp.snowflake.com" \
-H "X-MCP-Tools: notion.pages.export,snowflake.query.execute" \
-d '{
"table_name": "users",
"row_limit": 1000,
"include_pii": false
}'
# Health check (no auth)
curl http://localhost:8000/health
"""