Skip to content

Commit 15ddeea

Browse files
committed
add OpenAPI tutorial
1 parent b47c52e commit 15ddeea

File tree

3 files changed

+174
-0
lines changed

3 files changed

+174
-0
lines changed
194 KB
Loading

articles/app-service/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ items:
5959
href: tutorial-ai-openai-search-dotnet.md
6060
- name: RAG Azure OpenAI and Azure SQL
6161
href: deploy-intelligent-apps-dotnet-to-azure-sql.md
62+
- name: Integrate into Foundry Agent with OpenAPI
63+
href: tutorial-ai-integrate-azure-ai-agent-openapi-dotnet.md
6264
- name: Invoke OpenAPI app from Azure AI Agent
6365
href: invoke-openapi-web-app-from-azure-ai-agent-service.md
6466
- name: Chatbot with local SLM
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
title: Integrate web app with OpenAPI in Azure AI Foundry Agent Service
3+
description: Empower your existing web apps by integrating their capabilities into Azure AI Foundry Agent Service with OpenAPI, enabling AI agents to perform real-world tasks.
4+
author: cephalin
5+
ms.author: cephalin
6+
ms.date: 06/16/2025
7+
ms.topic: tutorial
8+
ms.custom:
9+
- devx-track-dotnet
10+
ms.collection: ce-skilling-ai-copilot
11+
---
12+
13+
# Add an App Service app as a OpenAPI tool in Azure AI Foundry Agent Service (.NET)
14+
15+
In this tutorial, you'll learn how to expose your app's functionality through OpenAPI, add it as a tool to Azure AI Foundry Agent Service, and interact with your app using natural language in the agents playground.
16+
17+
If your web application already has useful features, like shopping, hotel booking, or data management, it's easy to make those capabilities available to an AI agent in Azure AI Foundry Agent Service. By simply adding an OpenAPI schema to your app, you enable the agent to understand and use your app's capabilities when it responds to users' prompts. This means anything your app can do, your AI agent can do too, with minimal effort beyond creating an OpenAPI endpoint for your app. In this tutorial, you start with a simple to-do list app. By the end, you'll be able to create, update, and manage tasks with an agent through conversational AI.
18+
19+
:::image type="content" source="media/tutorial-ai-integrate-azure-ai-agent-openapi-dotnet/agents-playground.png" alt-text="Screenshot showing the agents playground in the middle of a conversation that takes actions by using the OpenAPI tool.":::
20+
21+
> [!div class="checklist"]
22+
> * Add OpenAPI functionality to your web app.
23+
> * Make sure OpenAPI schema compatible with Azure AI Foundry Agent Service.
24+
> * Register your app as an OpenAPI tool in Azure AI Foundry Agent Service.
25+
> * Test your agent in the the agents playground.
26+
27+
## Prerequisites
28+
29+
This tutorial assumes you're working with the sample used in [Tutorial: Deploy an ASP.NET Core and Azure SQL Database app to Azure App Service](tutorial-dotnetcore-sqldb-app.md).
30+
31+
At a minimum, open the [sample application](https://github.com/Azure-Samples/msdocs-app-service-sqldb-dotnetcore) in GitHub Codespaces and deploy the app by running `azd up`.
32+
33+
## Add OpenAPI functionality to your web app
34+
35+
> [!TIP]
36+
> You can make all the following changes by telling GitHub Copilot in Agent mode:
37+
>
38+
> `I'd like to add OpenAPI functionality to all the methods in Controllers/TodosController.cs, but I don't want to change the existing functionality with MVC. Please also generate the server URL and operation ID in the schema.`
39+
40+
1. In the codespace terminal, add the NuGet [Swashbuckle](/aspnet/core/tutorials/getting-started-with-swashbuckle) packages to your project:
41+
42+
```dotnetcli
43+
dotnet add package Swashbuckle.AspNetCore --version 6.5.0
44+
dotnet add package Swashbuckle.AspNetCore.Annotations --version 6.5.0
45+
```
46+
47+
1. In *Controllers/TodosController.cs*, add the following API methods. To make them compatible with the Azure AI Foundry Agent Service, you must specify the `OperationId` property in the `SwaggerOperation` attribute (see [How to use Azure AI Foundry Agent Service with OpenAPI Specified Tools: Prerequisites](/azure/ai-services/agents/how-to/tools/openapi-spec#prerequisites)).
48+
49+
```csharp
50+
// POST: api/todos
51+
[HttpPost("api/todos")]
52+
[ProducesResponseType(StatusCodes.Status201Created)]
53+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
54+
[SwaggerOperation(Summary = "Creates a new todo item.", Description = "Creates a new todo item and returns it.", OperationId = "CreateTodo")]
55+
public async Task<IActionResult> CreateTodo([FromBody] Todo todo)
56+
{
57+
if (!ModelState.IsValid)
58+
{
59+
return BadRequest(ModelState);
60+
}
61+
_context.Add(todo);
62+
await _context.SaveChangesAsync();
63+
return CreatedAtAction(nameof(GetTodo), new { id = todo.ID }, todo);
64+
}
65+
66+
// PUT: api/todos/{id}
67+
[HttpPut("api/todos/{id}")]
68+
[ProducesResponseType(StatusCodes.Status204NoContent)]
69+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
70+
[ProducesResponseType(StatusCodes.Status404NotFound)]
71+
[SwaggerOperation(Summary = "Updates a todo item.", Description = "Updates an existing todo item by ID.", OperationId = "UpdateTodo")]
72+
public async Task<IActionResult> UpdateTodo(int id, [FromBody] Todo todo)
73+
{
74+
// Use the id from the URL fragment only, ignore mismatching check
75+
if (!TodoExists(id))
76+
{
77+
return NotFound();
78+
}
79+
todo.ID = id;
80+
_context.Entry(todo).State = EntityState.Modified;
81+
await _context.SaveChangesAsync();
82+
return NoContent();
83+
}
84+
85+
// DELETE: api/todos/{id}
86+
[HttpDelete("api/todos/{id}")]
87+
[ProducesResponseType(StatusCodes.Status204NoContent)]
88+
[ProducesResponseType(StatusCodes.Status404NotFound)]
89+
[SwaggerOperation(Summary = "Deletes a todo item.", Description = "Deletes a todo item by ID.", OperationId = "DeleteTodo")]
90+
public async Task<IActionResult> DeleteTodo(int id)
91+
{
92+
var todo = await _context.Todo.FindAsync(id);
93+
if (todo == null)
94+
{
95+
return NotFound();
96+
}
97+
_context.Todo.Remove(todo);
98+
await _context.SaveChangesAsync();
99+
return NoContent();
100+
}
101+
```
102+
103+
1. At the top of *Controllers/TodosController.cs*, add the following usings:
104+
105+
```csharp
106+
using Microsoft.AspNetCore.Http;
107+
using Swashbuckle.AspNetCore.Annotations;
108+
```
109+
110+
This code is duplicating the functionality of the existing controller, which is unnecessary, but you'll keep it for simplicity. A best practice would be to move the app logic to a service class, then call the service methods both from the MVC actions and from the API methods.
111+
112+
1. In *Program.cs*, register the Swagger generator service. This enables OpenAPI documentation for your API, which lets Azure AI Foundry Agent Service understand your APIs. Be sure to specify the server URL. Azure AI Foundry Agent Service needs a schema that contains the server URL.
113+
114+
```csharp
115+
builder.Services.AddSwaggerGen(c =>
116+
{
117+
c.EnableAnnotations();
118+
var websiteHostname = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");
119+
if (!string.IsNullOrEmpty(websiteHostname))
120+
{
121+
c.AddServer(new Microsoft.OpenApi.Models.OpenApiServer { Url = $"https://{websiteHostname}" });
122+
}
123+
});
124+
```
125+
126+
1. In *Program.cs*, enable the Swagger middleware. This middleware serves the generated OpenAPI documentation at runtime, making it accessible via a browser.
127+
128+
```csharp
129+
app.UseSwagger();
130+
app.UseSwaggerUI();
131+
```
132+
133+
1. In the codespace terminal, run the application with `dotnet run`.
134+
135+
1. Select **Open in Browser**.
136+
137+
1. Navigate to the Swagger UI by adding `/swagger/index.html` to the URL.
138+
139+
1. Confirm that the API operations work by trying them out. in the Swagger UI.
140+
141+
1. Back in the codespace terminal, deploy your changes by committing your changes (GitHub Actions method) or run `azd up` (Azure Developer CLI method).
142+
143+
1. Once your changes are deployed, navigate to https://<your-app's-url>/swagger/v1/swagger.json and copy the schema for later.
144+
145+
## Create an agent in Azure AI Foundry
146+
147+
1. Create an agent in the Azure AI Foundry portal by following the steps at:[Quickstart: Create a new agent](/azure/ai-services/agents/quickstart?pivots=ai-foundry-portal).
148+
149+
Note the [models you can use and the available regions](https://learn.microsoft.com/en-us/azure/ai-services/agents/concepts/model-region-support#azure-openai-models).
150+
151+
1. Select the new agent and add an action with the OpenAPI 3.0 specified tool by following the steps at [How to use the OpenAPI spec tool](https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/tools/openapi-spec-samples?pivots=portal).
152+
153+
1. In the **Define schema** page, paste the schema that you copied earlier. Review and save the action.
154+
155+
## Test the agent
156+
157+
1. If the agents playground isn't already opened in the foundry portal, select the agent and select **Try in playground**.
158+
159+
1. In **Instructions**, give some simple instructions, like *"Please use the todosApp tool to help manage tasks."*
160+
161+
1. Chat with the agent with the following prompt suggestions:
162+
163+
- Show me all the tasks.
164+
- Create a task called "Come up with three lettuce jokes."
165+
- Change that to "Come up with three knock-knock jokes."
166+
167+
:::image type="content" source="media/tutorial-ai-integrate-azure-ai-agent-openapi-dotnet/agents-playground.png" alt-text="Screenshot showing the agents playground in the middle of a conversation that takes actions by using the OpenAPI tool.":::
168+
169+
## More resources
170+
171+
[Integrate AI into your Azure App Service applications](overview-ai-integration.md)
172+
[What is Azure AI Foundry Agent Service?](/azure/ai-services/agents/overview)

0 commit comments

Comments
 (0)