Edit

Share via


Get started with .NET AI and the Model Context Protocol

The Model Context Protocol (MCP) is an open protocol designed to standardize integrations between AI apps and external tools and data sources. By using MCP, developers can enhance the capabilities of AI models, enabling them to produce more accurate, relevant, and context-aware responses.

For example, using MCP, you can connect your LLM to resources such as:

  • Document databases or storage services.
  • Web APIs that expose business data or logic.
  • Tools that manage files or performing local tasks on a user's device.

Many Microsoft products already support MCP, including:

You can use the MCP C# SDK to quickly create your own MCP integrations and switch between different AI models without significant code changes.

MCP client-server architecture

MCP uses a client-server architecture that enables an AI-powered app (the host) to connect to multiple MCP servers through MCP clients:

  • MCP Hosts: AI tools, code editors, or other software that enhance their AI models using contextual resources through MCP. For example, GitHub Copilot in Visual Studio Code can act as an MCP host and use MCP clients and servers to expand its capabilities.
  • MCP Clients: Clients used by the host application to connect to MCP servers to retrieve contextual data.
  • MCP Servers: Services that expose capabilities to clients through MCP. For example, an MCP server might provide an abstraction over a REST API or local data source to provide business data to the AI model.

The following diagram illustrates this architecture:

A diagram showing the architecture pattern of MCP, including hosts, clients, and servers.

MCP client and server can exchange a set of standard messages:

Message Description
InitializeRequest This request is sent by the client to the server when it first connects, asking it to begin initialization.
ListToolsRequest Sent by the client to request a list of tools the server has.
CallToolRequest Used by the client to invoke a tool provided by the server.
ListResourcesRequest Sent by the client to request a list of available server resources.
ReadResourceRequest Sent by the client to the server to read a specific resource URI.
ListPromptsRequest Sent by the client to request a list of available prompts and prompt templates from the server.
GetPromptRequest Used by the client to get a prompt provided by the server.
PingRequest A ping, issued by either the server or the client, to check that the other party is still alive.
CreateMessageRequest A request by the server to sample an LLM via the client. The client has full discretion over which model to select. The client should also inform the user before beginning sampling, to allow them to inspect the request (human in the loop) and decide whether to approve it.
SetLevelRequest A request by the client to the server, to enable or adjust logging.

Develop with the MCP C# SDK

As a .NET developer, you can use MCP by creating MCP clients and servers to enhance your apps with custom integrations. MCP reduces the complexity involved in connecting an AI model to various tools, services, and data sources.

The official MCP C# SDK is available through NuGet and enables you to build MCP clients and servers for .NET apps and libraries. The SDK is maintained through collaboration between Microsoft, Anthropic, and the MCP open protocol organization.

To get started, add the MCP C# SDK to your project:

dotnet add package ModelContextProtocol --prerelease

Instead of building unique connectors for each integration point, you can often leverage or reference prebuilt integrations from various providers such as GitHub and Docker:

Integration with Microsoft.Extensions.AI

The MCP C# SDK depends on the Microsoft.Extensions.AI libraries to handle various AI interactions and tasks. These extension libraries provides core types and abstractions for working with AI services, so developers can focus on coding against conceptual AI capabilities rather than specific platforms or provider implementations.

View the MCP C# SDK dependencies on the NuGet package page.

More .NET MCP development resources

Various tools, services, and learning resources are available in the .NET and Azure ecosystems to help you build MCP clients and servers or integrate with existing MCP servers.

Get started with the following development tools:

Learn more about .NET and MCP using these resources: