Sandbox
@nisalgunawardhana/How-To-Create-MCP-Server

Guide for a .NET MCP server in Copilot Chat

This repo shows how to build a basic MCP server in .NET, expose a tool with the Model Context Protocol, and connect it to VS Code. The guide covers package install, `Program.cs`, and a `mcp.json` server entry so Copilot Chat can call the tool.

39 starsβ€’77 forksβ€’Updated 8mo ago
Who it's for

Builders who want to create an MCP server in .NET and use it from VS Code Copilot Chat.

What it delivers

You can run a simple MCP tool from Copilot Chat instead of wiring everything by hand.

What it does

.NET MCP server setup

Shows how to create a console app, add `ModelContextProtocol` and `Microsoft.Extensions.Hosting`, and build a hosted MCP server.

Stdio transport configuration

Uses `AddMcpServer().WithStdioServerTransport()` so the server can be launched from the editor through standard input and output.

Tool definition in code

Defines a sample `HelloTool` with `[McpServerToolType]` and a `SayHello()` method that returns a greeting.

VS Code MCP config

Provides a `mcp.json` example that points VS Code to `dotnet run --project ...HelloSriLankaServer.csproj`.

Copilot Chat hookup

Shows how to add the server as a tool in Copilot Chat and send a prompt that triggers the MCP tool.

Contribution workflow

Includes fork, branch, pull request, and badge-claim steps for people contributing to the tutorial project.

How to get it

  1. 1Verify installation in your terminal
    dotnet --version
  2. 2Install the necessary NuGet packages
    dotnet add package ModelContextProtocol --prerelease
    dotnet add package Microsoft.Extensions.Hosting

README

How to Create MCP Server Using .NET

This guide will help you set up a basic MCP (Model Context Protocol) server in .NET, configure it in VS Code, and interact with it using Copilot Chat.

Follow me on GitHub

How to Clone This Repo and Start Working

  1. Fork this repository to your own GitHub account.

  2. Clone your fork to your local machine:

    git clone https://github.com/<your-username>/How-To-Create-MCP-Server
    cd How-To-Create-MCP-Server
    

    Note: Replace <your-username> with your actual GitHub username in the clone URL above.

  3. Create a new branch for your changes (for example, feature/my-contribution):

    git checkout -b feature/my-contribution
    

    πŸ›‘ Already completed steps 1–3?
    If you've already forked, cloned, and created your branch, you can skip ahead to πŸš€ Start Work Now and continue with the rest of the setup!

  4. Make your changes and commit them.

    git add .
    git commit -m"my-contribution"
    
  5. Push your branch to your fork:

    git push origin feature/my-contribution
    

    Once you've pushed your changes, go to your GitHub repository in your browser. Switch to the feature/my-contribution branch

Add Tool in Copilot Chat

  1. Open a Pull Request (PR) from your branch to the Submit branch of this repository (not main).

Step 01 Add Tool in Copilot Chat Step 02 Add Tool in Copilot Chat


Pull Request Comment Guidelines

When you create your Pull Request (PR), please add a comment including your full name and email address. This helps us track contributions and contact you if needed.

Example:

Full Name: Nisal Gunawardhana
Email: info@nisalgunawardhana.com

Note: Please do not open PRs directly to the main branch. Always target the Submit branch for contributions.


πŸŽ–οΈ How to Claim Your Digital Badge

After completing the tutorial and submitting your pull request, you can claim your digital badge by following these steps:

Step 1: Complete Your Pull Request

  1. Ensure your pull request is submitted to the Submit branch (as described above)
  2. Make sure your code implements the MCP server functionality
  3. Include your full name and email in the PR comment

Step 2: Submit an Issue to Claim Your Badge

  1. Go to the Issues tab

  2. Click on "New Issue"

  3. Select the "🎯 Submission for Digital Badge" template

  4. Fill out the form with:

    • Your full name (as it should appear on the badge)
    • Your email address
    • Your GitHub username
    • Link to your pull request
    • A brief summary of what you implemented
    • (Optional) Challenges you faced and feedback
  5. Make sure to check all the requirement boxes at the bottom

  6. Click "Submit new issue"

Step 3: Automated Review Process

Once you submit your issue:

  • βœ… An automated comment will acknowledge your submission
  • 🏷️ Your issue will be labeled with submission and pending review
  • πŸ“‹ A maintainer will review your work within 2-3 business days
  • πŸ’¬ You may receive feedback or questions

Step 4: Receive Your Digital Badge

When your submission is approved:

  • 🎊 The issue will be closed as completed by @nisalgunawardhana
  • πŸŽ–οΈ A digital badge will be automatically generated and posted in the issue comments
  • 🏷️ Labels will be updated: approved and completed will be added, pending review will be removed
  • πŸ“§ You'll receive the badge with instructions on how to display it

Note: Digital badges are only issued when the issue is closed by @nisalgunawardhana to ensure proper verification of submissions.

Badge Display

You'll receive:

  • A custom digital badge image that you can add to your GitHub profile
  • Markdown code to easily embed the badge in your README files
  • HTML code for websites or portfolios
  • Completion date and achievement details

Example of what you'll receive:

MCP Server Completion Badge

Follow the steps below to set up and run the MCP server.


πŸš€ Start Work Now

Ready to dive in?
After cloning and setting up your branch, you can immediately begin making changes or adding features to the MCP server project. Use the provided steps below to guide your development process. If you get stuck, refer to the resources and discussion links at the end of this guide.


1. Install .NET

Download and install the .NET SDK for your OS.

Note: This guide uses .NET SDK 8. Make sure you download the correct version for your operating system.

If you're using VS Code, install the C# Dev Kit extension for the best development experience.

Verify installation in your terminal:

dotnet --version

2. Create a New Project

Open your terminal and run:

dotnet new console -o HelloSriLankaServer
cd HelloSriLankaServer

3. Install Required Packages

Install the necessary NuGet packages:

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

Project Folder Structure

After creating the project, your folder structure should look like this:

How-To-create-MCP-Server/
β”œβ”€β”€ HelloSriLankaServer/
β”‚   β”œβ”€β”€ Program.cs
β”‚   β”œβ”€β”€ HelloSriLankaServer.csproj
β”‚   └── (other project files)
β”œβ”€β”€ images/
β”‚   β”œβ”€β”€ image1.png
β”‚   └── image2.png
β”œβ”€β”€ README.md
β”œβ”€β”€ .gitignore.md
└── LICENSE

This structure helps keep your source code, configuration, and documentation organized.


4. Update Program.cs

Clear the contents of Program.cs and replace with:

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

var builder = Host.CreateApplicationBuilder(args);

builder.Logging.AddConsole(options => {
    options.LogToStandardErrorThreshold = LogLevel.Trace;
});

builder.Services.AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

Console.WriteLine(builder.Environment.ContentRootPath);
var app = builder.Build();

await app.RunAsync();

[McpServerToolType]
public static class HelloTool
{
    [McpServerTool(Name = "HelloTool"), Description("Say hello to Sri Lanka")]
    public static void SayHello()
    {
        Console.WriteLine("Hello Sri Lanka!");
        
    }
}

5. Configure MCP in VS Code

  1. In your project root, create a .vscode directory:

Before creating the .vscode directory, navigate back to the project root if you're inside the HelloSriLankaServer folder:

cd ..
mkdir -p .vscode

After creating the .vscode directory, your folder structure should look like this:

How-To-create-MCP-Server/
β”œβ”€β”€ HelloSriLankaServer/
β”‚   β”œβ”€β”€ Program.cs
β”‚   β”œβ”€β”€ HelloSriLankaServer.csproj
β”‚   └── (other project files)
β”œβ”€β”€ images/
β”‚   β”œβ”€β”€ image1.png
β”‚   └── image2.png
β”œβ”€β”€ .vscode/
β”œβ”€β”€ README.md
β”œβ”€β”€ .gitignore.md
└── LICENSE
  1. Inside .vscode, create a file named mcp.json and add your server configuration.

    • Once created, you will see an Add Server button. Click it.
      Add Server Button

    • Choose "Command (stdio)" as the server type.
      Choose Command (stdio)

    • Enter "dotnet" as the command and press Enter.
      Enter dotnet

    • Enter the server name "Hello-SriLankaServer" and press Enter.
      Enter Server Name

    • The mcp.json file will be populated automatically. Replace missing contents with the following:

      {
        "servers": {
          "Hello-SriLankaServer": {
            "type": "stdio",
            "command": "dotnet",
            "args": [
              "run",
              "--project",
              "${workspaceFolder}/HelloSriLankaServer/HelloSriLankaServer.csproj"
            ]
          }
        }
      }
      

      Note: ${workspaceFolder} will automatically resolve to your project root. If it does not work, replace it with the actual path to your .csproj file (right-click the file in VS Code and select "Copy Path").

6. Run the MCP Server(test if it's work)

From the HelloSriLankaServer directory, start the server:

dotnet run

Note: Once you've confirmed the server is running successfully, you can stop it (press Ctrl+C in the terminal) and proceed to the next step (Step 7).


7. Add the Tool in Copilot Chat

Note: Before adding the tool in Copilot Chat, make sure your MCP server is running. Then, open the mcp.json file in VS Code. You will see a small "Add Tool" button appear just below the(2nd Line) "servers": { line in the JSON codeβ€”click this button to proceed.

  1. Open Copilot Chat in VS Code.
  2. Click the gear icon (βš™οΈ) or the "Add Tool" button.
  3. Select your MCP server (Hello-SriLankaServe) from the list.

Image Example:

Note: The example images may display "LocationServer" or a similar name, but it actually refers to your MCP server (Hello-SriLankaServer). Add Tool in Copilot Chat Add Tool in Copilot Chat


8. Send a Message Using Copilot Chat

  1. In Copilot Chat, select the Hello-SriLankaServer tool.

  2. Type a message to invoke your tool, for example:

    Can you Say hello to Sri Lank
    

    Note: You may need to grant permission or continue when prompted by VS Code. Sometimes, Copilot Chat or other AI tools do not have access to the terminal or workspace by default. If you see a prompt asking for access, make sure to allow it so your tool can run successfully.

  3. You should see a reply from your MCP server in the chat.

    Hello Sri Lanka !
    
    

9. Get a Reply from the MCP Server

You will receive a response from your MCP server in the Copilot Chat window.


10. Advanced Demo: Try the MCP Location Server

For a more advanced example, you can explore the Try-mcp-location-server-demo repository. This demo showcases how to build and interact with a location-based MCP server using .NET.


🎁 Share and Win Amazing Tech Swag!

Love this project? Share it with your friends and community for a chance to win exclusive tech swag!

  • How to participate:
    Fill out this form to request your personalized share link. We'll send your unique link to your email within 2–3 business days.

  • Share and Win:
    Once you receive your link, share it with your friends. Ask them to complete the project and include your referral link when they submit.

  • Why share?
    The more friends who use your referral link, the higher your chances to win cool tech goodiesβ€”stickers, shirts, and more!

Note: Make sure your email is visible in your GitHub profile or mention it in the form when you request your link.

Stay tunedβ€”winners will be announced in the Discussions tab!

Additional Learning Resources

For a deeper understanding of MCP and more hands-on examples, check out the Introduction to MCP repository. This resource provides tutorials, sample projects, and further guidance on working with MCP in .NET.


πŸ’¬ Join the Discussion!

Have questions, ideas, or want to share your experience?
We welcome you to use GitHub Discussions for:

  • Asking questions about setup or usage
  • Sharing feedback or suggestions
  • Requesting new features
  • Connecting with other contributors

πŸ‘‰ Click the "Discussions" tab at the top of this repo to start or join a conversation!

Let's build and learn together!


Connect with Me

Follow me on social media for more sessions, tech tips, and giveaways:

  • LinkedIn β€” Professional updates and networking
  • Twitter (X) β€” Insights and announcements
  • Instagram β€” Behind-the-scenes and daily tips
  • GitHub β€” Repositories and project updates
  • YouTube β€” Video tutorials and sessions

Feel free to connect and stay updated!

Files in the repo

Repository payloadβ€’5 top-level entries
  • .github
  • images
  • .gitignore
  • LICENSE
  • README.md

Discussion (0)

Ask about usage, or say what you built with it

Sign in to join the discussion.

No comments yet. Be the first to say what this is good for.

More tutorials & guides

shareAI-lab/
learn-claude-code

Bash is all you need - A nano claude code–like γ€Œagent harness」, built from 0 to 1

77k
luongnv89/
claude-howto
luongnv89/claude-howtoTutorials & Guides

A visual, example-driven guide to Claude Code β€” from basic concepts to advanced agents, with copy-paste templates that bring immediate value.

41k
agentskills/
agentskills
agentskills/agentskillsTutorials & Guides

Specification and documentation for Agent Skills

25k
datawhalechina/easy-vibeTutorials & Guides

πŸ’» vibe coding 101|The first course for AI-native product builders.

19k