|
1 | 1 | using ModelContextProtocol; |
2 | 2 | using ModelContextProtocol.Server; |
3 | 3 | using System.ComponentModel; |
4 | | -using System.Net.Http.Json; |
5 | 4 | using System.Text.Json; |
6 | 5 |
|
7 | 6 | namespace QuickstartWeatherServer.Tools; |
@@ -43,22 +42,17 @@ public static async Task<string> GetForecast( |
43 | 42 | [Description("Longitude of the location.")] double longitude) |
44 | 43 | { |
45 | 44 | using var jsonDocument = await client.ReadJsonDocumentAsync($"/points/{latitude},{longitude}"); |
46 | | - var jsonElement = jsonDocument.RootElement; |
47 | | - var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray(); |
| 45 | + var properties = jsonDocument.RootElement.GetProperty("properties"); |
| 46 | + var forecastUrl = properties.GetProperty("forecast").GetString(); |
48 | 47 |
|
49 | | - return string.Join("\n---\n", |
50 | | - periods.Select(period => |
51 | | - { |
52 | | - return $""" |
53 | | - Name: {period.GetProperty("name").GetString()} |
54 | | - Start Time: {period.GetProperty("startTime").GetString()} |
55 | | - End Time: {period.GetProperty("endTime").GetString()} |
56 | | - Temperature: {period.GetProperty("temperature").GetInt32()}°F |
57 | | - Wind Speed: {period.GetProperty("windSpeed").GetString()} |
58 | | - Wind Direction: {period.GetProperty("windDirection").GetString()} |
59 | | - Short Forecast: {period.GetProperty("shortForecast").GetString()} |
60 | | - Detailed Forecast: {period.GetProperty("detailedForecast").GetString()} |
61 | | - """; |
62 | | - })); |
| 48 | + using var forecastDocument = await client.ReadJsonDocumentAsync(forecastUrl ?? $"/{properties.GetProperty("gridId")}/{properties.GetProperty("gridX")},{properties.GetProperty("gridY")}/forecast"); |
| 49 | + var periods = forecastDocument.RootElement.GetProperty("properties").GetProperty("periods").EnumerateArray(); |
| 50 | + |
| 51 | + return string.Join("\n---\n", periods.Select(period => $""" |
| 52 | + {period.GetProperty("name").GetString()} |
| 53 | + Temperature: {period.GetProperty("temperature").GetInt32()}°F |
| 54 | + Wind: {period.GetProperty("windSpeed").GetString()} {period.GetProperty("windDirection").GetString()} |
| 55 | + Forecast: {period.GetProperty("detailedForecast").GetString()} |
| 56 | + """)); |
63 | 57 | } |
64 | 58 | } |
0 commit comments