This lab demonstrates how to build and consume MCP (Model Context Protocol) servers using Microsoft Agent Framework in .NET 10.
- Build Local MCP Servers - Using STDIO transport (.NET)
- Build Remote MCP Servers - Using HTTP/SSE transport that calls REST APIs
- Consume MCP Servers - From AI Agents using Microsoft Agent Framework
To explore MCP concepts interactively, open and run the Jupyter notebook:
cd begin
jupyter notebook mcp-concepts.ipynbOr open begin/mcp-concepts.ipynb directly in VS Code.
For hands-on exercises, see begin/EXERCISES.md.
LOCAL MCP (STDIO)
+-----------------------------+
| |
+------------------+ | +-----------------------+ |
| | ---------+-->| McpLocal | |
| McpAgentClient | | | (STDIO) | |
| | | +-----------------------+ |
| (Consumes | | |
| MCP Servers) | +-----------------------------+
| |
+--------+---------+
|
| REMOTE MCP (HTTP/SSE -> REST)
| +---------------------------------------------+
| | |
| HTTP/SSE | +-------------+ HTTP +-------------+ |
+------------------->| | McpBridge | --------> |RemoteServer | |
| | Port: 5070 | | Port: 5060 | |
| | /sse | | (REST API) | |
| +-------------+ +-------------+ |
| |
+---------------------------------------------+
lab2-mcp/
+-- McpLab.sln
+-- README.md
+-- begin/ # Lab exercises (incomplete code)
+-- solution/ # Complete working solution
+-- begin/MCP-Concepts.ipynb # Educational notebook
+-- begin/McpAgentClient/ # AI Agent that consumes MCP servers
+-- begin/McpLocal/ # Local MCP Server (.NET, STDIO)
+-- begin/McpBridge/ # MCP Bridge (HTTP/SSE -> REST API)
+-- begin/RemoteServer/ # REST API backend
| Project | Transport | Port | Description |
|---|---|---|---|
| McpLocal | STDIO | N/A | Local .NET MCP with Config & Ticket tools |
| RemoteServer | HTTP | 5060 | REST API backend (no MCP) |
| McpBridge | HTTP/SSE | 5070 | MCP server that wraps REST API |
| McpAgentClient | N/A | N/A | AI Agent consuming all MCP servers |
GetConfig- Get configuration value by keyUpdateConfig- Update configuration valueGetTicket- Get support ticket by IDUpdateTicket- Update support ticket status
GetTicket- Get ticket via REST APIUpdateTicket- Update ticket via REST API
- .NET 10 SDK installed
- Azure OpenAI resource with a deployed model
Create or update appsettings.Local.json in the dotnet folder with your Azure OpenAI credentials:
{
"AzureOpenAI": {
"Endpoint": "https://your-resource.openai.azure.com/",
"DeploymentName": "gpt-4o-mini",
// Option 1: API Key authentication
"ApiKey": "your-api-key",
// Option 2: Service Principal authentication
"TenantId": "",
"ClientId": "",
"ClientSecret": "",
// Option 3: Managed Identity authentication
"UseManagedIdentity": false,
"ManagedIdentityClientId": ""
}
}Alternatively, set environment variables:
$env:AZURE_OPENAI_ENDPOINT = "https://your-resource.openai.azure.com/"
$env:AZURE_OPENAI_DEPLOYMENT_NAME = "gpt-4o-mini"
$env:AZURE_OPENAI_API_KEY = "your-api-key"Option 1: Local MCP Server only (Demo 1)
# Terminal 1: Run the Agent Client
cd McpAgentClient
dotnet run
# Select option 1 for Local MCP ServerOption 2: Remote MCP Server (Demo 3)
# Terminal 1: Start REST API (port 5060)
cd RemoteServer
dotnet run
# Terminal 2: Start MCP Bridge (port 5070)
cd McpBridge
dotnet run
# Terminal 3: Run the Agent Client
cd McpAgentClient
dotnet run
# Select option 3 for Remote MCP ServerWhen you run the McpAgentClient, you'll see:
===================================================================
Select a demo to run:
1. Local MCP Server (.NET EXE via STDIO)
2. Local MCP Server (Node.js via STDIO)
3. Remote MCP Server (HTTP/SSE)
4. All MCP Servers Combined
5. Exit
===================================================================
During a chat session:
- Type your questions to interact with the AI agent
- Type
backto return to the main menu - Type
exitorquitto exit the application
| Transport | Use Case | Example |
|---|---|---|
| STDIO | Local servers on same machine | dotnet run spawns subprocess |
| HTTP/SSE | Remote servers over network | http://localhost:5070/sse |
[McpServerToolType]
public sealed class ConfigurationTools
{
[McpServerTool]
[Description("Gets a configuration value by key")]
public string GetConfig(
[Description("The configuration key")] string? key = null)
{
// Implementation
}
}// STDIO transport (local)
var client = await McpClientFactory.CreateAsync(
new StdioClientTransport(new StdioClientTransportOptions
{
Command = "dotnet",
Arguments = ["run", "--project", "McpLocal/McpLocal.csproj"]
}));
// HTTP/SSE transport (remote)
var client = await McpClientFactory.CreateAsync(
new SseClientTransport(new SseClientTransportOptions
{
Endpoint = new Uri("http://localhost:5070/sse")
}));The McpAgentClient supports multiple authentication methods:
- API Key - Set
AzureOpenAI:ApiKeyorAZURE_OPENAI_API_KEY - Service Principal - Set
TenantId,ClientId,ClientSecret - Managed Identity - Set
UseManagedIdentitytotrue - Azure CLI - Default fallback for local development
- See MCP-Concepts.ipynb for detailed explanations
- MCP Specification
- Microsoft Agent Framework