These samples use the Model Context Protocol (MCP) C# SDK and show:
-
How to create an MCP server powered by SK:
- Expose SK plugins as MCP tools.
- Expose SK prompt templates as MCP prompts.
- Use Kernel Function as MCP
Read
resource handlers. - Use Kernel Function as MCP
Read
resource template handlers.
-
How a hosting app can use MCP client and SK:
- Import MCP tools as SK functions and utilize them via the Chat Completion service.
- Use MCP prompts as additional context for prompting.
- Use MCP resources and resource templates as additional context for prompting.
- Intercept and handle sampling requests from the MCP server in human-in-the-loop scenarios.
- Import MCP tools as SK functions and utilize them via Chat Completion and Azure AI agents.
Please refer to the MCP introduction to get familiar with the protocol.
The samples require credentials and other secrets to access AI models. If you have set up those credentials as secrets within Secret Manager or through environment variables for other samples from the solution in which this project is found, they will be re-used.
cd dotnet/samples/Demos/ModelContextProtocolClientServer/MCPClient
dotnet user-secrets init
dotnet user-secrets set "OpenAI:ChatModelId" "..."
dotnet user-secrets set "OpenAI:ApiKey" "..."
dotnet user-secrets set "AzureAI:ConnectionString" "..."
dotnet user-secrets set "AzureAI:ChatModelId" "..."
Use these names:
# OpenAI
OpenAI__ChatModelId
OpenAI__ApiKey
AzureAI__ConnectionString
AzureAI__ChatModelId
To run the sample, follow these steps:
- Right-click on the
MCPClient
project in Visual Studio and selectSet as Startup Project
. - Press
F5
to run the project. - All samples will be executed sequentially. You can find the output in the console window.
- You can run individual samples by commenting out the other samples in the
Main
method of theProgram.cs
file of theMCPClient
project.
Both the MCP Inspector and the Claude desktop app can be used to access MCP servers for exploring and testing MCP server capabilities: tools, prompts, resources, etc.
To use the MCP Inspector follow these steps:
- Open a terminal in the MCPServer project directory.
- Run the
npx @modelcontextprotocol/inspector dotnet run
command to start the MCP Inspector. Make sure you have node.js and npm installednpx @modelcontextprotocol/inspector dotnet run
- When the inspector is running, it will display a URL in the terminal, like this:
MCP Inspector is up and running at http://127.0.0.1:6274
- Open a web browser and navigate to the URL displayed in the terminal. This will open the MCP Inspector interface.
- Find and click the "Connect" button in the MCP Inspector interface to connect to the MCP server.
- As soon as the connection is established, you will see a list of available tools, prompts, and resources in the MCP Inspector interface.
To use the Claude desktop app to access the MCP server, follow these steps:
-
- Download and install the app from the Claude website.
- In the app, go to File->Settings->Developer->Edit Config.
- Open the
claude_desktop_config.json
file in a text editor and add the following configuration to the file:{ "mcpServers": { "demo_mcp_server": { "command": "<Path to SK repo>/dotnet/samples/Demos/ModelContextProtocolClientServer/MCPServer/bin/Debug/net8.0/MCPServer.exe", "args": [] } } }
- Save the file and restart the app.
To debug the MCP server in Visual Studio, follow these steps:
- Connect to the MCP server using either the MCP Inspector or the Claude desktop app. This should start the MCP server process.
- Set breakpoints in the MCP server code where you want to debug.
- In Visual Studio, go to
Debug
->Attach to Process
. - In the
Attach to Process
dialog, find theMCPServer.exe
process and select it. - Click
Attach
to attach the debugger to the process. - Once the debugger is attached, access the MCP server tools, prompts, or resources using the MCP Inspector or the Claude desktop app. This will trigger the breakpoints you set in the MCP server code.
The MCP specification supports remote MCP servers. You can find more information at the following links: Server-Side Events (SSE) and HTTP with SSE.
The MCP C# SDK provides all the necessary components to easily create a remote MCP server. To get started, follow this sample: AspNetCoreSseServer.
While details of native support for OAuth 2.1 are still being discussed, you can consider a solution based on APIM acting as an AI Gateway. This approach is demonstrated by the sample: Secure Remote Microsoft Graph MCP Servers using Azure API Management (Experimental).