Skip to content

Commit 5b29132

Browse files
authored
Merge pull request #302900 from cephalin/springai2
Springai2
2 parents b3c6e0d + b5ab59e commit 5b29132

11 files changed

+639
-24
lines changed

.openpublishing.publish.config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,12 @@
626626
"branch": "main",
627627
"branch_mapping": {}
628628
},
629+
{
630+
"path_to_root": "app-service-agentic-semantic-kernel-java",
631+
"url": "https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java",
632+
"branch": "main",
633+
"branch_mapping": {}
634+
},
629635
{
630636
"path_to_root": "playwright-testing-service",
631637
"url": "https://github.com/microsoft/playwright-testing-service",
87 KB
Loading
92.7 KB
Loading
114 KB
Loading

articles/app-service/toc.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ items:
118118
href: tutorial-ai-openai-search-java.md
119119
- name: Chatbot with local SLM
120120
href: tutorial-ai-slm-spring-boot.md
121+
- name: Add MCP server to app
122+
href: tutorial-ai-model-context-protocol-server-java.md
123+
- name: AI Foundry Agent calling web app
124+
href: tutorial-ai-integrate-azure-ai-agent-java.md
125+
- name: Agentic web app
126+
href: tutorial-ai-agent-web-app-semantic-kernel-java.md
121127
- name: Node.js
122128
items:
123129
- name: Quickstart

articles/app-service/tutorial-ai-agent-web-app-semantic-kernel-foundry-dotnet.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ms.collection: ce-skilling-ai-copilot
1414

1515
# Tutorial: Build an agentic web app in Azure App Service with Microsoft Semantic Kernel or Azure AI Foundry Agent Service (.NET)
1616

17-
This tutorial demonstrates how to add agentic capability to an existing data-driven CRUD application. It does this using two different approaches: Microsoft Semantic Kernel and Azure AI Foundry Agent Service.
17+
This tutorial demonstrates how to add agentic capability to an existing data-driven ASP.NET Core CRUD application. It does this using two different approaches: Microsoft Semantic Kernel and Azure AI Foundry Agent Service.
1818

1919
If your web application already has useful features, like shopping, hotel booking, or data management, it's relatively straightforward to add agent functionality to your web application by wrapping those functionalities in a plugin (for Semantic Kernel) or as an OpenAPI endpoint (for AI Foundry Agent Service). 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 in an App Service app.
2020

@@ -58,9 +58,9 @@ The easiest way to get started is by using GitHub Codespaces, which provides a c
5858

5959
1. Navigate to the GitHub repository at [https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-ai-foundry-agent](https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-ai-foundry-agent).
6060

61-
2. Select the **Code** button, select the **Codespaces** tab, and click **Create codespace on main**.
61+
2. Select the **Code** button, select the **Codespaces** tab, and select **Create codespace on main**.
6262

63-
3. Wait a few moments for your Codespace to initialize. When ready, you'll see a fully configured VS Code environment in your browser.
63+
3. Wait a few moments for your Codespace to initialize. When ready, you'll see a fully configured development environment in your browser.
6464

6565
4. Run the application locally:
6666

@@ -104,6 +104,8 @@ The OpenAPI code is defined in *Program.cs*. For example, the "get tasks" API de
104104

105105
## Deploy the sample application
106106

107+
The sample respository contains an Azure Developer CLI (AZD) template, which creates an App Service app with managed identity and deploys your sample application.
108+
107109
1. In the terminal, log into Azure using Azure Developer CLI:
108110

109111
```bash
@@ -243,7 +245,7 @@ When you're done with the application, you can delete the App Service resources
243245
azd down --purge
244246
```
245247
246-
Sind the AZD template doesn't include the Azure AI Foundry resources, you need to delete them manually if you want.
248+
Since the AZD template doesn't include the Azure AI Foundry resources, you need to delete them manually if you want.
247249

248250
## More resources
249251

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
---
2+
title: Agentic app with Semantic Kernel (Java)
3+
description: Learn how to quickly deploy a production-ready, agentic web application using Java with Azure App Service and Microsoft Semantic Kernel.
4+
ms.service: azure-app-service
5+
author: cephalin
6+
ms.author: cephalin
7+
ms.devlang: csharp
8+
ms.topic: tutorial
9+
ms.date: 07/16/2025
10+
ms.custom:
11+
- devx-track-java
12+
ms.collection: ce-skilling-ai-copilot
13+
---
14+
15+
# Tutorial: Build an agentic web app in Azure App Service with Microsoft Semantic Kernel (Spring Boot)
16+
17+
This tutorial demonstrates how to add agentic capability to an existing data-driven Spring Boot WebFlux CRUD application. It does this using Microsoft Semantic Kernel.
18+
19+
If your web application already has useful features, like shopping, hotel booking, or data management, it's relatively straightforward to add agent functionality to your web application by wrapping those functionalities in a plugin (for Semantic Kernel). 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 in an App Service app.
20+
21+
:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-java/semantic-kernel-agent.png" alt-text="Screenshot of a chat completion session with a semantic kernel agent.":::
22+
23+
> [!NOTE]
24+
> Azure AI Foundry Agent Service currently doesn't have a Java SDK, so isn't included in the scope of this article.
25+
26+
In this tutorial, you learn how to:
27+
28+
> [!div class="checklist"]
29+
> * Convert existing app functionality into a plugin for Semantic Kernel.
30+
> * Add the plugin to a Semantic Kernel agent and use it in a web app.
31+
> * Assign the required permissions for managed identity connectivity.
32+
33+
## Prerequisites
34+
35+
- An Azure account with an active subscription - [Create an account for free](https://azure.microsoft.com/free/java).
36+
- GitHub account to use GitHub Codespaces - [Learn more about GitHub Codespaces](https://docs.github.com/codespaces/overview).
37+
38+
## Open the sample with Codespaces
39+
40+
The easiest way to get started is by using GitHub Codespaces, which provides a complete development environment with all required tools preinstalled.
41+
42+
[![Open in GitHub Codespaces.](https://github.com/codespaces/badge.svg)](https://codespaces.new/Azure-Samples/app-service-agentic-semantic-kernel-java)
43+
44+
1. Navigate to the GitHub repository at [https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java](https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java).
45+
46+
2. Select the **Code** button, select the **Codespaces** tab, and select **Create codespace on main**.
47+
48+
3. Wait a few moments for your Codespace to initialize. When ready, you'll see a fully configured development environment in your browser.
49+
50+
4. Run the application locally:
51+
52+
```bash
53+
mvn spring-boot:run
54+
```
55+
56+
5. When you see **Your application running on port 8080 is available**, select **Open in Browser** and add a few tasks.
57+
58+
## Review the agent code
59+
60+
The Semantic Kernel agent is initialized in [src/main/java/com/example/crudtaskswithagent/controller/AgentController.java](https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java/blob/main/src/main/java/com/example/crudtaskswithagent/controller/AgentController.java), when the user enters the first prompt in a new browser session.
61+
62+
You can find the initialization code in the `SemanticKernelAgentService` constructor (in [src/main/java/com/example/crudtaskswithagent/service/SemanticKernelAgentService.java](https://github.com/Azure-Samples/app-service-agentic-semantic-kernel-java/blob/main/src/main/java/com/example/crudtaskswithagent/service/SemanticKernelAgentService.java)). The initialization code does the following:
63+
64+
- Creates a kernel with chat completion.
65+
- Adds a kernel plugin that encapsulates the functionality of the CRUD application (in *src/main/java/com/example/crudtaskswithagent/plugin/TaskCrudPlugin.java*). The interesting parts of the plugin are the `DefineKernelFunction` annotations on the method declarations and the `description` and `returnType` parameters that help the kernel call the plugin intelligently.
66+
- Creates a [chat completion agent](/semantic-kernel/frameworks/agent/agent-types/chat-completion-agent?pivots=programming-language-java), and configures it to let the AI model automatically invoke functions (`FunctionChoiceBehavior.auto(true)`).
67+
- Creates an agent thread that automatically manages the chat history.
68+
69+
:::code language="csharp" source="~/app-service-agentic-semantic-kernel-java/src/main/java/com/example/crudtaskswithagent/service/SemanticKernelAgentService.java" range="38-90" highlight="11-48,58" :::
70+
71+
Each time the prompt is received, the server code uses `ChatCompletionAgent.invokeAsync()` to invoke the agent with the user prompt and the agent thread. The agent thread keeps track of the chat history.
72+
73+
:::code language="csharp" source="~/app-service-agentic-semantic-kernel-java/src/main/java/com/example/crudtaskswithagent/service/SemanticKernelAgentService.java" range="109-158" highlight="8" :::
74+
75+
## Deploy the sample application
76+
77+
The sample respository contains an Azure Developer CLI (AZD) template, which creates an App Service app with managed identity and deploys your sample application.
78+
79+
1. In the terminal, log into Azure using Azure Developer CLI:
80+
81+
```bash
82+
azd auth login
83+
```
84+
85+
Follow the instructions to complete the authentication process.
86+
87+
4. Deploy the Azure App Service app with the AZD template:
88+
89+
```bash
90+
azd up
91+
```
92+
93+
1. When prompted, give the following answers:
94+
95+
|Question |Answer |
96+
|---------|---------|
97+
|Enter a new environment name: | Type a unique name. |
98+
|Select an Azure Subscription to use: | Select the subscription. |
99+
|Pick a resource group to use: | Select **Create a new resource group**. |
100+
|Select a location to create the resource group in:| Select any region. The resources will actually be created in **East US 2**.|
101+
|Enter a name for the new resource group:| Type **Enter**.|
102+
103+
1. In the AZD output, find the URL of your app and navigate to it in the browser. The URL looks like this in the AZD output:
104+
105+
<pre>
106+
Deploying services (azd deploy)
107+
108+
(✓) Done: Deploying service web
109+
- Endpoint: &lt;URL>
110+
</pre>
111+
112+
1. After successful deployment, you'll see a URL for your deployed application.
113+
114+
You now have an App Service app with a system-assigned managed identity.
115+
116+
## Create and configure the Azure AI Foundry resource
117+
118+
1. In the [Azure AI Foundry portal](https://ai.azure.com), deploy a model of your choice (see [Quickstart: Get started with Azure AI Foundry](/azure/ai-foundry/quickstarts/get-started-code?tabs=azure-ai-foundry&pivots=fdp-project)). A project and a model deployment are created for you in the process.
119+
120+
1. From the left menu, select **Overview**.
121+
122+
1. Select **Azure OpenAI** and copy the URL in **Azure OpenAI endpoint** for later.
123+
124+
:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-java/foundry-openai-endpoint.png" alt-text="Screenshot showing how to copy the OpenAI endpoint in the foundry portal.":::
125+
126+
1. Select **Models + endpoints** and copy the name of the model deployment for later.
127+
128+
:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-java/foundry-model-deployment.png" alt-text="Screenshot showing how to copy the model deployment name in the foundry portal.":::
129+
130+
## Assign required permissions
131+
132+
1. At the upper right corner of the foundry portal, select the name of the resource, then select **Resource Group** to open it in the Azure portal.
133+
134+
:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-foundry-dotnet/go-to-azure-portal.png" alt-text="Screenshot showing how to quickly go to the resource group view for the foundry resource in the Azure portal.":::
135+
136+
1. Add a role for each of the two resources for the App Service app's manage identity using the following table:
137+
138+
| Target resource | Required role | Needed for |
139+
|--------------------------------|-------------------------------------|-------------------------|
140+
| Azure AI Foundry | Cognitive Services OpenAI User | The chat completion service in the semantic kernel. |
141+
142+
For instructions, see [Assign Azure roles using the Azure portal](/azure/role-based-access-control/role-assignments-portal).
143+
144+
## Configure connection variables in your sample application
145+
146+
1. Open *src/main/resources/application.properties*. Using the values you copied earlier from the AI Foundry portal, configure the following variables:
147+
148+
| Variable | Description |
149+
|-------------------------------|----------------------------------------------------------|
150+
| `azure.openai.endpoint` | Azure OpenAI endpoint (copied from the Overview page). This is needed by the Semantic Kernel agent. |
151+
| `azure.openai.deployment` | Model name in the deployment (copied from the Models + endpoints page). This is needed by the Semantic Kernel agent. |
152+
153+
> [!NOTE]
154+
> To keep the tutorial simple, you'll use these variables in *src/main/resources/application.properties* instead of overwriting them with app settings in App Service.
155+
156+
1. Sign in to Azure with the Azure CLI:
157+
158+
```bash
159+
az login
160+
```
161+
162+
This allows the Azure Identity client library in the sample code to receive an authentication token for the logged in user. Remember that you added the required role for this user earlier.
163+
164+
1. Run the application locally:
165+
166+
```bash
167+
mvn spring-boot:run
168+
```
169+
170+
1. When you see **Your application running on port 8080 is available**, select **Open in Browser**.
171+
172+
1. Try out the chat interface. If you get a response, your application is connecting successfully to the Azure AI Foundry resource.
173+
174+
1. Back in the GitHub codespace, deploy your app changes.
175+
176+
```bash
177+
azd up
178+
```
179+
180+
1. Navigate to the deployed application again and test the chat agents.
181+
182+
:::image type="content" source="media/tutorial-ai-agent-web-app-semantic-kernel-java/semantic-kernel-agent.png" alt-text="Screenshot of a chat completion session with a semantic kernel agent.":::
183+
184+
## Clean up resources
185+
186+
When you're done with the application, you can delete the App Service resources to avoid incurring further costs:
187+
188+
```bash
189+
azd down --purge
190+
```
191+
192+
Since the AZD template doesn't include the Azure AI Foundry resources, you need to delete them manually if you want.
193+
194+
## More resources
195+
196+
- [Integrate AI into your Azure App Service applications](overview-ai-integration.md)
197+
- [Introduction to Semantic Kernel](/semantic-kernel/overview/)

0 commit comments

Comments
 (0)