@@ -1484,12 +1484,29 @@ When creating the `ApplicationHostBuilder`, ensure you use `CreateEmptyApplicati
1484
1484
This code sets up a basic console application that uses the Model Context Protocol SDK to create an MCP server with standard I/O transport.
1485
1485
1486
1486
### 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
+
1487
1504
Next, define a class with the tool execution handlers for querying and converting responses from the National Weather Service API:
1488
1505
1489
1506
``` csharp
1490
1507
using ModelContextProtocol .Server ;
1491
1508
using System .ComponentModel ;
1492
- using System .Net . Http . Json ;
1509
+ using System .Globalization ;
1493
1510
using System .Text .Json ;
1494
1511
1495
1512
namespace QuickstartWeatherServer .Tools ;
@@ -1502,7 +1519,8 @@ public static class WeatherTools
1502
1519
HttpClient client ,
1503
1520
[Description (" The US state to get alerts for." )] string state )
1504
1521
{
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 ;
1506
1524
var alerts = jsonElement .GetProperty (" features" ).EnumerateArray ();
1507
1525
1508
1526
if (! alerts .Any ())
@@ -1529,8 +1547,13 @@ public static class WeatherTools
1529
1547
[Description (" Latitude of the location." )] double latitude ,
1530
1548
[Description (" Longitude of the location." )] double longitude )
1531
1549
{
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 ();
1534
1557
1535
1558
return string .Join (" \n ---\n " , periods .Select (period => $"""
1536
1559
{period .GetProperty (" name" ).GetString ()}
0 commit comments