| 1 | /* |
| 2 | dotnet add package ModelContextProtocol --prerelease |
| 3 | dotnet add package ModelContextProtocol.AspNetCore --prerelease |
| 4 | dotnet add package Microsoft.Extensions.Hosting |
| 5 | */ |
| 6 | |
| 7 | using ModelContextProtocol.Server; |
| 8 | using System.ComponentModel; |
| 9 | |
| 10 | var builder = WebApplication.CreateBuilder(args); |
| 11 | builder.Services.AddMcpServer() |
| 12 | .WithHttpTransport() |
| 13 | .WithToolsFromAssembly(); |
| 14 | var app = builder.Build(); |
| 15 | |
| 16 | app.MapMcp(); |
| 17 | |
| 18 | app.Run("http://localhost:3001"); |
| 19 | |
| 20 | [McpServerToolType] |
| 21 | public static class EchoTool |
| 22 | { |
| 23 | [McpServerTool, Description("Echoes the message back to the client.")] |
| 24 | public static string Echo(string message) => $"Yo! You said '{message}'"; |
| 25 | } |
| 26 | |
| 27 | // Add a strongly-typed response class for weather |
| 28 | public record WeatherResponse(string City, int Temperature, int Humidity, string Condition); |
| 29 | |
| 30 | [McpServerToolType] |
| 31 | public static class WeatherTool |
| 32 | { |
| 33 | [McpServerTool, Description("Returns weather data as a typed object; framework will serialize to JSON.")] |
| 34 | public static WeatherResponse GetWeather(string? city) |
| 35 | { |
| 36 | string cityName = city ?? string.Empty; |
| 37 | int temperature = cityName.Length; |
| 38 | int humidity = temperature * 5; |
| 39 | var conditions = new[] { "Sunny", "Cloudy", "Rainy", "Windy" }; |
| 40 | string condition = conditions[temperature % conditions.Length]; |
| 41 | return new WeatherResponse(cityName, temperature, humidity, condition); |
| 42 | } |
| 43 | } |
| 44 |
jeff / basic_dotnet_mcp_server.cs
Last active 6 months ago
Revision 29837b43bc901a0cc7bd2b3903740ae5b1e3a75d