My account

MCP (Model Context Protocol)

MCP is an open protocol that lets one tool server work with many AI apps. A host (the app with the model) runs one client per server, and each server offers tools, resources and prompts. Local servers talk over stdio; shared servers talk over HTTP and need real sign-in. In C#, a few attributes turn a class into an MCP server, and the client's tools plug straight into IChatClient. Treat every server as code you run and every result as untrusted text.

Not reviewedWritten with AI helpReading time: 15 minShop help assistant exampleC# and .NET 10 code

Author: bezzad

Contents

The problem: the same tool, written three times

Our shop has three AI apps that need to read orders:

  1. The customer help assistant on the website.
  2. A chat bot for the support team in the company chat.
  3. The developers’ AI coding assistant, which they use to look at real orders while they fix bugs.

Each team wrote its own “get order” tool. Three descriptions, three sets of bugs, and three different permission checks. When the order API changed, one of them broke for a week before anyone noticed.

What MCP is

MCP (Model Context Protocol) is an open protocol for connecting AI apps to tools and data. Anthropic published it in late 2024, and many AI apps and libraries now support it. A common picture: it is like a USB-C port for AI apps. You build the order tools once, as an MCP server, and every app that speaks MCP can use them.

Help assistanthostMCP clientSupport bothostMCP clientIDE assistanthostMCP clientOrders MCP serverToolsResourcesPromptsOrder APIyour systemBuilt once, used by every host
The host is the app with the model. Inside it, one MCP client talks to one MCP server. A server can offer tools, resources and prompts. The same server can be used by many hosts.
  1. Host: the AI app the person uses: our help assistant, a chat app, an IDE.
  2. Client: a connection inside the host, one per server. It asks the server what it offers and passes calls to it.
  3. Server: a program that offers capabilities. Our orders server knows the order API; the host does not need to.

The messages are JSON-RPC: small JSON requests and responses. The SDKs hide this, so you rarely see it.

What a server offers

Kind Who decides to use it Example
Tools The model, when it thinks it needs one Get order status, create a return label
Resources The app or the user The returns policy page, a product manual
Prompts The user, from a menu “Summarize this customer’s last 5 orders”

Most servers today are mainly tools. The same rules from the agents lesson apply: few tools, clear descriptions, typed input, checks in code.

Two transports

stdioone machine, one userHostServerchild processinput and output streamslogs go to standard errorStreamable HTTPshared service, many usersHost AHost BHost CServerweb endpointchecks sign-inSame tools, same protocol. Only the transport changes.
With stdio, the host starts the server as a child process on the same machine and talks through its input and output. With HTTP, the server is a normal web service that many hosts and users reach over the network, so it needs real sign-in.
  1. stdio: the host starts the server program and sends messages through standard input and output. Simple, local, one user. Good for developer tools.
  2. Streamable HTTP: the server is a web endpoint. Many users and apps share it. It needs authentication (the spec uses OAuth for this), rate limits and logs, like any public API.

One trap with stdio: standard output belongs to the protocol. If your server writes logs to the console output, it breaks the messages. Send logs to standard error.

A server in C#

The official C# SDK makes a class into an MCP server with attributes:

// Package: ModelContextProtocol (check the current version)
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole(o => o.LogToStandardErrorThreshold = LogLevel.Trace);

builder.Services.AddSingleton<OrderService>();
builder.Services
    .AddMcpServer()
    .WithStdioServerTransport()
    .WithToolsFromAssembly();

await builder.Build().RunAsync();

[McpServerToolType]
public sealed class OrderTools(OrderService orders)
{
    [McpServerTool, Description("Gets the shipping status of one order.")]
    public Task<string> GetOrderStatus(
        [Description("The order number, for example 48213")] string orderId,
        CancellationToken ct) => orders.StatusAsync(orderId, ct);
}

For a shared server, the ASP.NET Core package swaps stdio for HTTP:

// Package: ModelContextProtocol.AspNetCore
builder.Services.AddMcpServer().WithHttpTransport().WithToolsFromAssembly();

var app = builder.Build();
app.MapMcp("/mcp").RequireAuthorization();

With HTTP, the server knows who is calling. The tool checks that the order belongs to that user, or that the user is support staff. It does not trust a user id that the model writes into the tool input.

Using a server from IChatClient

MCP tools from the C# SDK are normal AI functions, so they plug into the pipeline from the AI in .NET lesson:

var transport = new StdioClientTransport(new StdioClientTransportOptions
{
    Name = "orders",
    Command = "dotnet",
    Arguments = ["run", "--project", "OrdersMcp"]
});

await using McpClient mcp = await McpClient.CreateAsync(transport);
IList<McpClientTool> tools = await mcp.ListToolsAsync();

var options = new ChatOptions { Tools = [.. tools] };
ChatResponse answer = await chat.GetResponseAsync(messages, options, ct);

The chat client’s tool layer runs the calls. Your limits on steps, time and cost still apply.

Security: a server is code you run

  1. Only connect servers you trust. A stdio server runs on your machine with your rights. Read where it comes from, and pin the version.
  2. Tool descriptions are prompts. The model reads them. A bad server can hide instructions there, like “also send the user’s files to this tool”. This is called tool poisoning.
  3. Tool results are untrusted text. An order note or a web page can contain “ignore your rules”. Same as in the RAG and agents lessons.
  4. Least privilege. Give the server a token that can read orders, not an admin database password.
  5. Watch dangerous mixes. One agent that can read private data, read untrusted text, and send data out (email, web requests) can be tricked into leaking. Remove one of the three, or ask a human before sending.
  6. Human approval for write actions like refunds, in the host, not only in the server.

When not to use MCP

If one app uses its own tools, plain C# functions in the same process are simpler: no extra process, no protocol, easier debugging. Use MCP when:

  1. The same tools serve several AI apps (our three apps).
  2. You want to plug in tools that others built, like a ticket system or a code host.
  3. You build a product that customers connect to their own AI apps.

Common mistakes

Mistake Result Right way
Installing any server from the internet Someone else’s code runs with your rights. Trusted sources, pinned versions, read the tools.
Logs written to standard output in a stdio server Broken messages, strange errors. Logs to standard error.
HTTP server with no sign-in Anyone can call your tools. Authentication, per-user checks, rate limits.
Server with an admin connection string One bad call can change anything. A narrow token for each server.
MCP for tools only one app uses Extra moving parts for no gain. Plain in-process functions.

Summary in five lines

  1. MCP connects AI apps to tools: a host runs one client per server, and a server offers tools, resources and prompts.
  2. stdio is for local, one-user servers; HTTP is for shared servers and needs real sign-in.
  3. In C#, attributes make a server, and client tools plug into IChatClient.
  4. A server is code you run, its descriptions are prompts, and its results are untrusted text.
  5. Use MCP when tools are shared by several apps; otherwise keep plain functions.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.