The Model Context Protocol (MCP) is an open standard that lets AI assistants and agents use external tools and data in a uniform way. You write an MCP server once and every compatible client can use it: development environments, desktop assistants, agents built with .NET frameworks.
A minimal MCP server
The official C# SDK (the ModelContextProtocol package) integrates with the .NET generic host:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Server;
using System.ComponentModel;
var builder = Host.CreateApplicationBuilder(args);
builder.Services
.AddMcpServer()
.WithStdioServerTransport()
.WithToolsFromAssembly();
await builder.Build().RunAsync();
[McpServerToolType]
public static class WarehouseTools
{
[McpServerTool, Description("Returns the stock level of an item given its code.")]
public static int Stock([Description("Item code")] string code) =>
code == "ABC-123" ? 42 : 0;
}
With the stdio transport the client starts the server as a local process; for shared services you use the HTTP transport, hosted in an ASP.NET Core application.
Tools, resources and prompts
- Tools: functions the agent can execute.
- Resources: data the client can read, such as documents or records.
- Prompts: predefined request templates offered to the user.
Security first
An MCP server opens your application to an agent acting on behalf of a user. The rules of any API apply: authentication for remote servers, least privilege, argument validation, rate limits and an operations log. Tools that modify data should require explicit confirmation, and text returned from external sources must be treated as untrusted, because it may contain malicious instructions aimed at the agent.
Comments (0)
No comments yet.