Prerequisites
First of all create a very basic ASP.NET Core website that contains just the Program.cs file. You can see the source code here ASP.NET Core Server Snippet on Github.
//- see the first video below for explanation //- and for how to discover these tools through //- github co-pilot on visual studio community edition // Install-Package ModelContextProtocol -prerelease // Install-Package ModelContextProtocol.AspNetCore -prerelease using ModelContextProtocol.Server; using System.ComponentModel; var builder = WebApplication.CreateBuilder(args); // this adds an mcp server // and then exposes the server through the http // and finally tells our server to search for // the tools in the running assembly builder.Services .AddMcpServer() .WithHttpTransport() .WithToolsFromAssembly(); var app = builder.Build(); app.MapMcp(); app.Run("https://localhost:5001/"); // ---- the AI Tools that will get compiled into the assembly [McpServerToolType] public sealed class MyAITools { [McpServerTool, Description("Gives the square of a number")] public static int MakeSquare(int n) { return n * n; } [McpServerTool, Description("Gives the cube of a number")] public static int MakeCube(int n) { return n * n * n; } }
Video on Writing AI Tools with MCP Server on an ASPNET Core Website
Please watch the following youtube video:
The video is available in English, French, German, Hindi, Indonesian, Italian, Japanese, Polish, Portuguese and Spanish dubbings.
Microsoft Official SDK for MCP Server
The official SDK samples are available at the github link Microsoft Official MCP SDK Samples.
Step 2 - Obtain your OpenAI developer Key - it's free, no sign-up required
You can obtain your OpenAI developer key in the manner explained in the video below.
You can also see the details on this page - How to get keys for OpenAPI and a lot others using Github Models, without the need of paying even a single cent to them
Video on How to Obtain free API Key for OpenAI, no sign up required
Please watch the following youtube video:
The video is available in English, French, German, Hindi, Indonesian, Italian, Japanese, Polish, Portuguese and Spanish dubbings.
Last step - Programmatically Calling the Tools with Open AI C# SDK
First of all ensure the server explained in the first video is running.
After that create a C# console application with two files - .mcp.json, and the Program.cs file. Place them side by side.
Place this file .mcp.json adjacent to the Program.cs file. Please note that the name of the file starts with a dot.
{ "servers": { "my-mcp": { "type": "http", "url": "https://localhost:5001" } } }
The following is the code for the Program.cs file. Notice that we make a complex call to the MCP server - "find square of the cube of 3". We haven't asked it to specifically call a tool. The LLM shall decide on which of the tools to call, and in what sequence. This is the main difference between Web API and the AI Tools of an MCP Server. You do not need to know the names of the functions in the latter case - your LLM decides it for you. The situation is very different for Web API, where you need to know the name of the exact web api, and the data-types of the parameters to be passed to it.
// Install-Package Microsoft.Extensions.AI // Install-Package Microsoft.Extensions.AI.OpenAI -prerelease // Install-Package ModelContextProtocol -prerelease using Microsoft.Extensions.AI; using ModelContextProtocol.Client; using OpenAI; using System.ClientModel; // see the server code in the first step above // for the localhost:5001 hard-coded here var clientTransport = new SseClientTransport(new SseClientTransportOptions() { Endpoint = new Uri("https://localhost:5001/"), Name = "My_Client" }); IMcpClient? mcpClient_t; try { mcpClient_t = await McpClientFactory.CreateAsync(clientTransport); } catch (Exception ex) { Console.WriteLine($"ERROR McpClientFactory.CreateAsync {ex.Message}"); Console.WriteLine("HAVE YOU STARTED THE SERVER?"); return; } // create an IMcpClient await using IMcpClient mcpClient = mcpClient_t!; // get a list of MCP Tools for display IList<McpClientTool>? tools; try { tools = await mcpClient.ListToolsAsync(); } catch (Exception ex) { Console.WriteLine($"Error ListToolsAsync {ex.Message}"); return; } if (!tools.Any()) { Console.WriteLine("No tools found on the server. Quitting..."); return; } // display the tools Console.WriteLine("Connected to server with tools:"); foreach (var tool in tools) { Console.WriteLine($"{tool.Name}, {tool.Description}"); } // open AI code starts here // see the second video above for getting the key var endpoint = "https://models.github.ai/inference"; var key = "--your-OpenAI key -- see the second video above"; var model = "openai/gpt-4o"; ApiKeyCredential cred = new(key); var openAIOptions = new OpenAIClientOptions() { Endpoint = new Uri(endpoint), }; // Create the IChatClient IChatClient client = new ChatClientBuilder( new OpenAIClient(cred, openAIOptions) .GetChatClient(model).AsIChatClient() ) .UseFunctionInvocation() .Build(); var response = await client.GetResponseAsync( "find square of the cube of 3", new ChatOptions() { Tools = [.. tools], }); Console.Write(response.Text);
This Blog Post/Article "(1) Integrating an MCP Server [plus its AI Tools] into an ASPNET Core Website, and (2) accessing those tools through Visual Studio GitHub Co-Pilot and also (3) programmatically accessing them through a C# console app written with OpenAI SDK" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.