Edit

Share via


Create and connect to a minimal MCP server using .NET

In this quickstart, you create a minimal Model Context Protocol (MCP) server using the C# SDK for MCP and connect to it using GitHub Copilot. MCP servers are services that expose capabilities to clients through the Model Context Protocol (MCP).

Prerequisites

Create the project

  1. In a terminal window, navigate to the directory where you want to create your app, and create a new console app with the dotnet new command:

    dotnet new console -n MinimalMcpServer
    
  2. Navigate to the MinimalMcpServer directory:

    cd MinimalMcpServer
    
  3. Add the following NuGet packages to your app:

    dotnet add package ModelContextProtocol --prerelease
    dotnet add package Microsoft.Extensions.Hosting
    

Add the app code

Replace the contents of Program.cs with the following code to implement a minimal MCP server that exposes simple echo tools. The AI model invokes these tools as necessary to generate responses to user prompts.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using ModelContextProtocol.Server;
using System.ComponentModel;

// Create a generic host builder for
// dependency injection, logging, and configuration.
var builder = Host.CreateApplicationBuilder(args);

// Configure logging for better integration with MCP clients.
builder.Logging.AddConsole(consoleLogOptions =>
{
consoleLogOptions.LogToStandardErrorThreshold = LogLevel.Trace;
});

// Register the MCP server and configure it to use stdio transport.
// Scan the assembly for tool definitions.
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

// Build and run the host. This starts the MCP server.
await builder.Build().RunAsync();

// Define a static class to hold MCP tools.
[McpServerToolType]
public static class EchoTool
{
    // Expose a tool that echoes the input message back to the client.
    [McpServerTool, Description("Echoes the message back to the client.")]
    public static string Echo(string message) => $"Hello from C#: {message}";

    // Expose a tool that returns the input message in reverse.
    [McpServerTool, Description("Echoes in reverse the message sent by the client.")]
    public static string ReverseEcho(string message) => new string(message.Reverse().ToArray());
}

The preceding code:

  • Creates a generic host builder for dependency injection, logging, and configuration.
  • Configures logging for better integration with MCP clients.
  • Registers the MCP server, configures it to use stdio transport, and scans the assembly for tool definitions.
  • Builds and runs the host, which starts the MCP server.
  • Defines a static class to hold two MCP tools that echo values back to the client.

Configure the MCP server in Visual Studio Code

Configure GitHub Copilot for Visual Studio Code to use your custom MCP server:

  1. If you haven't already, open your project folder in Visual Studio Code.

  2. Create a .vscode folder at the root of your project.

  3. Add an mcp.json file in the .vscode folder with the following content:

    {
      "inputs": [],
      "servers": {
        "MinimalMcpServer": {
          "type": "stdio",
          "command": "dotnet",
          "args": [
            "run",
            "--project",
            "${workspaceFolder}/MinimalMcpServer.csproj"
          ]
        }
      }
    }
    
  4. Save the file.

Test the MCP server

  1. Open GitHub Copilot in Visual Studio Code and switch to agent mode.

  2. Select the Select tools icon to verify your MinimalMcpServer is available with both tools listed.

    A screenshot showing the available MCP tools.

  3. Enter a prompt to run the ReverseEcho tool:

    Reverse the following: "Hello, minimal MCP server!"
    
  4. GitHub Copilot requests permission to run the ReverseEcho tool for your prompt. Select Continue or use the arrow to select a more specific behavior:

    • Current session always runs the operation in the current GitHub Copilot Agent Mode session.
    • Current workspace always runs the command for the current Visual Studio Code workspace.
    • Always allow sets the operation to always run for any GitHub Copilot Agent Mode session or any Visual Studio Code workspace.
  5. Verify that the server responds with the echoed message:

    !revres PCM laminim ,olleH
    

Build a minimal MCP client Get started with .NET AI and the Model Context Protocol