Files
agent-framework/dotnet/samples/05-end-to-end/HostedAgents
T
Roger Barreto de791fb8a9 .Net: Add additional Hosted Agent Samples (#4325)
* Add 3 new hosted agent samples: AgentWithTools, AgentWithLocalTools, AgentThreadAndHITL

- AgentWithTools: Foundry tools (MCP + code interpreter) via UseFoundryTools
- AgentWithLocalTools: Local C# function tool (Seattle hotel search) with AIProjectClient
- AgentThreadAndHITL: Human-in-the-loop with ApprovalRequiredAIFunction and thread persistence

All samples follow agent-framework conventions (net10.0, AzureCliCredential, CPM disabled).
AgentWithTools includes comprehensive README with setup guide and troubleshooting.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Add root HostedAgents README, replace test_requests.py with .http, update sample READMEs

- Create root README.md with shared prerequisites, Azure AI Foundry setup,
  troubleshooting, and samples index
- Replace test_requests.py with run-requests.http in AgentThreadAndHITL
- Add pointer to root README in all 6 sample READMEs
- Trim AgentWithTools README to concise style

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Fix dotnet format issues in AgentWithLocalTools/Program.cs

- Add UTF-8 BOM (CHARSET)
- Sort System.ClientModel.Primitives import alphabetically (IMPORTS)
- Use target-typed new for AIProjectClient (IDE0090)
- Add internal accessibility modifier to Hotel record (IDE0040)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Address PR review: align model names and package versions

- Change default model from gpt-4.1-mini to gpt-4o-mini in AgentWithLocalTools
  (Program.cs, agent.yaml, README.md) to match existing samples
- Change README example from gpt-5.2 to gpt-4o-mini in AgentWithTools and root README
- Align AgentWithLocalTools package versions with other samples:
  Azure.AI.AgentServer.AgentFramework beta.6 -> beta.8
  Azure.AI.OpenAI 2.8.0-beta.1 -> 2.7.0-beta.2
  Microsoft.Extensions.AI.OpenAI 10.2.0-preview -> 10.1.1-preview

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Upgrade new samples to latest package versions

- Azure.AI.OpenAI: 2.7.0-beta.2 -> 2.8.0-beta.1
- Microsoft.Extensions.AI.OpenAI: 10.1.1-preview -> 10.3.0

Aligns with AgentWithHostedMCP which uses the latest versions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* Pin AgentThreadAndHITL to Microsoft.Extensions.AI.OpenAI 10.1.1

Azure.AI.AgentServer.AgentFramework beta.8 was compiled against
Microsoft.Extensions.AI.Abstractions with the single-param
FunctionApprovalRequestContent.CreateResponse(bool). Version 10.3.0
changed the signature to include an optional reason parameter, causing
a binary incompatibility at runtime. Pin to 10.1.1 until the framework
is recompiled against the newer abstractions.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
de791fb8a9 · 2026-03-02 10:56:28 +00:00
History
..

Hosted Agent Samples

These samples demonstrate how to build and host AI agents using the Azure AI AgentServer SDK. Each sample can be run locally and deployed to Microsoft Foundry as a hosted agent.

Samples

Sample Description
AgentWithTools Foundry tools (MCP + code interpreter) via UseFoundryTools
AgentWithLocalTools Local C# function tool execution (Seattle hotel search)
AgentThreadAndHITL Human-in-the-loop with ApprovalRequiredAIFunction and thread persistence
AgentWithHostedMCP Hosted MCP server tool (Microsoft Learn search)
AgentWithTextSearchRag RAG with TextSearchProvider (Contoso Outdoors)
AgentsInWorkflows Sequential workflow pipeline (translation chain)

Common Prerequisites

Before running any sample, ensure you have:

  1. .NET 10 SDK or later — Download
  2. Azure CLI installed — Install guide
  3. Azure OpenAI or Azure AI Foundry project with a chat model deployed (e.g., gpt-4o-mini)

Authenticate with Azure CLI

All samples use AzureCliCredential for authentication. Make sure you're logged in:

az login
az account show  # Verify the correct subscription

Common Environment Variables

Most samples require one or more of these environment variables:

Variable Used By Description
AZURE_OPENAI_ENDPOINT Most samples Your Azure OpenAI resource endpoint URL
AZURE_OPENAI_DEPLOYMENT_NAME Most samples Chat model deployment name (defaults to gpt-4o-mini)
AZURE_AI_PROJECT_ENDPOINT AgentWithTools, AgentWithLocalTools Azure AI Foundry project endpoint
MCP_TOOL_CONNECTION_ID AgentWithTools Foundry MCP tool connection name
MODEL_DEPLOYMENT_NAME AgentWithLocalTools Chat model deployment name (defaults to gpt-4o-mini)

See each sample's README for the specific variables required.

Azure AI Foundry Setup (for samples that use Foundry)

Some samples (AgentWithTools, AgentWithLocalTools) connect to an Azure AI Foundry project. If you're using these samples, you'll need additional setup.

Azure AI Developer Role

The UseFoundryTools extension requires the Azure AI Developer role on the Cognitive Services resource. Even if you created the project, you may not have this role by default.

az role assignment create `
  --role "Azure AI Developer" `
  --assignee "your-email@microsoft.com" `
  --scope "/subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.CognitiveServices/accounts/{account-name}"

Note

: You need Owner or User Access Administrator permissions on the resource to assign roles. If you don't have this, you may need to request JIT (Just-In-Time) elevated access via Azure PIM.

For more details on permissions, see Azure AI Foundry Permissions.

Creating an MCP Tool Connection

The AgentWithTools sample requires an MCP tool connection configured in your Foundry project:

  1. Go to the Azure AI Foundry portal
  2. Navigate to your project
  3. Go to Connected resources+ New connectionModel Context Protocol tool
  4. Fill in:
    • Name: SampleMCPTool (or any name you prefer)
    • Remote MCP Server endpoint: https://learn.microsoft.com/api/mcp
    • Authentication: Unauthenticated
  5. Click Connect

The connection name (e.g., SampleMCPTool) is used as the MCP_TOOL_CONNECTION_ID environment variable.

Important

: Use only the connection name, not the full ARM resource ID.

Running a Sample

Each sample runs as a standalone hosted agent on http://localhost:8088/:

cd <sample-directory>
dotnet run

Interacting with the Agent

Each sample includes a run-requests.http file for testing with the VS Code REST Client extension, or you can use PowerShell:

$body = @{ input = "Your question here" } | ConvertTo-Json
Invoke-RestMethod -Uri "http://localhost:8088/responses" -Method Post -Body $body -ContentType "application/json"

Deploying to Microsoft Foundry

Each sample includes a Dockerfile and agent.yaml for deployment. To deploy your agent to Microsoft Foundry, follow the hosted agents deployment guide.

Troubleshooting

PermissionDenied — lacks agents/write data action

Assign the Azure AI Developer role to your user. See Azure AI Developer Role above.

Project connection ... was not found

Make sure MCP_TOOL_CONNECTION_ID contains only the connection name (e.g., SampleMCPTool), not the full ARM resource ID path.

AZURE_AI_PROJECT_ENDPOINT must be set

The UseFoundryTools extension requires AZURE_AI_PROJECT_ENDPOINT. Set it to your Foundry project endpoint (e.g., https://your-resource.services.ai.azure.com/api/projects/your-project).

Multi-framework error when running dotnet run

If you see "Your project targets multiple frameworks", specify the framework:

dotnet run --framework net10.0