nissynicolas/CSharp-MCP-Server
If you are the rightful owner of CSharp-MCP-Server and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.
A Model Context Protocol (MCP) server implementation in C# using the official MCP SDK, providing tools for file system management, text processing, and system information collection.
C# MCP Server
A Model Context Protocol (MCP) server implementation in C# using the official MCP SDK. This server provides 18 comprehensive tools across three categories for file system management, text processing, and system information collection.
โจ Features
This MCP server includes 18 individual tools across three categories:
๐ File System Tools (3 tools)
- ListDirectory: Browse directory contents with detailed file information
- ReadFile: Read file contents with security checks and size limits
- FileExists: Check if files or directories exist in the system
๐ Text Processing Tools (9 tools)
- CountWords: Count the number of words in provided text
- CountCharacters: Count total characters in text
- CountLines: Count the number of lines in text
- ToUpperCase: Convert text to uppercase
- ToLowerCase: Convert text to lowercase
- ReverseText: Reverse the provided text
- TrimAndCleanWhitespace: Remove extra whitespace and trim text
- ExtractWords: Extract words and return as comma-separated list
- FindAndReplace: Find and replace text using regex patterns
๐ฅ๏ธ System Information Tools (7 tools)
- GetOperatingSystemInfo: Get OS details, platform, and architecture
- GetUserInfo: Get current user and machine information
- GetRuntimeInfo: Get .NET runtime and version information
- GetEnvironmentVariables: Get system environment variables (limited for security)
- GetSystemPerformanceInfo: Get memory usage and system performance metrics
- GetDirectoryInfo: Get current directory and special folder paths
- GetNetworkInfo: Get hostname and network configuration details
๐ Quick Start
Prerequisites
- .NET 9.0 SDK or later
- VS Code with MCP extensions or Cursor IDE (recommended)
Dependencies
ModelContextProtocol
(0.4.0-preview.1) - Official MCP SDK for .NETMicrosoft.Extensions.Hosting
(9.0.9) - For dependency injection and hosting
Build and Run
# Build the project
dotnet build
# Run the MCP server
dotnet run
The server communicates via JSON-RPC over stdin/stdout, making it compatible with any MCP client.
๐ ๏ธ Integration with IDEs
Cursor IDE (Recommended)
Add to your Cursor MCP configuration:
{
"mcpServers": {
"csharp-tools": {
"command": "dotnet",
"args": ["run", "--project", "path/to/CSharp-MCP-Server", "--no-build"],
"cwd": "path/to/CSharp-MCP-Server"
}
}
}
VS Code
Install one of these MCP extensions:
- Copilot MCP (
automatalabs.copilot-mcp
) - MCP-Client (
m1self.mcp-client
) - MCP Server Runner (
zebradev.mcp-server-runner
)
Then configure your server in the extension settings.
๐ Usage Examples
List All Available Tools
{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}
File System Operations
{
"jsonrpc": "2.0", "id": 2, "method": "tools/call",
"params": {
"name": "ListDirectory",
"arguments": {"path": "C:\\Users"}
}
}
Text Processing
{
"jsonrpc": "2.0", "id": 3, "method": "tools/call",
"params": {
"name": "CountWords",
"arguments": {"text": "Hello world, this is a test."}
}
}
System Information
{
"jsonrpc": "2.0", "id": 4, "method": "tools/call",
"params": {
"name": "GetOperatingSystemInfo",
"arguments": {}
}
}
๐๏ธ Project Structure
CSharp-MCP-Server/
โโโ ๐ .github/ # GitHub workflows and configurations
โโโ ๐ .vscode/ # VS Code settings and configurations
โโโ ๐ CSharpMcpServer.csproj # Project file with dependencies
โโโ ๐ Program.cs # Application entry point with MCP SDK setup
โโโ ๐ FileSystemTools.cs # File system operations (3 tools)
โโโ ๐ TextProcessorTools.cs # Text processing operations (9 tools)
โโโ ๐ SystemInfoTools.cs # System information collection (7 tools)
โโโ ๐ test-mcp.bat # Testing script for Windows
โโโ ๐ README.md # This documentation
๐ง Technical Implementation
This server follows the official MCP SDK patterns:
Key Features
- ๐ Attribute-based Discovery: Tools are automatically discovered using
[McpServerToolType]
and[McpServerTool]
attributes - ๐ Dependency Injection: Built on
Microsoft.Extensions.Hosting
for robust dependency management - ๐ก Standard IO Transport: Uses stdin/stdout for JSON-RPC communication
- ๐ Automatic Registration:
WithToolsFromAssembly()
automatically registers all annotated tools
Code Structure
Each tool category is implemented as a static class:
[McpServerToolType]
public static class FileSystemTools
{
[McpServerTool, Description("Lists files and directories")]
public static string ListDirectory(string path) { /* implementation */ }
}
Program.cs (Entry Point)
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateEmptyApplicationBuilder(null);
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
var app = builder.Build();
await app.RunAsync();
๐ก๏ธ Security & Best Practices
- ๐ File Access: File operations respect system permissions and include size limits
- ๐ Environment Variables: Limited exposure of sensitive environment data
- โ Input Validation: All tools include comprehensive parameter validation
- โก Resource Limits: Processing operations have reasonable size and time limits
- ๐ซ Error Handling: Graceful error handling with descriptive messages
๐งช Testing Your MCP Server
Manual Testing
# Test tools list
echo '{"jsonrpc": "2.0", "id": 1, "method": "tools/list"}' | dotnet run
# Test a specific tool
echo '{"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "ListDirectory", "arguments": {"path": "."}}}' | dotnet run
Using the Test Script
# Windows
test-mcp.bat
๐ Extending the Server
To add new tools:
- Add to existing classes or create new
[McpServerToolType]
classes - Annotate methods with
[McpServerTool]
and[Description]
attributes - Document parameters using
[Description]
on parameters - Automatic discovery - the MCP SDK will find and register new tools
Example New Tool
[McpServerTool, Description("Calculates the factorial of a number")]
public static long CalculateFactorial([Description("The number to calculate factorial for")] int number)
{
if (number < 0) throw new ArgumentException("Number must be non-negative");
if (number == 0) return 1;
long result = 1;
for (int i = 1; i <= number; i++)
{
result *= i;
}
return result;
}
๐ฏ Compatible MCP Clients
This server works with any MCP client that supports JSON-RPC over stdin/stdout:
- โ Cursor IDE (Native MCP support)
- โ VS Code (With MCP extensions)
- โ Claude Desktop (With configuration)
- โ Custom MCP clients (Following MCP specification)
๐ Requirements
- Runtime: .NET 9.0 SDK or later
- OS Support: Windows, macOS, and Linux
- MCP Client: Any client supporting JSON-RPC over stdin/stdout
- Development: Visual Studio Code or Cursor IDE recommended
๐ License
This project is available under the MIT License.
๐ References
Built with โค๏ธ using the official MCP SDK for .NET