File size: 11,527 Bytes
881f9f2 | 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 | """
WO MCP Server — Work Order creation and management for Smart Grid transformers.
WO = Work Order. After an agent diagnoses a fault (FMSR) and forecasts
remaining life (TSFM), it creates a work order here to schedule maintenance.
Work orders are stored in-memory during a session. In production this would
write to a CMMS (Computerised Maintenance Management System) database.
Historical fault records from the dataset are pre-loaded as a read-only
reference; new work orders created by the agent are tracked separately.
Tools exposed to the LLM agent:
list_fault_records — browse historical fault events from the dataset
get_fault_record — retrieve one historical fault event
create_work_order — create a new maintenance work order
list_work_orders — list agent-created work orders (this session)
update_work_order — update priority, status, or assignee
estimate_downtime — estimate repair downtime based on fault type / severity
Data source: data/processed/fault_records.csv (read-only history)
In-memory dict (agent-created work orders, session-scoped)
"""
from __future__ import annotations
import sys
import uuid
from datetime import UTC, datetime
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
import pandas as pd
from mcp.server.fastmcp import FastMCP
from mcp_servers.base import load_asset_metadata, load_fault_records
mcp = FastMCP("smart-grid-wo")
_fault_records: pd.DataFrame | None = None
_asset_metadata: pd.DataFrame | None = None
# Session-scoped work order store: {wo_id: dict}
_work_orders: dict[str, dict] = {}
# Downtime estimates (hours) by fault severity — derived from dataset statistics
_DOWNTIME_ESTIMATES = {
"low": {"min": 2, "max": 6, "typical": 4},
"medium": {"min": 6, "max": 16, "typical": 8},
"high": {"min": 16, "max": 48, "typical": 24},
"critical": {"min": 48, "max": 120, "typical": 72},
}
_VALID_PRIORITIES = {"low", "medium", "high", "critical"}
_VALID_STATUSES = {"open", "in_progress", "resolved", "closed"}
def _normalize_record(record: dict) -> dict:
return {key: (None if pd.isna(value) else value) for key, value in record.items()}
def _normalize_priority(priority: str | None) -> str | None:
if priority is None:
return None
return priority.strip().lower()
def _normalize_status(status: str | None) -> str | None:
if status is None:
return None
return status.strip().lower()
def _get_fault_records() -> pd.DataFrame:
global _fault_records
if _fault_records is None:
_fault_records = load_fault_records()
return _fault_records
def _get_asset_metadata() -> pd.DataFrame:
global _asset_metadata
if _asset_metadata is None:
_asset_metadata = load_asset_metadata()
return _asset_metadata
# ---------------------------------------------------------------------------
# Tools
# ---------------------------------------------------------------------------
@mcp.tool()
def list_fault_records(
transformer_id: str | None = None,
fault_type: str | None = None,
maintenance_status: str | None = None,
limit: int = 20,
) -> list[dict]:
"""
Browse historical fault records from the Smart Grid dataset.
These are read-only historical events, not agent-created work orders.
Use list_work_orders() to see work orders created in this session.
Args:
transformer_id: Filter by asset, e.g. "T-018". Optional.
fault_type: Substring filter on fault type, e.g. "Transformer".
maintenance_status: Filter by status ("Scheduled", "Pending", "Completed").
limit: Max records to return (default 20, max 100).
Returns:
List of fault record dicts.
"""
df = _get_fault_records()
if transformer_id:
df = df[df["transformer_id"] == transformer_id]
if fault_type:
df = df[df["fault_type"].str.contains(fault_type, case=False, na=False)]
if maintenance_status:
df = df[
df["maintenance_status"].fillna("").str.lower()
== maintenance_status.lower()
]
records = df.head(min(limit, 100)).to_dict(orient="records")
return [_normalize_record(record) for record in records]
@mcp.tool()
def get_fault_record(fault_id: str) -> dict:
"""
Retrieve a single historical fault record by its ID.
Args:
fault_id: e.g. "F001". Use list_fault_records() to discover IDs.
Returns:
Fault record dict, or an error dict if not found.
"""
df = _get_fault_records()
row = df[df["fault_id"] == fault_id]
if row.empty:
return {"error": f"Fault record '{fault_id}' not found."}
return _normalize_record(row.iloc[0].to_dict())
@mcp.tool()
def create_work_order(
transformer_id: str,
issue_description: str,
priority: str = "medium",
fault_type: str | None = None,
estimated_downtime_hours: float | None = None,
) -> dict:
"""
Create a new maintenance work order for a transformer.
Args:
transformer_id: Asset requiring maintenance, e.g. "T-016".
issue_description: Plain-language description of the problem.
priority: One of "low", "medium", "high", "critical".
Defaults to "medium".
fault_type: Optional fault classification for tracking,
e.g. "Arc Discharge" or "Thermal Fault T3".
estimated_downtime_hours: Override the auto-estimated downtime.
Returns:
Dict with keys: work_order_id, transformer_id, issue_description,
priority, fault_type, status, estimated_downtime_hours,
created_at, assigned_technician (null until assigned).
"""
priority = _normalize_priority(priority) or "medium"
if priority not in _VALID_PRIORITIES:
return {
"error": f"Invalid priority '{priority}'. "
f"Must be one of: {sorted(_VALID_PRIORITIES)}"
}
try:
metadata = _get_asset_metadata()
except FileNotFoundError as exc:
return {"error": str(exc)}
if transformer_id not in set(metadata["transformer_id"]):
return {
"error": f"Unknown transformer_id '{transformer_id}'.",
"valid_transformer_id_source": "data/processed/asset_metadata.csv",
}
if estimated_downtime_hours is None:
est = _DOWNTIME_ESTIMATES.get(priority, _DOWNTIME_ESTIMATES["medium"])
estimated_downtime_hours = est["typical"]
wo_id = f"WO-{uuid.uuid4().hex[:8].upper()}"
wo = {
"work_order_id": wo_id,
"transformer_id": transformer_id,
"issue_description": issue_description,
"priority": priority,
"fault_type": fault_type,
"status": "open",
"estimated_downtime_hours": estimated_downtime_hours,
"created_at": datetime.now(UTC).isoformat().replace("+00:00", "Z"),
"assigned_technician": None,
"notes": [],
}
_work_orders[wo_id] = wo
return wo
@mcp.tool()
def list_work_orders(
transformer_id: str | None = None,
status: str | None = None,
priority: str | None = None,
) -> list[dict]:
"""
List work orders created by the agent in this session.
Args:
transformer_id: Filter by asset. Optional.
status: Filter by status ("open", "in_progress", "resolved", "closed").
priority: Filter by priority ("low", "medium", "high", "critical").
Returns:
List of work order dicts, newest first.
"""
wos = list(_work_orders.values())
if transformer_id:
wos = [w for w in wos if w["transformer_id"] == transformer_id]
status = _normalize_status(status)
priority = _normalize_priority(priority)
if status:
wos = [w for w in wos if w["status"] == status]
if priority:
wos = [w for w in wos if w["priority"] == priority]
return sorted(wos, key=lambda w: w["created_at"], reverse=True)
@mcp.tool()
def update_work_order(
work_order_id: str,
status: str | None = None,
priority: str | None = None,
assigned_technician: str | None = None,
note: str | None = None,
) -> dict:
"""
Update an existing work order's status, priority, assignee, or add a note.
Args:
work_order_id: ID returned by create_work_order(), e.g. "WO-A1B2C3D4".
status: New status: "open", "in_progress", "resolved", "closed".
priority: New priority: "low", "medium", "high", "critical".
assigned_technician: Technician identifier, e.g. "TEC-03".
note: Free-text note to append to the work order log.
Returns:
Updated work order dict, or an error dict if not found / invalid.
"""
if work_order_id not in _work_orders:
return {"error": f"Work order '{work_order_id}' not found in this session."}
wo = _work_orders[work_order_id]
status = _normalize_status(status)
priority = _normalize_priority(priority)
if status is not None:
if status not in _VALID_STATUSES:
return {
"error": f"Invalid status '{status}'. "
f"Must be one of: {sorted(_VALID_STATUSES)}"
}
wo["status"] = status
if priority is not None:
if priority not in _VALID_PRIORITIES:
return {
"error": f"Invalid priority '{priority}'. "
f"Must be one of: {sorted(_VALID_PRIORITIES)}"
}
wo["priority"] = priority
if assigned_technician is not None:
wo["assigned_technician"] = assigned_technician
if note:
wo["notes"].append(
{
"timestamp": datetime.now(UTC).isoformat().replace("+00:00", "Z"),
"text": note,
}
)
return wo
@mcp.tool()
def estimate_downtime(
transformer_id: str,
severity: str,
fault_type: str | None = None,
) -> dict:
"""
Estimate the expected downtime (hours) for maintenance on a transformer.
Estimates are derived from the Smart Grid Fault Records dataset statistics.
The full project may replace this with a learned model.
Args:
transformer_id: Asset requiring maintenance, e.g. "T-019".
severity: Fault severity: "low", "medium", "high", or "critical".
fault_type: Optional fault type for context (not used in baseline
estimate but recorded for traceability).
Returns:
Dict with keys: transformer_id, severity, fault_type,
estimated_min_hours, estimated_max_hours, estimated_typical_hours,
source.
"""
if severity not in _VALID_PRIORITIES:
return {
"error": f"Invalid severity '{severity}'. "
f"Must be one of: {sorted(_VALID_PRIORITIES)}"
}
est = _DOWNTIME_ESTIMATES[severity]
return {
"transformer_id": transformer_id,
"severity": severity,
"fault_type": fault_type,
"estimated_min_hours": est["min"],
"estimated_max_hours": est["max"],
"estimated_typical_hours": est["typical"],
"source": "dataset_statistics_baseline",
}
# ---------------------------------------------------------------------------
# Entry point
# ---------------------------------------------------------------------------
if __name__ == "__main__":
mcp.run()
|