Skip to content

MarcinSredzinski/McpServerExample

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCP - Creating your own tools

Creating your own MCP server is surprisingly easy, once you know what to do. First you need:

  • .net (9+) installed on your machine (or dev container etc.),
  • IDE of choice - here we'll use Visual Studio Code,
  1. Create the project:

dotnet new console -n McpServerExample

  1. Add necessary packages:

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

  1. In the Program.cs add the following code:
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

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

await builder.Build().RunAsync();
  1. Create a new folder, called Tools
  2. In this new folder, add "DotnetFoundationRepositoryReporter.cs" class
  3. Type in the following code:
using ModelContextProtocol.Server;
using System.ComponentModel;
using System.Net.Http.Headers;

namespace McpServerExample.Tools;

[McpServerToolType]
internal class DotnetFoundationRepositoryReporter
{
    [McpServerTool(Name = "GetDotnetFoundationRepositories")]
    [Description("Fetches repositories from the .NET Foundation GitHub organization.")]
    internal async Task<string> GetDotnetFoundationRepositories()
    {
        using HttpClient client = new();
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Add("User-Agent", "McpServerExample");
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));

        var json = await client.GetStringAsync("https://api.github.com/orgs/dotnet/repos");
        return json;
    }
}
  1. Build the project:

dotnet build

  1. Now the fun part - Using our MCP server:
    a) open a new, empty folder using Visual Studio Code
    b) create a new folder with name ".vscode"
    c) in this folder create mcp.json file, with the following content:
{
    "inputs": [],
    "servers": {
        "McpServerExample":{
            "type": "stdio",
            "command": "dotnet",
            "args": [
                "run",
                "--project",
                "McpServerExample.csproj", //Path to your project created in first point
                "--no-build"
            ]
        }   
    }
}
  1. If all went well, on top of the "McpServerExample" file a play icon and word "Start" should appear. Click it.
  2. Open the copilot chat in your visual studio in agentic mode and ask something along the lines "What github repositories does .net foundation currenlty maintain".
    Your agent will ask for permission to use the newly created tool. Grant it, and it will return a list of public GitHub repositories. Tool calling Code execution Final response

Resources:

About

Code demo for post about MCP server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages