Build Model Context Protocol (MCP) servers and clients in .NET with MCPSharp. Create MCP-compliant tools and functions that AI models can discover and use. Features easy to use attribute-based API allowing anyone to spin up a server in minutes, and a Microsoft.Extensions.AI compatible client that generates AIFunctions, ready to be consumed by any IChatClient or compatible system. await MCPServer.StartAsync("EchoServer", "1.0.0"); --- server: class MyTool { [McpTool("echo","returns the input string back to you")] public string echo(string input) => input; } ---client: var client = new MCPClient("MyClient","1.0.0", "EchoServer.exe" //or dotnet EchoServer.dll if you wish await client.CallToolAsync("echo", new Dictionary<string, object>{{"input", "input string to echo"}});
$ dotnet add package MCPSharpMCPSharp is a .NET library that helps you build Model Context Protocol (MCP) servers - the standardized API protocol used by AI assistants and models. With MCPSharp, you can:
Use MCPSharp when you want to:
[McpTool], [McpFunction])dotnet add package MCPSharp
Create a class and mark it with the [McpTool] attribute:
using MCPSharp;
[McpTool]
public class Calculator
{
[McpFunction]
public static int Add([McpParameter(true)] int a, [McpParameter(true)] int b)
{
return a + b;
}
}
Then just start the server:
await MCPServer.StartAsync("CalculatorServer", "1.0.0");
[McpTool] - Marks a class as an MCP tool
Name - The tool name (default: class name)Description - A description of the tool. This should be used to provide additional context to the tool. Will use the XML summary (see relevant section) if not provided.)[McpFunction] - Marks a method as an MCP function
Name - The function name (default: method name)Description - A description of the function. This should be used to provide additional context to the function. Will use the XML summary (see relevant section) if not provided.)[McpParameter] - Provides metadata for function parameters
Description - A description of the parameter. This should be used to provide additional context to the parameter.Required - Whether the parameter is required (default: false)Here's a complete example:
using MCPSharp;
MCPServer.StartAsync("StringTools", "1.0.0");
[McpTool("string_tools", "String manipulation utilities")]
public class StringTools
{
[McpFunction("concat", "Concatenates two strings with separator")]
public static string Concat(
[McpParameter(true)] string first,
[McpParameter(true)] string second,
[McpParameter(Description = "Separator between strings")] string separator = " "
)
{
return $"{first}{separator}{second}";
}
}MCPSharp can automatically extract documentation from XML comments to provide rich descriptions for your tools and functions in the MCP schema.
.csproj), add or modify the following settings:<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>Use standard XML documentation comments on your tools and functions:
/// <summary>
/// Provides mathematical operations
/// </summary>
[McpTool("calculator")]
public class Calculator
{
/// <summary>
/// Adds two numbers together
/// </summary>
/// <param name="a">The first number to add</param>
/// <param name="b">The second number to add</param>
/// <returns>The sum of the two numbers</returns>
[McpFunction]
public static int Add(
[McpParameter(true)] int a,
[McpParameter(true)] int b)
{
return a + b;
}
}MCPSharp uses the following priority order when determining descriptions:
[McpTool(Description = "...")])We have several features planned for future releases of MCPSharp:
The XML documentation also provides IntelliSense support in IDEs when consuming your MCP tools as a library.
We welcome contributions! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.