Skip to content

A starter MCP server in C# using the official ModelContextProtocol SDK 0.5.0-preview.1. Features tools, resources, prompts with annotations and enums.

License

Notifications You must be signed in to change notification settings

SamMorrowDrums/mcp-csharp-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

MCP C# Starter

CI .NET C# License: MIT MCP

A feature-complete Model Context Protocol (MCP) server template in C# using the official csharp-sdk. This starter demonstrates all major MCP features with clean, idiomatic C# code leveraging .NET 8 and dependency injection.

πŸ“š Documentation

✨ Features

Category Feature Description
Tools hello Basic tool with annotations
get_weather Tool returning structured JSON
ask_llm Tool that invokes LLM sampling
long_task Tool with progress updates
load_bonus_tool Dynamically loads a new tool
bonus_calculator Calculator (dynamically loaded)
Resources info://about Static informational resource
file://example.md File-based markdown resource
Templates greeting://{name} Personalized greeting
data://items/{id} Data lookup by ID
Prompts greet Greeting in various styles
code_review Code review with focus areas

πŸš€ Quick Start

Prerequisites

Installation

# Clone the repository
git clone https://github.com/SamMorrowDrums/mcp-csharp-starter.git
cd mcp-csharp-starter

# Restore packages
dotnet restore

Running the Server

stdio transport (for local development):

dotnet run

HTTP transport (for remote/web deployment):

dotnet run -- --http
# Or with custom port:
dotnet run -- --http --port 8080
# Server runs on http://localhost:3000 by default

πŸ”§ VS Code Integration

This project includes VS Code configuration for seamless development:

  1. Open the project in VS Code
  2. The MCP configuration is in .vscode/mcp.json
  3. Build with Ctrl+Shift+B (or Cmd+Shift+B on Mac)
  4. Debug with F5 (configurations for both transports)
  5. Test the server using VS Code's MCP tools

Using DevContainers

  1. Install the Dev Containers extension
  2. Open command palette: "Dev Containers: Reopen in Container"
  3. Everything is pre-configured and ready to use!

πŸ“ Project Structure

.
β”œβ”€β”€ Program.cs             # Main entry point (stdio/HTTP)
β”œβ”€β”€ Tools/
β”‚   └── AllTools.cs        # All tool definitions
β”œβ”€β”€ Resources/
β”‚   └── AllResources.cs    # All resource definitions
β”œβ”€β”€ Prompts/
β”‚   └── AllPrompts.cs      # All prompt definitions
β”œβ”€β”€ .vscode/
β”‚   β”œβ”€β”€ mcp.json           # MCP server configuration
β”‚   β”œβ”€β”€ tasks.json         # Build/run tasks
β”‚   β”œβ”€β”€ launch.json        # Debug configurations
β”‚   └── extensions.json
β”œβ”€β”€ .devcontainer/
β”‚   └── devcontainer.json
β”œβ”€β”€ McpCSharpStarter.csproj
β”œβ”€β”€ global.json
└── appsettings.json

πŸ› οΈ Development

# Development with live reload (recommended)
dotnet watch run

# Build
dotnet build

# Run tests
dotnet test

# Format code
dotnet format

# Clean
dotnet clean

# Publish for production
dotnet publish -c Release

Live Reload

The dotnet watch run command provides automatic rebuilds during development. Changes to any .cs file will automatically rebuild and restart the server.

πŸ” MCP Inspector

The MCP Inspector is an essential development tool for testing and debugging MCP servers.

Running Inspector

npx @modelcontextprotocol/inspector -- dotnet run -- --stdio

What Inspector Provides

  • Tools Tab: List and invoke all registered tools with parameters
  • Resources Tab: Browse and read resources and templates
  • Prompts Tab: View and test prompt templates
  • Logs Tab: See JSON-RPC messages between client and server
  • Schema Validation: Verify tool input/output schemas

Debugging Tips

  1. Start Inspector before connecting your IDE/client
  2. Use the "Logs" tab to see exact request/response payloads
  3. Test tool annotations (ReadOnlyHint, etc.) are exposed correctly
  4. Verify progress notifications appear for long_task
  5. Check that McpServer injection works for sampling tools

πŸ“– Feature Examples

Tool with Attributes

[McpServerToolType]
public class GreetingTools
{
    [McpServerTool(Name = "hello", Title = "Say Hello")]
    [Description("A friendly greeting tool")]
    public static string Hello(
        [Description("The name to greet")] string name)
    {
        return $"Hello, {name}!";
    }
}

Resource Template

[McpServerResourceType]
public class StaticResources
{
    [McpServerResource(
        UriTemplate = "greeting://{name}",
        Name = "Personalized Greeting",
        MimeType = "text/plain")]
    public static string Greeting(string name)
    {
        return $"Hello, {name}!";
    }
}

Tool with Sampling

[McpServerTool(Name = "ask_llm")]
public static async Task<string> AskLlm(
    McpServer server,
    [Description("The prompt")] string prompt,
    CancellationToken cancellationToken)
{
    var result = await server.SampleAsync(
        new CreateMessageRequestParams
        {
            Messages = [
                new SamplingMessage
                {
                    Role = Role.User,
                    Content = [new TextContentBlock { Text = prompt }]
                }
            ],
            MaxTokens = 100
        },
        cancellationToken: cancellationToken);
    
    return result.Content.OfType<TextContentBlock>()
        .FirstOrDefault()?.Text ?? "";
}

Prompt Definition

[McpServerPromptType]
public class CodeReviewPrompts
{
    [McpServerPrompt(Name = "code_review", Title = "Code Review")]
    public static IEnumerable<PromptMessage> CodeReview(
        [Description("The code to review")] string code,
        [Description("Programming language")] string language)
    {
        return [
            new PromptMessage
            {
                Role = Role.User,
                Content = new TextContentBlock 
                { 
                    Text = $"Review this {language} code:\n```{language}\n{code}\n```" 
                }
            }
        ];
    }
}

πŸ” Configuration

Configuration via appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  }
}

🀝 Contributing

Contributions welcome! Please ensure your changes maintain feature parity with other language starters.

πŸ“„ License

MIT License - see LICENSE for details.

About

A starter MCP server in C# using the official ModelContextProtocol SDK 0.5.0-preview.1. Features tools, resources, prompts with annotations and enums.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages