Skip to content

Commit e80bb7d

Browse files
Copilotkzu
andcommitted
Simplify AgentCatalogMinimalAPI: remove OpenAPI, return anonymous objects
Co-authored-by: kzu <[email protected]>
1 parent 8709b50 commit e80bb7d

3 files changed

Lines changed: 25 additions & 110 deletions

File tree

‎dotnet/samples/HostedAgents/AgentCatalogMinimalAPI/AgentCatalogMinimalAPI.csproj‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
<ItemGroup>
1010
<PackageReference Include="Azure.AI.OpenAI" />
1111
<PackageReference Include="Azure.Identity" />
12-
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
1312
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" />
1413
</ItemGroup>
1514

‎dotnet/samples/HostedAgents/AgentCatalogMinimalAPI/Program.cs‎

Lines changed: 4 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88
using Microsoft.Agents.AI;
99
using Microsoft.Agents.AI.Hosting;
1010
using Microsoft.Extensions.AI;
11-
using System.Text.Json.Serialization;
1211

1312
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
1413
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
1514

1615
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
1716

18-
// Configure OpenAPI/Swagger
19-
builder.Services.AddOpenApi();
20-
2117
// Add a chat client to the service collection
2218
builder.Services.AddSingleton(sp => new AzureOpenAIClient(
2319
new Uri(endpoint),
@@ -82,91 +78,16 @@
8278

8379
WebApplication app = builder.Build();
8480

85-
// Configure the HTTP request pipeline
86-
app.MapOpenApi();
87-
88-
// Create the /agents endpoint
8981
app.MapGet("/agents", async (AgentCatalog agentCatalog, CancellationToken cancellationToken) =>
9082
{
91-
List<AgentInfo> agents = new List<AgentInfo>();
83+
List<object> agents = new List<object>();
9284

9385
await foreach (AIAgent agent in agentCatalog.GetAgentsAsync(cancellationToken))
9486
{
95-
AgentInfo agentInfo = new AgentInfo
96-
{
97-
Name = agent.Name ?? "Unknown"
98-
};
99-
100-
// Extract additional properties if they exist
101-
if (agent.AdditionalProperties is not null)
102-
{
103-
if (agent.AdditionalProperties.TryGetValue("icon", out object? iconValue) && iconValue is string icon)
104-
{
105-
agentInfo.Icon = icon;
106-
}
107-
108-
if (agent.AdditionalProperties.TryGetValue("beta", out object? betaValue) && betaValue is bool beta)
109-
{
110-
agentInfo.Beta = beta;
111-
}
112-
113-
if (agent.AdditionalProperties.TryGetValue("visibility", out object? visibilityValue) && visibilityValue is string visibilityString &&
114-
Enum.TryParse<AgentVisibility>(visibilityString, out AgentVisibility visibility))
115-
{
116-
agentInfo.Visibility = visibility;
117-
}
118-
}
119-
120-
agents.Add(agentInfo);
87+
agents.Add(new { name = agent.Name, properties = agent.AdditionalProperties });
12188
}
12289

123-
return Results.Ok(agents);
124-
})
125-
.WithName("GetAgents")
126-
.WithOpenApi();
90+
return agents;
91+
});
12792

12893
app.Run();
129-
130-
/// <summary>
131-
/// Represents information about an agent including its metadata and additional properties.
132-
/// </summary>
133-
internal sealed class AgentInfo
134-
{
135-
/// <summary>
136-
/// Gets or sets the name of the agent.
137-
/// </summary>
138-
public required string Name { get; set; }
139-
140-
/// <summary>
141-
/// Gets or sets the URL to the agent's icon.
142-
/// </summary>
143-
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
144-
public string? Icon { get; set; }
145-
146-
/// <summary>
147-
/// Gets or sets whether the agent is in beta.
148-
/// </summary>
149-
public bool Beta { get; set; }
150-
151-
/// <summary>
152-
/// Gets or sets the visibility of the agent.
153-
/// </summary>
154-
public AgentVisibility Visibility { get; set; }
155-
}
156-
157-
/// <summary>
158-
/// Defines the visibility options for an agent.
159-
/// </summary>
160-
[JsonConverter(typeof(JsonStringEnumConverter))]
161-
internal enum AgentVisibility
162-
{
163-
/// <summary>
164-
/// The agent is visible to all users.
165-
/// </summary>
166-
Visible,
167-
168-
/// <summary>
169-
/// The agent is unlisted and only accessible via direct reference.
170-
/// </summary>
171-
Unlisted
172-
}

‎dotnet/samples/HostedAgents/AgentCatalogMinimalAPI/README.md‎

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@ This sample demonstrates how to create a minimal API application that uses the `
44

55
## Overview
66

7-
The sample creates a minimal ASP.NET Core web API with:
7+
The sample creates an absolutely minimal ASP.NET Core web API with:
88
- Multiple AI agents registered with custom metadata
9-
- An `/agents` endpoint that returns a JSON list of all agents
10-
- Custom additional properties for each agent (icon URL, beta status, visibility)
9+
- An `/agents` endpoint that returns a JSON array of all agents with their names and additional properties
1110

1211
## Features
1312

1413
- **AgentCatalog Integration**: Uses the `AgentCatalog` to enumerate all registered agents
15-
- **Custom Metadata**: Each agent includes additional properties:
16-
- `icon`: A URL pointing to the agent's icon
17-
- `beta`: A boolean indicating if the agent is in beta
18-
- `visibility`: An enum (`Visible` or `Unlisted`) indicating the agent's visibility
19-
- **RESTful API**: Exposes a GET `/agents` endpoint that returns agent information as JSON
20-
- **OpenAPI/Swagger**: Includes OpenAPI documentation for easy API exploration
14+
- **Custom Metadata**: Each agent includes additional properties stored in `AdditionalProperties` dictionary
15+
- **Simple API**: Returns anonymous objects with agent name and properties - clients can handle the data as needed
2116

2217
## Prerequisites
2318

@@ -46,12 +41,7 @@ export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini"
4641
dotnet run
4742
```
4843

49-
3. Open a browser and navigate to the OpenAPI documentation:
50-
```
51-
http://localhost:5000/openapi/v1.json
52-
```
53-
54-
4. Test the `/agents` endpoint:
44+
3. Test the `/agents` endpoint:
5545
```bash
5646
curl http://localhost:5000/agents
5747
```
@@ -62,21 +52,27 @@ export AZURE_OPENAI_DEPLOYMENT_NAME="gpt-4o-mini"
6252
[
6353
{
6454
"name": "weather-agent",
65-
"icon": "https://example.com/icons/weather.png",
66-
"beta": false,
67-
"visibility": "Visible"
55+
"properties": {
56+
"icon": "https://example.com/icons/weather.png",
57+
"beta": false,
58+
"visibility": "Visible"
59+
}
6860
},
6961
{
7062
"name": "travel-agent",
71-
"icon": "https://example.com/icons/travel.png",
72-
"beta": true,
73-
"visibility": "Visible"
63+
"properties": {
64+
"icon": "https://example.com/icons/travel.png",
65+
"beta": true,
66+
"visibility": "Visible"
67+
}
7468
},
7569
{
7670
"name": "experimental-agent",
77-
"icon": "https://example.com/icons/experimental.png",
78-
"beta": true,
79-
"visibility": "Unlisted"
71+
"properties": {
72+
"icon": "https://example.com/icons/experimental.png",
73+
"beta": true,
74+
"visibility": "Unlisted"
75+
}
8076
}
8177
]
8278
```
@@ -87,8 +83,7 @@ The sample demonstrates:
8783

8884
1. **Agent Registration**: Multiple agents are registered using `builder.AddAIAgent()` with custom additional properties
8985
2. **AgentCatalog Usage**: The `AgentCatalog` service is injected into the endpoint handler
90-
3. **Metadata Extraction**: Additional properties are extracted from each agent and mapped to a response model
91-
4. **Type Safety**: Uses strongly-typed classes and enums for the API response
86+
3. **Simple Response**: Returns anonymous objects with `name` and `properties` - clients handle parsing as needed
9287

9388
## Key Concepts
9489

0 commit comments

Comments
 (0)