basic_dotnet_mcp_server.cs
· 1.7 KiB · C#
Raw
/*
dotnet add package ModelContextProtocol --prerelease
dotnet add package ModelContextProtocol.AspNetCore --prerelease
dotnet add package Microsoft.Extensions.Hosting
*/
using ModelContextProtocol.Server;
using System.ComponentModel;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMcpServer()
.WithHttpTransport()
.WithToolsFromAssembly();
var app = builder.Build();
// validate Authorization header on incoming requests
app.Use(async (context, next) =>
{
if (!context.Request.Headers.TryGetValue("Authorization", out var auth)
|| auth != "Bearer hello-world")
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
return;
}
await next();
});
app.MapMcp();
app.Run("http://localhost:3001");
[McpServerToolType]
public static class EchoTool
{
[McpServerTool, Description("Echoes the message back to the client.")]
public static string Echo(string message) => $"Yo! You said '{message}'";
}
// Add a strongly-typed response class for weather
public record WeatherResponse(string City, int Temperature, int Humidity, string Condition);
[McpServerToolType]
public static class WeatherTool
{
[McpServerTool, Description("Returns weather data as a typed object; framework will serialize to JSON.")]
public static WeatherResponse GetWeather(string? city)
{
string cityName = city ?? string.Empty;
int temperature = cityName.Length;
int humidity = temperature * 5;
var conditions = new[] { "Sunny", "Cloudy", "Rainy", "Windy" };
string condition = conditions[temperature % conditions.Length];
return new WeatherResponse(cityName, temperature, humidity, condition);
}
}
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 | // validate Authorization header on incoming requests |
17 | app.Use(async (context, next) => |
18 | { |
19 | if (!context.Request.Headers.TryGetValue("Authorization", out var auth) |
20 | || auth != "Bearer hello-world") |
21 | { |
22 | context.Response.StatusCode = StatusCodes.Status401Unauthorized; |
23 | return; |
24 | } |
25 | await next(); |
26 | }); |
27 | |
28 | app.MapMcp(); |
29 | |
30 | app.Run("http://localhost:3001"); |
31 | |
32 | [McpServerToolType] |
33 | public static class EchoTool |
34 | { |
35 | [McpServerTool, Description("Echoes the message back to the client.")] |
36 | public static string Echo(string message) => $"Yo! You said '{message}'"; |
37 | } |
38 | |
39 | // Add a strongly-typed response class for weather |
40 | public record WeatherResponse(string City, int Temperature, int Humidity, string Condition); |
41 | |
42 | [McpServerToolType] |
43 | public static class WeatherTool |
44 | { |
45 | [McpServerTool, Description("Returns weather data as a typed object; framework will serialize to JSON.")] |
46 | public static WeatherResponse GetWeather(string? city) |
47 | { |
48 | string cityName = city ?? string.Empty; |
49 | int temperature = cityName.Length; |
50 | int humidity = temperature * 5; |
51 | var conditions = new[] { "Sunny", "Cloudy", "Rainy", "Windy" }; |
52 | string condition = conditions[temperature % conditions.Length]; |
53 | return new WeatherResponse(cityName, temperature, humidity, condition); |
54 | } |
55 | } |
56 | |
57 |