Build an MCP Server in .NET 11: Let Your AI App Use Real Tools
A from-scratch walkthrough of an ASP.NET Core MCP server, client, and Azure OpenAI host
Instructor: Nitish Kaushik — 6-time Microsoft MVP in Developer Technologies
Tags: dotnet, csharp, ai, mcp
A model can explain what .NET is. But ask it, “How many employees are in my company?” and it needs access to your application’s data. Without a tool, it could only guess.
In this walkthrough, I build a way for a .NET AI app to discover and use tools exposed by another application—with MCP, the Model Context Protocol.
I’m Nitish Kaushik, a six-time Microsoft MVP in Developer Technologies. We’ll start with two ASP.NET Core applications, connect them over HTTP, and finish by asking an Azure OpenAI model questions about employee data.
The architecture: host, client, and server
The example has two applications:
-
McpServerexposes employee tools over MCP. -
McpHostaccepts chat requests, connects to Azure OpenAI, and uses an MCP client to discover and invoke those tools.
The MCP client lives inside the host; it isn’t a separate application. The host coordinates the model and the MCP server:
User │ ▼ McpHost ───────────────► Azure OpenAI │ ▲ │ MCP client │ tool result ▼ │ McpServer ──────────────── EmployeeService The model doesn’t call your MCP server directly. The host gives the model the available tool descriptions. If the model requests a tool, the host’s MCP client calls the server and returns the result to the model.
MCP doesn’t replace your APIs either. In this example, the server has an EmployeesController and MCP tools, and both reuse the same EmployeeService. If your MCP server is a separate application from your existing API, you can have it call that API instead.
Packages in the repository
These are the package versions currently pinned in the repo’s project files.
McpServer/McpServer.csproj:
| Package | Version | Purpose |
|---|---|---|
Microsoft.AspNetCore.OpenApi | 11.0.0 | OpenAPI support for the web API |
ModelContextProtocol.AspNetCore | 2.4.0 | MCP server and HTTP transport for ASP.NET Core |
McpHost/McpHost.csproj:
| Package | Version | Purpose |
|---|---|---|
Azure.AI.OpenAI | 2.1.0 | Azure OpenAI client |
Azure.Identity | 1.21.0 | Azure identity support |
Microsoft.AspNetCore.OpenApi | 11.0.0 | OpenAPI support for the host API |
Microsoft.Extensions.AI | 11.0.1 | Common .NET AI abstractions |
Microsoft.Extensions.AI.OpenAI | 11.0.1 | Connects OpenAI client types to IChatClient |
ModelContextProtocol | 2.4.0 | MCP client functionality |
The current host code authenticates with an API key using AzureKeyCredential; Azure.Identity is included in the project for Azure identity scenarios. The repo doesn’t declare a separate OpenAI package reference.
1. Make employee operations available as tools
The server contains an in-memory EmployeeService with three sample employees. The REST controller and MCP tools both use this service, so there’s only one place that owns the employee logic.
Here’s the MCP tool class from the project. Its primary constructor receives the service:
using System.ComponentModel; using McpServer; using ModelContextProtocol.Server; namespace McpServer.Tools; [McpServerToolType] public sealed class EmployeesTools(EmployeeService employeeService) { [McpServerTool] [Description("Get all employees")] public List<EmployeeModel> GetEmployees() { return employeeService.GetEmployees(); } [McpServerTool] [Description("Get an employee by ID. Example: 1")] public EmployeeModel? GetEmployeeById( [Description("The ID of the employee")] int id) { return employeeService.GetEmployeeById(id); } } The descriptions help the model distinguish between the two tools and understand the id parameter. They’re important metadata—but they are not security controls. Your server still needs to enforce authorization and validate inputs.
2. Expose the MCP endpoint
In McpServer/Program.cs, the app registers the employee service, enables HTTP transport, and discovers tools in the assembly:
using McpServer; var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); builder.Services.AddOpenApi(); builder.Services.AddSingleton<EmployeeService>(); builder.Services .AddMcpServer() .WithHttpTransport() .WithToolsFromAssembly(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.MapOpenApi(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.MapMcp("/mcp"); app.Run(); WithToolsFromAssembly() finds classes marked with [McpServerToolType] and registers their [McpServerTool] methods. MapMcp("/mcp") exposes the MCP endpoint at /mcp.
In the repo, the server URL is https://localhost:7038/mcp. Your port may differ if your launch settings change.
3. Connect the host to the MCP server
The host reads McpServerUrl from configuration, connects over HTTP, and asks the server which tools it provides:
using Microsoft.Extensions.AI; using ModelContextProtocol.Client; var mcpServerUrl = builder.Configuration.GetValue<string>("McpServerUrl"); await using var mcpClient = await McpClient.CreateAsync( new HttpClientTransport( new HttpClientTransportOptions { Endpoint = new Uri(mcpServerUrl!) })); var tools = await mcpClient.ListToolsAsync(); builder.Services.AddSingleton<IReadOnlyList<AITool>>( [.. tools.Cast<AITool>()]); The host doesn’t hard-code the employee tool definitions; it discovers them from the server. The MCP client remains available for the lifetime of the host, and the discovered tools are registered for the chat controller.
4. Connect Azure OpenAI
The host uses IChatClient so the chat controller can work with a common .NET AI abstraction. This is the Azure OpenAI setup from the repo:
using Azure; using Azure.AI.OpenAI; using Microsoft.Extensions.AI; #pragma warning disable OPENAI001 builder.Services.AddChatClient(_ => { var azureOpenAiConfig = builder.Configuration.GetSection("AzureOpenAI"); var apiKey = azureOpenAiConfig["ApiKey"]; var endpoint = azureOpenAiConfig["Endpoint"]; var deploymentName = azureOpenAiConfig["DeploymentName"]; var azureClient = new AzureOpenAIClient( new Uri(endpoint!), new AzureKeyCredential(apiKey!)); var clientProvider = azureClient.GetResponsesClient(); return clientProvider.AsIChatClient(deploymentName); }).UseFunctionInvocation(); #pragma warning restore OPENAI001 The project uses the Azure Responses client and adapts it with AsIChatClient. UseFunctionInvocation() is what lets the chat pipeline handle the tool-call loop: when the model requests a tool, the host invokes it and sends the result back so the model can answer.
The OPENAI001 pragma is present in the repo around this setup. If your package version no longer emits that diagnostic, remove the pragma rather than suppressing unrelated warnings.
5. Let the chat endpoint use the tools
The chat controller also uses a primary constructor. It receives both the IChatClient and discovered tools through dependency injection:
using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.AI; namespace McpHost.Controllers; [ApiController] [Route("api/[controller]")] public class ChatController( IChatClient chatClient, IReadOnlyList<AITool> tools) : ControllerBase { [HttpPost("")] public async Task<IActionResult> Post( [FromBody] string request, CancellationToken cancellationToken) { var response = await chatClient.GetResponseAsync( request, new ChatOptions { Tools = [.. tools] }, cancellationToken: cancellationToken); return Ok(response.Text); } } Now the host can send the user’s message along with the tools available from the MCP server.
6. Try it yourself
Configure Azure OpenAI in user secrets—don’t put API keys in appsettings.json or commit them:
dotnet user-secrets set "AzureOpenAI:ApiKey" "<your-api-key>" --project McpHost dotnet user-secrets set "AzureOpenAI:Endpoint" "https://<your-resource>.openai.azure.com/" --project McpHost dotnet user-secrets set "AzureOpenAI:DeploymentName" "<your-deployment-name>" --project McpHost Run the server and host in separate terminals:
dotnet run --project McpServer --launch-profile https dotnet run --project McpHost --launch-profile http The repo’s host listens at http://localhost:5006. Send a chat request:
curl -X POST http://localhost:5006/api/chat \ -H "Content-Type: application/json" \ -d '"List all employees"' Try these prompts too:
- “What is .NET?” The model can answer without calling an employee tool.
- “How many employees are there?” The model can request employee data from the MCP server instead of guessing.
- “Get the employee with ID 1.” The model can use the employee-by-ID tool.
That’s the payoff: a single chat endpoint can answer general questions and use your application’s tools when it needs real data.
Source code and full walkthrough
The complete source code is here: mcp-server-example-in-aspnet-core.
I’m Nitish Kaushik, a six-time Microsoft MVP in Developer Technologies. Watch the full video to build the MCP server, client, and host step by step:
What would you connect to your first MCP server? Share your idea or question in the comments.
