
Security News
TC39 Advances 11 Proposals for Math Precision, Binary APIs, and More
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
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"}});
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.
FAQs
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"}});
We found that mcpsharp demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.
Research
/Security News
A flawed sandbox in @nestjs/devtools-integration lets attackers run code on your machine via CSRF, leading to full Remote Code Execution (RCE).
Product
Customize license detection with Socket’s new license overlays: gain control, reduce noise, and handle edge cases with precision.