|
| 1 | +using ModelContextProtocol; |
| 2 | +using ModelContextProtocol.Server; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Globalization; |
| 5 | +using System.Text.Json; |
| 6 | + |
| 7 | +namespace AspNetCoreMcpServer.Tools; |
| 8 | + |
| 9 | +[McpServerToolType] |
| 10 | +public sealed class WeatherTools |
| 11 | +{ |
| 12 | + private readonly IHttpClientFactory _httpClientFactory; |
| 13 | + |
| 14 | + public WeatherTools(IHttpClientFactory httpClientFactory) |
| 15 | + { |
| 16 | + _httpClientFactory = httpClientFactory; |
| 17 | + } |
| 18 | + |
| 19 | + [McpServerTool, Description("Get weather alerts for a US state.")] |
| 20 | + public async Task<string> GetAlerts( |
| 21 | + [Description("The US state to get alerts for. Use the 2 letter abbreviation for the state (e.g. NY).")] string state) |
| 22 | + { |
| 23 | + var client = _httpClientFactory.CreateClient("WeatherApi"); |
| 24 | + using var responseStream = await client.GetStreamAsync($"/alerts/active/area/{state}"); |
| 25 | + using var jsonDocument = await JsonDocument.ParseAsync(responseStream) |
| 26 | + ?? throw new McpException("No JSON returned from alerts endpoint"); |
| 27 | + |
| 28 | + var alerts = jsonDocument.RootElement.GetProperty("features").EnumerateArray(); |
| 29 | + |
| 30 | + if (!alerts.Any()) |
| 31 | + { |
| 32 | + return "No active alerts for this state."; |
| 33 | + } |
| 34 | + |
| 35 | + return string.Join("\n--\n", alerts.Select(alert => |
| 36 | + { |
| 37 | + JsonElement properties = alert.GetProperty("properties"); |
| 38 | + return $""" |
| 39 | + Event: {properties.GetProperty("event").GetString()} |
| 40 | + Area: {properties.GetProperty("areaDesc").GetString()} |
| 41 | + Severity: {properties.GetProperty("severity").GetString()} |
| 42 | + Description: {properties.GetProperty("description").GetString()} |
| 43 | + Instruction: {properties.GetProperty("instruction").GetString()} |
| 44 | + """; |
| 45 | + })); |
| 46 | + } |
| 47 | + |
| 48 | + [McpServerTool, Description("Get weather forecast for a location.")] |
| 49 | + public async Task<string> GetForecast( |
| 50 | + [Description("Latitude of the location.")] double latitude, |
| 51 | + [Description("Longitude of the location.")] double longitude) |
| 52 | + { |
| 53 | + var client = _httpClientFactory.CreateClient("WeatherApi"); |
| 54 | + var pointUrl = string.Create(CultureInfo.InvariantCulture, $"/points/{latitude},{longitude}"); |
| 55 | + |
| 56 | + using var locationResponseStream = await client.GetStreamAsync(pointUrl); |
| 57 | + using var locationDocument = await JsonDocument.ParseAsync(locationResponseStream); |
| 58 | + var forecastUrl = locationDocument?.RootElement.GetProperty("properties").GetProperty("forecast").GetString() |
| 59 | + ?? throw new McpException($"No forecast URL provided by {client.BaseAddress}points/{latitude},{longitude}"); |
| 60 | + |
| 61 | + using var forecastResponseStream = await client.GetStreamAsync(forecastUrl); |
| 62 | + using var forecastDocument = await JsonDocument.ParseAsync(forecastResponseStream); |
| 63 | + var periods = forecastDocument?.RootElement.GetProperty("properties").GetProperty("periods").EnumerateArray() |
| 64 | + ?? throw new McpException("No JSON returned from forecast endpoint"); |
| 65 | + |
| 66 | + return string.Join("\n---\n", periods.Select(period => $""" |
| 67 | + {period.GetProperty("name").GetString()} |
| 68 | + Temperature: {period.GetProperty("temperature").GetInt32()}°F |
| 69 | + Wind: {period.GetProperty("windSpeed").GetString()} {period.GetProperty("windDirection").GetString()} |
| 70 | + Forecast: {period.GetProperty("detailedForecast").GetString()} |
| 71 | + """)); |
| 72 | + } |
| 73 | +} |
0 commit comments