@@ -1484,12 +1484,29 @@ When creating the `ApplicationHostBuilder`, ensure you use `CreateEmptyApplicati
14841484This code sets up a basic console application that uses the Model Context Protocol SDK to create an MCP server with standard I/O transport.
14851485
14861486### Weather API helper functions
1487+
1488+ Create an extension class for ` HttpClient ` which helps simplify JSON request handling:
1489+
1490+ ``` csharp
1491+ using System .Text .Json ;
1492+
1493+ internal static class HttpClientExt
1494+ {
1495+ public static async Task <JsonDocument > ReadJsonDocumentAsync (this HttpClient client , string requestUri )
1496+ {
1497+ using var response = await client .GetAsync (requestUri );
1498+ response .EnsureSuccessStatusCode ();
1499+ return await JsonDocument .ParseAsync (await response .Content .ReadAsStreamAsync ());
1500+ }
1501+ }
1502+ ```
1503+
14871504Next, define a class with the tool execution handlers for querying and converting responses from the National Weather Service API:
14881505
14891506``` csharp
14901507using ModelContextProtocol .Server ;
14911508using System .ComponentModel ;
1492- using System .Net . Http . Json ;
1509+ using System .Globalization ;
14931510using System .Text .Json ;
14941511
14951512namespace QuickstartWeatherServer .Tools ;
@@ -1502,7 +1519,8 @@ public static class WeatherTools
15021519 HttpClient client ,
15031520 [Description (" The US state to get alerts for." )] string state )
15041521 {
1505- var jsonElement = await client .GetFromJsonAsync <JsonElement >($" /alerts/active/area/{state }" );
1522+ using var jsonDocument = await client .ReadJsonDocumentAsync ($" /alerts/active/area/{state }" );
1523+ var jsonElement = jsonDocument .RootElement ;
15061524 var alerts = jsonElement .GetProperty (" features" ).EnumerateArray ();
15071525
15081526 if (! alerts .Any ())
@@ -1529,8 +1547,13 @@ public static class WeatherTools
15291547 [Description (" Latitude of the location." )] double latitude ,
15301548 [Description (" Longitude of the location." )] double longitude )
15311549 {
1532- var jsonElement = await client .GetFromJsonAsync <JsonElement >($" /points/{latitude },{longitude }" );
1533- var periods = jsonElement .GetProperty (" properties" ).GetProperty (" periods" ).EnumerateArray ();
1550+ var pointUrl = string .Create (CultureInfo .InvariantCulture , $" /points/{latitude },{longitude }" );
1551+ using var jsonDocument = await client .ReadJsonDocumentAsync (pointUrl );
1552+ var forecastUrl = jsonDocument .RootElement .GetProperty (" properties" ).GetProperty (" forecast" ).GetString ()
1553+ ?? throw new Exception ($" No forecast URL provided by {client .BaseAddress }points/{latitude },{longitude }" );
1554+
1555+ using var forecastDocument = await client .ReadJsonDocumentAsync (forecastUrl );
1556+ var periods = forecastDocument .RootElement .GetProperty (" properties" ).GetProperty (" periods" ).EnumerateArray ();
15341557
15351558 return string .Join (" \n ---\n " , periods .Select (period => $"""
15361559 {period .GetProperty (" name" ).GetString ()}
0 commit comments