Build MCPSharp for creating and managing AI-centric MCP protocol servers and clients in .NET
MCPSharp is a .NET library designed to facilitate the creation of Model Context Protocol (MCP) servers and clients. MCP servers enable standardization in AI application integration, similar to how USB-C connects various devices with a universal interface. MCPSharp simplifies this process by providing developers with easy-to-use attributes and methods for exposing their tools and resources as API endpoints.
MCPSharp provides several core features that make it an invaluable tool for AI application integration:
[McpTool]
for easy decoration in .NET applications.The architecture of MCPSharp is designed to adhere closely to the Model Context Protocol (MCP) standards, ensuring seamless integration with various AI applications. The protocol implementation involves several key components:
Here is a Mermaid diagram outlining the protocol flow and data architecture of an MCP server:
graph TD
A[AI Application] -->|MCP Client| B[MCP Protocol]
B --> C[MCP Server]
C --> D[Data Source/Tool]
style A fill:#e1f5fe
style C fill:#f3e5f5
style D fill:#e8f5e8
The compatibility of MCPSharp with various AI applications is as follows:
MCP Client | Resources | Tools | Prompts | Status |
---|---|---|---|---|
Claude Desktop | ✅ | ✅ | ✅ | Full Support |
Continue | ✅ | ✅ | ✅ | Full Support |
Cursor | ❌ | ✅ | ❌ | Tools Only |
To get started, you can install MCPSharp via NuGet. Add the following line to your project file:
dotnet add package MCPSharp
Define a Tool: Classify methods as tools using attributes.
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;
}
}
Start the Server: Initialize and start the MCP server.
await MCPServer.StartAsync("CalculatorServer", "1.0.0");
Suppose you are developing a financial analysis tool that needs to integrate with AI applications like Continue for real-time data access and processing. You can expose your tool as an MCP server, making it available to the AI application without modifying its existing codebase.
[KernelFunction("financialAnalysis")]
public string AnalyzeData(string input) => $"Processed: {input}";
You can create a custom prompt generation tool that helps writers by providing creative prompts. This tool will be accessible through MCPSharp, allowing AI applications like Claude Desktop to utilize it seamlessly.
[KernelFunction("createPrompt")]
public string GeneratePrompt(string topic) => $"Here's your prompt: {topic}";
MCPSharp allows seamless integration with various MCP clients. For example, you can plug in an AI application via its client library to access the tools and resources exposed by your server.
// Client-side integration
MCPClient client = new("AIClient", "1.0", "path/to/mcp/server");
IList<AIFunction> functions = await client.GetFunctionsAsync();
MCPSharp is designed to be compatible with multiple MCP clients while ensuring optimal performance:
Let's consider a real-world example where we create an instant calculation tool using MCPSharp, which can be consumed by any MCP client.
public class Calculator
{
[McpTool("add", "Add two numbers")]
public static int Add([McpParameter(true)] int x, [McpParameter(true)] int y)
{
return x + y;
}
}
await MCPServer.StartAsync("CalculatorToolServer", "1.0.0");
You can configure your MCP server by setting environment variables or specifying command-line arguments.
{
"mcpServers": {
"[server-name]": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-[name]"],
"env": {
"API_KEY": "your-api-key"
}
}
}
}
Q: How do I install MCPSharp?
A: Install via NuGet using the command dotnet add package MCPSharp
.
Q: Can I use MCPSharp with Microsoft.Extensions.AI and Semantic Kernel? A: Yes, MCPSharp now integrates seamlessly with both.
Q: Is MCPSharp compatible with all AI applications? A: While compatibility is broad, specific clients may have limitations (refer to the matrix).
Q: How do I start implementing an MCP client in my application?
A: You can easily integrate by creating a new instance of MCPClient
and calling its methods.
Q: What are some best practices for securing MCP servers? A: Use secure API keys, implement authentication & authorization mechanisms.
To contribute to MCPSharp:
dotnet restore
to install project dependencies.dotnet build
followed by tests using dotnet test
.Join the MCP ecosystem by exploring additional resources:
This comprehensive documentation positions MCPSharp as a powerful tool for developers looking to integrate AI applications with ease, ensuring robust compatibility and seamless communication through the Model Context Protocol.
RuinedFooocus is a local AI image generator and chatbot image server for seamless creative control
Learn to set up MCP Airflow Database server for efficient database interactions and querying airflow data
Simplify MySQL queries with Java-based MysqlMcpServer for easy standard input-output communication
Build stunning one-page websites track engagement create QR codes monetize content easily with Acalytica
Access NASA APIs for space data, images, asteroids, weather, and exoplanets via MCP integration
Explore CoRT MCP server for advanced self-arguing AI with multi-LLM inference and enhanced evaluation methods