UNPKG

mcp-server-ddd-sample

Version:

A sample of MCP implementation using DDD structure with some APIs call.

64 lines (63 loc) 2.69 kB
export class WeatherService { apiService; constructor(apiService) { this.apiService = apiService; } // Format alert data formatAlert(feature) { const props = feature.properties; return [ `Event: ${props.event || "Unknown"}`, `Area: ${props.areaDesc || "Unknown"}`, `Severity: ${props.severity || "Unknown"}`, `Status: ${props.status || "Unknown"}`, `Headline: ${props.headline || "No headline"}`, "---", ].join("\n"); } // Format forecast period formatForecastPeriod(period) { return [ `${period.name || "Unknown"}:`, `Temperature: ${period.temperature || "Unknown"}°${period.temperatureUnit || "F"}`, `Wind: ${period.windSpeed || "Unknown"} ${period.windDirection || ""}`, `${period.shortForecast || "No forecast available"}`, "---", ].join("\n"); } async getAlertsForState(stateCode) { const alertsData = await this.apiService.getAlerts(stateCode.toUpperCase()); if (!alertsData) { return "Failed to retrieve alerts data"; } const features = alertsData.features || []; if (features.length === 0) { return `No active alerts for ${stateCode.toUpperCase()}`; } const formattedAlerts = features.map((feature) => this.formatAlert(feature)); return `Active alerts for ${stateCode.toUpperCase()}:\n\n${formattedAlerts.join("\n")}`; } async getForecastForLocation(latitude, longitude) { // Get grid point data const pointsData = await this.apiService.getPoints(latitude, longitude); if (!pointsData) { return `Failed to retrieve grid point data for coordinates: ${latitude}, ${longitude}. This location may not be supported by the NWS API (only US locations are supported).`; } const forecastUrl = pointsData.properties?.forecast; if (!forecastUrl) { return "Failed to get forecast URL from grid point data"; } // Get forecast data const forecastData = await this.apiService.getForecast(forecastUrl); if (!forecastData) { return "Failed to retrieve forecast data"; } const periods = forecastData.properties?.periods || []; if (periods.length === 0) { return "No forecast periods available"; } // Format forecast periods const formattedForecast = periods.map((period) => this.formatForecastPeriod(period)); return `Forecast for ${latitude}, ${longitude}:\n\n${formattedForecast.join("\n")}`; } }