
Write HTML. Render video. Built for agents.
MCPSharp gives C# projects a way to speak Model Context Protocol without dealing with the protocol details directly. You can mark methods with attributes like `[McpTool]` and `[McpResource]`, start an MCP server, or create a client that discovers tools and calls them. It also includes support for Microsoft.Extensions.AI and Semantic Kernel, so builders can turn MCP tools into AIFunctions or register KernelFunction methods. XML comments, parameter validation, and dynamic tool registration are part of the library.
Builders who want to connect .NET apps to MCP-compatible assistants or expose their code as MCP tools.
You can add standardized AI tool and resource endpoints to a .NET app without building the MCP plumbing yourself.
Use `[McpTool]`, `[McpParameter]`, and `[McpResource]` to describe callable tools and exposed resources.
Start a server with `MCPServer.StartAsync(...)` and register tool classes with `MCPServer.Register<T>()`.
Create an `MCPClient` to list tools, call tools, and fetch resources from another MCP server.
Add tools at runtime with `MCPServer.AddToolHandler(...)` and provide custom implementation logic.
Convert MCP tools into `AIFunction` objects with `client.GetFunctionsAsync()`.
Register methods marked with `KernelFunction` so they can be exposed through MCPSharp.
Pull tool names and descriptions from XML comments to keep MCP metadata in sync with code.
dotnet add package MCPSharp
MCPSharp is a .NET library that helps you build Model Context Protocol (MCP) servers and clients - the standardized API protocol used by AI assistants and models. With MCPSharp, you can:
Use MCPSharp when you want to:
[McpTool], [McpResource])dotnet add package MCPSharp
Create a class and mark your method(s) with the [McpTool] attribute:
using MCPSharp;
public class Calculator
{
[McpTool("add", "Adds two numbers")] // Note: [McpFunction] is deprecated, use [McpTool] instead
public static int Add([McpParameter(true)] int a, [McpParameter(true)] int b)
{
return a + b;
}
}
await MCPServer.StartAsync("CalculatorServer", "1.0.0");
The StartAsync() method will automatically find any methods in the base assembly that are marked with the McpTool attribute. In order to add any methods that are in a referenced library, you can manually register them by calling MCPServer.Register<T>(); with T being the class containing the desired methods. If your methods are marked with Semantic Kernel attributes, this will work as well. If the client supports list changed notifications, it will be notified when additional tools are registered.
Register tools dynamically with custom implementation:
MCPServer.AddToolHandler(new Tool()
{
Name = "dynamicTool",
Description = "A dynamic tool",
InputSchema = new InputSchema {
Type = "object",
Required = ["input"],
Properties = new Dictionary<string, ParameterSchema>{
{"input", new ParameterSchema{Type="string", Description="Input value"}}
}
}
}, (string input) => { return $"You provided: {input}"; });
// Client-side integration
MCPClient client = new("AIClient", "1.0", "path/to/mcp/server");
IList<AIFunction> functions = await client.GetFunctionsAsync();
This list can be plugged into the ChatOptions.Tools property for an IChatClient, Allowing MCP servers to be used seamlessly with Any IChatClient Implementation.
using Microsoft.SemanticKernel;
public class MySkillClass
{
[KernelFunction("MyFunction")]
[Description("Description of my function")]
public string MyFunction(string input) => $"Processed: {input}";
}
// Register with MCPServer
MCPServer.Register<MySkillClass>();
Currently, This is the only way to make a Semantic kernel method registerable with the MCP server. If you have a use case that is not covered here, please reach out!
[McpTool] - Marks a class or method as an MCP tool
Name - The tool name (default: class/method name)Description - Description of the tool[McpParameter] - Provides metadata for function parameters
Description - Parameter descriptionRequired - Whether the parameter is required (default: false)[McpResource] - Marks a property or method as an MCP resource
Name - Resource nameUri - Resource URI (can include templates)MimeType - MIME type of the resourceDescription - Resource descriptionMCPServer.StartAsync(string serverName, string version) - Starts the MCP serverMCPServer.Register<T>() - Registers a class containing tools or resourcesMCPServer.AddToolHandler(Tool tool, Delegate func) - Registers a dynamic toolnew MCPClient(string name, string version, string server, string args = null, IDictionary<string, string> env = null) - Create a client instanceclient.GetToolsAsync() - Get available toolsclient.CallToolAsync(string name, Dictionary<string, object> parameters) - Call a toolclient.GetResourcesAsync() - Get available resourcesclient.GetFunctionsAsync() - Get tools as AIFunctionsMCPSharp automatically extracts documentation from XML comments:
/// <summary>
/// Provides mathematical operations
/// </summary>
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>
[McpTool]
public static int Add(
[McpParameter(true)] int a,
[McpParameter(true)] int b)
{
return a + b;
}
}
Enable XML documentation in your project file:
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
This allows you to be able to quickly change the names and descriptions of your MCP tools without having to recompile. For example, if you find the model is having trouble understanding how to use it correctly.
[McpFunction] is deprecated and replaced with [McpTool] for better alignment with MCP standardsMCPServer.Register<T>() instead of MCPServer.RegisterTool<T>() for consistency (old method still works but is deprecated)We welcome contributions! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
Sign in to join the discussion.
No comments yet. Be the first to say what this is good for.

Write HTML. Render video. Built for agents.
Ultra-lightweight, open-source, self-hosted personal AI agent framework in Python with WebUI, tools, memory, MCP, multi-agent workflows, automation, and chat apps
SkillOpt is a text-space optimizer that trains reusable natural-language skills for frozen LLM agents through trajectory-driven edits, validation-gated updates, and deployable best_skill.md artifacts.

Omnigent is an open-source AI agent framework and meta-harness: orchestrate Claude Code, Codex, Cursor, Pi, and custom agents โ swap harnesses without rewriting, enforce policies and sandboxing, and collaborate in real time from any device.
A theoretical reconstruction of the Claude Mythos architecture, built from first principles using the available research literature.
๐ท๏ธ An adaptive Web Scraping framework that handles everything from a single request to a full-scale crawl!