-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathfunction_app.py
More file actions
405 lines (356 loc) · 14.2 KB
/
function_app.py
File metadata and controls
405 lines (356 loc) · 14.2 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
import json
import logging
from pathlib import Path
from typing import Dict, Any
import azure.functions as func
from weather_service import WeatherService
from iot_service import IoTService
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
# Weather service instance
weather_service = WeatherService()
# Constants for the Weather Widget resource
WEATHER_WIDGET_URI = "ui://weather/index.html"
WEATHER_WIDGET_NAME = "Weather Widget"
WEATHER_WIDGET_DESCRIPTION = "Interactive weather display for MCP Apps"
WEATHER_WIDGET_MIME_TYPE = "text/html;profile=mcp-app"
# Metadata for the tool (as valid JSON string)
TOOL_METADATA = '{"ui": {"resourceUri": "ui://weather/index.html"}}'
# Metadata for the resource (as valid JSON string)
RESOURCE_METADATA = '{"ui": {"prefersBorder": true}}'
# Constants for the Azure Blob Storage container, file, and blob path
_SNIPPET_NAME_PROPERTY_NAME = "snippetname"
_BLOB_PATH = "snippets/{mcptoolargs." + _SNIPPET_NAME_PROPERTY_NAME + "}.json"
# IoT service instance
iot_service = IoTService()
# Constants for the IoT Blob Storage path
_IOT_DEVICE_ID_PROPERTY_NAME = "device_id"
_IOT_BLOB_PATH = "iot-sensors/{mcptoolargs." + _IOT_DEVICE_ID_PROPERTY_NAME + "}.json"
# Constants for the IoT Dashboard resource
IOT_DASHBOARD_URI = "ui://iot/index.html"
IOT_DASHBOARD_NAME = "IoT Sensor Dashboard"
IOT_DASHBOARD_DESCRIPTION = "Interactive dashboard for industrial IoT sensor data"
IOT_DASHBOARD_MIME_TYPE = "text/html;profile=mcp-app"
IOT_TOOL_METADATA = '{"ui": {"resourceUri": "ui://iot/index.html"}}'
IOT_RESOURCE_METADATA = '{"ui": {"prefersBorder": true}}'
@app.mcp_tool()
def hello_mcp() -> str:
"""Hello world."""
return "Hello I am MCPTool!"
@app.mcp_tool()
@app.mcp_tool_property(arg_name="snippetname", description="The name of the snippet.")
@app.blob_input(arg_name="file", connection="AzureWebJobsStorage", path=_BLOB_PATH)
def get_snippet(file: func.InputStream, snippetname: str) -> str:
"""Retrieve a snippet by name from Azure Blob Storage."""
snippet_content = file.read().decode("utf-8")
logging.info(f"Retrieved snippet: {snippet_content}")
return snippet_content
@app.mcp_tool()
@app.mcp_tool_property(arg_name="snippetname", description="The name of the snippet.")
@app.mcp_tool_property(arg_name="snippet", description="The content of the snippet.")
@app.blob_output(arg_name="file", connection="AzureWebJobsStorage", path=_BLOB_PATH)
def save_snippet(file: func.Out[str], snippetname: str, snippet: str) -> str:
"""Save a snippet with a name to Azure Blob Storage."""
if not snippetname:
return "No snippet name provided"
if not snippet:
return "No snippet content provided"
file.set(snippet)
logging.info(f"Saved snippet: {snippet}")
return f"Snippet '{snippet}' saved successfully"
# Weather Widget Resource - returns HTML content for the weather widget
@app.mcp_resource_trigger(
arg_name="context",
uri=WEATHER_WIDGET_URI,
resource_name=WEATHER_WIDGET_NAME,
description=WEATHER_WIDGET_DESCRIPTION,
mime_type=WEATHER_WIDGET_MIME_TYPE,
metadata=RESOURCE_METADATA
)
def get_weather_widget(context) -> str:
"""Get the weather widget HTML content."""
logging.info("Getting weather widget")
try:
# Get the path to the widget HTML file
# Current file is src/function_app.py, look for src/app/index.html
current_dir = Path(__file__).parent
file_path = current_dir / "app" / "dist" / "index.html"
if file_path.exists():
return file_path.read_text(encoding="utf-8")
else:
logging.warning(f"Weather widget file not found at: {file_path}")
# Return a fallback HTML if file not found
return """<!DOCTYPE html>
<html>
<head><title>Weather Widget</title></head>
<body>
<h1>Weather Widget</h1>
<p>Widget content not found. Please ensure the app/index.html file exists.</p>
</body>
</html>"""
except Exception as e:
logging.error(f"Error reading weather widget file: {e}")
return """<!DOCTYPE html>
<html>
<head><title>Weather Widget Error</title></head>
<body>
<h1>Weather Widget</h1>
<p>Error loading widget content.</p>
</body>
</html>"""
# Get Weather Tool - returns current weather for a location
@app.mcp_tool(metadata=TOOL_METADATA)
@app.mcp_tool_property(arg_name="location", description="City name to check weather for (e.g., Seattle, New York, Miami)")
def get_weather(location: str) -> Dict[str, Any]:
"""Returns current weather for a location via Open-Meteo."""
logging.info(f"Getting weather for location: {location}")
try:
result = weather_service.get_current_weather(location)
if "TemperatureC" in result:
logging.info(f"Weather fetched for {result['Location']}: {result['TemperatureC']}°C")
else:
logging.warning(f"Weather error for {result['Location']}: {result.get('Error', 'Unknown error')}")
return json.dumps(result)
except Exception as e:
logging.error(f"Failed to get weather for {location}: {e}")
return json.dumps({
"Location": location or "Unknown",
"Error": f"Unable to fetch weather: {str(e)}",
"Source": "api.open-meteo.com"
})
# ---------------------------------------------------------------------------
# IoT Tools
# ---------------------------------------------------------------------------
# IoT Dashboard Resource - returns HTML content for the IoT dashboard widget
@app.mcp_resource_trigger(
arg_name="context",
uri=IOT_DASHBOARD_URI,
resource_name=IOT_DASHBOARD_NAME,
description=IOT_DASHBOARD_DESCRIPTION,
mime_type=IOT_DASHBOARD_MIME_TYPE,
metadata=IOT_RESOURCE_METADATA
)
def get_iot_dashboard(context) -> str:
"""Get the IoT sensor dashboard HTML content."""
logging.info("Getting IoT dashboard")
try:
current_dir = Path(__file__).parent
file_path = current_dir / "iot-app" / "dist" / "index.html"
if file_path.exists():
return file_path.read_text(encoding="utf-8")
else:
logging.warning(f"IoT dashboard file not found at: {file_path}")
return """<!DOCTYPE html>
<html>
<head><title>IoT Dashboard</title></head>
<body>
<h1>IoT Sensor Dashboard</h1>
<p>Dashboard not found. Run <code>cd src/iot-app && npm install && npm run build</code> first.</p>
</body>
</html>"""
except Exception as e:
logging.error(f"Error reading IoT dashboard file: {e}")
return """<!DOCTYPE html>
<html>
<head><title>IoT Dashboard Error</title></head>
<body>
<h1>IoT Sensor Dashboard</h1>
<p>Error loading dashboard content.</p>
</body>
</html>"""
@app.mcp_tool()
@app.mcp_tool_property(
arg_name="device_id",
description="Unique identifier of the IoT device (e.g. sensor-industrial-001).",
property_type=func.McpPropertyType.STRING
)
@app.mcp_tool_property(
arg_name="location",
description="Physical location of the device (e.g. Plant-A / Zone-3 / Line-2).",
property_type=func.McpPropertyType.STRING
)
@app.mcp_tool_property(
arg_name="timestamp",
description="ISO 8601 timestamp of the reading (e.g. 2026-03-19T10:30:00Z).",
property_type=func.McpPropertyType.DATETIME
)
@app.mcp_tool_property(
arg_name="firmware_version",
description="Firmware version of the device using semver (e.g. 2.4.1).",
property_type=func.McpPropertyType.STRING
)
@app.mcp_tool_property(
arg_name="status",
description="Operational status of the device: operational | degraded | offline | maintenance.",
property_type=func.McpPropertyType.STRING
)
@app.mcp_tool_property(
arg_name="tags",
description='Labels associated with the device (e.g. "critical", "area-b").',
property_type=func.McpPropertyType.STRING,
as_array=True
)
@app.mcp_tool_property(
arg_name="metrics",
description=(
"Current physical measurements: "
"temperature_celsius (float), humidity_percent (float 0-100), "
"pressure_bar (float > 0), vibration_hz (float >= 0), power_consumption_kw (float >= 0)."
),
property_type=func.McpPropertyType.OBJECT
)
@app.mcp_tool_property(
arg_name="alerts",
description=(
"Alert objects. Each must have: "
"code (string), severity (info|warning|critical), message (string), "
"triggered_at (ISO 8601 string), resolved (boolean)."
),
property_type=func.McpPropertyType.OBJECT,
as_array=True
)
@app.mcp_tool_property(
arg_name="maintenance",
description=(
"Maintenance info: "
"last_service_date (YYYY-MM-DD), next_service_date (YYYY-MM-DD), "
"technician (string), notes (string)."
),
property_type=func.McpPropertyType.OBJECT
)
@app.mcp_tool_property(
arg_name="network",
description=(
"Network info: "
"ip_address (string), protocol (string, e.g. MQTT), "
"signal_strength_dbm (int <= 0), connected_gateway (string)."
),
property_type=func.McpPropertyType.OBJECT
)
@app.mcp_tool_property(
arg_name="history_last_5_readings",
description="Last temperature readings (up to 10 values), oldest first.",
property_type=func.McpPropertyType.FLOAT,
as_array=True
)
@app.blob_output(arg_name="file", connection="AzureWebJobsStorage", path=_IOT_BLOB_PATH)
def ingest_sensor_data(
file: func.Out[str],
device_id: str,
location: str,
timestamp: str,
firmware_version: str,
status: str,
tags: str,
metrics: str,
alerts: str,
maintenance: str,
network: str,
history_last_5_readings: str,
) -> str:
"""Validate and save an IoT sensor payload to Azure Blob Storage."""
if not device_id:
return "Error: device_id not provided."
try:
data = {
"device_id": device_id,
"location": location,
"timestamp": timestamp,
"firmware_version": firmware_version,
"status": status,
"tags": tags,
"metrics": metrics,
"alerts": alerts if alerts else [],
"maintenance": maintenance if maintenance else {},
"network": network,
"history_last_5_readings": history_last_5_readings if history_last_5_readings else [],
}
except json.JSONDecodeError as e:
return f"Error: one or more JSON fields are invalid. Detail: {e}"
try:
sensor = iot_service.validate_payload(data)
except Exception as e:
return f"Payload validation error: {e}"
file.set(sensor.to_json())
logging.info(f"IoT payload saved for device '{device_id}'")
return f"Payload for device '{device_id}' saved successfully."
@app.mcp_tool(metadata=IOT_TOOL_METADATA)
@app.mcp_tool_property(
arg_name="device_id",
description="Unique identifier of the IoT device to generate the report for."
)
@app.blob_input(arg_name="file", connection="AzureWebJobsStorage", path=_IOT_BLOB_PATH)
def get_sensor_report(file: func.InputStream, device_id: str) -> str:
"""Read IoT sensor data from Azure Blob Storage and return a full report
with health score, metrics, active alerts, and maintenance information."""
if not device_id:
return json.dumps({"error": "device_id not provided."})
try:
raw = file.read().decode("utf-8")
except Exception as e:
logging.error(f"Error reading blob for device '{device_id}': {e}")
return json.dumps({"error": f"Device '{device_id}' not found or read error."})
try:
data = json.loads(raw)
sensor = iot_service.validate_payload(data)
report = iot_service.generate_report(sensor)
logging.info(f"Report generated for device '{device_id}'")
return json.dumps(report)
except Exception as e:
logging.error(f"Error generating report for '{device_id}': {e}")
return json.dumps({"error": str(e)})
@app.mcp_tool()
@app.mcp_tool_property(
arg_name="device_id",
description="Unique identifier of the IoT device whose active alerts should be listed."
)
@app.blob_input(arg_name="file", connection="AzureWebJobsStorage", path=_IOT_BLOB_PATH)
def list_active_alerts(file: func.InputStream, device_id: str) -> str:
"""Read IoT sensor data from Azure Blob Storage and return
only the alerts that have not yet been resolved (resolved=false)."""
if not device_id:
return json.dumps({"error": "device_id not provided."})
try:
raw = file.read().decode("utf-8")
except Exception as e:
logging.error(f"Error reading blob for device '{device_id}': {e}")
return json.dumps({"error": f"Device '{device_id}' not found or read error."})
try:
data = json.loads(raw)
sensor = iot_service.validate_payload(data)
alerts = iot_service.get_active_alerts(sensor)
result = {
"device_id": device_id,
"active_alerts_count": len(alerts),
"active_alerts": alerts,
}
logging.info(f"Active alerts for device '{device_id}': {len(alerts)}")
return json.dumps(result)
except Exception as e:
logging.error(f"Error retrieving alerts for '{device_id}': {e}")
return json.dumps({"error": str(e)})
@app.mcp_tool()
@app.mcp_tool_property(
arg_name="device_id",
description="Unique identifier of the IoT device to compute the metrics summary for."
)
@app.blob_input(arg_name="file", connection="AzureWebJobsStorage", path=_IOT_BLOB_PATH)
def get_sensor_metrics_summary(file: func.InputStream, device_id: str) -> str:
"""Read IoT sensor data from Azure Blob Storage and return a statistical
summary of the current metrics and reading history
(min, max, average, standard deviation, trend)."""
if not device_id:
return json.dumps({"error": "device_id not provided."})
try:
raw = file.read().decode("utf-8")
except Exception as e:
logging.error(f"Error reading blob for device '{device_id}': {e}")
return json.dumps({"error": f"Device '{device_id}' not found or read error."})
try:
data = json.loads(raw)
sensor = iot_service.validate_payload(data)
summary = iot_service.get_metrics_summary(sensor)
logging.info(f"Metrics computed for device '{device_id}'")
return json.dumps(summary)
except Exception as e:
logging.error(f"Error computing metrics for '{device_id}': {e}")
return json.dumps({"error": str(e)})