Use this file to discover all available pages before exploring further.
Tools are executable functions that MCP servers expose to clients. The C# SDK provides attribute-based tools that automatically handle parameter binding, validation, and result marshalling.
Use the [McpServerTool] attribute to mark methods as MCP tools:
WeatherTools.cs
using ModelContextProtocol.Server;using System.ComponentModel;[McpServerToolType]public sealed 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. Use the 2 letter abbreviation for the state (e.g. NY).")] string state) { using var jsonDocument = await client.ReadJsonDocumentAsync($"/alerts/active/area/{state}"); var alerts = jsonDocument.RootElement.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()} """; })); }}
[McpServerToolType]public class AddTool{ [McpServerTool( Name = "add", IconSource = "https://example.com/plus.svg" )] [Description("Adds two numbers.")] public static string Add(int a, int b) => $"The sum of {a} and {b} is {a + b}";}
For tools that need services, use instance methods:
[McpServerToolType]public sealed class WeatherTools{ private readonly IHttpClientFactory _httpClientFactory; public WeatherTools(IHttpClientFactory httpClientFactory) { _httpClientFactory = httpClientFactory; } [McpServerTool] [Description("Get weather forecast for a location.")] public async Task<string> GetForecast( [Description("Latitude of the location.")] double latitude, [Description("Longitude of the location.")] double longitude) { var client = _httpClientFactory.CreateClient("WeatherApi"); // ... implementation }}
// Register by type.WithTools<WeatherTools>()// Register from assembly.WithToolsFromAssembly()// Register from types collection.WithTools([typeof(Tool1), typeof(Tool2)])// Register with custom serializer.WithTools<CustomTools>(serializerOptions)