From d84d7e348f3c0494c6ab6ee408ecf5acf9fbe999 Mon Sep 17 00:00:00 2001 From: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com> Date: Thu, 5 Feb 2026 14:30:59 +0000 Subject: [PATCH] .NET: Add OpenAPI Tools sample #3674 --- .../FoundryAgents_Step19_OpenAPITools.csproj | 22 +++++ .../Program.cs | 99 +++++++++++++++++++ .../README.md | 47 +++++++++ 3 files changed, 168 insertions(+) create mode 100644 dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/FoundryAgents_Step19_OpenAPITools.csproj create mode 100644 dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/Program.cs create mode 100644 dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/README.md diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/FoundryAgents_Step19_OpenAPITools.csproj b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/FoundryAgents_Step19_OpenAPITools.csproj new file mode 100644 index 0000000000..7e864d9641 --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/FoundryAgents_Step19_OpenAPITools.csproj @@ -0,0 +1,22 @@ + + + + Exe + net10.0 + + enable + enable + $(NoWarn);CA1812 + + + + + + + + + + + + + \ No newline at end of file diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/Program.cs b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/Program.cs new file mode 100644 index 0000000000..e2b8eac3c2 --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/Program.cs @@ -0,0 +1,99 @@ +// Copyright (c) Microsoft. All rights reserved. + +// This sample shows how to use OpenAPI Tools with AI Agents. + +using Azure.AI.Projects; +using Azure.AI.Projects.OpenAI; +using Azure.Identity; +using Microsoft.Agents.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 use the countries API to retrieve information about countries by their currency code."; +const string AgentName = "OpenAPIToolsAgent"; + +// A simple OpenAPI specification for the REST Countries API +const string CountriesOpenApiSpec = """ +{ + "openapi": "3.1.0", + "info": { + "title": "REST Countries API", + "description": "Retrieve information about countries by currency code", + "version": "v3.1" + }, + "servers": [ + { + "url": "https://restcountries.com/v3.1" + } + ], + "paths": { + "/currency/{currency}": { + "get": { + "description": "Get countries that use a specific currency code (e.g., USD, EUR, GBP)", + "operationId": "GetCountriesByCurrency", + "parameters": [ + { + "name": "currency", + "in": "path", + "description": "Currency code (e.g., USD, EUR, GBP)", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "Successful response with list of countries", + "content": { + "application/json": { + "schema": { + "type": "array", + "items": { + "type": "object" + } + } + } + } + }, + "404": { + "description": "No countries found for the currency" + } + } + } + } + } +} +"""; + +// Get a client to create/retrieve/delete server side agents with Azure Foundry Agents. +AIProjectClient aiProjectClient = new(new Uri(endpoint), new AzureCliCredential()); + +// Create the OpenAPI tool definition +ResponseTool openApiTool = (ResponseTool)AgentTool.CreateOpenApiTool( + new OpenAPIFunctionDefinition( + "get_countries", + BinaryData.FromString(CountriesOpenApiSpec), + new OpenAPIAnonymousAuthenticationDetails()) + { + Description = "Retrieve information about countries by currency code" + }); + +// Create an agent with OpenAPI tool using PromptAgentDefinition (native SDK approach) +AIAgent agent = await aiProjectClient.CreateAIAgentAsync( + name: AgentName, + creationOptions: new AgentVersionCreationOptions( + new PromptAgentDefinition(model: deploymentName) + { + Instructions = AgentInstructions, + Tools = { openApiTool } + }) +); + +// Run the agent with a question about countries +Console.WriteLine(await agent.RunAsync("What countries use the Euro (EUR) as their currency? Please list them.")); + +// Cleanup by deleting the agent +await aiProjectClient.Agents.DeleteAgentAsync(agent.Name); \ No newline at end of file diff --git a/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/README.md b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/README.md new file mode 100644 index 0000000000..3e0e817b03 --- /dev/null +++ b/dotnet/samples/GettingStarted/FoundryAgents/FoundryAgents_Step19_OpenAPITools/README.md @@ -0,0 +1,47 @@ +# Using OpenAPI Tools with AI Agents + +This sample demonstrates how to use OpenAPI tools with AI agents. OpenAPI tools allow agents to call external REST APIs defined by OpenAPI specifications. + +## What this sample demonstrates + +- Creating agents with OpenAPI tool capabilities +- Using AgentTool.CreateOpenApiTool with an embedded OpenAPI specification +- Anonymous authentication for public APIs +- Running an agent that can call external REST APIs +- 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 CLI installed and authenticated (for Azure credential authentication) + +**Note**: This demo uses Azure CLI credentials for authentication. Make sure you're logged in with `az login` and have access to the Azure Foundry resource. For more information, see the [Azure CLI documentation](https://learn.microsoft.com/cli/azure/authenticate-azure-cli-interactively). + +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_Step19_OpenAPITools +``` + +## Expected behavior + +The sample will: + +1. Create an agent with an OpenAPI tool configured to call the REST Countries API +2. Ask the agent: "What countries use the Euro (EUR) as their currency?" +3. The agent will use the OpenAPI tool to call the REST Countries API +4. Display the response containing the list of countries that use EUR +5. Clean up resources by deleting the agent \ No newline at end of file