diff --git a/samples/AspNetCoreSseServer/AspNetCoreSseServer.csproj b/samples/AspNetCoreSseServer/AspNetCoreSseServer.csproj index c17cf9c45..94a5ccdb9 100644 --- a/samples/AspNetCoreSseServer/AspNetCoreSseServer.csproj +++ b/samples/AspNetCoreSseServer/AspNetCoreSseServer.csproj @@ -4,6 +4,7 @@ net9.0 enable enable + true diff --git a/samples/AspNetCoreSseServer/Program.cs b/samples/AspNetCoreSseServer/Program.cs index 9f94eb2c7..306a6e8f7 100644 --- a/samples/AspNetCoreSseServer/Program.cs +++ b/samples/AspNetCoreSseServer/Program.cs @@ -1,5 +1,10 @@ +using TestServerWithHosting.Tools; + var builder = WebApplication.CreateBuilder(args); -builder.Services.AddMcpServer().WithToolsFromAssembly(); +builder.Services.AddMcpServer() + .WithTools() + .WithTools(); + var app = builder.Build(); app.MapMcp(); diff --git a/samples/AspNetCoreSseServer/Tools/EchoTool.cs b/samples/AspNetCoreSseServer/Tools/EchoTool.cs index 636b4063a..7913b73e4 100644 --- a/samples/AspNetCoreSseServer/Tools/EchoTool.cs +++ b/samples/AspNetCoreSseServer/Tools/EchoTool.cs @@ -4,7 +4,7 @@ namespace TestServerWithHosting.Tools; [McpServerToolType] -public static class EchoTool +public sealed class EchoTool { [McpServerTool, Description("Echoes the input back to the client.")] public static string Echo(string message) diff --git a/samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs b/samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs index 4f175a453..4fbca594a 100644 --- a/samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs +++ b/samples/AspNetCoreSseServer/Tools/SampleLlmTool.cs @@ -8,7 +8,7 @@ namespace TestServerWithHosting.Tools; /// This tool uses dependency injection and async method /// [McpServerToolType] -public static class SampleLlmTool +public sealed class SampleLlmTool { [McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")] public static async Task SampleLLM( diff --git a/samples/ChatWithTools/ChatWithTools.csproj b/samples/ChatWithTools/ChatWithTools.csproj index af8fac198..8e08a455d 100644 --- a/samples/ChatWithTools/ChatWithTools.csproj +++ b/samples/ChatWithTools/ChatWithTools.csproj @@ -5,6 +5,10 @@ net8.0 enable enable + diff --git a/samples/QuickstartClient/QuickstartClient.csproj b/samples/QuickstartClient/QuickstartClient.csproj index 076e28b16..b68f15e5f 100644 --- a/samples/QuickstartClient/QuickstartClient.csproj +++ b/samples/QuickstartClient/QuickstartClient.csproj @@ -6,6 +6,10 @@ enable enable a4e20a70-5009-4b81-b5b6-780b6d43e78e + diff --git a/samples/QuickstartWeatherServer/Program.cs b/samples/QuickstartWeatherServer/Program.cs index 8c3b38420..301eeed5e 100644 --- a/samples/QuickstartWeatherServer/Program.cs +++ b/samples/QuickstartWeatherServer/Program.cs @@ -1,13 +1,14 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using QuickstartWeatherServer.Tools; using System.Net.Http.Headers; var builder = Host.CreateApplicationBuilder(args); builder.Services.AddMcpServer() .WithStdioServerTransport() - .WithToolsFromAssembly(); + .WithTools(); builder.Logging.AddConsole(options => { diff --git a/samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj b/samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj index 2e9154fd2..dc1108a8f 100644 --- a/samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj +++ b/samples/QuickstartWeatherServer/QuickstartWeatherServer.csproj @@ -5,6 +5,7 @@ net8.0 enable enable + true diff --git a/samples/QuickstartWeatherServer/Tools/HttpClientExt.cs b/samples/QuickstartWeatherServer/Tools/HttpClientExt.cs new file mode 100644 index 000000000..f7b2b5499 --- /dev/null +++ b/samples/QuickstartWeatherServer/Tools/HttpClientExt.cs @@ -0,0 +1,13 @@ +using System.Text.Json; + +namespace ModelContextProtocol; + +internal static class HttpClientExt +{ + public static async Task ReadJsonDocumentAsync(this HttpClient client, string requestUri) + { + using var response = await client.GetAsync(requestUri); + response.EnsureSuccessStatusCode(); + return await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync()); + } +} \ No newline at end of file diff --git a/samples/QuickstartWeatherServer/Tools/WeatherTools.cs b/samples/QuickstartWeatherServer/Tools/WeatherTools.cs index 697b80952..8463e3501 100644 --- a/samples/QuickstartWeatherServer/Tools/WeatherTools.cs +++ b/samples/QuickstartWeatherServer/Tools/WeatherTools.cs @@ -1,3 +1,4 @@ +using ModelContextProtocol; using ModelContextProtocol.Server; using System.ComponentModel; using System.Net.Http.Json; @@ -6,14 +7,15 @@ namespace QuickstartWeatherServer.Tools; [McpServerToolType] -public static class WeatherTools +public sealed class WeatherTools { [McpServerTool, Description("Get weather alerts for a US state.")] public static async Task GetAlerts( HttpClient client, [Description("The US state to get alerts for.")] string state) { - var jsonElement = await client.GetFromJsonAsync($"/alerts/active/area/{state}"); + using var jsonDocument = await client.ReadJsonDocumentAsync($"/alerts/active/area/{state}"); + var jsonElement = jsonDocument.RootElement; var alerts = jsonElement.GetProperty("features").EnumerateArray(); if (!alerts.Any()) @@ -40,7 +42,8 @@ public static async Task GetForecast( [Description("Latitude of the location.")] double latitude, [Description("Longitude of the location.")] double longitude) { - var jsonElement = await client.GetFromJsonAsync($"/points/{latitude},{longitude}"); + using var jsonDocument = await client.ReadJsonDocumentAsync($"/points/{latitude},{longitude}"); + var jsonElement = jsonDocument.RootElement; var periods = jsonElement.GetProperty("properties").GetProperty("periods").EnumerateArray(); return string.Join("\n---\n", periods.Select(period => $""" diff --git a/samples/TestServerWithHosting/Program.cs b/samples/TestServerWithHosting/Program.cs index ee009084b..1ab6fc7a2 100644 --- a/samples/TestServerWithHosting/Program.cs +++ b/samples/TestServerWithHosting/Program.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; +using TestServerWithHosting.Tools; Log.Logger = new LoggerConfiguration() .MinimumLevel.Verbose() // Capture all log levels @@ -19,7 +20,8 @@ builder.Services.AddSerilog(); builder.Services.AddMcpServer() .WithStdioServerTransport() - .WithToolsFromAssembly(); + .WithTools() + .WithTools(); var app = builder.Build(); diff --git a/samples/TestServerWithHosting/TestServerWithHosting.csproj b/samples/TestServerWithHosting/TestServerWithHosting.csproj index 137d753bb..0f3918d95 100644 --- a/samples/TestServerWithHosting/TestServerWithHosting.csproj +++ b/samples/TestServerWithHosting/TestServerWithHosting.csproj @@ -5,6 +5,10 @@ net9.0 enable enable + @@ -13,6 +17,9 @@ + diff --git a/samples/TestServerWithHosting/Tools/EchoTool.cs b/samples/TestServerWithHosting/Tools/EchoTool.cs index 636b4063a..7913b73e4 100644 --- a/samples/TestServerWithHosting/Tools/EchoTool.cs +++ b/samples/TestServerWithHosting/Tools/EchoTool.cs @@ -4,7 +4,7 @@ namespace TestServerWithHosting.Tools; [McpServerToolType] -public static class EchoTool +public sealed class EchoTool { [McpServerTool, Description("Echoes the input back to the client.")] public static string Echo(string message) diff --git a/samples/TestServerWithHosting/Tools/SampleLlmTool.cs b/samples/TestServerWithHosting/Tools/SampleLlmTool.cs index b1a0353d4..3539b4cdc 100644 --- a/samples/TestServerWithHosting/Tools/SampleLlmTool.cs +++ b/samples/TestServerWithHosting/Tools/SampleLlmTool.cs @@ -8,7 +8,7 @@ namespace TestServerWithHosting.Tools; /// This tool uses depenency injection and async method /// [McpServerToolType] -public static class SampleLlmTool +public sealed class SampleLlmTool { [McpServerTool(Name = "sampleLLM"), Description("Samples from an LLM using MCP's sampling feature")] public static async Task SampleLLM(