/ Directory / Playground / MCPSharp
● Community afrise ⚡ Instant

MCPSharp

by afrise · afrise/MCPSharp

Ship MCP in C#/.NET — decorate a method with [McpTool], start the server, done. Works with Microsoft.Extensions.AI and Semantic Kernel.

MCPSharp is a .NET NuGet package that turns C# methods into MCP tools (and resources) via attributes, and also provides a client to call other MCP servers from C#. It handles JSON-RPC plumbing, automatic parameter validation and type conversion, and pulls tool descriptions from your XML doc comments. First-class integrations for Microsoft.Extensions.AI and Semantic Kernel mean you can plug MCP-tooled agents into .NET apps with minimal boilerplate.

Why use it

Key features

Live Demo

What it looks like in practice

mcpsharp.replay ▶ ready
0/0

Install

Pick your client

~/Library/Application Support/Claude/claude_desktop_config.json  · Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "mcpsharp": {
      "command": "dotnet",
      "args": [
        "add",
        "package",
        "MCPSharp"
      ],
      "_inferred": false
    }
  }
}

Open Claude Desktop → Settings → Developer → Edit Config. Restart after saving.

~/.cursor/mcp.json · .cursor/mcp.json
{
  "mcpServers": {
    "mcpsharp": {
      "command": "dotnet",
      "args": [
        "add",
        "package",
        "MCPSharp"
      ],
      "_inferred": false
    }
  }
}

Cursor uses the same mcpServers schema as Claude Desktop. Project config wins over global.

VS Code → Cline → MCP Servers → Edit
{
  "mcpServers": {
    "mcpsharp": {
      "command": "dotnet",
      "args": [
        "add",
        "package",
        "MCPSharp"
      ],
      "_inferred": false
    }
  }
}

Click the MCP Servers icon in the Cline sidebar, then "Edit Configuration".

~/.codeium/windsurf/mcp_config.json
{
  "mcpServers": {
    "mcpsharp": {
      "command": "dotnet",
      "args": [
        "add",
        "package",
        "MCPSharp"
      ],
      "_inferred": false
    }
  }
}

Same shape as Claude Desktop. Restart Windsurf to pick up changes.

~/.continue/config.json
{
  "mcpServers": [
    {
      "name": "mcpsharp",
      "command": "dotnet",
      "args": [
        "add",
        "package",
        "MCPSharp"
      ]
    }
  ]
}

Continue uses an array of server objects rather than a map.

~/.config/zed/settings.json
{
  "context_servers": {
    "mcpsharp": {
      "command": {
        "path": "dotnet",
        "args": [
          "add",
          "package",
          "MCPSharp"
        ]
      }
    }
  }
}

Add to context_servers. Zed hot-reloads on save.

claude mcp add mcpsharp -- dotnet add package MCPSharp

One-liner. Verify with claude mcp list. Remove with claude mcp remove.

Use Cases

Real-world ways to use MCPSharp

Expose existing C# services as MCP tools

👤 .NET shops with internal libraries they want AI agents to call ⏱ ~30 min intermediate

When to use: You have a working business library in C# and need to make it agent-accessible.

Prerequisites
  • .NET 8+ SDK — https://dotnet.microsoft.com/download
  • MCPSharp package — dotnet add package MCPSharp
Flow
  1. Decorate methods
    Add [McpTool] to the public methods I want exposed in my OrdersService. Keep signatures; let MCPSharp validate.✓ Copied
    → Attributes applied, XML docs used as descriptions
  2. Start the server
    Add a Program.cs entry point that calls MCPServer.StartAsync("orders", "1.0.0").✓ Copied
    → Runs via dotnet run; MCP clients can discover tools
  3. Wire into Claude Desktop
    Emit a claude_desktop_config.json snippet that launches my dotnet binary.✓ Copied
    → Config block with command=dotnet args=[run --project, path]

Outcome: An MCP server your C# services power, with little new code.

Pitfalls
  • Returning large objects without shaping them — Return DTOs — don't serialize EF entities with nav properties, you'll blow up responses
Combine with: fastmcp

Use MCP tools from a Semantic Kernel agent

👤 Teams building agents with Microsoft Semantic Kernel ⏱ ~45 min intermediate

When to use: You're on SK and want to consume an external MCP server's tools.

Flow
  1. Add the client
    Add MCPSharp's client and register the remote server as an SK plugin.✓ Copied
    → Tools appear in the SK plan
  2. Call from a function
    Show a chat function that calls one of the remote tools when the user asks.✓ Copied
    → Round-trip: prompt → SK selects tool → MCP call → result in reply

Outcome: Your SK app can consume any MCP server cleanly.

Register tools at runtime from a plugin folder

👤 Teams shipping plugin systems or low-code products ⏱ ~60 min advanced

When to use: Users drop in new tool definitions and you want them available without redeploy.

Flow
  1. Scan the folder
    Write a loader that reflects each .dll and registers [McpTool] methods dynamically.✓ Copied
    → Tools appear without changing hard-coded lists
  2. Hot-reload safely
    Add AssemblyLoadContext isolation so reloads don't leak.✓ Copied
    → Old assemblies unloaded between reloads

Outcome: Pluggable tool set with proper assembly lifecycle.

Pitfalls
  • Leaking assemblies with default load context — Use collectible AssemblyLoadContext for isolated reloads

Combinations

Pair with other MCPs for X10 leverage

mcpsharp + fastmcp

Compare .NET vs Python MCP server authoring

Port my Python FastMCP tools to MCPSharp for the existing .NET stack.✓ Copied
mcpsharp + csharp-sdk

Official SDK vs community package — pick what fits

Compare MCPSharp and the official csharp-sdk for my needs.✓ Copied
mcpsharp + azure-ai-gateway

Put the C# MCP server behind Azure AI gateway policies

Front my MCPSharp server with the Azure AI gateway for auth/rate limits.✓ Copied

Tools

What this MCP exposes

ToolInputsWhen to callCost
[McpTool] C# method signature On every method you want an agent to call 0
[McpResource] C# member When you want to expose readable resources (files/data) not tools 0
[McpParameter] method parameter Mark required params and give parameter descriptions 0
MCPServer.StartAsync name, version Start the server over stdio (or configured transport) 0
MCPClient server address From C# to talk to another MCP server 0

Cost & Limits

What this costs to run

API quota
None — it's a library
Tokens per call
Depends on your tools; the library itself is tiny
Monetary
Free (MIT-style OSS)
Tip
Shape response DTOs intentionally — big JSON blobs inflate both tokens and latency.

Security

Permissions, secrets, blast radius

Credential storage: Whatever your services use (ASP.NET config, KeyVault, etc.). MCPSharp doesn't own credentials.
Data egress: Transport-dependent (stdio local; HTTP/remote if configured).

Troubleshooting

Common errors and fixes

Tool not discovered by client

Confirm the method is public, has [McpTool], and the attribute name matches what you call it. Also check the server name passed to StartAsync.

Verify: MCPServer.ListTools() at startup
Serialization exception on return

Return a POCO/DTO; avoid lazy-loaded EF entities and types with cyclic references.

Verify: Unit test serialization with System.Text.Json
Semantic Kernel doesn't see tools

Register the MCP plugin explicitly and confirm the kernel builder is aware; some SK versions require explicit import.

Verify: Log plugins at startup

Alternatives

MCPSharp vs others

AlternativeWhen to use it insteadTradeoff
csharp-sdk (official)You want the official Anthropic/MCP C# SDKDifferent ergonomics; watch for feature differences
fastmcpYou prefer PythonDifferent ecosystem
mcp-agentYou want Python-based agent patterns, not just server authoringNot .NET

More

Resources

📖 Read the official README on GitHub

🐙 Browse open issues

🔍 Browse all 400+ MCP servers and Skills