Samples for quickstart docs (#104)
* Server quickstart for docs * WIP * Client quickstart for docs * Code review feedback * Fixing anthropic SDK and improving the prompting * Little cleanup * Code review feedback
This commit is contained in:
parent
8be681543d
commit
5171ccc3c3
3 changed files with 223 additions and 1 deletions
19
samples/QuickstartWeatherServer/Program.cs
Normal file
19
samples/QuickstartWeatherServer/Program.cs
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using ModelContextProtocol;
|
||||
using System.Net.Http.Headers;
|
||||
|
||||
var builder = Host.CreateEmptyApplicationBuilder(settings: null);
|
||||
|
||||
builder.Services.AddMcpServer()
|
||||
.WithStdioServerTransport()
|
||||
.WithToolsFromAssembly();
|
||||
|
||||
builder.Services.AddSingleton(_ =>
|
||||
{
|
||||
var client = new HttpClient() { BaseAddress = new Uri("https://api.weather.gov") };
|
||||
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("weather-tool", "1.0"));
|
||||
return client;
|
||||
});
|
||||
|
||||
await builder.Build().RunAsync();
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\ModelContextProtocol\ModelContextProtocol.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
53
samples/QuickstartWeatherServer/Tools/WeatherTools.cs
Normal file
53
samples/QuickstartWeatherServer/Tools/WeatherTools.cs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace QuickstartWeatherServer.Tools;
|
||||
|
||||
[McpServerToolType]
|
||||
public static class WeatherTools
|
||||
{
|
||||
[McpServerTool, Description("Get weather alerts for a US state.")]
|
||||
public static async Task<string> GetAlerts(
|
||||
HttpClient client,
|
||||
[Description("The US state to get alerts for.")] string state)
|
||||
{
|
||||
var jsonElement = await client.GetFromJsonAsync<JsonElement>($"/alerts/active/area/{state}");
|
||||
var alerts = jsonElement.GetProperty("features").EnumerateArray();
|
||||
|
||||
if (!alerts.Any())
|
||||
{
|
||||
return "No active alerts for this state.";
|
||||
}
|
||||
|
||||
return string.Join("\n--\n", alerts.Select(alert =>
|
||||
{
|
||||
JsonElement properties = alert.GetProperty("properties");
|
||||
return $"""
|
||||
Event: {properties.GetProperty("event").GetString()}
|
||||
Area: {properties.GetProperty("areaDesc").GetString()}
|
||||
Severity: {properties.GetProperty("severity").GetString()}
|
||||
Description: {properties.GetProperty("description").GetString()}
|
||||
Instruction: {properties.GetProperty("instruction").GetString()}
|
||||
""";
|
||||
}));
|
||||
}
|
||||
|
||||
[McpServerTool, Description("Get weather forecast for a location.")]
|
||||
public static async Task<string> GetForecast(
|
||||
HttpClient client,
|
||||
[Description("Latitude of the location.")] double latitude,
|
||||
[Description("Longitude of the location.")] double longitude)
|
||||
{
|
||||
var jsonElement = await client.GetFromJsonAsync<JsonElement>($"/points/{latitude},{longitude}");
|
||||
var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray();
|
||||
|
||||
return string.Join("\n---\n", periods.Select(period => $"""
|
||||
{period.GetProperty("name").GetString()}
|
||||
Temperature: {period.GetProperty("temperature").GetInt32()}°F
|
||||
Wind: {period.GetProperty("windSpeed").GetString()} {period.GetProperty("windDirection").GetString()}
|
||||
Forecast: {period.GetProperty("detailedForecast").GetString()}
|
||||
"""));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue