-
Notifications
You must be signed in to change notification settings - Fork 1.2k
.NET: Add Foundry Agents Tool Sample - Web Search #4040
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
rogerbarreto
merged 7 commits into
microsoft:main
from
rogerbarreto:feature/3674-web-search
Feb 23, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4949bc6
.NET: Add Web Search sample #3674
rogerbarreto ab64a21
.NET: Fix WebSearch sample to use Responses API built-in web search
rogerbarreto e800d8a
Fix README to align DefaultAzureCredential docs with code
rogerbarreto 108c8a6
Merge branch 'main' into feature/3674-web-search
rogerbarreto 984b92e
Address review: add project to solution, README, simplify response text
rogerbarreto 6ba0736
Merge branch 'main' into feature/3674-web-search
rogerbarreto cc544a0
Fix merge conflict in slnx solution file
rogerbarreto 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
Some comments aren't visible on the classic Files Changed page.
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
22 changes: 22 additions & 0 deletions
22
...tarted/FoundryAgents/FoundryAgents_Step25_WebSearch/FoundryAgents_Step25_WebSearch.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,22 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFrameworks>net10.0</TargetFrameworks> | ||
|
|
||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <NoWarn>$(NoWarn);CA1812;CS8321</NoWarn> | ||
rogerbarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Console" /> | ||
| <PackageReference Include="Azure.Identity" /> | ||
| <PackageReference Include="Azure.AI.Projects" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\..\..\src\Microsoft.Agents.AI.AzureAI\Microsoft.Agents.AI.AzureAI.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
65 changes: 65 additions & 0 deletions
65
dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/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,65 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| // This sample shows how to use the Responses API Web Search Tool with AI Agents. | ||
|
|
||
| using Azure.AI.Projects; | ||
| using Azure.AI.Projects.OpenAI; | ||
| using Azure.Identity; | ||
| using Microsoft.Agents.AI; | ||
| using Microsoft.Extensions.AI; | ||
| using OpenAI.Responses; | ||
|
|
||
| string endpoint = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_FOUNDRY_PROJECT_ENDPOINT is not set."); | ||
| string deploymentName = Environment.GetEnvironmentVariable("AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; | ||
|
|
||
| const string AgentInstructions = "You are a helpful assistant that can search the web to find current information and answer questions accurately."; | ||
| const string AgentName = "WebSearchAgent"; | ||
|
|
||
| // Get a client to create/retrieve/delete server side agents with Azure Foundry Agents. | ||
| AIProjectClient aiProjectClient = new(new Uri(endpoint), new DefaultAzureCredential()); | ||
rogerbarreto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Option 1 - Using HostedWebSearchTool (MEAI + AgentFramework) | ||
| AIAgent agent = await CreateAgentWithMEAIAsync(); | ||
|
|
||
| // Option 2 - Using PromptAgentDefinition with the Responses API native type | ||
| // AIAgent agent = await CreateAgentWithNativeSDKAsync(); | ||
|
|
||
| AgentResponse response = await agent.RunAsync("What's the weather today in Seattle?"); | ||
|
|
||
| // Get the text response | ||
| Console.WriteLine($"Response: {response.Text}"); | ||
|
|
||
| // Getting any annotations/citations generated by the web search tool | ||
| foreach (AIAnnotation annotation in response.Messages.SelectMany(m => m.Contents).SelectMany(c => c.Annotations ?? [])) | ||
| { | ||
| Console.WriteLine($"Annotation: {annotation}"); | ||
| if (annotation.RawRepresentation is UriCitationMessageAnnotation urlCitation) | ||
| { | ||
| Console.WriteLine($$""" | ||
| Title: {{urlCitation.Title}} | ||
| URL: {{urlCitation.Uri}} | ||
| """); | ||
| } | ||
| } | ||
|
|
||
| // Cleanup by agent name removes the agent version created. | ||
| await aiProjectClient.Agents.DeleteAgentAsync(agent.Name); | ||
|
|
||
| // Creates the agent using the HostedWebSearchTool MEAI abstraction that maps to the built-in Responses API web search tool. | ||
| async Task<AIAgent> CreateAgentWithMEAIAsync() | ||
| => await aiProjectClient.CreateAIAgentAsync( | ||
| name: AgentName, | ||
| model: deploymentName, | ||
| instructions: AgentInstructions, | ||
| tools: [new HostedWebSearchTool()]); | ||
|
|
||
| // Creates the agent using the PromptAgentDefinition with the Responses API native ResponseTool.CreateWebSearchTool(). | ||
| async Task<AIAgent> CreateAgentWithNativeSDKAsync() | ||
| => await aiProjectClient.CreateAIAgentAsync( | ||
| AgentName, | ||
| new AgentVersionCreationOptions( | ||
| new PromptAgentDefinition(model: deploymentName) | ||
| { | ||
| Instructions = AgentInstructions, | ||
| Tools = { ResponseTool.CreateWebSearchTool() } | ||
| })); | ||
52 changes: 52 additions & 0 deletions
52
...t/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/README.md
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,52 @@ | ||
| # Using Web Search with AI Agents | ||
|
|
||
| This sample demonstrates how to use the Responses API web search tool with AI agents. The web search tool allows agents to search the web for current information to answer questions accurately. | ||
|
|
||
| ## What this sample demonstrates | ||
|
|
||
| - Creating agents with web search capabilities | ||
| - Using HostedWebSearchTool (MEAI abstraction) | ||
| - Using native SDK web search tools (ResponseTool.CreateWebSearchTool) | ||
| - Extracting text responses and URL citations from agent responses | ||
| - Managing agent lifecycle (creation and deletion) | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| Before you begin, ensure you have the following prerequisites: | ||
|
|
||
| - .NET 10 SDK or later | ||
| - Azure Foundry service endpoint and deployment configured | ||
| - Azure authentication configured for `DefaultAzureCredential` (for example, Azure CLI logged in with `az login`, environment variables, managed identity, or IDE sign-in) | ||
|
|
||
| **Note**: This sample authenticates using `DefaultAzureCredential` from the Azure Identity library, which will try several credential sources (including Azure CLI, environment variables, managed identity, and IDE sign-in). Ensure at least one supported credential source is available. For more information, see the [Azure Identity documentation](https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme). | ||
|
|
||
| **Note**: The web search tool uses the built-in web search capability from the OpenAI Responses API. | ||
|
|
||
| Set the following environment variables: | ||
|
|
||
| ```powershell | ||
| $env:AZURE_FOUNDRY_PROJECT_ENDPOINT="https://your-foundry-service.services.ai.azure.com/api/projects/your-foundry-project" # Replace with your Azure Foundry resource endpoint | ||
| $env:AZURE_FOUNDRY_PROJECT_DEPLOYMENT_NAME="gpt-4o-mini" # Optional, defaults to gpt-4o-mini | ||
| ``` | ||
|
|
||
| ## Run the sample | ||
|
|
||
| Navigate to the FoundryAgents sample directory and run: | ||
|
|
||
| ```powershell | ||
| cd dotnet/samples/GettingStarted/FoundryAgents | ||
| dotnet run --project .\FoundryAgents_Step25_WebSearch | ||
| ``` | ||
|
|
||
| ## Expected behavior | ||
|
|
||
| The sample will: | ||
|
|
||
| 1. Create an agent with web search capabilities using HostedWebSearchTool (MEAI abstraction) | ||
| - Alternative: Using native SDK web search tools (commented out in code) | ||
| - Alternative: Retrieving an existing agent by name (commented out in code) | ||
| 2. Run the agent with a query: "What's the weather today in Seattle?" | ||
| 3. The agent will use the web search tool to find current information | ||
| 4. Display the text response from the agent | ||
| 5. Display any URL citations from web search results | ||
| 6. Clean up resources by deleting the agent |
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
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.