@@ -1450,7 +1450,7 @@ After creating the project, add NuGet package for the Model Context Protocol SDK
1450
1450
1451
1451
``` bash
1452
1452
# Add the Model Context Protocol SDK NuGet package
1453
- dotnet add package ModelContextProtocol --preview
1453
+ dotnet add package ModelContextProtocol --prerelease
1454
1454
# Add the .NET Hosting NuGet package
1455
1455
dotnet add package Microsoft.Extensions.Hosting
1456
1456
```
@@ -1461,14 +1461,24 @@ Now let’s dive into building your server.
1461
1461
Open the ` Program.cs ` file in your project and replace its contents with the following code:
1462
1462
1463
1463
``` csharp
1464
+ using Microsoft .Extensions .DependencyInjection ;
1464
1465
using Microsoft .Extensions .Hosting ;
1465
1466
using ModelContextProtocol ;
1467
+ using System .Net .Http .Headers ;
1466
1468
1467
1469
var builder = Host .CreateEmptyApplicationBuilder (settings : null );
1470
+
1468
1471
builder .Services .AddMcpServer ()
1469
1472
.WithStdioServerTransport ()
1470
1473
.WithToolsFromAssembly ();
1471
1474
1475
+ builder .Services .AddSingleton (_ =>
1476
+ {
1477
+ var client = new HttpClient () { BaseAddress = new Uri (" https://api.weather.gov" ) };
1478
+ client .DefaultRequestHeaders .UserAgent .Add (new ProductInfoHeaderValue (" weather-tool" , " 1.0" ));
1479
+ return client ;
1480
+ });
1481
+
1472
1482
var app = builder .Build ();
1473
1483
1474
1484
await app .RunAsync ();
@@ -1485,7 +1495,7 @@ Next, define a class with the tool execution handlers for querying and convertin
1485
1495
``` csharp
1486
1496
using ModelContextProtocol .Server ;
1487
1497
using System .ComponentModel ;
1488
- using System .Net .Http .Headers ;
1498
+ using System .Net .Http .Json ;
1489
1499
using System .Text .Json ;
1490
1500
1491
1501
namespace QuickstartWeatherServer .Tools ;
@@ -1495,71 +1505,45 @@ public static class WeatherTools
1495
1505
{
1496
1506
[McpServerTool , Description (" Get weather alerts for a US state." )]
1497
1507
public static async Task <string > GetAlerts (
1508
+ HttpClient client ,
1498
1509
[Description (" The US state to get alerts for." )] string state )
1499
1510
{
1500
- using HttpClient client = GetWeatherClient ();
1501
-
1502
- var response = await client .GetAsync ($" /alerts/active/area/{state }" );
1503
-
1504
- var json = await response .Content .ReadAsStringAsync ();
1505
- var jsonElement = JsonSerializer .Deserialize <JsonElement >(json );
1511
+ var jsonElement = await client .GetFromJsonAsync <JsonElement >($" /alerts/active/area/{state }" );
1506
1512
var alerts = jsonElement .GetProperty (" features" ).EnumerateArray ();
1507
1513
1508
1514
if (! alerts .Any ())
1509
1515
{
1510
1516
return " No active alerts for this state." ;
1511
1517
}
1512
1518
1513
- // Process the alerts and return a formatted string
1514
- var alertMessages = new List <string >();
1515
- foreach (var alert in alerts )
1519
+ return string .Join (" \n --\n " , alerts .Select (alert =>
1516
1520
{
1517
1521
JsonElement properties = alert .GetProperty (" properties" );
1518
- alertMessages . Add ( $"""
1522
+ return $"""
1519
1523
Event: {properties .GetProperty (" event" ).GetString ()}
1520
1524
Area: {properties .GetProperty (" areaDesc" ).GetString ()}
1521
1525
Severity: {properties .GetProperty (" severity" ).GetString ()}
1522
1526
Description: {properties .GetProperty (" description" ).GetString ()}
1523
1527
Instruction: {properties .GetProperty (" instruction" ).GetString ()}
1524
- """ );
1525
- }
1526
- return string .Join (" \n ---\n " , alertMessages );
1528
+ """ ;
1529
+ }));
1527
1530
}
1528
1531
1529
1532
[McpServerTool , Description (" Get weather forecast for a location." )]
1530
1533
public static async Task <string > GetForecast (
1534
+ HttpClient client ,
1531
1535
[Description (" Latitude of the location." )] double latitude ,
1532
1536
[Description (" Longitude of the location." )] double longitude )
1533
1537
{
1534
- using HttpClient client = GetWeatherClient ();
1535
- var response = await client .GetAsync ($" /points/{latitude },{longitude }" );
1536
- if (! response .IsSuccessStatusCode )
1537
- {
1538
- return " Failed to retrieve forecast." ;
1539
- }
1540
-
1541
- var json = await response .Content .ReadAsStringAsync ();
1542
- var jsonElement = JsonSerializer .Deserialize <JsonElement >(json );
1538
+ var jsonElement = await client .GetFromJsonAsync <JsonElement >($" /points/{latitude },{longitude }" );
1543
1539
var periods = jsonElement .GetProperty (" properties" ).GetProperty (" periods" ).EnumerateArray ();
1544
- // Process the forecast and return a formatted string
1545
- var forecastMessages = new List <string >();
1546
- foreach (var period in periods )
1547
- {
1548
- forecastMessages .Add ($"""
1549
- {period .GetProperty (" name" ).GetString ()}
1550
- Temperature: {period .GetProperty (" temperature" ).GetInt32 ()}°F
1551
- Wind: {period .GetProperty (" windSpeed" ).GetString ()} {period .GetProperty (" windDirection" ).GetString ()}
1552
- Forecast: {period .GetProperty (" detailedForecast" ).GetString ()}
1553
- """ );
1554
- }
1555
- return string .Join (" \n ---\n " , forecastMessages );
1556
- }
1557
1540
1558
- private static HttpClient GetWeatherClient ()
1559
- {
1560
- var client = new HttpClient () { BaseAddress = new Uri (" https://api.weather.gov" ) };
1561
- client .DefaultRequestHeaders .UserAgent .Add (new ProductInfoHeaderValue (" weather-tool" , " 1.0" ));
1562
- return client ;
1541
+ return string .Join (" \n ---\n " , periods .Select (period => $"""
1542
+ {period .GetProperty (" name" ).GetString ()}
1543
+ Temperature: {period .GetProperty (" temperature" ).GetInt32 ()}°F
1544
+ Wind: {period .GetProperty (" windSpeed" ).GetString ()} {period .GetProperty (" windDirection" ).GetString ()}
1545
+ Forecast: {period .GetProperty (" detailedForecast" ).GetString ()}
1546
+ """ ));
1563
1547
}
1564
1548
}
1565
1549
```
0 commit comments