Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
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
- .NET 8.0 SDK or higher
- Visual Studio Code
- GitHub Copilot extension for Visual Studio Code
Create the project
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
Navigate to the
MinimalMcpServer
directory:cd MinimalMcpServer
Add the following NuGet packages to your app:
dotnet add package ModelContextProtocol --prerelease dotnet add package Microsoft.Extensions.Hosting
- The ModelContextProtocol package is the official C# SDK for working with the Model Context Protocol.
- The Microsoft.Extensions.Hosting package provides the generic .NET
HostBuilder
and services for logging and dependency injection.
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:
If you haven't already, open your project folder in Visual Studio Code.
Create a
.vscode
folder at the root of your project.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" ] } } }
Save the file.
Test the MCP server
Open GitHub Copilot in Visual Studio Code and switch to agent mode.
Select the Select tools icon to verify your MinimalMcpServer is available with both tools listed.
Enter a prompt to run the ReverseEcho tool:
Reverse the following: "Hello, minimal MCP server!"
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.
Verify that the server responds with the echoed message:
!revres PCM laminim ,olleH
Related content
Build a minimal MCP client Get started with .NET AI and the Model Context Protocol