-
Notifications
You must be signed in to change notification settings - Fork 871
Fixes Foundry Hosted Agents HTTP endpoints #16130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
eerhardt
merged 36 commits into
microsoft:main
from
tommasodotNET:fix/hosted-agent-overwrite-http-endpoints
Apr 16, 2026
Merged
Changes from 15 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
a1506c4
Remove existing HTTP/HTTPS endpoints before adding new ones in run mode
4a66ae7
include sample for foundry hosted agents
abbb568
Refactor PublishAsHostedAgent method to accept resources with endpoin…
b4ea257
updates foundry names
2f9be79
Update playground/FoundryHostedAgents/FoundryHostedAgents.AppHost/app…
tommasodotNET 038a43d
Fix: Update HTTP endpoint proxying behavior in run mode test
801c8fa
Merge branch 'fix/hosted-agent-overwrite-http-endpoints' of github.co…
2793d22
Update src/Aspire.Hosting.Foundry/HostedAgent/HostedAgentBuilderExten…
tommasodotNET c52d654
adds dotnet example
541b4e1
Merge branch 'fix/hosted-agent-overwrite-http-endpoints' of github.co…
d0a5b80
reverts foundry names in sample
5b02382
allow multiple hosted agents to run locally by configuring different …
dd27397
update Microsoft.Agents.AI.OpenAI and OpenAI package versions to late…
4f41344
add Azure.AI.AgentServer package source and remove unused project ref…
cc944ce
set DefaultTargetFramework on samples
a92606b
fixes on the playground
e187d4b
refactor: streamline container resource deployment logic in HostedAge…
85b4a41
-fix apphost var ref
5aa7773
chore: update Microsoft.Agents.AI.OpenAI and Microsoft.Extensions.AI …
f320db2
adds end2end test
094aea5
fix: preserve existing HTTP endpoint target port in run mode
8af2bb5
fix: reorder method calls for clarity in hosted agent configuration
4a7bc24
fix: reorder method calls to ensure proper reference waiting in hoste…
40156eb
fix: revert Microsoft.Extensions.AI and Microsoft.Extensions.AI.OpenA…
tommasodotNET 81fd3d0
only show responses endpoint
tommasodotNET e401c8e
Fix versions
eerhardt 7c56f25
Merge remote-tracking branch 'upstream/main' into fix/hosted-agent-ov…
eerhardt c8f6848
Remove unnecessary code
eerhardt 8fd7c88
PR feedback
eerhardt e583b05
fix: extend generic constraints for PublishAsHostedAgent methods to i…
tommasodotNET 24984ad
fix: update resource handling to require PublishAsDockerFile for both…
tommasodotNET 5005a21
Merge branch 'fix/hosted-agent-overwrite-http-endpoints' of github.co…
tommasodotNET 6c7eee4
Fix deploying ProjectResources.
eerhardt 6f3951a
Add workaround for role assignments
eerhardt 7c1a311
Merge remote-tracking branch 'upstream/main' into fix/hosted-agent-ov…
eerhardt 9ea1f30
Fix versioning issues.
eerhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
playground/FoundryHostedAgents/DotNetHostedAgent/.dockerignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| **/.dockerignore | ||
| **/.env | ||
| **/.git | ||
| **/.gitignore | ||
| **/.project | ||
| **/.settings | ||
| **/.toolstarget | ||
| **/.vs | ||
| **/.vscode | ||
| **/*.*proj.user | ||
| **/*.dbmdl | ||
| **/*.jfm | ||
| **/azds.yaml | ||
| **/bin | ||
| **/charts | ||
| **/docker-compose* | ||
| **/Dockerfile* | ||
| **/node_modules | ||
| **/npm-debug.log | ||
| **/obj | ||
| **/secrets.dev.yaml | ||
| **/values.dev.yaml | ||
| LICENSE | ||
| README.md |
33 changes: 33 additions & 0 deletions
33
playground/FoundryHostedAgents/DotNetHostedAgent/DotNetHostedAgent.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>$(DefaultTargetFramework)</TargetFrameworks> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <EnablePreviewFeatures>true</EnablePreviewFeatures> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageVersion Update="Microsoft.Extensions.AI" Version="10.3.0" /> | ||
| <PackageVersion Update="Microsoft.Extensions.AI.OpenAI" Version="10.3.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Remove="Microsoft.CodeAnalysis.NetAnalyzers" /> | ||
| <PackageReference Remove="Microsoft.VisualStudio.Threading.Analyzers" /> | ||
| <PackageReference Remove="xunit.analyzers" /> | ||
| <PackageReference Remove="Moq.Analyzers" /> | ||
| <PackageReference Remove="Roslynator.Analyzers" /> | ||
| <PackageReference Remove="Roslynator.CodeAnalysis.Analyzers" /> | ||
| <PackageReference Remove="Roslynator.Formatting.Analyzers" /> | ||
| <PackageReference Include="Azure.AI.AgentServer.AgentFramework" /> | ||
| <PackageReference Include="Azure.AI.Projects" /> | ||
| <PackageReference Include="Azure.AI.OpenAI" /> | ||
| <PackageReference Include="Azure.Identity" /> | ||
| <PackageReference Include="Microsoft.Agents.AI.OpenAI" /> | ||
| <PackageReference Include="Microsoft.Extensions.AI" /> | ||
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
90 changes: 90 additions & 0 deletions
90
playground/FoundryHostedAgents/DotNetHostedAgent/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.ComponentModel; | ||
| using System.Data.Common; | ||
| using Azure.AI.AgentServer.AgentFramework.Extensions; | ||
| using Azure.AI.OpenAI; | ||
| using Azure.Identity; | ||
| using Microsoft.Agents.AI; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| string chatConnectionString = Environment.GetEnvironmentVariable("ConnectionStrings__chat") | ||
| ?? throw new InvalidOperationException("ConnectionStrings__chat is not set."); | ||
|
|
||
| DbConnectionStringBuilder chatConnectionBuilder = new() | ||
| { | ||
| ConnectionString = chatConnectionString, | ||
| }; | ||
|
|
||
| string endpoint = GetRequiredConnectionValue(chatConnectionBuilder, "Endpoint"); | ||
| string deploymentName = GetRequiredConnectionValue(chatConnectionBuilder, "Deployment"); | ||
|
|
||
| if (!Uri.TryCreate(endpoint, UriKind.Absolute, out Uri? openAiEndpoint) || openAiEndpoint is null) | ||
| { | ||
| throw new InvalidOperationException("ConnectionStrings__chat contains an invalid Endpoint value."); | ||
| } | ||
|
|
||
| Console.WriteLine($"OpenAI Endpoint: {openAiEndpoint}"); | ||
| Console.WriteLine($"Model Deployment: {deploymentName}"); | ||
|
|
||
| // Read the port from environment variable (set by Aspire), default to 8088 | ||
| string? portString = Environment.GetEnvironmentVariable("DEFAULT_AD_PORT"); | ||
| int port = int.TryParse(portString, out int parsedPort) ? parsedPort : 8088; | ||
|
|
||
| [Description("Get a weather forecast")] | ||
| WeatherForecast[]? GetWeatherForecast() | ||
| { | ||
| string[] summaries = ["Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"]; | ||
| var forecast = Enumerable.Range(1, 5).Select(index => | ||
| new WeatherForecast | ||
| ( | ||
| DateOnly.FromDateTime(DateTime.Now.AddDays(index)), | ||
| Random.Shared.Next(-20, 55), | ||
| summaries[Random.Shared.Next(summaries.Length)] | ||
| )) | ||
| .ToArray(); | ||
| return forecast; | ||
| } | ||
|
|
||
| DefaultAzureCredential credential = new(); | ||
|
|
||
| IChatClient chatClient = new AzureOpenAIClient(openAiEndpoint, credential) | ||
| .GetChatClient(deploymentName) | ||
| .AsIChatClient() | ||
| .AsBuilder() | ||
| .UseOpenTelemetry(sourceName: "Agents", configure: cfg => cfg.EnableSensitiveData = true) | ||
| .Build(); | ||
|
|
||
| AIAgent agent = chatClient.AsAIAgent( | ||
| name: "WeatherAgent", | ||
| instructions: """You are the Weather Intelligence Agent that can return weather forecast using your tools.""", | ||
| tools: [AIFunctionFactory.Create(GetWeatherForecast)]) | ||
| .AsBuilder() | ||
| .UseOpenTelemetry(sourceName: "Agents", configure: cfg => cfg.EnableSensitiveData = true) | ||
| .Build(); | ||
|
|
||
| Console.WriteLine($"Weather Agent Server running on http://localhost:{port}"); | ||
| await agent.RunAIAgentAsync(telemetrySourceName: "Agents"); | ||
|
|
||
| string GetRequiredConnectionValue(DbConnectionStringBuilder connectionBuilder, string key) | ||
| { | ||
| if (!connectionBuilder.TryGetValue(key, out object? rawValue) || rawValue is null) | ||
| { | ||
| throw new InvalidOperationException($"ConnectionStrings__chat is missing '{key}'."); | ||
| } | ||
|
|
||
| string? value = rawValue.ToString(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(value)) | ||
| { | ||
| throw new InvalidOperationException($"ConnectionStrings__chat has an empty '{key}' value."); | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| internal sealed record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) | ||
| { | ||
| public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
| } |
26 changes: 26 additions & 0 deletions
26
playground/FoundryHostedAgents/FoundryHostedAgents.AppHost/AppHost.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Aspire.Hosting.Foundry; | ||
|
|
||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var tenantId = builder.AddParameterFromConfiguration("tenant", "Azure:TenantId"); | ||
|
tommasodotNET marked this conversation as resolved.
Outdated
|
||
|
|
||
| var foundry = builder.AddFoundry("aif-myfoundry"); | ||
|
tommasodotNET marked this conversation as resolved.
|
||
| var project = foundry.AddProject("proj-myproject"); | ||
| var chat = project.AddModelDeployment("chat", FoundryModel.OpenAI.Gpt41); | ||
|
|
||
| builder.AddPythonApp("weather-hosted-agent", "../app", "main.py") | ||
| .WithUv() | ||
| .WithReference(project).WaitFor(project) | ||
|
tommasodotNET marked this conversation as resolved.
Outdated
|
||
| .WithReference(chat).WaitFor(chat) | ||
| .WithEnvironment("AZURE_TENANT_ID", tenantId) | ||
| .PublishAsHostedAgent(project); | ||
|
|
||
| builder.AddProject<Projects.DotNetHostedAgent>("proj-dotnet-hosted-agent") | ||
|
tommasodotNET marked this conversation as resolved.
|
||
| .WithReference(chat).WaitFor(chat) | ||
| .PublishAsHostedAgent() | ||
|
tommasodotNET marked this conversation as resolved.
Outdated
|
||
| .WithEndpoint("http", e => e.TargetPort = 9000); | ||
|
|
||
| builder.Build().Run(); | ||
24 changes: 24 additions & 0 deletions
24
...ground/FoundryHostedAgents/FoundryHostedAgents.AppHost/FoundryHostedAgents.AppHost.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>$(DefaultTargetFramework)</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| <UserSecretsId>232bfcff-4739-4857-9b6f-6d7681cb0980</UserSecretsId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="..\..\KnownResourceNames.cs" Link="KnownResourceNames.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <AspireProjectOrPackageReference Include="Aspire.Hosting.Azure" /> | ||
| <AspireProjectOrPackageReference Include="Aspire.Hosting.Foundry" /> | ||
| <AspireProjectOrPackageReference Include="Aspire.Hosting.AppHost" /> | ||
| <AspireProjectOrPackageReference Include="Aspire.Hosting.Python" /> | ||
| <ProjectReference Include="../DotNetHostedAgent/DotNetHostedAgent.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
40 changes: 40 additions & 0 deletions
40
playground/FoundryHostedAgents/FoundryHostedAgents.AppHost/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:17145;http://localhost:15099", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development", | ||
| "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21011", | ||
| "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22200" | ||
| } | ||
| }, | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:15099", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development", | ||
| "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19103", | ||
| "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20134", | ||
| "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true" | ||
| } | ||
| }, | ||
| "generate-manifest": { | ||
| "commandName": "Project", | ||
| "launchBrowser": true, | ||
| "dotnetRunMessages": true, | ||
| "commandLineArgs": "--publisher manifest --output-path aspire-manifest.json", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
playground/FoundryHostedAgents/FoundryHostedAgents.AppHost/appsettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning", | ||
| "Aspire.Hosting.Dcp": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Byte-compiled / optimized / DLL files | ||
| **/__pycache__/ | ||
| **/*.py[cod] | ||
|
|
||
| # Virtual environment | ||
| .env | ||
| .venv/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import asyncio | ||
| import datetime | ||
| import json | ||
| import os | ||
| import random | ||
|
|
||
| # Microsoft Agent Framework | ||
| from agent_framework import Agent, tool | ||
| from agent_framework.azure import AzureOpenAIChatClient | ||
| from azure.ai.agentserver.agentframework import from_agent_framework | ||
| from azure.identity import DefaultAzureCredential | ||
|
|
||
| @tool(name="get_forecast", description="Get a weather forecast") | ||
| async def get_forecast() -> str: | ||
| try: | ||
| summaries = [ | ||
| "Freezing", | ||
| "Bracing", | ||
| "Chilly", | ||
| "Cool", | ||
| "Mild", | ||
| "Warm", | ||
| "Balmy", | ||
| "Hot", | ||
| "Sweltering", | ||
| "Scorching", | ||
| ] | ||
|
|
||
| forecast = [] | ||
| for index in range(1, 6): # Range 1 to 5 (inclusive) | ||
| temp_c = random.randint(-20, 55) | ||
| forecast_date = datetime.datetime.now() + datetime.timedelta(days=index) | ||
| forecast_item = { | ||
| "date": forecast_date.isoformat(), | ||
| "temperatureC": temp_c, | ||
| "temperatureF": int(temp_c * 9 / 5) + 32, | ||
| "summary": random.choice(summaries), | ||
| } | ||
| forecast.append(forecast_item) | ||
|
|
||
| return json.dumps(forecast, indent=2) | ||
| except Exception as e: | ||
| return json.dumps({"error": str(e)}) | ||
|
|
||
| async def main(): | ||
| """Main function to run the agent as a web server.""" | ||
|
|
||
| # client = FoundryChatClient(project_endpoint=os.getenv("CHAT_URI"), credential=AzureCliCredential(), model="chat") | ||
| agent = AzureOpenAIChatClient(endpoint=os.getenv("CHAT_URI"), credential=DefaultAzureCredential(), deployment_name="chat").as_agent( | ||
| # client = client, | ||
| name="weather-agent", | ||
| instructions="""You are the Weather Intelligence Agent that can return weather forecast using your tools.""", | ||
| tools=[get_forecast], | ||
| ) | ||
|
|
||
|
|
||
| app = from_agent_framework(agent) | ||
|
|
||
| await app.run_async() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [project] | ||
| name = "weather-agent-python" | ||
| version = "0.1.0" | ||
| description = "Weather intelligence agent for AlpineAI ski resort" | ||
| requires-python = ">=3.11" | ||
| dependencies = [ | ||
| "fastapi>=0.104.1", | ||
| "agent-framework", | ||
| "azure-ai-agentserver-agentframework>=1.0.0b17", | ||
| "azure-identity", | ||
| ] | ||
|
|
||
| [tool.uv] | ||
| prerelease = "allow" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "appHost": { | ||
| "path": "FoundryHostedAgents.AppHost/FoundryHostedAgents.AppHost.csproj" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.