Build an MCP Server in C#: .NET Tutorial for AI Tools
Your application already knows how to look up a product, calculate a price, or check an order. The interesting question is how to let an AI assistant use that logic without building a custom integration for every client.
An MCP server in C# provides that connection. This guide walks through a small .NET example: a price-calculation tool that an MCP-compatible assistant can discover and call. You will create the project, add the tool, connect a client, and check the result.
This example uses local standard input/output (stdio). It does not require a database, an AI API key, or a public web server.
How do you build an MCP server in C#?
To build an MCP server in C#, create a .NET console project, install the ModelContextProtocol and Microsoft.Extensions.Hosting packages, register a tool method, and configure stdio transport. Build the project, connect it to an MCP-compatible client, and inspect a real tool call to verify the result.
Example in this guide: a C# price-calculation tool. A unit price of 19.95 and quantity of 3 returns 59.85, excluding tax and shipping.
Why MCP matters for .NET developers in 2026
The official MCP C# SDK reached version 2.0 in July 2026. The release adds support for the newer protocol revision and changes how remote HTTP integrations work. That gives .NET teams a timely reason to learn the basics, but you do not need to start with a remote deployment.
This tutorial deliberately uses a local tool. Once you can see the inputs, tool call, and output, it is easier to decide where MCP fits in a real application. See Microsoft’s C# SDK 2.0 announcement for the release details.
What does an MCP server actually do?
Model Context Protocol (MCP) gives AI applications a common way to discover and call external capabilities. A server exposes tools, while a client connects those tools to an application such as an AI coding assistant. For a .NET developer, a tool can wrap an existing C# method with a clear purpose and input contract.
For this walkthrough, the tool multiplies a unit price by a quantity. It is intentionally simple: you can check the answer yourself, and there is no database or external API to debug at the same time.
What you will build: a local C# server with one tool, input validation, and an explicit rounding rule. Tax and shipping are outside this example.
Prerequisites
- Install the .NET 10 SDK for this walkthrough.
- Use a terminal and a C# editor such as Visual Studio or VS Code.
- Have an MCP-compatible client available for the connection test.
Check that the SDK is installed:
Step 1: Create a .NET project for your MCP server
These commands install stable packages available from your configured NuGet sources. Keep the resolved package versions in your project file so teammates can restore the same dependencies. If you later upgrade, check the SDK documentation for API changes.
Step 2: Register your first C# MCP tool
Replace Program.cs with this example:
The attributes identify the tool and describe its intent. Assembly discovery registers it with the server. Input validation limits this example to non-negative prices and quantities from 1 to 1000. The rounding rule is explicit; change it only when your application's pricing requirements call for a different rule.
Keep stdout reserved for MCP messages. Send diagnostic logs to stderr, as configured above. Extra console output on stdout can break the client's protocol connection.
Step 3: Build before connecting
Resolve compilation or restore errors before configuring the client. Running the server alone may appear to wait without displaying a prompt: a stdio server expects protocol messages from its client.
Step 4: Connect from your MCP client
In VS Code, open the Command Palette and run MCP: Add Server. Choose Command (stdio). Use a command like the following, replacing the path with the actual absolute path to your project:
Give the server a recognizable ID, such as price-tools, and choose the configuration scope you need. Review the launch command before trusting it. Other MCP clients use different configuration files, so use that client's documented stdio setup rather than copying a configuration format blindly.
Step 5: Verify an actual tool call
Ask your assistant:
Use the price-tools MCP server to calculate the total for a unit price of 19.95 and a quantity of 3. Show the tool result.
The expected numeric result is 59.85. Inspect the tool call to confirm that the client invoked your server; an answer containing the right arithmetic alone does not prove the connection works. Also try a zero quantity and confirm that invalid input produces a tool error.
Common problems and fixes
- Server fails to start: check the project path, SDK installation, and client output logs.
- Tool does not appear: verify the attributes and registration, rebuild, and restart the client connection.
- Protocol errors: remove stdout diagnostic messages and keep logs on stderr.
- Old behavior after editing: rebuild because the launch command uses
--no-build.
When should you use ASP.NET Core instead?
Stdio suits a local client that launches a server process. For remote connections, the C# SDK offers ModelContextProtocol.AspNetCore and Streamable HTTP. A remote deployment needs its own authentication, authorization, host validation, and operational design. Start with this local example before exposing business data or write operations.
Frequently asked questions
Does this server need an LLM API key?
No. The calculation tool runs ordinary C# code. Your chosen AI client may separately require an account or model subscription.
Can I connect an existing ASP.NET MVC application?
You can place reusable business logic behind an MCP tool. For an older .NET Framework application, a separate modern .NET service that calls an existing API can be a practical boundary. Review compatibility before adding packages directly to a legacy project.
Can I query SQL Server through a tool?
Yes, but design a specific, authorized query operation. Avoid exposing arbitrary SQL execution as a beginner tool. Limit returned data and use parameterized queries.
Is MCP the same as a REST API?
No. A REST API exposes HTTP endpoints, while MCP defines how a compatible client discovers and uses capabilities such as tools. An MCP tool can call an existing REST API. For this tutorial, the client launches a local process and communicates over stdio, so no HTTP endpoint is needed.
Should I choose stdio or HTTP for a C# MCP server?
Choose stdio when your client will launch the server locally. Choose an HTTP-based deployment when clients need to connect remotely. The C# SDK provides the ModelContextProtocol.AspNetCore package for HTTP hosting; review its transport and security documentation before deployment.
Official references
- Official MCP C# SDK getting started guide
- Official C# SDK repository
- Connect MCP servers in VS Code
- Microsoft's introduction to .NET and MCP
What to build after the first tool works
A useful next step is a read-only product lookup: accept a product code, call your existing service, and return just the name, price, and stock status the user is allowed to see. Keep authentication and business rules in that service rather than expecting the assistant to enforce them.
If you work with an older MVC application, you can start with a separate .NET process that calls an existing API. A small, well-defined tool is easier to maintain than exposing an entire application at once.
Before you add more features, confirm three things: the client found your tool, it called that tool with the expected inputs, and the returned result matches your application’s rules.
Comments
Post a Comment