The Model Context Protocol (MCP) is an open standard for building context-aware AI applications by connecting language models with external data sources. This example demonstrates how to implement a basic MCP server in C# using the modelcontextprotocol/csharp-sdk
library, which provides a streamlined API for creating and managing MCP servers.
- .NET SDK version 9.0
- npx (Node.js package runner) installed
- Visual Studio Code or any IDE
If you want to create the project from scratch, you can use the following commands to create a new console application and add the necessary dependencies.
dotnet new console -n MCPServer
cd MCPServer
dotnet add package ModelContextProtocol --version 0.1.0-preview.1.25171.12
dotnet add package Microsoft.Extensions.Hosting
# Add the MCPServer/Program.cs file to the location where you created the project.
First, build the project using the following command:
dotnet run
Then start the MCP server by running the following command in a separate terminal:
npx @modelcontextprotocol/inspector dotnet run
This command will start the MCP Inspector, which allows you to interact with the MCP server and test its functionality. You can use the Inspector to send requests to the server and receive responses based on the defined context.
- Click on the "Connect" button to establish a connection with the MCP server.
- Once connected, click list tools.
- You should see the method name from the MCP server in the list of available tools.
- Add a value for the argument
- Click "Run Tool".
- The result will be displayed in the Inspector.