MCPSharp 1.0.4
dotnet add package MCPSharp --version 1.0.4
NuGet\Install-Package MCPSharp -Version 1.0.4
<PackageReference Include="MCPSharp" Version="1.0.4" />
paket add MCPSharp --version 1.0.4
#r "nuget: MCPSharp, 1.0.4"
// Install MCPSharp as a Cake Addin #addin nuget:?package=MCPSharp&version=1.0.4 // Install MCPSharp as a Cake Tool #tool nuget:?package=MCPSharp&version=1.0.4
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:
- Create MCP-compliant tools and functions that AI models can discover and use
- Connect directly to existing MCP servers from C# code with an easy to use client
- Expose your .NET methods as MCP endpoints with simple attributes
- Handle MCP protocol details and JSON-RPC communication seamlessly
🚀 What's New in MCPSharp
- Microsoft.Extensions.AI Integration: MCPSharp now integrates with Microsoft.Extensions.AI, allowing tools to be exposed as AIFunctions
- Semantic Kernel Support: Add tools using Semantic Kernel's KernelFunctionAttribute
- Dynamic Tool Registration: Register tools on-the-fly with custom implementation logic
- Tool Change Notifications: Server now notifies clients when tools are added, updated, or removed
- Complex Object Parameter Support: Better handling of complex objects in tool parameters
- Better Error Handling: Improved error handling with detailed stack traces
When to Use MCPSharp
Use MCPSharp when you want to:
- Create tools that AI assistants like Anthropic's Claude Desktop can use
- Build MCP-compliant APIs without dealing with the protocol details
- Expose existing .NET code as MCP endpoints
- Add AI capabilities to your applications through standardized interfaces
- Integrate with Microsoft.Extensions.AI and/or Semantic Kernel without locking into a single vendor
Features
- Easy-to-use attribute-based API (
[McpTool]
,[McpResource]
) - Built-in JSON-RPC support with automatic request/response handling
- Automatic parameter validation and type conversion
- Rich documentation support through XML comments
- Near zero configuration required for basic usage
Prerequisites
- Any version of .NET that supports standard 2.0
Installation
dotnet add package MCPSharp
Quick Start
1. Define a Tool
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;
}
}
2. Start the Server
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.
Advanced Usage
Dynamic Tool Registration
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}"; });
Resources API
Define and expose resources:
[McpResource("settings", "resource://settings", "application/json", "Application settings")]
public string Settings { get; set; } = "{ \"theme\": \"dark\" }";
// Or as a method
[McpResource("user", "resource://{name}", "text/plain", "User information")]
public string GetUser(string name) => $"Information about {name}";
Use with Microsoft.Extensions.AI
// 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.
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>();
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!
API Reference
Attributes
[McpTool]
- Marks a class or method as an MCP tool- Optional parameters:
Name
- The tool name (default: class/method name)Description
- Description of the tool
- Optional parameters:
[McpParameter]
- Provides metadata for function parameters- Optional parameters:
Description
- Parameter descriptionRequired
- Whether the parameter is required (default: false)
- Optional parameters:
[McpResource]
- Marks a property or method as an MCP resource- Parameters:
Name
- Resource nameUri
- Resource URI (can include templates)MimeType
- MIME type of the resourceDescription
- Resource description
- Parameters:
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, 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 AIFunctions
XML Documentation Support
MCPSharp automatically extracts documentation from XML comments:
/// <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>
[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.
Migration Notes
[McpFunction]
is deprecated and replaced with[McpTool]
for better alignment with MCP standards- Use
MCPServer.Register<T>()
instead ofMCPServer.RegisterTool<T>()
for consistency (old method still works but is deprecated)
Contributing
We welcome contributions! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.AspNetCore.Hosting (>= 2.3.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
- Microsoft.AspNetCore.Routing (>= 2.3.0)
- Microsoft.AspNetCore.Server.Kestrel (>= 2.3.0)
- Microsoft.Bcl.AsyncInterfaces (>= 9.0.2)
- Microsoft.Extensions.AI (>= 9.3.0-preview.1.25114.11)
- Microsoft.Extensions.AI.Abstractions (>= 9.3.0-preview.1.25114.11)
- Microsoft.Extensions.Hosting (>= 9.0.2)
- Microsoft.SemanticKernel.Abstractions (>= 1.38.0)
- StreamJsonRpc (>= 2.21.10)
- System.Net.ServerSentEvents (>= 9.0.2)
-
net8.0
- Microsoft.AspNetCore.Hosting (>= 2.3.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
- Microsoft.AspNetCore.Routing (>= 2.3.0)
- Microsoft.AspNetCore.Server.Kestrel (>= 2.3.0)
- Microsoft.Bcl.AsyncInterfaces (>= 9.0.2)
- Microsoft.Extensions.AI (>= 9.3.0-preview.1.25114.11)
- Microsoft.Extensions.AI.Abstractions (>= 9.3.0-preview.1.25114.11)
- Microsoft.Extensions.Hosting (>= 9.0.2)
- Microsoft.SemanticKernel.Abstractions (>= 1.38.0)
- StreamJsonRpc (>= 2.21.10)
- System.Net.ServerSentEvents (>= 9.0.2)
-
net9.0
- Microsoft.AspNetCore.Hosting (>= 2.3.0)
- Microsoft.AspNetCore.Mvc.Core (>= 2.3.0)
- Microsoft.AspNetCore.Routing (>= 2.3.0)
- Microsoft.AspNetCore.Server.Kestrel (>= 2.3.0)
- Microsoft.Bcl.AsyncInterfaces (>= 9.0.2)
- Microsoft.Extensions.AI (>= 9.3.0-preview.1.25114.11)
- Microsoft.Extensions.AI.Abstractions (>= 9.3.0-preview.1.25114.11)
- Microsoft.Extensions.Hosting (>= 9.0.2)
- Microsoft.SemanticKernel.Abstractions (>= 1.38.0)
- StreamJsonRpc (>= 2.21.10)
- System.Net.ServerSentEvents (>= 9.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.4 | 125 | 3/3/2025 |
1.0.3 | 61 | 3/2/2025 |
1.0.2 | 49 | 3/2/2025 |
0.9.12 | 114 | 2/27/2025 |
0.9.11 | 123 | 2/26/2025 |
0.9.10 | 91 | 2/25/2025 |
0.9.9 | 122 | 2/25/2025 |
0.9.8 | 99 | 2/21/2025 |
0.9.7 | 108 | 2/18/2025 |
0.9.6 | 102 | 2/18/2025 |
0.9.5 | 95 | 2/18/2025 |
0.9.4 | 92 | 2/18/2025 |
0.9.3 | 91 | 2/17/2025 |
0.9.2 | 91 | 2/17/2025 |
* Microsoft.Extensions.AI Integration: MCPSharp now integrates with Microsoft.Extensions.AI, allowing tools to be exposed as AIFunctions
* Semantic Kernel Support: Add tools using Semantic Kernel's KernelFunctionAttribute
* Dynamic Tool Registration: Register tools on-the-fly with custom implementation logic
* Tool Change Notifications: Server now notifies clients when tools are added, updated, or removed
* Complex Object Parameter Support: Better handling of complex objects in tool parameters
* Better Error Handling: Improved error handling with detailed stack traces