Skip to main content
Items marked (preview) in this article are currently in public preview. This preview is provided without a service-level agreement, and we don’t recommend it for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.
A custom code interpreter gives you full control over the runtime environment for agent-generated Python code. You can configure custom Python packages, compute resources, and Azure Container Apps environment settings. The code interpreter container exposes a Model Context Protocol (MCP) server. Use a custom code interpreter when the built-in Code Interpreter tool for agents doesn’t meet your requirements—for example, when you need specific Python packages, custom container images, or dedicated compute resources. For more information about MCP and how agents connect to MCP tools, see Connect to Model Context Protocol servers (preview).
Consider adding this tool using a toolbox. By using a toolbox, you are able to reuse the tool across agents and runtimes, as well as centralizing credential management, versioning, and policy enforcement through a managed MCP endpoint. See the toolbox quickstart.

Prerequisites

  • Azure CLI version 2.60.0 or later.
  • Python 3.12 or later for the maintained sample project.
  • (Optional) uv for faster Python package management.
  • An Azure subscription and resource group with the following role assignments:
    • Foundry User on the Foundry project for configuring and running the agent after provisioning.
The Foundry RBAC roles were recently renamed. Foundry User, Foundry Owner, Foundry Account Owner, and Foundry Project Manager were previously named Azure AI User, Azure AI Owner, Azure AI Account Owner, and Azure AI Project Manager. You might still see the previous names in some places while the rename rolls out. The role IDs and core permissions are unchanged by the rename.
Activate the provisioning roles just in time through Microsoft Entra Privileged Identity Management (PIM), and deactivate them after deployment. Day-to-day agent developers and runtime users don’t need these provisioning roles.

Usage support

This article uses the Azure CLI and a runnable sample project. The following table shows SDK and setup support. For the latest SDK and API support for agents tools, see Best practices for using tools in Microsoft Foundry Agent Service.

SDK support

The custom code interpreter uses the MCP tool type. Any SDK that supports MCP tools can create a custom code interpreter agent. The .NET SDK is currently in preview. For the infrastructure provisioning steps (Azure CLI, Bicep), see Create an agent with custom code interpreter.

Before you begin

This procedure provisions Azure infrastructure, including Azure Container Apps resources. Review your organization’s Azure cost and governance requirements before deploying.

Create an agent with custom code interpreter

The following steps show how to provision the infrastructure and create an agent that uses a custom code interpreter MCP server. The infrastructure setup applies to all languages. Language-specific code samples follow.

Register the preview feature

Register the MCP server feature for Azure Container Apps Dynamic Sessions:

Get the sample code

Clone the sample code in the GitHub repo and navigate to the samples/python/prompt-agents/code-interpreter-custom folder in your terminal.

Provision the infrastructure

The maintained direct-agent sample stores the session pool MCP endpoint in the project connection. Toolbox definitions also require the endpoint as server_url. Add this output to the cloned infra.bicep file:
Don’t use poolManagementEndpoint. That value is the Dynamic Sessions management endpoint, not the MCP server endpoint. To provision the infrastructure, run the following command by using the Azure CLI (az):
Deployment can take up to one hour, depending on the number of standby instances you request. The dynamic session pool allocation is the longest step.

Configure and run the agent

Copy the .env.sample file from the repository to .env. Map the Bicep deployment outputs to the matching environment variables: The inline examples use PROJECT_ENDPOINT for AZURE_AI_PROJECT_ENDPOINT and MCP_CONNECTION_ID for AZURE_AI_CONNECTION_ID. The maintained direct-agent sample resolves the MCP target through the project connection and uses https://localhost as a required placeholder URL. For a toolbox, set MCP_SERVER_URL to the mcpServerEndpoint output because MCPToolboxTool requires server_url or connector_id even when you also provide a project connection. Install the Python dependencies and run the maintained sample with one of these command pairs:
Or create a virtual environment and install the checked-in requirements:

Verify your setup

After you’ve provisioned the infrastructure and run the sample:
  1. Confirm the Azure deployment completed successfully.
  2. Confirm the sample connects using the values in your .env file.
  3. In Microsoft Foundry, verify your agent calls the tool using tracing. For more information, see Best practices for using tools in Microsoft Foundry Agent Service.

Troubleshooting

Limitations

The APIs don’t directly support file input or output, or the use of file stores. To get data in and out, you must use URLs, such as data URLs for small files and Azure Blob Service shared access signature (SAS) URLs for large files.

Security

Treat generated code and its dependencies as untrusted. Use an approved base image and package allow list, run with the minimum required compute and permissions, and restrict outbound network access to required destinations. Don’t mount sensitive data or production credentials into the session. If you use SAS URLs to pass data in or out of the runtime:
  • Use short-lived SAS tokens.
  • Don’t log SAS URLs or store them in source control.
  • Scope permissions to the minimum required (for example, read-only or write-only).

Clean up

To stop billing for provisioned resources, delete the resources created by the sample deployment. If you used a dedicated resource group for this article, delete the resource group.