MCPSharp
by: afrise
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.
📌Overview
Purpose: MCPSharp is designed to facilitate the development of Model Context Protocol (MCP) servers and clients, providing a standardized API for AI assistants and models.
Overview: MCPSharp is a robust .NET library that allows developers to create and expose MCP-compliant tools and APIs seamlessly. It abstracts the complexities of the MCP protocol, enabling easy integration with AI functionality in applications while supporting a flexible and attribute-based programming model.
Key Features:
-
Easy-to-use Attribute-based API: Developers can easily define tools and resources using attributes like
[McpTool]
, simplifying the process of creating MCP endpoints. -
Built-in JSON-RPC Support: Automatic handling of request and response communication streamlines interactions with the server, eliminating the need for extensive boilerplate code.
-
Dynamic Tool Registration: Allows for on-the-fly registration of tools, accommodating custom implementations and enhancing flexibility in tool management.
-
Error Handling & Complex Object Support: Improved error handling features provide detailed insights during development, while enhanced support for complex object parameters simplifies interactions.
-
Integration with Microsoft.Extensions.AI and Semantic Kernel: MCPSharp allows broad compatibility with existing AI frameworks, making it easier to enhance applications with advanced AI capabilities.
MCPSharp
MCPSharp is a .NET library for building Model Context Protocol (MCP) servers and clients used by AI assistants and models. With MCPSharp, you can:
- Create MCP-compliant tools and functions for AI models
- Connect to existing MCP servers from C# code
- Expose your .NET methods as MCP endpoints
- Handle MCP protocol details and JSON-RPC communication seamlessly
What's New in MCPSharp
- Microsoft.Extensions.AI Integration: Tools can now be exposed as AIFunctions
- Semantic Kernel Support: Add tools with Semantic Kernel's KernelFunctionAttribute
- Dynamic Tool Registration: Register tools dynamically with custom logic
- Tool Change Notifications: Clients notified of updates to tools
- Complex Object Parameter Support: Improved handling of complex objects
- Better Error Handling: Enhanced error management with detailed stack traces
When to Use MCPSharp
Use MCPSharp to:
- Create tools for AI assistants like Claude Desktop
- Build MCP-compliant APIs without dealing with protocol details
- Expose existing .NET code as MCP endpoints
- Add standardized AI capabilities to applications
Features
- Attribute-based API (
[McpTool]
,[McpResource]
) - Built-in JSON-RPC support
- Automatic parameter validation
- Rich documentation support with XML comments
- Minimal configuration required for basic usage
Prerequisites
- .NET Standard 2.0 or later
Installation
dotnet add package MCPSharp
Quick Start
1. Define a Tool
Create a class and mark methods with the [McpTool]
attribute:
using MCPSharp;
public class Calculator
{
[McpTool("add", "Adds two numbers")]
public static int Add([McpParameter(true)] int a, [McpParameter(true)] int b)
{
return a + b;
}
}
2. Start the Server
await MCPServer.StartAsync("CalculatorServer", "1.0.0");
Advanced Usage
Dynamic Tool Registration
Register tools dynamically:
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}"; });
Use with Microsoft.Extensions.AI
MCPClient client = new("AIClient", "1.0", "path/to/mcp/server");
IList<AIFunction> functions = await client.GetFunctionsAsync();
Semantic Kernel Integration
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>();
API Reference
Attributes
[McpTool]
- Marks a method as an MCP tool[McpParameter]
- Provides metadata for function parameters[McpResource]
- Marks a property or method as an MCP resource
Server Methods
MCPServer.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 tool
Client Methods
new MCPClient(string name, string version, string server)
- 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 AIFunctions
XML Documentation Support
MCPSharp automatically extracts documentation from XML comments:
/// <summary>
/// Provides mathematical operations
/// </summary>
public class Calculator
{
/// <summary>
/// Adds two numbers together
/// </summary>
[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>
Migration Notes
[McpFunction]
is deprecated; use[McpTool]
instead- Use
MCPServer.Register<T>()
for consistency
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.