Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
cb4eb9c
Add two hosted agent samples using the foundry agent
huimiu Mar 12, 2026
32d892b
Refactor formatting and improve readability in main.py
huimiu Mar 12, 2026
cf444e2
Merge branch 'main' into main
huimiu Mar 12, 2026
e4839d0
Add agent-framework dependency to requirements and update copyright n…
huimiu Mar 12, 2026
2c3efee
Merge branch 'main' of github.com:huimiu/agent-framework
huimiu Mar 12, 2026
0db4999
Refactor agent imports and update credential handling in hosted agent…
huimiu Mar 12, 2026
40e62ff
Update agent framework dependency in requirements for hosted agents
huimiu Mar 12, 2026
032bb03
Merge branch 'main' into main
huimiu Mar 12, 2026
74493d3
Merge branch 'main' into main
huimiu Mar 13, 2026
e831b2c
Merge branch 'main' into main
huimiu Mar 14, 2026
bd7f3e4
Merge branch 'main' into main
huimiu Mar 17, 2026
2e48760
chore: update Python version to 3.14 and improve Dockerfile for hoste…
huimiu Mar 17, 2026
fd2dca6
Merge branch 'main' into main
huimiu Mar 17, 2026
6b6584d
feat: add hosted agent samples for Azure AI with local tools and mult…
huimiu Mar 17, 2026
a287377
Merge branch 'main' of github.com:huimiu/agent-framework
huimiu Mar 17, 2026
e260389
Merge branch 'main' into main
huimiu Mar 17, 2026
ecf1bd4
fix: update Azure AI client import and refactor agent initialization …
huimiu Mar 18, 2026
ea0f1ef
feat: add hosted agent samples for Seattle hotel search and writer-re…
huimiu Mar 18, 2026
602f968
Merge branch 'main' into main
huimiu Mar 18, 2026
cceae58
fix: correct agent name in YAML configuration for local tools agent
huimiu Mar 18, 2026
74cba81
Merge branch 'main' of github.com:huimiu/agent-framework
huimiu Mar 18, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions python/samples/05-end-to-end/hosted_agents/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Hosted Agent Samples

These samples demonstrate how to build and host AI agents in Python using the [Azure AI AgentServer SDK](https://pypi.org/project/azure-ai-agentserver-agentframework/) together with Microsoft Agent Framework. Each sample runs locally as a hosted agent and includes `Dockerfile` and `agent.yaml` assets for deployment to Microsoft Foundry.

## Samples

| Sample | Description |
| ----------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| [`agent_with_hosted_mcp`](./agent_with_hosted_mcp/) | Hosted MCP tool that connects to Microsoft Learn via `https://learn.microsoft.com/api/mcp` |
| [`agent_with_text_search_rag`](./agent_with_text_search_rag/) | Retrieval-augmented generation using a custom `BaseContextProvider` with Contoso Outdoors sample data |
| [`agents_in_workflow`](./agents_in_workflow/) | Concurrent workflow that combines researcher, marketer, and legal specialist agents |
| [`agent_with_local_tools`](./agent_with_local_tools/) | Local Python tool execution for Seattle hotel search |
| [`writer_reviewer_agents_in_workflow`](./writer_reviewer_agents_in_workflow/) | Writer/Reviewer workflow using `AzureOpenAIResponsesClient` |

## Common Prerequisites

Before running any sample, ensure you have:

1. Python 3.10 or later
2. [Azure CLI](https://learn.microsoft.com/cli/azure/install-azure-cli) installed
3. An Azure OpenAI resource or a Microsoft Foundry project with a chat model deployment

### Authenticate with Azure CLI

All samples rely on Azure credentials. For local development, the simplest approach is Azure CLI authentication:

```powershell
az login
az account show
```

## Running a Sample

Each sample folder contains its own `requirements.txt`. Run commands from the specific sample directory you want to try.

### Recommended: `uv`

The sample dependencies include preview packages, so allow prerelease installs:

```powershell
cd <sample-directory>
uv venv .venv
uv pip install --prerelease=allow -r requirements.txt
uv run main.py
```

### Alternative: `venv`

Windows PowerShell:

```powershell
cd <sample-directory>
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
python main.py
```

macOS/Linux:

```bash
cd <sample-directory>
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python main.py
```

Each sample starts a hosted agent locally on `http://localhost:8088/`.

## Environment Variable Setup

You can either export variables in your shell or create a local `.env` file in the sample directory.

Example `.env` for Azure OpenAI samples:

```dotenv
AZURE_OPENAI_ENDPOINT=https://<your-openai-resource>.openai.azure.com/
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME=gpt-4.1
```

Example `.env` for Foundry project samples:

```dotenv
PROJECT_ENDPOINT=https://<your-resource>.services.ai.azure.com/api/projects/<your-project>
MODEL_DEPLOYMENT_NAME=gpt-4.1
```

## Interacting with the Agent

After starting a sample, send requests to the Responses endpoint.

PowerShell:

```powershell
$body = @{
input = "Your question here"
stream = $false
} | ConvertTo-Json

Invoke-RestMethod -Uri "http://localhost:8088/responses" -Method Post -Body $body -ContentType "application/json"
```

curl:

```bash
curl -sS -H "Content-Type: application/json" -X POST http://localhost:8088/responses \
-d '{"input":"Your question here","stream":false}'
```

Example prompts by sample:

| Sample | Example input |
| ------------------------------------ | ---------------------------------------------------------------------------- |
| `agent_with_hosted_mcp` | `What does Microsoft Learn say about managed identities in Azure?` |
| `agent_with_text_search_rag` | `What is Contoso Outdoors' return policy for refunds?` |
| `agents_in_workflow` | `Create a launch strategy for a budget-friendly electric SUV.` |
| `agent_with_local_tools` | `Find me Seattle hotels from 2025-03-15 to 2025-03-18 under $200 per night.` |
| `writer_reviewer_agents_in_workflow` | `Write a slogan for a new affordable electric SUV.` |

## Deploying to Microsoft Foundry

Each sample includes a `Dockerfile` and `agent.yaml` for deployment. For deployment steps, follow the hosted agents guidance in Microsoft Foundry:

- [Hosted agents overview](https://learn.microsoft.com/en-us/azure/ai-foundry/agents/concepts/hosted-agents)
- [Create a hosted agent with CLI](https://learn.microsoft.com/en-us/azure/ai-foundry/agents/concepts/hosted-agents?tabs=cli#create-a-hosted-agent)
- [Create a hosted agent in Visual Studio Code](https://learn.microsoft.com/en-us/azure/foundry/agents/how-to/vs-code-agents-workflow-pro-code?tabs=windows-powershell&pivots=python)

## Troubleshooting

### Missing Azure credentials

If startup fails with authentication errors, run `az login` and verify the selected subscription with `az account show`.

### Preview package install issues

These samples depend on preview packages such as `azure-ai-agentserver-agentframework`. Use `uv pip install --prerelease=allow -r requirements.txt` or `pip install -r requirements.txt`.

### ARM64 container images fail after deployment

If you build images locally on ARM64 hardware such as Apple Silicon, build for `linux/amd64`:

```bash
docker build --platform=linux/amd64 -t image .
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Virtual environments
.venv/
venv/
env/
.python-version

# Environment files with secrets
.env
.env.*
*.local

# Python build artifacts
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg

# Testing
.tox/
.nox/
.coverage
.coverage.*
htmlcov/
.pytest_cache/
.mypy_cache/

# IDE and OS files
.DS_Store
.idea/
.vscode/
*.swp
*.swo
*~

# Foundry config
.foundry/
build-source-*/

# Git
.git/
.gitignore

# Docker
.dockerignore

# Documentation
docs/
*.md
!README.md
LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# IMPORTANT: Never commit .env to version control - add it to .gitignore
PROJECT_ENDPOINT=
MODEL_DEPLOYMENT_NAME=
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM python:3.14-slim

WORKDIR /app

COPY ./ .

RUN pip install --upgrade pip && \
if [ -f requirements.txt ]; then \
pip install -r requirements.txt; \
else \
echo "No requirements.txt found"; \
fi

EXPOSE 8088

CMD ["python", "main.py"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
**IMPORTANT!** All samples and other resources made available in this GitHub repository ("samples") are designed to assist in accelerating development of agents, solutions, and agent workflows for various scenarios. Review all provided resources and carefully test output behavior in the context of your use case. AI responses may be inaccurate and AI actions should be monitored with human oversight. Learn more in the transparency documents for [Agent Service](https://learn.microsoft.com/en-us/azure/ai-foundry/responsible-ai/agents/transparency-note) and [Agent Framework](https://github.com/microsoft/agent-framework/blob/main/TRANSPARENCY_FAQ.md).

Agents, solutions, or other output you create may be subject to legal and regulatory requirements, may require licenses, or may not be suitable for all industries, scenarios, or use cases. By using any sample, you are acknowledging that any output created using those samples are solely your responsibility, and that you will comply with all applicable laws, regulations, and relevant safety standards, terms of service, and codes of conduct.

Third-party samples contained in this folder are subject to their own designated terms, and they have not been tested or verified by Microsoft or its affiliates.

Microsoft has no responsibility to you or others with respect to any of these samples or any resulting output.

# What this sample demonstrates

This sample demonstrates a **key advantage of code-based hosted agents**:

- **Local Python tool execution** - Run custom Python functions as agent tools

Code-based agents can execute **any Python code** you write. This sample includes a Seattle Hotel Agent with a `get_available_hotels` tool that searches for available hotels based on check-in/check-out dates and budget preferences.

The agent is hosted using the [Azure AI AgentServer SDK](https://pypi.org/project/azure-ai-agentserver-agentframework/) and can be deployed to Microsoft Foundry using the Azure Developer CLI.

## How It Works

### Local Tools Integration

In [main.py](main.py), the agent uses a local Python function (`get_available_hotels`) that simulates a hotel availability API. This demonstrates how code-based agents can execute custom server-side logic that prompt agents cannot access.

The tool accepts:

- **check_in_date** - Check-in date in YYYY-MM-DD format
- **check_out_date** - Check-out date in YYYY-MM-DD format
- **max_price** - Maximum price per night in USD (optional, defaults to $500)

### Agent Hosting

The agent is hosted using the [Azure AI AgentServer SDK](https://pypi.org/project/azure-ai-agentserver-agentframework/),
which provisions a REST API endpoint compatible with the OpenAI Responses protocol.

### Agent Deployment

The hosted agent can be deployed to Microsoft Foundry using the Azure Developer CLI [ai agent](https://learn.microsoft.com/en-us/azure/ai-foundry/agents/concepts/hosted-agents?view=foundry&tabs=cli#create-a-hosted-agent) extension.

## Running the Agent Locally

### Prerequisites

Before running this sample, ensure you have:

1. **Microsoft Foundry Project**
- Project created in [Microsoft Foundry](https://learn.microsoft.com/en-us/azure/ai-foundry/what-is-foundry?view=foundry#microsoft-foundry-portals)
- Chat model deployed (e.g., `gpt-4o` or `gpt-4.1`)
- Note your project endpoint URL and model deployment name

2. **Azure CLI**
- Installed and authenticated
- Run `az login` and verify with `az account show`

3. **Python 3.10 or higher**
- Verify your version: `python --version`

### Environment Variables

Set the following environment variables (matching `agent.yaml`):

- `PROJECT_ENDPOINT` - Your Microsoft Foundry project endpoint URL (required)
- `MODEL_DEPLOYMENT_NAME` - The deployment name for your chat model (defaults to `gpt-4.1-mini`)

This sample loads environment variables from a local `.env` file if present.

Create a `.env` file in this directory with the following content:

```
PROJECT_ENDPOINT=https://<your-resource>.services.ai.azure.com/api/projects/<your-project>
MODEL_DEPLOYMENT_NAME=gpt-4.1-mini
```

Or set them via PowerShell:

```powershell
# Replace with your actual values
$env:PROJECT_ENDPOINT="https://<your-resource>.services.ai.azure.com/api/projects/<your-project>"
$env:MODEL_DEPLOYMENT_NAME="gpt-4.1-mini"
```

### Running the Sample

**Recommended (`uv`):**

We recommend using [uv](https://docs.astral.sh/uv/) to create and manage the virtual environment for this sample.

```bash
uv venv .venv
uv pip install --prerelease=allow -r requirements.txt
uv run main.py
```

The sample depends on preview packages, so `--prerelease=allow` is required when installing with `uv`.

**Alternative (`venv`):**

If you do not have `uv` installed, you can use Python's built-in `venv` module instead:

**Windows (PowerShell):**

```powershell
python -m venv .venv
.\.venv\Scripts\Activate.ps1
pip install -r requirements.txt
python main.py
```

**macOS/Linux:**

```bash
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python main.py
```

This will start the hosted agent locally on `http://localhost:8088/`.

### Interacting with the Agent

**PowerShell (Windows):**

```powershell
$body = @{
input = "I need a hotel in Seattle from 2025-03-15 to 2025-03-18, budget under $200 per night"
stream = $false
} | ConvertTo-Json

Invoke-RestMethod -Uri http://localhost:8088/responses -Method Post -Body $body -ContentType "application/json"
```

**Bash/curl (Linux/macOS):**

```bash
curl -sS -H "Content-Type: application/json" -X POST http://localhost:8088/responses \
-d '{"input": "Find me hotels in Seattle for March 20-23, 2025 under $200 per night","stream":false}'
```

The agent will use the `get_available_hotels` tool to search for available hotels matching your criteria.

### Deploying the Agent to Microsoft Foundry

To deploy your agent to Microsoft Foundry, follow the comprehensive deployment guide at https://learn.microsoft.com/en-us/azure/ai-foundry/agents/concepts/hosted-agents?view=foundry&tabs=cli

## Troubleshooting

### Images built on Apple Silicon or other ARM64 machines do not work on our service

We **recommend using `azd` cloud build**, which always builds images with the correct architecture.

If you choose to **build locally**, and your machine is **not `linux/amd64`** (for example, an Apple Silicon Mac), the image will **not be compatible with our service**, causing runtime failures.

**Fix for local builds**

Use this command to build the image locally:

```shell
docker build --platform=linux/amd64 -t image .
```

This forces the image to be built for the required `amd64` architecture.
Loading
Loading