Skip to content

Commit f652b1a

Browse files
agockestephentoub
andauthored
Mark samples as AOT compatible (#184)
* Revert changes to remove APIs * Apply suggestions from code review Co-authored-by: Stephen Toub <stoub@microsoft.com> --------- Co-authored-by: Stephen Toub <stoub@microsoft.com>
1 parent c452dc8 commit f652b1a

File tree

14 files changed

+51
-10
lines changed

14 files changed

+51
-10
lines changed

samples/AspNetCoreSseServer/AspNetCoreSseServer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net9.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<PublishAot>true</PublishAot>
78
</PropertyGroup>
89

910
<ItemGroup>

samples/AspNetCoreSseServer/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
using TestServerWithHosting.Tools;
2+
13
var builder = WebApplication.CreateBuilder(args);
2-
builder.Services.AddMcpServer().WithToolsFromAssembly();
4+
builder.Services.AddMcpServer()
5+
.WithTools<EchoTool>()
6+
.WithTools<SampleLlmTool>();
7+
38
var app = builder.Build();
49

510
app.MapMcp();

samples/AspNetCoreSseServer/Tools/EchoTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace TestServerWithHosting.Tools;
55

66
[McpServerToolType]
7-
public static class EchoTool
7+
public sealed class EchoTool
88
{
99
[McpServerTool, Description("Echoes the input back to the client.")]
1010
public static string Echo(string message)

samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace TestServerWithHosting.Tools;
88
/// This tool uses dependency injection and async method
99
/// </summary>
1010
[McpServerToolType]
11-
public static class SampleLlmTool
11+
public sealed class SampleLlmTool
1212
{
1313
[McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")]
1414
public static async Task<string> SampleLLM(

samples/ChatWithTools/ChatWithTools.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<!--
9+
Anthropic SDK isn't AOT compatible yet
10+
<PublishAot>true</PublishAot>
11+
-->
812
</PropertyGroup>
913

1014
<ItemGroup>

samples/QuickstartClient/QuickstartClient.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<UserSecretsId>a4e20a70-5009-4b81-b5b6-780b6d43e78e</UserSecretsId>
9+
<!--
10+
Anthropic SDK isn't AOT compatible yet
11+
<PublishAot>true</PublishAot>
12+
-->
913
</PropertyGroup>
1014

1115
<ItemGroup>

samples/QuickstartWeatherServer/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Hosting;
33
using Microsoft.Extensions.Logging;
4+
using QuickstartWeatherServer.Tools;
45
using System.Net.Http.Headers;
56

67
var builder = Host.CreateApplicationBuilder(args);
78

89
builder.Services.AddMcpServer()
910
.WithStdioServerTransport()
10-
.WithToolsFromAssembly();
11+
.WithTools<WeatherTools>();
1112

1213
builder.Logging.AddConsole(options =>
1314
{

samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<PublishAot>true</PublishAot>
89
</PropertyGroup>
910

1011
<ItemGroup>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Text.Json;
2+
3+
namespace ModelContextProtocol;
4+
5+
internal static class HttpClientExt
6+
{
7+
public static async Task<JsonDocument> ReadJsonDocumentAsync(this HttpClient client, string requestUri)
8+
{
9+
using var response = await client.GetAsync(requestUri);
10+
response.EnsureSuccessStatusCode();
11+
return await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
12+
}
13+
}

samples/QuickstartWeatherServer/Tools/WeatherTools.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using ModelContextProtocol;
12
using ModelContextProtocol.Server;
23
using System.ComponentModel;
34
using System.Net.Http.Json;
@@ -6,14 +7,15 @@
67
namespace QuickstartWeatherServer.Tools;
78

89
[McpServerToolType]
9-
public static class WeatherTools
10+
public sealed class WeatherTools
1011
{
1112
[McpServerTool, Description("Get weather alerts for a US state.")]
1213
public static async Task<string> GetAlerts(
1314
HttpClient client,
1415
[Description("The US state to get alerts for.")] string state)
1516
{
16-
var jsonElement = await client.GetFromJsonAsync<JsonElement>($"/alerts/active/area/{state}");
17+
using var jsonDocument = await client.ReadJsonDocumentAsync($"/alerts/active/area/{state}");
18+
var jsonElement = jsonDocument.RootElement;
1719
var alerts = jsonElement.GetProperty("features").EnumerateArray();
1820

1921
if (!alerts.Any())
@@ -40,7 +42,8 @@ public static async Task<string> GetForecast(
4042
[Description("Latitude of the location.")] double latitude,
4143
[Description("Longitude of the location.")] double longitude)
4244
{
43-
var jsonElement = await client.GetFromJsonAsync<JsonElement>($"/points/{latitude},{longitude}");
45+
using var jsonDocument = await client.ReadJsonDocumentAsync($"/points/{latitude},{longitude}");
46+
var jsonElement = jsonDocument.RootElement;
4447
var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray();
4548

4649
return string.Join("\n---\n", periods.Select(period => $"""

0 commit comments

Comments
 (0)