AI

Build Your First MCP Server in .NET 11 — Give Your AI App Real Tools

DEV Community: #csharp Nitish Kaushik (Microsoft MVP)
AI

A hands-on walkthrough that pairs an ASP.NET Core MCP server with an Azure OpenAI host built on Microsoft.Extensions.AI, so one chat endpoint can discover your app's tools and answer with real data instead of guessing.

Key points

  • The sample pins `ModelContextProtocol.AspNetCore` 2.4.0 on the server and `ModelContextProtocol` 2.4.0 on the client.
  • `[McpServerToolType]` and `[McpServerTool]` mark the class and methods that `WithToolsFromAssembly()` registers.
  • `WithHttpTransport()` with `MapMcp("/mcp")` publishes the endpoint, at https://localhost:7038/mcp in the repo.
  • The host learns tools at runtime via `McpClient.CreateAsync`, `HttpClientTransport` and `ListToolsAsync()` instead of hard-coding them.
  • `AddChatClient(...).UseFunctionInvocation()` runs the tool-call loop, with the Azure Responses client adapted through `AsIChatClient`.
  • Tool `[Description]` text guides the model's choice but is metadata, not authorization or input validation.

Why it matters

Anyone shipping a .NET AI feature hits the same wall: the model knows nothing about your data, and hand-rolled function calling gets old fast. An MCP server gives you one tool surface that any MCP-capable host — your own chat endpoint, an IDE assistant, an agent — can reuse, while controllers and tools share the same service.

Before copying the code, keep keys in user secrets and drop the `OPENAI001` pragma once your packages stop emitting it. And budget for real authorization on the MCP endpoint: the model does not police tool calls, and `[Description]` text is not a guard.

Real-world use

  • An internal chat endpoint that answers questions about employee data already owned by an existing ASP.NET Core app.
  • An MCP server that fronts an existing REST API instead of duplicating the business logic in a second place.
  • A host that answers general model questions directly and requests application data only when a tool is truly needed.
  • A REST controller and a set of MCP tools sharing one service, so the employee logic keeps a single owner.

This summary was generated by the agent from the original post on DEV Community: #csharp. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message