Skip to content

Commit dafd567

Browse files
authored
Update main.py
1 parent 3695fa9 commit dafd567

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

main/api/mcp/main.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from fastapi import FastAPI, Depends, HTTPException, Security
1+
from fastapi import FastAPI, Depends, HTTPException, Security, WebSocket
22
from fastapi.security import OAuth2PasswordBearer
33
from fastapi_limiter import FastAPILimiter
44
from fastapi_limiter.depends import RateLimiter
@@ -11,6 +11,8 @@
1111
from tools.wallet import WalletTool
1212
from lib.security import SecurityHandler
1313
from lib.mcp_transport import MCPTransport
14+
from lib.monitoring import MonitoringHandler
15+
from lib.data_privacy import DataPrivacyHandler
1416
from lib.logger import logger
1517
from lib.errors import ValidationError
1618
from neondatabase import AsyncClient
@@ -27,13 +29,15 @@
2729
wallet_tool = WalletTool(db_client)
2830
security_handler = SecurityHandler(db_client)
2931
mcp_transport = MCPTransport()
32+
monitoring_handler = MonitoringHandler(db_client)
33+
data_privacy_handler = DataPrivacyHandler(db_client)
3034

3135
# CORS configuration
3236
app.add_middleware(
3337
CORSMiddleware,
3438
allow_origins=["https://webxos.netlify.app"],
3539
allow_credentials=True,
36-
allow_methods=["GET", "POST"],
40+
allow_methods=["GET", "POST", "DELETE"],
3741
allow_headers=["Authorization", "X-Session-ID", "Content-Type"]
3842
)
3943

@@ -46,7 +50,6 @@ class JSONRPCRequest(BaseModel):
4650
def sanitize_input(value: Any) -> Any:
4751
"""Sanitize input to prevent injection attacks."""
4852
if isinstance(value, str):
49-
# Remove potentially malicious characters and escape HTML
5053
value = re.sub(r'[<>;{}]', '', value)
5154
return escape(value)
5255
elif isinstance(value, dict):
@@ -84,13 +87,11 @@ async def shutdown_event():
8487
@app.post("/mcp/execute", response_model=JSONRPCRequest, dependencies=[Depends(RateLimiter(times=100, seconds=900))])
8588
async def execute(request: JSONRPCRequest, user: Dict[str, Any] = Depends(get_current_user)):
8689
try:
87-
# Sanitize request parameters
8890
sanitized_params = sanitize_input(request.params)
8991
method = request.method
9092
params = sanitized_params
9193
params["user_id"] = user["user_id"]
9294

93-
# Apply stricter rate limiting for cash-out
9495
if method == "wallet.cashOut":
9596
await RateLimiter(times=5, seconds=900)(request)
9697

@@ -122,6 +123,18 @@ async def execute(request: JSONRPCRequest, user: Dict[str, Any] = Depends(get_cu
122123
id=request.id
123124
)
124125

126+
@app.get("/monitoring/kpis", dependencies=[Depends(RateLimiter(times=10, seconds=60))])
127+
async def get_kpis(time_window_hours: int = 24, handler: MonitoringHandler = Depends(lambda: MonitoringHandler(DatabaseConfig()))):
128+
return await handler.get_security_kpis(time_window_hours)
129+
130+
@app.websocket("/monitoring/kpis/stream")
131+
async def stream_kpis(websocket: WebSocket, handler: MonitoringHandler = Depends(lambda: MonitoringHandler(DatabaseConfig()))):
132+
await handler.stream_kpis(websocket)
133+
134+
@app.post("/privacy/erase", dependencies=[Depends(RateLimiter(times=3, seconds=3600))])
135+
async def erase_data(input: DataErasureInput, handler: DataPrivacyHandler = Depends(lambda: DataPrivacyHandler(DatabaseConfig()))):
136+
return await handler.erase_user_data(input)
137+
125138
@app.get("/openapi.json")
126139
async def get_openapi():
127140
return app.openapi()

0 commit comments

Comments
 (0)