mcpField
M
Model Context Protocol .NET Template

Model Context Protocol .NET Template

Local
nikiforovall.blogDeveloper Tools26Updated 16w ago

About Model Context Protocol .NET Template

A .NET project template for building Model Context Protocol applications. It provides a basic project structure and example code for developers who want to create MCP servers in C#. The template supports both Stdio and HTTP modes and can be packed as a .NET tool.

Key features

  1. 01Provides dotnet new templates such as mcp-server
  2. 02Example echo server with McpServerTool attribute
  3. 03Runs in Stdio and HTTP modes
  4. 04Packs as a global .NET tool
  5. 05Includes Aspire hosting integration and MCP Inspector

Use cases

  1. 01Starting a new C# MCP server project
  2. 02Debugging a local server with MCP Inspector
  3. 03Distributing an MCP server as a command line tool

MCP support

The template generates an MCP server that registers tools from the assembly via WithToolsFromAssembly() and exposes them to MCP clients.

Configuration

Local server config is the same for every client. Copy it into your client’s MCP config file.

mcp.json · Universal config
{
"mcpServers": {
"template-dotnet": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/inspector"
]
}
}
}
Run commandnpx -y @modelcontextprotocol/inspector

Local servers need a runtime on your machine (e.g. Node.js 18+). Config file locations differ by client; see your client’s docs.

Packages
npm@modelcontextprotocol/inspector

Tools

No tool list yet

Local servers expose tools only once running on your machine, and the maintainer has not provided a tool list.

Details

From the project README

Model Context Protocol .NET Template

MCP Logo

A protocol for seamless integration between LLM applications and external data sources

Documentation | Specification | Discussions

This repository contains a template for creating a Model Context Protocol (MCP) application in .NET. It provides a basic structure and example code to help you get started with building your own MCP-enabled applications.

PackageVersionDescription
Nall.ModelContextProtocol.TemplateNugetTemplates
Nall.ModelContextProtocol.Inspector.Aspire.HostingNugetAspire Hosting Integration

Getting Started

Install:
dotnet new install Nall.ModelContextProtocol.Template
# dotnet new install ./Artefact/Nall.ModelContextProtocol.Template

Verify installation:

dotnet new list mcp
# These templates matched your input: 'mcp'

# Template Name      Short Name         Language  Tags
# -----------------  -----------------  --------  -------------
# Template Name         Short Name            Language  Tags
# --------------------  --------------------  --------  -------------
# MCP Server            mcp-server            [C#]      dotnet/ai/mcp
# MCP Server HTTP       mcp-server-http       [C#]      dotnet/ai/mcp
# MCP Server HTTP Auth  mcp-server-http-auth  [C#]      dotnet/ai/mcp
# MCP Server Hybrid     mcp-server-hybrid     [C#]      dotnet/ai/mcp

Tip

Practically, you want to switch between modes, so I recommend using the mcp-server-hybrid template. It allows you to run the server in both Stdio and HTTP modes.

Verify output:

dotnet new mcp-server -o MyAwesomeMCPServer -n MyAwesomeMCPServer --dry-run
# File actions would have been taken:
#   Create: MyAwesomeMCPServer\.vscode\launch.json
#   Create: MyAwesomeMCPServer\MyAwesomeMCPServer.csproj
#   Create: MyAwesomeMCPServer\Program.cs
#   Create: MyAwesomeMCPServer\Properties\launchSettings.json
#   Create: MyAwesomeMCPServer\README.md
#   Create: MyAwesomeMCPServer\appsettings.Development.json
#   Create: MyAwesomeMCPServer\appsettings.json

Create from template:

dotnet new mcp-server -o MyAwesomeMCPServer -n MyAwesomeMCPServer

Here is a content of Program.cs:

using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

await builder.Build().RunAsync();

[McpServerToolType]
public static class EchoTool
{
    [McpServerTool, Description("Echoes the message back to the client.")]
    public static string Echo(string message) => $"hello {message}";
}

It is a simple echo server that listens for incoming messages and responds with a greeting. You can add more tools by creating additional methods with the [McpServerTool] attribute. The WithToolsFromAssembly() method automatically registers all tools in the assembly.

Run Locally

Build from the project directory:

dotnet build -o Artefacts -c Release

Run the inspector:

npx @modelcontextprotocol/inspector -e DOTNET_ENVIRONMENT=Production dotnet "$(PWD)/Artefacts/MyAwesomeMCPServer.dll"

Open inspector in your browser and test the server:

Inspector Demo

Distribute as .NET Tool

Pack from the project directory:

dotnet pack -o Artefacts -c Release

Install the tool globally:

dotnet tool install --global --add-source ./Artefacts MyAwesomeMCPServer

Now, after you installed this tool globally, you can run it from anywhere on your system. The tool will be available as MyAwesomeMCPServer (or myawesomemcpserver) in your terminal.

💡 You can also create local tool manifest and install MCPs as tools locally.

Run the inspector:

npx @modelcontextprotocol/inspector -e DOTNET_ENVIRONMENT=Production myawesomemcpserver

Run with Aspire

In Stdio mode

In AppHost/Program.cs:

var builder = DistributedApplication.CreateBuilder(args);

builder.AddMCPInspector().WithStdio<Projects.MCPServer>();

builder.Build().Run();
In HTTP mode

In AppHost/Program.cs:

var builder = DistributedApplication.CreateBuilder(args);

var mcp = builder.AddProject<Projects.MCPServerRemote>("server");
builder.AddMCPInspector().WithMcp(mcp);

builder.Build().Run();

Reference

Comments

Markdown supported

New to MCP?

From what the protocol is to installing your first server, these answers cover most first questions.

An open protocol that defines how AI applications connect to external tools and data in a uniform way. Clients talk to the model; MCP servers provide tools, resources and prompt templates. They exchange standard messages, so one server can be reused by many clients.