name | description | languages | products | page_type | urlFragment | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
.NET OpenAI MCP Agent |
This is an MCP agent app written in .NET, using OpenAI, with a remote MCP server written in TypeScript. |
|
|
sample |
openai-mcp-agent-dotnet |
This is an MCP agent app written in .NET, using Azure OpenAI, with a remote MCP server written in TypeScript.
This app provides features like:
- The MCP host + MCP client app is written in .NET Blazor.
- The MCP client app connects to a to-do MCP server written in TypeScript.
- Both MCP client and server apps are running on Azure Container Apps (ACA).
- The MCP client app is secured by the built-in auth of ACA.
- The MCP server app is only accessible from the MCP client app.
- .NET 9 SDK
- Visual Studio Code + C# Dev Kit
- node.js LTS
- Docker Desktop or Podman Desktop
- Azure Subscription
You can now use GitHub Codespaces to run this sample app (takes several minutes to open it)! 👉 .
- To run this app, you should have either Azure AI Foundry instance or GitHub Models.
- If you use Azure AI Foundry, make sure you have the GPT-5-mini models deployed deployed.
- As a default, the deployed model name is
gpt-5-mini
.
-
Create a directory for the app.
# zsh/bash mkdir -p openai-mcp-agent-dotnet
# PowerShell New-Item -ItemType Directory -Path openai-mcp-agent-dotnet -Force
-
Initialize
azd
.cd openai-mcp-agent-dotnet azd init -t openai-mcp-agent-dotnet
NOTE: You'll be asked to enter an environment name, which will be the name of your Azure Resource Group. For example, the environment name might be
openai-mcp-agent
. -
Make sure that your deployed model name is
gpt-5-mini
. If your deployed model is different, updatesrc/McpTodo.ClientApp/appsettings.json
. -
Add Azure OpenAI endpoint and API key. The Azure OpenAI endpoint MUST end with
openai.azure.com/
.dotnet user-secrets --project ./src/McpTodo.ClientApp set OpenAI:Endpoint {{AZURE_OPENAI_ENDPOINT}} dotnet user-secrets --project ./src/McpTodo.ClientApp set OpenAI:ApiKey {{AZURE_OPENAI_API_KEY}}
NOTE: If you want to use OpenAI API, add the API key only. The OpenAI API key SHOULD start with
sk-proj-
.dotnet user-secrets --project ./src/McpTodo.ClientApp set OpenAI:ApiKey {{OPENAI_API_KEY}}
-
Clone the MCP server.
git clone https://github.com/Azure-Samples/mcp-container-ts.git ./src/McpTodo.ServerApp
-
Set JWT token.
# zsh/bash ./scripts/set-jwttoken.sh
# PowerShell ./scripts/Set-JwtToken.ps1
-
Check that you have the necessary permissions:
- Your Azure account must have the
Microsoft.Authorization/roleAssignments/write
permission, such as Role Based Access Control Administrator, User Access Administrator, or Owner at the subscription level. - Your Azure account must also have the
Microsoft.Resources/deployments/write
permission at the subscription level.
- Your Azure account must have the
-
Login to Azure.
azd auth login
-
Add user secrets to azd environment.
# zsh/bash secrets=$(dotnet user-secrets --project ./src/McpTodo.ClientApp list --json | \ grep -v '^//' | jq -r '.') azd env set OPENAI_ENDPOINT $(echo "$secrets" | jq -r '.["OpenAI:Endpoint"]') azd env set OPENAI_API_KEY $(echo "$secrets" | jq -r '.["OpenAI:ApiKey"]')
# PowerShell $secrets = dotnet user-secrets --project ./src/McpTodo.ClientApp list --json | ` Select-String -NotMatch '^//(BEGIN|END)' | ConvertFrom-Json azd env set OPENAI_ENDPOINT $secrets.'OpenAI:Endpoint' azd env set OPENAI_API_KEY $secrets.'OpenAI:ApiKey'
-
Add JWT token to azd environment.
# zsh/bash env_dir=".azure/$(azd env get-value AZURE_ENV_NAME)" mkdir -p "$env_dir" cat ./src/McpTodo.ServerApp/.env >> "$env_dir/.env"
# PowerShell $dotenv = Get-Content ./src/McpTodo.ServerApp/.env $dotenv | Add-Content -Path ./.azure/$(azd env get-value AZURE_ENV_NAME)/.env -Encoding utf8 -Force
-
Deploy apps to Azure.
azd up
NOTE:
-
By default, the MCP client app is protected by the ACA built-in auth feature. You can turn off this feature before running
azd up
by setting:azd env set USE_LOGIN false
-
During the deployment, you will be asked to enter the Azure Subscription and location.
-
-
In the terminal, get the client app URL deployed. It might look like:
https://mcptodo-clientapp.{{some-random-string}}.{{location}}.azurecontainerapps.io/
-
Navigate to the client app URL, log-in to the app and enter prompts like:
Give me list of to do. Set "meeting at 1pm". Give me list of to do. Mark #1 as completed. Delete #1 from the to-do list.
NOTE: You might not be asked to login, if you've set the
USE_LOGIN
value tofalse
. -
Clean up all the resources deployed.
azd down --force --prune