Blog Post

Apps on Azure Blog
4 MIN READ

How-to add an MCP tool to your Azure Foundry agent using dynamic sessions on Azure Container Apps

jeffmartinez's avatar
jeffmartinez
Icon for Microsoft rankMicrosoft
Nov 18, 2025

Use an Azure Container Apps dynamic session shell environment as an MCP server tool with Azure Foundry

Now available in preview, Azure Container Apps supports a new shell session pool type in dynamic sessions as well as MCP support for both shell and Python container types. With MCP enablement, you can bring the benefits of dynamics sessions to your AI agents by giving them the ability to execute code, run system commands, access file systems, and perform complex tasks in isolated, secure container environments that appear and disappear on demand.

 

The following is a tutorial on how to use an MCP enabled dynamic session as a tool used in an Azure Foundry agent giving it the capability to run remote shell commands. 

Setup

For this tutorial, we're going to use an ARM template to deploy our Container App Session Pool resource.

Begin by signing into Azure and creating a resource group.

az login

After you're logged in, set the following variables that will be used to deploy the resource. Replace the <> with your own values.

SUBSCRIPTION_ID=$(az account show --query id --output tsv)
RESOURCE_GROUP=<RESOURCE_GROUP_NAME>
SESSION_POOL_NAME=<SESSION_POOL_NAME>
LOCATION=<LOCATION>

Create a resource group

az group create --name $RESOURCE_GROUP --location $LOCATION

1. Create a shell dynamic session with MCP enabled

Use an ARM template to create a shell session pool with MCP server enabled.

Create a deployment template file named deploy.json and copy the following into the file:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "name": { "type": "String" },
        "location": { "type": "String" }
    },
    "resources": [
        {
            "type": "Microsoft.App/sessionPools",
            "apiVersion": "2025-02-02-preview",
            "name": "[parameters('name')]",
            "location": "[parameters('location')]",
            "properties": {
                "poolManagementType": "Dynamic",
                "containerType": "Shell", 
                "scaleConfiguration": {
                    "maxConcurrentSessions": 5
                },
                "sessionNetworkConfiguration": {
                    "status": "EgressEnabled"
                },
                "dynamicPoolConfiguration": {
                    "lifecycleConfiguration": {
                        "lifecycleType": "Timed",
                        "coolDownPeriodInSeconds": 300
                    }
                },
                "mcpServerSettings": { 
                    "isMCPServerEnabled": true 
                }
            }
        }
    ]
}

Next, deploy the ARM template you just created.

az deployment group create --resource-group $RESOURCE_GROUP --template-file deploy.json --name $SESSION_POOL_NAME --location $LOCATION

Once completed, your resource will be provisioned and available in the portal.

 

Get the MCP server endpoint and credentials

Next, retrieve the MCP server endpoint from the deployed container app session pool.

az rest --method GET --uri "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.App/sessionPools/$SESSION_POOL_NAME?api-version=2025-02-02-preview" --query "properties.mcpServerSettings.mcpServerEndpoint" -o tsv

Then, request the API credentials for the MCP server

az rest --method POST --uri "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.App/sessionPools/$SESSION_POOL_NAME/fetchMCPServerCredentials?api-version=2025-02-02-preview" --query "apiKey" -o tsv

Keep the fetched values nearby and copied to be used in the next step to connect your MCP server as a tool in the Azure Foundry agent.

2. Create an Azure Foundry project and agent

Go to the Azure Foundry website and follow the steps below to create a project. Make sure you are in the New Foundry portal by turning it on in the top nav.

  1. Navigate to the top left nav bar and click an existing project and click Create new project
  2. Give your project a Name
  3. Click on Advanced options to choose and fill out the following
    1. Foundry resource
    2. Subscription
    3. Region
    4. Resource group
  4. After filling the previous options out, click Create

Once your project is created, follow the steps below to create an agent.

  1. Click on the Build tab on the top right navigation bar
  2. Navigate to the Agents section in the left nav
  3. Click on the Create agent button
  4. Give your agent a name and click Create

3. Connect your remote MCP server as a Tool to your agent

After your agent is created, you can connect a remote MCP server as a tool for your agent to use. Follow the steps below to connect an MCP server to your agent.

  1. Navigate to Tools on the left nav bar
  2. Click on the Connect a tool button
  3. Go to the Custom tab
  4. Click on the Model Context Protocol (MCP) option, click Create
  5. Fill out the form on the following screen and click Create. Use the MCP server endpoint and API credentials from the previous section.
    1. Name="your-unique-name"
    2. Remote MCP server endpoint="your-mcp-server-endpoint"
    3. Authentication type= "Key-based"
    4. Credential= Key:"x-ms-apikey" Value: "your-api-key"

Once the tool is created you will see a Use in an agent button. Click on that and choose the agent you previously created.

After choosing the agent you should now see a connected tool in your agent view.

4. Use the connected tool to run shell commands with your agent

Now that the tool is connected you can use the playground to prompt the agent with a question that will launch the shell environment.

Try the following prompt with your agent:

"Create a hello world flask app in a new remote environment. Then send a request to the app to show that it works."

Once prompted, the connected MCP server will use it's tools to launch a shell environment and create an environmentId. This is used to track your session by creating a unique GUID so future commands are run in the same session.

The Foundry agent will then ask for approval for each step as the connected MCP tool will execute a series of shell commands that will install Python and Flask.

Once all requests have been completed and approved, you'll notice it has run the necessary shell commands in the remote environment and sent a curl request to the app with a response of Hello, World!

In this tutorial, we demonstrated how to extend Azure AI Foundry agents using Azure Container Apps dynamic session pools. By enabling MCP support on a shell session pool, we created a secure, on-demand execution environment that allows AI agents to run remote shell commands. With this setup, your AI agents can now bridge the gap between conversation and action, providing users with interactive and useful experiences. 

 

 

Updated Nov 18, 2025
Version 1.0
No CommentsBe the first to comment