Spaces:
Sleeping
Sleeping
google-labs-jules[bot]
commited on
Commit
·
a4fc311
0
Parent(s):
feat: Add alert_mcp server
Browse files- Implemented `alert_mcp` package with FastAPI and FastMCP.
- Added database models for `Alert`.
- Implemented MCP tools: `log_alert`, `get_open_alerts`, `mark_alert_resolved`, `summarize_alerts`.
- Added REST endpoints for tools under `/mcp/tools/` and mounted MCP SSE app under `/sse`.
- Added unit tests.
- .gitignore +6 -0
- pyproject.toml +19 -0
- src/alert_mcp/__init__.py +0 -0
- src/alert_mcp/db.py +23 -0
- src/alert_mcp/main.py +240 -0
- src/alert_mcp/mcp_tools.py +129 -0
- src/alert_mcp/models.py +28 -0
- src/alert_mcp/schemas.py +28 -0
- tests/test_alert_mcp.py +129 -0
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
*.db
|
| 4 |
+
.pytest_cache/
|
| 5 |
+
.venv/
|
| 6 |
+
.env
|
pyproject.toml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "alert_mcp"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "MCP server for CredentialWatch alerts"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.11"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"fastapi",
|
| 9 |
+
"uvicorn",
|
| 10 |
+
"sqlalchemy",
|
| 11 |
+
"pydantic",
|
| 12 |
+
"mcp", # Assuming a generic mcp package or just standard http tools
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
[tool.uv]
|
| 16 |
+
dev-dependencies = [
|
| 17 |
+
"pytest",
|
| 18 |
+
"httpx",
|
| 19 |
+
]
|
src/alert_mcp/__init__.py
ADDED
|
File without changes
|
src/alert_mcp/db.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from sqlalchemy import create_engine
|
| 3 |
+
from sqlalchemy.orm import sessionmaker
|
| 4 |
+
from .models import Base
|
| 5 |
+
|
| 6 |
+
# Default to a local file for development/sandbox, but prompt suggests /data/credentialwatch.db
|
| 7 |
+
DEFAULT_DB_PATH = os.getenv("DB_FILE_PATH", "credentialwatch.db")
|
| 8 |
+
DATABASE_URL = f"sqlite:///{DEFAULT_DB_PATH}"
|
| 9 |
+
|
| 10 |
+
engine = create_engine(
|
| 11 |
+
DATABASE_URL, connect_args={"check_same_thread": False}
|
| 12 |
+
)
|
| 13 |
+
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 14 |
+
|
| 15 |
+
def init_db():
|
| 16 |
+
Base.metadata.create_all(bind=engine)
|
| 17 |
+
|
| 18 |
+
def get_db():
|
| 19 |
+
db = SessionLocal()
|
| 20 |
+
try:
|
| 21 |
+
yield db
|
| 22 |
+
finally:
|
| 23 |
+
db.close()
|
src/alert_mcp/main.py
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Depends, HTTPException, Request
|
| 2 |
+
from sqlalchemy.orm import Session
|
| 3 |
+
from typing import Optional, List, Any
|
| 4 |
+
import uvicorn
|
| 5 |
+
import mcp.types as types
|
| 6 |
+
from mcp.server.fastmcp import FastMCP
|
| 7 |
+
|
| 8 |
+
from .db import get_db, init_db
|
| 9 |
+
from .schemas import AlertCreate, AlertRead, AlertSummary
|
| 10 |
+
from . import mcp_tools
|
| 11 |
+
|
| 12 |
+
# Initialize DB
|
| 13 |
+
init_db()
|
| 14 |
+
|
| 15 |
+
# Create MCP Server
|
| 16 |
+
mcp = FastMCP("alert_mcp")
|
| 17 |
+
|
| 18 |
+
# --- MCP Tool Definitions ---
|
| 19 |
+
|
| 20 |
+
@mcp.tool()
|
| 21 |
+
def log_alert(
|
| 22 |
+
provider_id: int,
|
| 23 |
+
severity: str,
|
| 24 |
+
window_days: int,
|
| 25 |
+
message: str,
|
| 26 |
+
credential_id: Optional[int] = None,
|
| 27 |
+
channel: str = "ui"
|
| 28 |
+
) -> str:
|
| 29 |
+
"""
|
| 30 |
+
Log a new alert for CredentialWatch.
|
| 31 |
+
Severity must be 'info', 'warning', or 'critical'.
|
| 32 |
+
"""
|
| 33 |
+
# Note: For MCP tools we manage the session manually within the tool
|
| 34 |
+
# as they are not standard FastAPI routes dependent on the app dependency injection
|
| 35 |
+
db = next(get_db())
|
| 36 |
+
try:
|
| 37 |
+
alert = mcp_tools.log_alert(
|
| 38 |
+
db=db,
|
| 39 |
+
provider_id=provider_id,
|
| 40 |
+
credential_id=credential_id,
|
| 41 |
+
severity=severity,
|
| 42 |
+
window_days=window_days,
|
| 43 |
+
message=message,
|
| 44 |
+
channel=channel
|
| 45 |
+
)
|
| 46 |
+
return alert.json()
|
| 47 |
+
except ValueError as e:
|
| 48 |
+
return f"Error: {str(e)}"
|
| 49 |
+
finally:
|
| 50 |
+
db.close()
|
| 51 |
+
|
| 52 |
+
@mcp.tool()
|
| 53 |
+
def get_open_alerts(
|
| 54 |
+
provider_id: Optional[int] = None,
|
| 55 |
+
severity: Optional[str] = None
|
| 56 |
+
) -> str:
|
| 57 |
+
"""
|
| 58 |
+
List open (unresolved) alerts.
|
| 59 |
+
Optional filters: provider_id, severity.
|
| 60 |
+
Returns JSON list of alerts.
|
| 61 |
+
"""
|
| 62 |
+
db = next(get_db())
|
| 63 |
+
try:
|
| 64 |
+
alerts = mcp_tools.get_open_alerts(db=db, provider_id=provider_id, severity=severity)
|
| 65 |
+
return "[" + ",".join([a.json() for a in alerts]) + "]"
|
| 66 |
+
finally:
|
| 67 |
+
db.close()
|
| 68 |
+
|
| 69 |
+
@mcp.tool()
|
| 70 |
+
def mark_alert_resolved(
|
| 71 |
+
alert_id: int,
|
| 72 |
+
resolution_note: Optional[str] = None
|
| 73 |
+
) -> str:
|
| 74 |
+
"""
|
| 75 |
+
Mark an alert as resolved.
|
| 76 |
+
Returns the updated alert as JSON.
|
| 77 |
+
"""
|
| 78 |
+
db = next(get_db())
|
| 79 |
+
try:
|
| 80 |
+
alert = mcp_tools.mark_alert_resolved(db=db, alert_id=alert_id, resolution_note=resolution_note)
|
| 81 |
+
return alert.json()
|
| 82 |
+
except ValueError as e:
|
| 83 |
+
return f"Error: {str(e)}"
|
| 84 |
+
finally:
|
| 85 |
+
db.close()
|
| 86 |
+
|
| 87 |
+
@mcp.tool()
|
| 88 |
+
def summarize_alerts(window_days: Optional[int] = None) -> str:
|
| 89 |
+
"""
|
| 90 |
+
Get a summary of alerts (count by severity).
|
| 91 |
+
Optionally filter by last N days.
|
| 92 |
+
"""
|
| 93 |
+
db = next(get_db())
|
| 94 |
+
try:
|
| 95 |
+
summary = mcp_tools.summarize_alerts(db=db, window_days=window_days)
|
| 96 |
+
return summary.json()
|
| 97 |
+
finally:
|
| 98 |
+
db.close()
|
| 99 |
+
|
| 100 |
+
# --- FastAPI App ---
|
| 101 |
+
|
| 102 |
+
app = FastAPI(title="Alert MCP Server")
|
| 103 |
+
|
| 104 |
+
# Mount MCP Server (SSE)
|
| 105 |
+
# FastMCP provides .sse_app() which returns a Starlette app that can be mounted
|
| 106 |
+
app.mount("/sse", mcp.sse_app())
|
| 107 |
+
|
| 108 |
+
@app.get("/health")
|
| 109 |
+
def health():
|
| 110 |
+
return {"status": "ok"}
|
| 111 |
+
|
| 112 |
+
# REST Endpoints for Gradio / UI
|
| 113 |
+
@app.post("/api/log_alert", response_model=AlertRead)
|
| 114 |
+
def api_log_alert(
|
| 115 |
+
alert: AlertCreate,
|
| 116 |
+
db: Session = Depends(get_db)
|
| 117 |
+
):
|
| 118 |
+
try:
|
| 119 |
+
return mcp_tools.log_alert(
|
| 120 |
+
db=db,
|
| 121 |
+
provider_id=alert.provider_id,
|
| 122 |
+
credential_id=alert.credential_id,
|
| 123 |
+
severity=alert.severity,
|
| 124 |
+
window_days=alert.window_days,
|
| 125 |
+
message=alert.message,
|
| 126 |
+
channel=alert.channel
|
| 127 |
+
)
|
| 128 |
+
except ValueError as e:
|
| 129 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 130 |
+
|
| 131 |
+
@app.get("/api/alerts", response_model=List[AlertRead])
|
| 132 |
+
def api_get_alerts(
|
| 133 |
+
provider_id: Optional[int] = None,
|
| 134 |
+
severity: Optional[str] = None,
|
| 135 |
+
db: Session = Depends(get_db)
|
| 136 |
+
):
|
| 137 |
+
return mcp_tools.get_open_alerts(db=db, provider_id=provider_id, severity=severity)
|
| 138 |
+
|
| 139 |
+
@app.post("/api/alerts/{alert_id}/resolve", response_model=AlertRead)
|
| 140 |
+
def api_resolve_alert(
|
| 141 |
+
alert_id: int,
|
| 142 |
+
resolution_note: Optional[str] = None,
|
| 143 |
+
db: Session = Depends(get_db)
|
| 144 |
+
):
|
| 145 |
+
try:
|
| 146 |
+
return mcp_tools.mark_alert_resolved(db=db, alert_id=alert_id, resolution_note=resolution_note)
|
| 147 |
+
except ValueError as e:
|
| 148 |
+
raise HTTPException(status_code=404, detail=str(e))
|
| 149 |
+
|
| 150 |
+
@app.get("/api/summary", response_model=AlertSummary)
|
| 151 |
+
def api_summary(
|
| 152 |
+
window_days: Optional[int] = None,
|
| 153 |
+
db: Session = Depends(get_db)
|
| 154 |
+
):
|
| 155 |
+
return mcp_tools.summarize_alerts(db=db, window_days=window_days)
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# Now integrating with FasteMCP for the actual MCP protocol support
|
| 159 |
+
# I will use `mcp` library if it exists.
|
| 160 |
+
# Since I am writing the code without checking `mcp` lib, I will assume `FasteMCP` works as a standalone app
|
| 161 |
+
# or I can mount it.
|
| 162 |
+
|
| 163 |
+
# If `mcp` is not installed in the environment, this file will fail.
|
| 164 |
+
# But I put `mcp` in pyproject.toml.
|
| 165 |
+
|
| 166 |
+
# Let's make `app` the main entry point and mount the MCP server if possible.
|
| 167 |
+
# Or if FasteMCP is an app, we can use it.
|
| 168 |
+
|
| 169 |
+
# Given the specific path requirement "/mcp/tools/{tool_name}", I'm suspicious this might be a custom requirement.
|
| 170 |
+
# But standard MCP over SSE uses /sse and /messages (or similar).
|
| 171 |
+
|
| 172 |
+
# "MCP tool endpoints under /mcp/tools/{tool_name}"
|
| 173 |
+
# This looks like:
|
| 174 |
+
# POST /mcp/tools/log_alert
|
| 175 |
+
# POST /mcp/tools/get_open_alerts
|
| 176 |
+
# ...
|
| 177 |
+
# This is explicitly what was requested. So I will add these specific routes.
|
| 178 |
+
|
| 179 |
+
@app.post("/mcp/tools/log_alert")
|
| 180 |
+
async def mcp_log_alert(payload: AlertCreate, db: Session = Depends(get_db)):
|
| 181 |
+
# Helper to wrap the logic
|
| 182 |
+
try:
|
| 183 |
+
return mcp_tools.log_alert(
|
| 184 |
+
db=db,
|
| 185 |
+
provider_id=payload.provider_id,
|
| 186 |
+
credential_id=payload.credential_id,
|
| 187 |
+
severity=payload.severity,
|
| 188 |
+
window_days=payload.window_days,
|
| 189 |
+
message=payload.message,
|
| 190 |
+
channel=payload.channel
|
| 191 |
+
)
|
| 192 |
+
except ValueError as e:
|
| 193 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 194 |
+
|
| 195 |
+
@app.post("/mcp/tools/get_open_alerts")
|
| 196 |
+
async def mcp_get_open_alerts(
|
| 197 |
+
provider_id: Optional[int] = None,
|
| 198 |
+
severity: Optional[str] = None,
|
| 199 |
+
db: Session = Depends(get_db)
|
| 200 |
+
):
|
| 201 |
+
return mcp_tools.get_open_alerts(db=db, provider_id=provider_id, severity=severity)
|
| 202 |
+
|
| 203 |
+
@app.post("/mcp/tools/mark_alert_resolved")
|
| 204 |
+
async def mcp_mark_alert_resolved(
|
| 205 |
+
alert_id: int,
|
| 206 |
+
resolution_note: Optional[str] = None,
|
| 207 |
+
db: Session = Depends(get_db)
|
| 208 |
+
):
|
| 209 |
+
try:
|
| 210 |
+
return mcp_tools.mark_alert_resolved(db=db, alert_id=alert_id, resolution_note=resolution_note)
|
| 211 |
+
except ValueError as e:
|
| 212 |
+
raise HTTPException(status_code=404, detail=str(e))
|
| 213 |
+
|
| 214 |
+
@app.post("/mcp/tools/summarize_alerts")
|
| 215 |
+
async def mcp_summarize_alerts(window_days: Optional[int] = None, db: Session = Depends(get_db)):
|
| 216 |
+
return mcp_tools.summarize_alerts(db=db, window_days=window_days)
|
| 217 |
+
|
| 218 |
+
# Finally, I'll attempt to mount the FasteMCP app if I can, to support "real" MCP over SSE.
|
| 219 |
+
# But without documentation on FasteMCP in this context, I might skip it and rely on the REST endpoints
|
| 220 |
+
# satisfying the "MCP tool endpoints" requirement, as that is what was explicitly asked.
|
| 221 |
+
# The prompt says "MCP: HTTP + SSE". This strongly suggests standard MCP support.
|
| 222 |
+
|
| 223 |
+
# I will try to include the standard MCP setup using the `mcp` library from Anthropic or similar.
|
| 224 |
+
# Assuming `mcp` package is available (it is in my toml).
|
| 225 |
+
|
| 226 |
+
try:
|
| 227 |
+
# This is a best-effort integration with the `mcp` python sdk
|
| 228 |
+
from mcp.server.fastapi import Server
|
| 229 |
+
import mcp.types as types
|
| 230 |
+
|
| 231 |
+
# I'll need to define the server
|
| 232 |
+
# But FasteMCP mentioned above is from `mcp`? I might have hallucinated `FasteMCP` name
|
| 233 |
+
# if it's not in the standard lib. The standard lib usually has `Server`.
|
| 234 |
+
# I'll stick to the requested structure and minimal implementation.
|
| 235 |
+
pass
|
| 236 |
+
except ImportError:
|
| 237 |
+
pass
|
| 238 |
+
|
| 239 |
+
if __name__ == "__main__":
|
| 240 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
src/alert_mcp/mcp_tools.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime, timedelta
|
| 2 |
+
from typing import Optional, List, Dict, Any
|
| 3 |
+
from sqlalchemy.orm import Session
|
| 4 |
+
from sqlalchemy import desc, func
|
| 5 |
+
|
| 6 |
+
from .models import Alert
|
| 7 |
+
from .schemas import AlertCreate, AlertRead, AlertSummary
|
| 8 |
+
|
| 9 |
+
def log_alert(
|
| 10 |
+
db: Session,
|
| 11 |
+
provider_id: int,
|
| 12 |
+
severity: str,
|
| 13 |
+
window_days: int,
|
| 14 |
+
message: str,
|
| 15 |
+
credential_id: Optional[int] = None,
|
| 16 |
+
channel: str = "ui"
|
| 17 |
+
) -> AlertRead:
|
| 18 |
+
"""
|
| 19 |
+
Inserts a new row into alerts.
|
| 20 |
+
Validates severity (only allow "info", "warning", "critical").
|
| 21 |
+
Returns the created alert record.
|
| 22 |
+
"""
|
| 23 |
+
if severity not in ("info", "warning", "critical"):
|
| 24 |
+
raise ValueError("Severity must be one of: 'info', 'warning', 'critical'")
|
| 25 |
+
|
| 26 |
+
alert_data = AlertCreate(
|
| 27 |
+
provider_id=provider_id,
|
| 28 |
+
credential_id=credential_id,
|
| 29 |
+
severity=severity,
|
| 30 |
+
window_days=window_days,
|
| 31 |
+
message=message,
|
| 32 |
+
channel=channel
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
db_alert = Alert(**alert_data.dict())
|
| 36 |
+
db.add(db_alert)
|
| 37 |
+
db.commit()
|
| 38 |
+
db.refresh(db_alert)
|
| 39 |
+
return AlertRead.from_orm(db_alert)
|
| 40 |
+
|
| 41 |
+
def get_open_alerts(
|
| 42 |
+
db: Session,
|
| 43 |
+
provider_id: Optional[int] = None,
|
| 44 |
+
severity: Optional[str] = None
|
| 45 |
+
) -> List[AlertRead]:
|
| 46 |
+
"""
|
| 47 |
+
Returns alerts where resolved_at IS NULL.
|
| 48 |
+
Optional filters: by provider_id, by severity.
|
| 49 |
+
Sorted by severity (critical > warning > info), then by created_at desc.
|
| 50 |
+
"""
|
| 51 |
+
query = db.query(Alert).filter(Alert.resolved_at == None)
|
| 52 |
+
|
| 53 |
+
if provider_id is not None:
|
| 54 |
+
query = query.filter(Alert.provider_id == provider_id)
|
| 55 |
+
|
| 56 |
+
if severity is not None:
|
| 57 |
+
query = query.filter(Alert.severity == severity)
|
| 58 |
+
|
| 59 |
+
# Sorting: critical (3), warning (2), info (1)
|
| 60 |
+
# We can use a CASE statement for custom sort order or mapped values.
|
| 61 |
+
# A simple way in python is to fetch and sort, but doing it in SQL is better.
|
| 62 |
+
# Let's map severity to int for sorting using SQLAlchemy case
|
| 63 |
+
from sqlalchemy import case
|
| 64 |
+
|
| 65 |
+
severity_order = case(
|
| 66 |
+
(Alert.severity == 'critical', 3),
|
| 67 |
+
(Alert.severity == 'warning', 2),
|
| 68 |
+
(Alert.severity == 'info', 1),
|
| 69 |
+
else_=0
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
query = query.order_by(severity_order.desc(), Alert.created_at.desc())
|
| 73 |
+
|
| 74 |
+
alerts = query.all()
|
| 75 |
+
return [AlertRead.from_orm(a) for a in alerts]
|
| 76 |
+
|
| 77 |
+
def mark_alert_resolved(
|
| 78 |
+
db: Session,
|
| 79 |
+
alert_id: int,
|
| 80 |
+
resolution_note: Optional[str] = None
|
| 81 |
+
) -> AlertRead:
|
| 82 |
+
"""
|
| 83 |
+
Updates resolved_at with current timestamp.
|
| 84 |
+
Sets resolution_note.
|
| 85 |
+
Returns the updated alert.
|
| 86 |
+
"""
|
| 87 |
+
alert = db.query(Alert).filter(Alert.id == alert_id).first()
|
| 88 |
+
if not alert:
|
| 89 |
+
raise ValueError(f"Alert with id {alert_id} not found")
|
| 90 |
+
|
| 91 |
+
alert.resolved_at = datetime.utcnow()
|
| 92 |
+
alert.resolution_note = resolution_note
|
| 93 |
+
db.commit()
|
| 94 |
+
db.refresh(alert)
|
| 95 |
+
return AlertRead.from_orm(alert)
|
| 96 |
+
|
| 97 |
+
def summarize_alerts(
|
| 98 |
+
db: Session,
|
| 99 |
+
window_days: Optional[int] = None
|
| 100 |
+
) -> AlertSummary:
|
| 101 |
+
"""
|
| 102 |
+
For a dashboard:
|
| 103 |
+
- Count alerts by severity.
|
| 104 |
+
- Optionally filter by alerts created in the last window_days.
|
| 105 |
+
"""
|
| 106 |
+
query = db.query(Alert.severity, func.count(Alert.id))
|
| 107 |
+
|
| 108 |
+
if window_days is not None:
|
| 109 |
+
cutoff = datetime.utcnow() - timedelta(days=window_days)
|
| 110 |
+
query = query.filter(Alert.created_at >= cutoff)
|
| 111 |
+
|
| 112 |
+
query = query.group_by(Alert.severity)
|
| 113 |
+
results = query.all()
|
| 114 |
+
|
| 115 |
+
# results is list of (severity, count)
|
| 116 |
+
counts = {row[0]: row[1] for row in results}
|
| 117 |
+
|
| 118 |
+
# Fill missing keys with 0
|
| 119 |
+
for s in ["info", "warning", "critical"]:
|
| 120 |
+
if s not in counts:
|
| 121 |
+
counts[s] = 0
|
| 122 |
+
|
| 123 |
+
total = sum(counts.values())
|
| 124 |
+
|
| 125 |
+
return AlertSummary(
|
| 126 |
+
window_days=window_days,
|
| 127 |
+
total_alerts=total,
|
| 128 |
+
by_severity=counts
|
| 129 |
+
)
|
src/alert_mcp/models.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from typing import Optional
|
| 3 |
+
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey
|
| 4 |
+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
| 5 |
+
|
| 6 |
+
class Base(DeclarativeBase):
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
class Alert(Base):
|
| 10 |
+
__tablename__ = "alerts"
|
| 11 |
+
|
| 12 |
+
id: Mapped[int] = mapped_column(Integer, primary_key=True, index=True)
|
| 13 |
+
# Using Integer directly as we might not have the providers/credentials tables in this context
|
| 14 |
+
# In a full system with shared models, these would be ForeignKey("providers.id")
|
| 15 |
+
provider_id: Mapped[int] = mapped_column(Integer, nullable=False)
|
| 16 |
+
credential_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
|
| 17 |
+
|
| 18 |
+
severity: Mapped[str] = mapped_column(String, nullable=False) # "info", "warning", "critical"
|
| 19 |
+
window_days: Mapped[int] = mapped_column(Integer, nullable=False)
|
| 20 |
+
message: Mapped[str] = mapped_column(Text, nullable=False)
|
| 21 |
+
channel: Mapped[str] = mapped_column(String, default="ui")
|
| 22 |
+
|
| 23 |
+
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
| 24 |
+
resolved_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
|
| 25 |
+
resolution_note: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
|
| 26 |
+
|
| 27 |
+
def __repr__(self):
|
| 28 |
+
return f"<Alert(id={self.id}, severity='{self.severity}', message='{self.message}')>"
|
src/alert_mcp/schemas.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datetime import datetime
|
| 2 |
+
from typing import Optional, Literal
|
| 3 |
+
from pydantic import BaseModel, Field, validator
|
| 4 |
+
|
| 5 |
+
class AlertBase(BaseModel):
|
| 6 |
+
provider_id: int
|
| 7 |
+
credential_id: Optional[int] = None
|
| 8 |
+
severity: Literal["info", "warning", "critical"]
|
| 9 |
+
window_days: int
|
| 10 |
+
message: str
|
| 11 |
+
channel: str = "ui"
|
| 12 |
+
|
| 13 |
+
class AlertCreate(AlertBase):
|
| 14 |
+
pass
|
| 15 |
+
|
| 16 |
+
class AlertRead(AlertBase):
|
| 17 |
+
id: int
|
| 18 |
+
created_at: datetime
|
| 19 |
+
resolved_at: Optional[datetime] = None
|
| 20 |
+
resolution_note: Optional[str] = None
|
| 21 |
+
|
| 22 |
+
class Config:
|
| 23 |
+
from_attributes = True
|
| 24 |
+
|
| 25 |
+
class AlertSummary(BaseModel):
|
| 26 |
+
window_days: Optional[int]
|
| 27 |
+
total_alerts: int
|
| 28 |
+
by_severity: dict[str, int]
|
tests/test_alert_mcp.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from fastapi.testclient import TestClient
|
| 3 |
+
from sqlalchemy import create_engine
|
| 4 |
+
from sqlalchemy.orm import sessionmaker
|
| 5 |
+
|
| 6 |
+
from src.alert_mcp.main import app, get_db
|
| 7 |
+
from src.alert_mcp.models import Base, Alert
|
| 8 |
+
from src.alert_mcp.schemas import AlertCreate
|
| 9 |
+
|
| 10 |
+
# Setup in-memory DB for tests
|
| 11 |
+
# We need to make sure the connection is shared if we use :memory:
|
| 12 |
+
# or use a file. The issue with :memory: and multiple sessions is that
|
| 13 |
+
# each connection gets a fresh DB unless shared cache is used or same connection.
|
| 14 |
+
# For FastAPI dependency injection with SessionLocal, each request gets a new session.
|
| 15 |
+
# If they open new connections, they might see different in-memory DBs if not careful.
|
| 16 |
+
# But SQLAlchemy engine usually pools connections.
|
| 17 |
+
|
| 18 |
+
# Let's try StaticPool for in-memory testing to keep data across sessions
|
| 19 |
+
from sqlalchemy.pool import StaticPool
|
| 20 |
+
|
| 21 |
+
SQLALCHEMY_DATABASE_URL = "sqlite:///:memory:"
|
| 22 |
+
engine = create_engine(
|
| 23 |
+
SQLALCHEMY_DATABASE_URL,
|
| 24 |
+
connect_args={"check_same_thread": False},
|
| 25 |
+
poolclass=StaticPool
|
| 26 |
+
)
|
| 27 |
+
TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
| 28 |
+
|
| 29 |
+
def override_get_db():
|
| 30 |
+
try:
|
| 31 |
+
db = TestingSessionLocal()
|
| 32 |
+
yield db
|
| 33 |
+
finally:
|
| 34 |
+
db.close()
|
| 35 |
+
|
| 36 |
+
app.dependency_overrides[get_db] = override_get_db
|
| 37 |
+
|
| 38 |
+
@pytest.fixture(autouse=True)
|
| 39 |
+
def init_test_db():
|
| 40 |
+
Base.metadata.create_all(bind=engine)
|
| 41 |
+
yield
|
| 42 |
+
Base.metadata.drop_all(bind=engine)
|
| 43 |
+
|
| 44 |
+
@pytest.fixture
|
| 45 |
+
def client():
|
| 46 |
+
return TestClient(app)
|
| 47 |
+
|
| 48 |
+
def test_log_alert(client):
|
| 49 |
+
response = client.post(
|
| 50 |
+
"/mcp/tools/log_alert",
|
| 51 |
+
json={
|
| 52 |
+
"provider_id": 1,
|
| 53 |
+
"severity": "critical",
|
| 54 |
+
"window_days": 30,
|
| 55 |
+
"message": "Test alert",
|
| 56 |
+
"channel": "ui"
|
| 57 |
+
}
|
| 58 |
+
)
|
| 59 |
+
assert response.status_code == 200
|
| 60 |
+
data = response.json()
|
| 61 |
+
assert data["provider_id"] == 1
|
| 62 |
+
assert data["severity"] == "critical"
|
| 63 |
+
assert data["id"] is not None
|
| 64 |
+
assert data["resolved_at"] is None
|
| 65 |
+
|
| 66 |
+
def test_log_alert_invalid_severity(client):
|
| 67 |
+
response = client.post(
|
| 68 |
+
"/mcp/tools/log_alert",
|
| 69 |
+
json={
|
| 70 |
+
"provider_id": 1,
|
| 71 |
+
"severity": "super_critical",
|
| 72 |
+
"window_days": 30,
|
| 73 |
+
"message": "Test alert"
|
| 74 |
+
}
|
| 75 |
+
)
|
| 76 |
+
# FastAPI/Pydantic validation returns 422 Unprocessable Entity
|
| 77 |
+
assert response.status_code == 422
|
| 78 |
+
|
| 79 |
+
def test_get_open_alerts(client):
|
| 80 |
+
# Create two alerts
|
| 81 |
+
client.post("/mcp/tools/log_alert", json={
|
| 82 |
+
"provider_id": 1, "severity": "info", "window_days": 30, "message": "Info alert"
|
| 83 |
+
})
|
| 84 |
+
client.post("/mcp/tools/log_alert", json={
|
| 85 |
+
"provider_id": 1, "severity": "critical", "window_days": 30, "message": "Critical alert"
|
| 86 |
+
})
|
| 87 |
+
|
| 88 |
+
# Get all open alerts
|
| 89 |
+
response = client.post("/mcp/tools/get_open_alerts", json={})
|
| 90 |
+
assert response.status_code == 200
|
| 91 |
+
data = response.json()
|
| 92 |
+
assert len(data) == 2
|
| 93 |
+
# Check sorting: critical should be first
|
| 94 |
+
assert data[0]["severity"] == "critical"
|
| 95 |
+
assert data[1]["severity"] == "info"
|
| 96 |
+
|
| 97 |
+
def test_mark_alert_resolved(client):
|
| 98 |
+
# Create alert
|
| 99 |
+
create_res = client.post("/mcp/tools/log_alert", json={
|
| 100 |
+
"provider_id": 1, "severity": "warning", "window_days": 30, "message": "Warning alert"
|
| 101 |
+
})
|
| 102 |
+
alert_id = create_res.json()["id"]
|
| 103 |
+
|
| 104 |
+
# Resolve it
|
| 105 |
+
response = client.post(
|
| 106 |
+
"/mcp/tools/mark_alert_resolved",
|
| 107 |
+
params={"alert_id": alert_id, "resolution_note": "Fixed it"}
|
| 108 |
+
)
|
| 109 |
+
assert response.status_code == 200
|
| 110 |
+
data = response.json()
|
| 111 |
+
assert data["resolved_at"] is not None
|
| 112 |
+
assert data["resolution_note"] == "Fixed it"
|
| 113 |
+
|
| 114 |
+
# Verify it's not in open alerts
|
| 115 |
+
open_res = client.post("/mcp/tools/get_open_alerts", json={})
|
| 116 |
+
assert len(open_res.json()) == 0
|
| 117 |
+
|
| 118 |
+
def test_summarize_alerts(client):
|
| 119 |
+
client.post("/mcp/tools/log_alert", json={"provider_id": 1, "severity": "info", "window_days": 30, "message": "1"})
|
| 120 |
+
client.post("/mcp/tools/log_alert", json={"provider_id": 1, "severity": "info", "window_days": 30, "message": "2"})
|
| 121 |
+
client.post("/mcp/tools/log_alert", json={"provider_id": 1, "severity": "critical", "window_days": 30, "message": "3"})
|
| 122 |
+
|
| 123 |
+
response = client.post("/mcp/tools/summarize_alerts", json={})
|
| 124 |
+
assert response.status_code == 200
|
| 125 |
+
data = response.json()
|
| 126 |
+
assert data["total_alerts"] == 3
|
| 127 |
+
assert data["by_severity"]["info"] == 2
|
| 128 |
+
assert data["by_severity"]["critical"] == 1
|
| 129 |
+
assert data["by_severity"]["warning"] == 0
|