Skip to content

Commit 19fdd4b

Browse files
committed
Server quickstart for docs
1 parent caca2ae commit 19fdd4b

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

ModelContextProtocol.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "workflows", "workflows", "{
4646
EndProject
4747
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChatWithTools", "samples\ChatWithTools\ChatWithTools.csproj", "{0C6D0512-D26D-63D3-5019-C5F7A657B28C}"
4848
EndProject
49+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickstartWeatherServer", "samples\QuickstartWeatherServer\QuickstartWeatherServer.csproj", "{ADD88C0E-8225-4350-8483-E863B8E7CC51}"
50+
EndProject
4951
Global
5052
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5153
Debug|Any CPU = Debug|Any CPU
@@ -80,6 +82,10 @@ Global
8082
{0C6D0512-D26D-63D3-5019-C5F7A657B28C}.Debug|Any CPU.Build.0 = Debug|Any CPU
8183
{0C6D0512-D26D-63D3-5019-C5F7A657B28C}.Release|Any CPU.ActiveCfg = Release|Any CPU
8284
{0C6D0512-D26D-63D3-5019-C5F7A657B28C}.Release|Any CPU.Build.0 = Release|Any CPU
85+
{ADD88C0E-8225-4350-8483-E863B8E7CC51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
86+
{ADD88C0E-8225-4350-8483-E863B8E7CC51}.Debug|Any CPU.Build.0 = Debug|Any CPU
87+
{ADD88C0E-8225-4350-8483-E863B8E7CC51}.Release|Any CPU.ActiveCfg = Release|Any CPU
88+
{ADD88C0E-8225-4350-8483-E863B8E7CC51}.Release|Any CPU.Build.0 = Release|Any CPU
8389
EndGlobalSection
8490
GlobalSection(SolutionProperties) = preSolution
8591
HideSolutionNode = FALSE
@@ -93,6 +99,7 @@ Global
9399
{B6F42305-423F-56FF-090F-B7263547F924} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
94100
{20AACB9B-307D-419C-BCC6-1C639C402295} = {1288ADA5-1BF1-4A7F-A33E-9EA29097AA40}
95101
{0C6D0512-D26D-63D3-5019-C5F7A657B28C} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
102+
{ADD88C0E-8225-4350-8483-E863B8E7CC51} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8}
96103
EndGlobalSection
97104
GlobalSection(ExtensibilityGlobals) = postSolution
98105
SolutionGuid = {384A3888-751F-4D75-9AE5-587330582D89}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Extensions.Hosting;
2+
using Microsoft.Extensions.Logging;
3+
using ModelContextProtocol;
4+
5+
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
6+
7+
//builder.Logging.ClearProviders();
8+
//builder.Logging.AddFilter("Microsoft", LogLevel.Warning); // Adjust the log level as needed
9+
10+
builder.Services.AddMcpServer()
11+
.WithStdioServerTransport()
12+
.WithToolsFromAssembly();
13+
14+
var app = builder.Build();
15+
16+
await app.RunAsync();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.Hosting" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using ModelContextProtocol.Server;
2+
using System.ComponentModel;
3+
using System.Net.Http.Headers;
4+
using System.Text.Json;
5+
6+
namespace QuickstartWeatherServer.Tools;
7+
8+
[McpServerToolType]
9+
public static class WeatherTools
10+
{
11+
[McpServerTool, Description("Get weather alerts for a US state.")]
12+
public static async Task<string> GetAlerts(
13+
[Description("The US state to get alerts for.")] string state)
14+
{
15+
using HttpClient client = GetWeatherClient();
16+
17+
var response = await client.GetAsync($"/alerts/active/area/{state}");
18+
19+
if (!response.IsSuccessStatusCode)
20+
{
21+
return "Failed to retrieve alerts.";
22+
}
23+
24+
var json = await response.Content.ReadAsStringAsync();
25+
var jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
26+
var alerts = jsonElement.GetProperty("features").EnumerateArray();
27+
28+
if (!alerts.Any())
29+
{
30+
return "No active alerts for this state.";
31+
}
32+
33+
// Process the alerts and return a formatted string
34+
var alertMessages = new List<string>();
35+
foreach (var alert in alerts)
36+
{
37+
JsonElement properties = alert.GetProperty("properties");
38+
alertMessages.Add($"""
39+
Event: {properties.GetProperty("event").GetString()}
40+
Area: {properties.GetProperty("areaDesc").GetString()}
41+
Severity: {properties.GetProperty("severity").GetString()}
42+
Description: {properties.GetProperty("description").GetString()}
43+
Instruction: {properties.GetProperty("instruction").GetString()}
44+
""");
45+
}
46+
return string.Join("\n---\n", alertMessages);
47+
}
48+
49+
[McpServerTool, Description("Get weather forecast for a location.")]
50+
public static async Task<string> GetForecast(
51+
[Description("Latitude of the location.")] double latitude,
52+
[Description("Longitude of the location.")] double longitude)
53+
{
54+
using HttpClient client = GetWeatherClient();
55+
var response = await client.GetAsync($"/points/{latitude},{longitude}");
56+
if (!response.IsSuccessStatusCode)
57+
{
58+
return "Failed to retrieve forecast.";
59+
}
60+
61+
var json = await response.Content.ReadAsStringAsync();
62+
var jsonElement = JsonSerializer.Deserialize<JsonElement>(json);
63+
var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray();
64+
// Process the forecast and return a formatted string
65+
var forecastMessages = new List<string>();
66+
foreach (var period in periods)
67+
{
68+
forecastMessages.Add($"""
69+
{period.GetProperty("name").GetString()}
70+
Temperature: {period.GetProperty("temperature").GetInt32()}°F
71+
Wind: {period.GetProperty("windSpeed").GetString()} {period.GetProperty("windDirection").GetString()}
72+
Forecast: {period.GetProperty("detailedForecast").GetString()}
73+
""");
74+
}
75+
return string.Join("\n---\n", forecastMessages);
76+
}
77+
78+
private static HttpClient GetWeatherClient()
79+
{
80+
var client = new HttpClient() { BaseAddress = new Uri("https://api.weather.gov") };
81+
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("weather-tool", "1.0"));
82+
return client;
83+
}
84+
}

0 commit comments

Comments
 (0)