From 8b69c2ea124040c2286a4d958e7af06ef7c74387 Mon Sep 17 00:00:00 2001
From: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
Date: Mon, 23 Feb 2026 17:15:02 +0000
Subject: [PATCH] .NET: Add Foundry Agents Tool Sample - Web Search (#4040)
* .NET: Add Web Search sample #3674
* .NET: Fix WebSearch sample to use Responses API built-in web search
Remove incorrect Bing Grounding connection ID requirement from the
WebSearch sample. The web search tool uses the OpenAI Responses API
built-in capability and does not need a connection ID.
- Remove AZURE_FOUNDRY_BING_CONNECTION_ID env var requirement
- Use HostedWebSearchTool() without connectionId properties
- Refactor creation options into local functions (MEAI + NativeSDK)
- Switch from AzureCliCredential to DefaultAzureCredential
- Update README to reflect correct prerequisites
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Fix README to align DefaultAzureCredential docs with code
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Address review: add project to solution, README, simplify response text
- Add FoundryAgents_Step25_WebSearch to agent-framework-dotnet.slnx
- Add web search sample entry to parent FoundryAgents README.md
- Simplify text response extraction to use response.Text directly
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Fix merge conflict in slnx solution file
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---
dotnet/agent-framework-dotnet.slnx | 5 +-
.../FoundryAgents_Step25_WebSearch.csproj | 22 +++++++
.../FoundryAgents_Step25_WebSearch/Program.cs | 65 +++++++++++++++++++
.../FoundryAgents_Step25_WebSearch/README.md | 52 +++++++++++++++
.../GettingStarted/FoundryAgents/README.md | 1 +
5 files changed, 143 insertions(+), 2 deletions(-)
create mode 100644 dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/FoundryAgents_Step25_WebSearch.csproj
create mode 100644 dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/Program.cs
create mode 100644 dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/README.md
diff --git a/dotnet/agent-framework-dotnet.slnx b/dotnet/agent-framework-dotnet.slnx
index 7cb13b1f66..2db09a8b98 100644
--- a/dotnet/agent-framework-dotnet.slnx
+++ b/dotnet/agent-framework-dotnet.slnx
@@ -181,9 +181,10 @@
-
-
+
+
+
diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/FoundryAgents_Step25_WebSearch.csproj b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/FoundryAgents_Step25_WebSearch.csproj
new file mode 100644
index 0000000000..4d17fe06bb
--- /dev/null
+++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/FoundryAgents_Step25_WebSearch.csproj
@@ -0,0 +1,22 @@
+
+
+
+ Exe
+ net10.0
+
+ enable
+ enable
+ $(NoWarn);CA1812;CS8321
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/Program.cs b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/Program.cs
new file mode 100644
index 0000000000..77e5d5aeb7
--- /dev/null
+++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/Program.cs
@@ -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());
+
+// 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 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 CreateAgentWithNativeSDKAsync()
+ => await aiProjectClient.CreateAIAgentAsync(
+ AgentName,
+ new AgentVersionCreationOptions(
+ new PromptAgentDefinition(model: deploymentName)
+ {
+ Instructions = AgentInstructions,
+ Tools = { ResponseTool.CreateWebSearchTool() }
+ }));
diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/README.md b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/README.md
new file mode 100644
index 0000000000..95de275e8d
--- /dev/null
+++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step25_WebSearch/README.md
@@ -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
diff --git a/dotnet/samples/GettingStarted/FoundryAgents/README.md b/dotnet/samples/GettingStarted/FoundryAgents/README.md
index 706c6f10d7..702749b331 100644
--- a/dotnet/samples/GettingStarted/FoundryAgents/README.md
+++ b/dotnet/samples/GettingStarted/FoundryAgents/README.md
@@ -58,6 +58,7 @@ Before you begin, ensure you have the following prerequisites:
|[Using plugins](./FoundryAgents_Step13_Plugins/)|This sample demonstrates how to use plugins with a Foundry agent|
|[Code interpreter](./FoundryAgents_Step14_CodeInterpreter/)|This sample demonstrates how to use the code interpreter tool with a Foundry agent|
|[Computer use](./FoundryAgents_Step15_ComputerUse/)|This sample demonstrates how to use computer use capabilities with a Foundry agent|
+|[Web search](./FoundryAgents_Step25_WebSearch/)|This sample demonstrates how to use the Responses API web search tool with a Foundry agent|
|[Memory search](./FoundryAgents_Step26_MemorySearch/)|This sample demonstrates how to use memory search tool with a Foundry agent|
|[File search](./FoundryAgents_Step18_FileSearch/)|This sample demonstrates how to use the file search tool with a Foundry agent|
|[Local MCP](./FoundryAgents_Step27_LocalMCP/)|This sample demonstrates how to use a local MCP client with a Foundry agent|