Skip to content

Commit ff74a1e

Browse files
authored
Updated code
1 parent c7c730b commit ff74a1e

1 file changed

Lines changed: 11 additions & 17 deletions

File tree

samples/QuickstartWeatherServer/Tools/WeatherTools.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using ModelContextProtocol;
22
using ModelContextProtocol.Server;
33
using System.ComponentModel;
4-
using System.Net.Http.Json;
54
using System.Text.Json;
65

76
namespace QuickstartWeatherServer.Tools;
@@ -43,22 +42,17 @@ public static async Task<string> GetForecast(
4342
[Description("Longitude of the location.")] double longitude)
4443
{
4544
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();
4847

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+
"""));
6357
}
6458
}

0 commit comments

Comments
 (0)