File size: 24,928 Bytes
d2a1db5 |
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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
import os
import logging
import random
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from dotenv import dotenv_values
import asyncio
from datetime import datetime, timedelta
import csv
from io import StringIO
from tools import open_meteo, tomorrow_io, google_weather, openweathermap, accuweather, openai_llm, geographic_tools, crop_calendar_tools, alert_generation_tools
from a2a_agents import sms_agent, whatsapp_agent, ussd_agent, ivr_agent, telegram_agent
from utils.weather_utils import get_tool_config
config = dotenv_values(".env")
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO")
logging.basicConfig(level=LOG_LEVEL)
logger = logging.getLogger(__name__)
app = FastAPI()
# CORS middleware for frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["https://mcp-ui.vercel.app"],
allow_credentials=True,
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
allow_headers=["*"],
expose_headers=["*"]
)
class MCPRequest(BaseModel):
tool: str
parameters: dict
class AlertRequest(BaseModel):
alert_json: dict
class WorkflowRequest(BaseModel):
state: str
district: str
def get_regional_crop_for_area(district: str, state: str):
"""Get typical crop for the region"""
if state.lower() == 'bihar':
district_crops = {
'patna': 'rice',
'gaya': 'wheat',
'bhagalpur': 'rice',
'muzaffarpur': 'sugarcane',
'darbhanga': 'rice',
'siwan': 'rice',
'begusarai': 'rice',
'katihar': 'maize',
}
return district_crops.get(district.lower(), 'rice')
return 'rice'
def get_current_crop_stage(crop: str):
"""Determine crop stage based on current date"""
current_month = datetime.now().month
if crop == 'rice':
if current_month in [6, 7]:
return 'planting'
elif current_month in [8, 9]:
return 'growing'
elif current_month in [10, 11]:
return 'flowering'
else:
return 'harvesting'
elif crop == 'wheat':
if current_month in [11, 12]:
return 'planting'
elif current_month in [1, 2]:
return 'growing'
elif current_month in [3, 4]:
return 'flowering'
else:
return 'harvesting'
elif crop == 'sugarcane':
if current_month in [2, 3, 4]:
return 'planting'
elif current_month in [5, 6, 7, 8]:
return 'growing'
elif current_month in [9, 10, 11]:
return 'maturing'
else:
return 'harvesting'
elif crop == 'maize':
if current_month in [6, 7]:
return 'planting'
elif current_month in [8, 9]:
return 'growing'
elif current_month in [10, 11]:
return 'flowering'
else:
return 'harvesting'
return 'growing'
async def generate_dynamic_alert(district: str, state: str):
"""Generate dynamic alert data using geographic functions and REAL weather data"""
try:
# Get villages for the district
villages_data = await geographic_tools.list_villages(state, district)
# Pick a random village or use default
village_name = f"Village in {district}"
if "villages" in villages_data and villages_data["villages"]:
village_name = random.choice(villages_data["villages"])
# Avoid village name being same as district
if village_name.lower() == district.lower() and len(villages_data["villages"]) > 1:
other_villages = [v for v in villages_data["villages"] if v.lower() != district.lower()]
if other_villages:
village_name = random.choice(other_villages)
# Get coordinates for the district/village
location_coords = [25.5941, 85.1376] # Default to Patna
# Try to get coordinates for the district first
try:
district_location = await geographic_tools.reverse_geocode(district)
if "error" not in district_location and "lat" in district_location:
location_coords = [district_location["lat"], district_location["lng"]]
except:
pass # Keep default coordinates
# Generate regional crop and stage
regional_crop = get_regional_crop_for_area(district, state)
crop_stage = get_current_crop_stage(regional_crop)
# GET WEATHER DATA
try:
current_weather_data = await open_meteo.get_current_weather(
latitude=location_coords[0],
longitude=location_coords[1]
)
forecast_data = await open_meteo.get_weather_forecast(
latitude=location_coords[0],
longitude=location_coords[1],
days=7
)
current_weather = current_weather_data.get('current_weather', {})
daily_forecast = forecast_data.get('daily', {})
current_temp = current_weather.get('temperature', 25)
current_windspeed = current_weather.get('windspeed', 10)
precipitation_list = daily_forecast.get('precipitation_sum', [0, 0, 0])
next_3_days_rain = sum(precipitation_list[:3]) if precipitation_list else 0
rain_probability = min(90, max(10, int(next_3_days_rain * 10))) if next_3_days_rain > 0 else 10
# Higher precipitation = higher humidity estimate
estimated_humidity = min(95, max(40, 60 + int(next_3_days_rain * 2)))
real_weather = {
"forecast_days": 3,
"rain_probability": rain_probability,
"expected_rainfall": f"{next_3_days_rain:.1f}mm",
"temperature": f"{current_temp:.1f}Β°C",
"humidity": f"{estimated_humidity}%",
"wind_speed": f"{current_windspeed:.1f} km/h"
}
# Generate alert message based on weather conditions
if next_3_days_rain > 25:
alert_type = "heavy_rain_warning"
urgency = "high"
alert_message = f"Heavy rainfall ({next_3_days_rain:.1f}mm) expected in next 3 days in {district}. Delay fertilizer application. Ensure proper drainage."
action_items = ["delay_fertilizer", "check_drainage", "monitor_crops", "prepare_harvest_protection"]
elif next_3_days_rain > 10:
alert_type = "moderate_rain_warning"
urgency = "medium"
alert_message = f"Moderate rainfall ({next_3_days_rain:.1f}mm) expected in next 3 days in {district}. Monitor soil moisture levels."
action_items = ["monitor_soil", "check_drainage", "adjust_irrigation"]
elif next_3_days_rain < 2 and current_temp > 35:
alert_type = "heat_drought_warning"
urgency = "high"
alert_message = f"High temperature ({current_temp:.1f}Β°C) with minimal rainfall expected in {district}. Increase irrigation frequency."
action_items = ["increase_irrigation", "mulch_crops", "monitor_plant_stress"]
else:
alert_type = "weather_update"
urgency = "low"
alert_message = f"Normal weather conditions expected in {district}. Temperature {current_temp:.1f}Β°C, rainfall {next_3_days_rain:.1f}mm."
action_items = ["routine_monitoring", "maintain_irrigation"]
logger.info(f"Real weather data retrieved for {district}: {current_temp}Β°C, {next_3_days_rain:.1f}mm rain")
except Exception as weather_error:
logger.error(f"Failed to get real weather data for {district}: {weather_error}")
raise Exception(f"Unable to retrieve current weather conditions for {district}")
return {
"alert_id": f"{state.upper()[:2]}_{district.upper()[:3]}_001_{datetime.now().strftime('%Y%m%d')}",
"timestamp": datetime.now().isoformat() + "Z",
"location": {
"village": village_name,
"district": district,
"state": state.capitalize(),
"coordinates": location_coords
},
"crop": {
"name": regional_crop,
"stage": crop_stage,
"planted_estimate": "2025-06-15"
},
"alert": {
"type": alert_type,
"urgency": urgency,
"message": alert_message,
"action_items": action_items,
"valid_until": (datetime.now() + timedelta(days=3)).isoformat() + "Z"
},
"weather": real_weather,
"data_source": "open_meteo_api"
}
except Exception as e:
logger.error(f"Error generating dynamic alert for {district}, {state}: {e}")
raise Exception(f"Failed to generate weather alert for {district}: {str(e)}")
@app.get("/")
async def root():
return {"message": "MCP Weather Server is running"}
@app.get("/api/health")
async def health_check():
return {"status": "healthy", "message": "API is working"}
# workflow endpoint for frontend
@app.post("/api/run-workflow")
async def run_workflow(request: WorkflowRequest):
logger.info(f"Received workflow request: {request.state}, {request.district}")
# Initialize variables
sample_alert = None
csv_content = ""
try:
# Create comprehensive workflow response
workflow_results = []
# Add workflow header
workflow_results.append(f"Workflow for {request.district}, {request.state}")
workflow_results.append("=" * 50)
# weather data collection
workflow_results.append("\nπ€οΈ Weather Data Collection")
workflow_results.append("-" * 30)
workflow_results.append("π‘ Fetching real-time weather data...")
try:
sample_alert = await generate_dynamic_alert(request.district, request.state)
workflow_results.append("β
Current weather data retrieved from Open-Meteo API")
workflow_results.append("β
7-day forecast collected")
workflow_results.append("β
Agricultural indices calculated")
except Exception as weather_error:
logger.error(f"Weather data error: {weather_error}")
workflow_results.append(f"β Weather data collection failed: {str(weather_error)}")
return {
"message": "\n".join(workflow_results),
"status": "error",
"csv": "",
"error": f"Unable to retrieve weather data: {str(weather_error)}"
}
if not sample_alert:
return {
"message": "Failed to generate alert data",
"status": "error",
"csv": "",
"error": "Alert generation failed"
}
# Alert generation
workflow_results.append("\nπ¨ Alert Generation")
workflow_results.append("-" * 30)
workflow_results.append("β
Weather alerts generated")
workflow_results.append(f" - Data Source: {sample_alert.get('data_source', 'API')}")
workflow_results.append(f" - Alert Type: {sample_alert['alert']['type']}")
workflow_results.append(f" - Severity: {sample_alert['alert']['urgency']}")
workflow_results.append(f" - Village: {sample_alert['location']['village']}")
workflow_results.append(f" - Coordinates: {sample_alert['location']['coordinates']}")
workflow_results.append(f" - Crop: {sample_alert['crop']['name']} ({sample_alert['crop']['stage']})")
workflow_results.append(f" - Temperature: {sample_alert['weather']['temperature']}")
workflow_results.append(f" - Humidity: {sample_alert['weather']['humidity']}")
workflow_results.append(f" - Expected Rainfall: {sample_alert['weather']['expected_rainfall']}")
workflow_results.append(f" - Rain Probability: {sample_alert['weather']['rain_probability']}%")
# WhatsApp Agent Response
workflow_results.append("\nπ± WhatsApp Agent Response")
workflow_results.append("-" * 30)
try:
whatsapp_message = whatsapp_agent.create_whatsapp_message(sample_alert)
workflow_results.append(f"β
Message created successfully")
workflow_results.append(f"Text: {whatsapp_message.get('text', 'N/A')}")
if 'buttons' in whatsapp_message:
workflow_results.append(f"Buttons: {len(whatsapp_message['buttons'])} button(s)")
except Exception as e:
workflow_results.append(f"β Error: {str(e)}")
# SMS Agent Response
workflow_results.append("\nπ± SMS Agent Response")
workflow_results.append("-" * 30)
try:
sms_message = sms_agent.create_sms_message(sample_alert)
workflow_results.append(f"β
SMS created successfully")
workflow_results.append(f"Content: {str(sms_message)}")
except Exception as e:
workflow_results.append(f"β Error: {str(e)}")
# USSD Agent Response
workflow_results.append("\nπ USSD Agent Response")
workflow_results.append("-" * 30)
try:
ussd_menu = ussd_agent.create_ussd_menu(sample_alert)
workflow_results.append(f"β
USSD menu created successfully")
workflow_results.append(f"Menu: {str(ussd_menu)}")
except Exception as e:
workflow_results.append(f"β Error: {str(e)}")
# IVR Agent Response
workflow_results.append("\nποΈ IVR Agent Response")
workflow_results.append("-" * 30)
try:
ivr_script = ivr_agent.create_ivr_script(sample_alert)
workflow_results.append(f"β
IVR script created successfully")
workflow_results.append(f"Script: {str(ivr_script)}")
except Exception as e:
workflow_results.append(f"β Error: {str(e)}")
# Telegram Agent Response
workflow_results.append("\nπ€ Telegram Agent Response")
workflow_results.append("-" * 30)
try:
telegram_message = telegram_agent.create_telegram_message(sample_alert)
workflow_results.append(f"β
Telegram message created successfully")
workflow_results.append(f"Content: {str(telegram_message)}")
except Exception as e:
workflow_results.append(f"β Error: {str(e)}")
# Summary
workflow_results.append("\nβ
Workflow Summary")
workflow_results.append("-" * 30)
workflow_results.append("Workflow execution completed with REAL weather data")
workflow_results.append(f"Location: {request.district}, {request.state}")
workflow_results.append(f"Weather Source: Open-Meteo API")
workflow_results.append(f"Timestamp: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
# Join all results into a single formatted string
formatted_output = "\n".join(workflow_results)
# Generate CSV
try:
csv_buffer = StringIO()
writer = csv.writer(csv_buffer)
# Write headers
headers = ["weather data", "whatsapp", "sms", "ussd", "ivr", "telegram"]
writer.writerow(headers)
# Prepare weather data as a single string with line breaks
weather_info = "\n".join([
f" - Data Source: {sample_alert.get('data_source', 'API')}",
f" - Alert Type: {sample_alert['alert']['type']}",
f" - Severity: {sample_alert['alert']['urgency']}",
f" - Village: {sample_alert['location']['village']}",
f" - Coordinates: {sample_alert['location']['coordinates']}",
f" - Crop: {sample_alert['crop']['name']} ({sample_alert['crop']['stage']})",
f" - Temperature: {sample_alert['weather']['temperature']}",
f" - Humidity: {sample_alert['weather']['humidity']}",
f" - Expected Rainfall: {sample_alert['weather']['expected_rainfall']}",
f" - Rain Probability: {sample_alert['weather']['rain_probability']}%"
])
weather_data = [weather_info]
# Extract agent outputs only (no status messages)
whatsapp_data = []
sms_data = []
ussd_data = []
ivr_data = []
telegram_data = []
# Get WhatsApp message
try:
whatsapp_message = whatsapp_agent.create_whatsapp_message(sample_alert)
whatsapp_text = whatsapp_message.get('text', 'N/A')
whatsapp_data.append(whatsapp_text)
if 'buttons' in whatsapp_message and whatsapp_message['buttons']:
whatsapp_data.append(f"Buttons: {whatsapp_message['buttons']}")
except Exception as e:
whatsapp_data.append(f"Error: {str(e)}")
# Get SMS message
try:
sms_message = sms_agent.create_sms_message(sample_alert)
sms_data.append(str(sms_message))
except Exception as e:
sms_data.append(f"Error: {str(e)}")
# Get USSD menu
try:
ussd_menu = ussd_agent.create_ussd_menu(sample_alert)
ussd_data.append(str(ussd_menu))
except Exception as e:
ussd_data.append(f"Error: {str(e)}")
# Get IVR script
try:
ivr_script = ivr_agent.create_ivr_script(sample_alert)
ivr_data.append(str(ivr_script))
except Exception as e:
ivr_data.append(f"Error: {str(e)}")
# Get Telegram message
try:
telegram_message = telegram_agent.create_telegram_message(sample_alert)
telegram_data.append(str(telegram_message))
except Exception as e:
telegram_data.append(f"Error: {str(e)}")
# Find the maximum number of rows needed
max_rows = max(
len(weather_data),
len(whatsapp_data) if whatsapp_data else 1,
len(sms_data) if sms_data else 1,
len(ussd_data) if ussd_data else 1,
len(ivr_data) if ivr_data else 1,
len(telegram_data) if telegram_data else 1
)
# Write data rows
for i in range(max_rows):
row = [
weather_data[i] if i < len(weather_data) else "",
whatsapp_data[i] if i < len(whatsapp_data) else "",
sms_data[i] if i < len(sms_data) else "",
ussd_data[i] if i < len(ussd_data) else "",
ivr_data[i] if i < len(ivr_data) else "",
telegram_data[i] if i < len(telegram_data) else ""
]
writer.writerow(row)
csv_content = csv_buffer.getvalue()
logger.info("CSV content generated successfully")
except Exception as csv_error:
logger.error(f"Error generating CSV: {csv_error}")
csv_content = f"Error generating CSV: {str(csv_error)}"
logger.info(f"Successfully completed workflow for {request.district}, {request.state}")
return {
"message": formatted_output,
"status": "success",
"csv": csv_content,
"raw_data": {
"state": request.state,
"district": request.district,
"alert_data": sample_alert
}
}
except Exception as e:
logger.exception(f"Error in workflow for {request.district}, {request.state}")
return {
"message": f"Error running workflow: {str(e)}",
"status": "error",
"csv": "",
"error": str(e)
}
@app.post("/mcp")
async def mcp_endpoint(request: MCPRequest):
logger.info(f"Received request for tool: {request.tool}")
tool_config = get_tool_config(request.tool)
if not tool_config:
logger.error(f"Tool not found: {request.tool}")
raise HTTPException(status_code=404, detail="Tool not found")
try:
if tool_config["module"] == "open_meteo":
result = await getattr(open_meteo, request.tool)(**request.parameters)
elif tool_config["module"] == "tomorrow_io":
api_key = config.get("TOMORROW_IO_API_KEY")
result = await getattr(tomorrow_io, request.tool)(**request.parameters, api_key=api_key)
elif tool_config["module"] == "google_weather":
api_key = config.get("GOOGLE_WEATHER_API_KEY")
result = await getattr(google_weather, request.tool)(**request.parameters, api_key=api_key)
elif tool_config["module"] == "openweathermap":
api_key = config.get("OPENWEATHERMAP_API_KEY")
result = await getattr(openweathermap, request.tool)(**request.parameters, api_key=api_key)
elif tool_config["module"] == "accuweather":
api_key = config.get("ACCUWEATHER_API_KEY")
result = await getattr(accuweather, request.tool)(**request.parameters, api_key=api_key)
elif tool_config["module"] == "openai_llm":
api_key = config.get("OPENAI_API_KEY")
result = await getattr(openai_llm, request.tool)(**request.parameters, api_key=api_key)
elif tool_config["module"] == "geographic_tools":
result = await getattr(geographic_tools, request.tool)(**request.parameters)
elif tool_config["module"] == "crop_calendar_tools":
result = await getattr(crop_calendar_tools, request.tool)(**request.parameters)
elif tool_config["module"] == "alert_generation_tools":
api_key = config.get("OPENAI_API_KEY")
result = await getattr(alert_generation_tools, request.tool)(**request.parameters, api_key=api_key)
else:
raise HTTPException(status_code=500, detail="Invalid tool module")
logger.info(f"Successfully executed tool: {request.tool}")
return result
except Exception as e:
logger.exception(f"Error executing tool: {request.tool}")
raise HTTPException(status_code=500, detail=str(e))
@app.post("/a2a/sms")
async def a2a_sms_endpoint(request: AlertRequest):
return {"message": sms_agent.create_sms_message(request.alert_json)}
@app.post("/a2a/whatsapp")
async def a2a_whatsapp_endpoint(request: AlertRequest):
return whatsapp_agent.create_whatsapp_message(request.alert_json)
@app.post("/a2a/ussd")
async def a2a_ussd_endpoint(request: AlertRequest):
return {"menu": ussd_agent.create_ussd_menu(request.alert_json)}
@app.post("/a2a/ivr")
async def a2a_ivr_endpoint(request: AlertRequest):
return {"script": ivr_agent.create_ivr_script(request.alert_json)}
@app.post("/a2a/telegram")
async def a2a_telegram_endpoint(request: AlertRequest):
return telegram_agent.create_telegram_message(request.alert_json)
# for smithery + context7
@app.post("/mcp")
async def mcp_rpc_handler(request: dict):
method = request.get("method")
params = request.get("params", {})
tool_name = params.get("tool_name")
arguments = params.get("arguments", {})
req_id = request.get("id")
# Handle run_workflow tool
if method == "call_tool" and tool_name == "run_workflow":
state = arguments.get("state")
district = arguments.get("district")
result = await run_workflow(WorkflowRequest(state=state, district=district))
return {"jsonrpc": "2.0", "result": result, "id": req_id}
# Handle other tools dynamically via your tool config
if method == "call_tool":
try:
result = await mcp_endpoint(MCPRequest(tool=tool_name, parameters=arguments))
return {"jsonrpc": "2.0", "result": result, "id": req_id}
except Exception as e:
return {"jsonrpc": "2.0", "error": {"code": -32000, "message": str(e)}, "id": req_id}
return {"jsonrpc": "2.0", "error": {"code": -32601, "message": "Unknown method"}, "id": req_id}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) |