Bemærk
Adgang til denne side kræver godkendelse. Du kan prøve at logge på eller ændre mapper.
Adgang til denne side kræver godkendelse. Du kan prøve at ændre mapper.
Create, test, and deploy hosted Foundry Agent workflows by using the Microsoft Foundry Toolkit for Visual Studio Code extension. The toolkit supports agent creation from templates, local testing and debugging with the Agent Inspector for visualization and trace support, and direct deployment to Foundry Agent Service from VS Code. Hosted workflows let multiple agents collaborate in sequence, each with its own model, tools, and instructions.
Before you start, build an agent in Foundry Agent Service by using the extension. You can then add hosted workflows to that agent.
This article covers creating a workflow project, running it locally, visualizing the execution, and deploying it to your Foundry workspace.
Prerequisites
A Foundry project with a deployed model, or an Azure OpenAI resource.
The Microsoft Foundry Toolkit for Visual Studio Code extension installed.
The project's managed identity with the Foundry User and AcrPull roles assigned. Also assign the
acrPullrole to the managed identity of the Foundry project where you plan to deploy the Hosted agent.Important
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.
A supported region for Hosted agents.
- Python 3.13 or higher.
- .NET 10 SDK or later.
Create a hosted agent workflow
You can use the Microsoft Foundry Toolkit for Visual Studio Code extension to create Hosted agent workflows. A Hosted agent workflow is a sequence of agents that work together to accomplish a task. Each agent in the workflow can have its own model, tools, and instructions.
Open the command palette (Ctrl+Shift+P).
Run this command:
>Foundry Toolkit: Create a New Hosted Agent.Select a programming language
Choose a framework, either Copilot SDK, Microsoft Agent Framework, or Bring your own.
Choose a protocol, either Responses API or Invocations API.
Choose a template from the list.
Select the "Next" button.
Select a folder where you want to save your new Hosted Agent.
For Environment Setup, selecting "Skip for now" will skip Foundry project and model setup, which requires you to manually configure them in the code later. Selecting "Configure with Microsoft Foundry" will auto-populate your project and model information with the existing Foundry Project.
The files for your Hosted agent project are generated in your selected folder based on the framework, template and language you selected to get you started. You can remove or modify that code as needed.
Install dependencies
Install the required dependencies for your Hosted agent project. The dependencies vary based on the programming language that you selected when you created the project.
Create virtual environment.
python -m venv .venvActivate the virtual environment.
# PowerShell ./.venv/Scripts/Activate.ps1 # Windows cmd .venv\Scripts\activate.bat # Unix/MacOS source .venv/bin/activateInstall the required packages:
pip install -r requirements.txt
Go to your project directory and run this command to get the necessary NuGet packages:
dotnet restore
Run your hosted workflow locally
The sample workflow project creates an .env file with the necessary environment variables. Create or update the .env file with your Foundry credentials:
FOUNDRY_PROJECT_ENDPOINT=https://<your-resource-name>.services.ai.azure.com/api/projects/<your-project-name>
AZURE_AI_MODEL_DEPLOYMENT_NAME=<your-model-deployment-name>
Important
Never commit the .env file to version control. Add it to your .gitignore file.
Authenticate your hosted agent
The Hosted agent sample authenticates using DefaultAzureCredential. Configure your development environment to provide credentials via one of the supported sources, for example:
- Azure CLI (
az login) - Visual Studio Code account sign-in
- Visual Studio account sign-in
- Environment variables for a service principal (AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET)
Confirm authentication locally by running either the Azure CLI az account show or az account get-access-token commands before running the sample.
You can run the Hosted agent in interactive mode or container mode.
Run your hosted agent in the Agent Inspector
Press F5 to start the local HTTP server with debugging enabled. The Foundry Toolkit Agent Inspector opens for interactive testing, and you can set breakpoints in your code.
To run the server without debugging:
python main.py
The agent listens on http://localhost:8088/. Send a test prompt with curl (or any HTTP client):
curl -sS -H "Content-Type: application/json" -X POST http://localhost:8088/responses \
-d '{"input": "Write a haiku about deploying cloud applications.", "stream": false}'
The sample workflow project creates an .env file with the necessary environment variables. Create or update the .env file with your Foundry credentials:
Set up your environment variables based on your operating system:
$env:FOUNDRY_PROJECT_ENDPOINT="https://<your-resource-name>.services.ai.azure.com/api/projects/<your-project-name>" $env:AZURE_AI_MODEL_DEPLOYMENT_NAME="your-deployment-name"
Authenticate your hosted agent
The Hosted agent sample authenticates using DefaultAzureCredential. Configure your development environment to provide credentials via one of the supported sources, for example:
- Azure CLI (
az login) - Visual Studio Code account sign-in
- Visual Studio account sign-in
- Environment variables for a service principal (AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET)
Confirm authentication locally by running either the Azure CLI az account show or az account get-access-token commands before running the sample.
You can run the Hosted agent in interactive mode or container mode.
Run your hosted agent in interactive mode
Run the Hosted agent directly for development and testing:
dotnet restore
dotnet build
dotnet run
Run your hosted agent in container mode
Tip
Open the local playground before starting the container agent to ensure the visualization functions correctly.
To run the agent in container mode:
- Open the Visual Studio Code Command Palette and execute the
Foundry Toolkit: Open Container Agent Playground Locallycommand. - Use the following command to initialize the containerized Hosted agent.
dotnet restore dotnet build dotnet run - Submit a request to the agent through the playground interface. For example, enter a prompt such as: "Create a slogan for a new electric SUV that's affordable and fun to drive."
- Review the agent's response in the playground interface.
Visualize hosted agent workflow execution
The Microsoft Foundry Toolkit for Visual Studio Code extension provides a real-time execution graph that shows how agents in your workflow interact and collaborate. Enable observability in your project to use this visualization.
Add the following reference to your csproj file:
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.10" />
</ItemGroup>
Update your program to include the following code snippet:
using System.Diagnostics;
using OpenTelemetry;
using OpenTelemetry.Logs;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
var otlpEndpoint =
Environment.GetEnvironmentVariable("OTLP_ENDPOINT") ?? "http://localhost:4319";
var resourceBuilder = OpenTelemetry
.Resources.ResourceBuilder.CreateDefault()
.AddService("WorkflowSample");
var s_tracerProvider = OpenTelemetry
.Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(resourceBuilder)
.AddSource("Microsoft.Agents.AI.*") // All agent framework sources
.SetSampler(new AlwaysOnSampler()) // Ensure all traces are sampled
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(otlpEndpoint);
options.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
})
.Build();
Monitor and visualize your hosted agent workflow
To monitor and visualize your Hosted agent workflow execution in real time:
Open the command palette (Ctrl+Shift+P).
Run this command:
>Foundry Toolkit: Open Visualizer for Hosted Agents.
A new tab opens in VS Code to display the execution graph. The visualization updates itself automatically as your workflow progresses, to show the flow between agents and their interactions.
Port conflicts
For port conflicts, you can change the visualization port by setting it in the Microsoft Foundry Toolkit for Visual Studio Code extension settings. To do that, follow these steps:
- In the left sidebar of VS Code, select the gear icon to open the settings menu.
- Select
Extensions>Microsoft Foundry Configuration. - Locate the
Hosted Agent Visualization Portsetting and change it to an available port number. - Restart VS Code to apply the changes.
Change port in code
For any port conflicts, change the visualization port by setting the FOUNDRY_OTLP_PORT environment variable. Update the OTLP endpoint in your program accordingly.
For example, to change the port to 4318, use this command:
$env:FOUNDRY_OTLP_PORT="4318"
In your program, update the OTLP endpoint to use the new port number:
var otlpEndpoint =
Environment.GetEnvironmentVariable("OTLP_ENDPOINT") ?? "http://localhost:4318";
Deploy the hosted agent
After testing your Hosted agent locally, deploy it to your Foundry workspace so other team members and applications can use it.
Important
Make sure you give the necessary permissions to deploy Hosted agents in your Foundry workspace, as stated in the Prerequisites. You might need to work with your Azure administrator to get the required role assignments.
- Open the Command Palette and select Foundry Toolkit: Deploy Hosted Agent. A deployment webview will open.
- For "Deployment Method", select Code or Container.
- If deploying with "Code", for "Package Mode", select Remote or Local.
- If deploying with "Container", select either Default ACR, Custom ACR, or Customer ACR Image.
- The "Agent Name" should auto-populate.
- Select the "Next" button.
- This "Review and Deploy" page should all auto-populate.
- Select the "Deploy" button.
- Open the Visual Studio Code Command Palette and run the
Foundry Toolkit: Deploy Hosted Agentcommand.
- Open the Visual Studio Code Command Palette and run the
Foundry Toolkit: Deploy Hosted Agentcommand. - For "Deployment Method", select Code or Container.
- If deploying with "Code", for "Package Mode", select Remote or Local.
- If deploying with "Container", select either Default ACR, Custom ACR, or Customer ACR Image.
- The "Agent Name" should auto-populate.
- Select the "Next" button.
- This "Review and Deploy" page should all auto-populate.
- Select the "Deploy" button.