Skip to main content
The C# samples use the ModelContextProtocol NuGet package and Microsoft.Extensions.Hosting to create MCP servers with minimal boilerplate.

Basic calculator server

The basic sample (03-GettingStarted/samples/csharp) is a stdio MCP server that exposes five calculator tools: Add, Subtract, Multiply, Divide, and IsPrime.
1

Clone and navigate

git clone https://github.com/microsoft/mcp-for-beginners.git
cd mcp-for-beginners/03-GettingStarted/samples/csharp
2

Review dependencies

The project file targets .NET 9 and references two packages:
calculator.csproj
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="9.*" />
    <PackageReference Include="ModelContextProtocol" Version="0.*-*" />
  </ItemGroup>
</Project>
3

Run the server

dotnet run --project ./src/calculator.csproj

Server code

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(consoleLogOptions =>
{
    // Configure all logs to go to stderr
    consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();
await builder.Build().RunAsync();
Tools are discovered automatically via WithToolsFromAssembly(). Any static method decorated with [McpServerTool] is registered as an MCP tool — no manual registration needed.

Configure in VS Code

Create .vscode/mcp.json in your workspace root:
.vscode/mcp.json
{
  "inputs": [
    {
      "type": "promptString",
      "id": "repository-root",
      "description": "Absolute path to the repository root"
    }
  ],
  "servers": {
    "calculator-mcp-dotnet": {
      "type": "stdio",
      "command": "dotnet",
      "args": [
        "run",
        "--project",
        "${input:repository-root}/03-GettingStarted/samples/csharp/src/calculator.csproj"
      ]
    }
  }
}

Run in Docker

Build and push a container image for the server:
# Build
docker build -t <YOUR-DOCKER-USERNAME>/mcp-calculator ./src

# Push
docker push <YOUR-DOCKER-USERNAME>/mcp-calculator
Then update mcp.json to run from the image:
.vscode/mcp.json
{
  "servers": {
    "mcp-calc": {
      "command": "docker",
      "args": ["run", "--rm", "-i", "<YOUR-DOCKER-USERNAME>/mcp-calculator"]
    }
  }
}

Build docs developers (and LLMs) love