Merk
Tilgang til denne siden krever autorisasjon. Du kan prøve å logge på eller endre kataloger.
Tilgang til denne siden krever autorisasjon. Du kan prøve å endre kataloger.
In Deploy your first hosted agent, you deployed a sample. In this quickstart, you deploy your own Python or .NET (C#) agent code to Foundry Agent Service. Your code can use any agent framework - such as Microsoft Agent Framework, LangGraph, the GitHub Copilot SDK, or the OpenAI Agents SDK - or plain code that calls a model directly.
Each code step has tabs for your language and protocol. Select the same combination, such as C# (Responses), in every step.
If you use a coding agent like GitHub Copilot, the Microsoft Foundry Skill can help adapt the quickstart to your own codebase and run the right azd deployment steps.
Prerequisites
Before you begin, you need:
An Azure subscription--Create one for free.
If you have an existing Foundry project, you need
Foundry Project Managerat project scope. If you need to create a new Foundry project, you needOwnerrole at resource group scope. For the full role matrix, see Hosted agent permissions reference.The
microsoft.foundryextension:azd ext install microsoft.foundryAn authenticated
azdsession:azd auth loginYour existing agent code in a local directory.
For the Python path, Python 3.13 or later.
For the C# path, the .NET 10 SDK or later.
(Optional) To start from a sample in Visual Studio Code, install Visual Studio Code and the Microsoft Foundry Toolkit for Visual Studio Code.
Your project directory should contain at minimum, for Python:
my-agent/
├── main.py # Your agent entry point
└── requirements.txt # Python dependencies
Or, for C#, a .NET web project:
my-agent/
├── Program.cs # Your agent entry point
└── my-agent.csproj # Project file (web SDK)
Choose your framework
The hosting library you add in Step 1 handles the protocol - the HTTP server, health checks, and request and response schemas. It doesn't depend on a specific agent framework, so your agent logic can use any packages you prefer, in Python or C#.
To use a framework, add its packages next to the hosting library, then call the framework from the handler. The following table lists common choices and a Python sample for each.
| Framework | Packages to add to requirements.txt |
Sample |
|---|---|---|
| Plain Python (call a model directly) | azure-ai-projects |
hello-world |
| GitHub Copilot SDK | github-copilot-sdk |
github-copilot |
| OpenAI Agents SDK | openai-agents |
openai-agents-sdk |
Each sample's requirements.txt lists the exact package versions. For the full set of bring-your-own samples, see the Python samples folder.
For C#, add framework packages to your project file next to the hosting package, then call the framework from the handler in Program.cs. For the full set of C# examples, see the C# bring-your-own samples folder.
Note
Microsoft Agent Framework has a built-in hosting integration that uses its own package. To deploy a Microsoft Agent Framework agent, see Host Microsoft Agent Framework agents as Foundry hosted agents.
Note
For Python agents built with LangChain, LangGraph, or Deep Agents, use the langchain-azure-ai[hosting] hosting integration. To deploy your agent, see Host LangGraph agents as Foundry hosted agents.
Start from a sample in Visual Studio Code
To start from a working framework template instead of your own code, use the sample gallery in the Microsoft Foundry Toolkit for Visual Studio Code:
- In the Visual Studio Code Activity Bar, select the Foundry Toolkit icon.
- Under Developer Tools > Agent Dev Tools, select Create Agent.
- Under Create in code with full control, select Use a sample.
- On Create Hosted Agent from Sample, use the filters to narrow the gallery:
- For Framework, select Agent Framework, Bring Your Own, LangGraph, or Copilot SDK.
- For Protocol Type, select Responses API or Invocations API.
- Select a sample, and then select Next.
- Enter an agent name, select your Foundry project, and then select Create.
The toolkit scaffolds the sample into a new workspace and sets up a one-click F5 debug experience. To install dependencies, test locally, and deploy from Visual Studio Code, follow the Visual Studio Code steps in Deploy your first hosted agent.
Choose your protocol
Each code step has a tab for every combination of language and protocol. Responses manages conversation history and is OpenAI-compatible. Invocations gives you full control over request and response schemas. Select the same language and protocol combination in every step.
Tip
Not sure which protocol to use? Start with Responses.
Step 1: Add the hosting library
Add the protocol library to your project. The library handles the HTTP server, health checks, and protocol compliance, independent of the agent framework you use. If you use a framework, add its packages alongside the hosting library - see Choose your framework.
Add the protocol library to your requirements.txt:
azure-ai-agentserver-responses>=1.0.0b7
Step 2: Add the hosting wrapper
Create or update your agent entry point with the hosting wrapper. The following examples show the minimal pattern. Replace the marked block with your existing agent logic.
Create or update main.py:
import asyncio
from azure.ai.agentserver.responses import (
CreateResponse,
ResponseContext,
ResponsesAgentServerHost,
TextResponse,
)
app = ResponsesAgentServerHost()
@app.response_handler
async def handler(
request: CreateResponse,
context: ResponseContext,
_cancellation_signal: asyncio.Event,
):
user_input = await context.get_input_text() or ""
# ─── YOUR AGENT LOGIC HERE ───
reply = f"Hello! You said: {user_input}"
# ─────────────────────────────
return TextResponse(context, request, text=reply)
app.run()
Note
These examples echo user input to demonstrate the hosting wrapper. Replace the marked block with your own agent logic - model calls, RAG, or a framework like LangGraph or the GitHub Copilot SDK. For complete examples, see the Python samples and C# samples.
Step 3: Initialize the project
Run azd ai agent init from your agent source directory. The Azure Developer CLI detects your language from the project files:
azd ai agent init --protocol responses --deploy-mode code
The interactive flow prompts for:
- Agent name: Customize the name or accept the default.
- Foundry Project: Select Use an existing Foundry project or Create a new Foundry project.
- Subscription: Select your Azure subscription.
- Location: Select an Azure region.
When complete, you see: AI agent definition added to your azd project successfully!
For either Python protocol, add PYTHONNOUSERSITE: "1" to the existing agent service's env map in azure.yaml before testing or deployment. This setting disables user-site package loading before the interpreter starts. Install dependencies in the agent's virtual environment or image, not with pip install --user. See Configure Python startup.
Step 4: Provision Azure resources
azd provision
This creates the required Azure resources, such as Application Insights.
Step 5: Test the agent locally
azd ai agent run
This command creates a virtual environment (Python) or restores and builds the project (C#), installs dependencies, and launches your agent. It also opens the agent inspector in your browser so you can chat with the agent.
You can also invoke from the CLI in a separate terminal:
azd ai agent invoke --local "Hello from my hosted agent"
Step 6: Deploy to Foundry Agent Service
azd deploy
When the command finishes, the output shows links to the agent playground and the agent endpoint:
Deploying services (azd deploy)
Done: Deploying service my-agent
- Agent playground (portal): https://ai.azure.com/.../build/agents/my-agent/build?version=1
- Agent endpoint: https://ai-account-<name>.services.ai.azure.com/api/projects/<project>/agents/my-agent/versions/1
Step 7: Invoke the deployed agent
azd ai agent invoke "Hello from my hosted agent"
You should see a response within a few seconds.
Clean up resources
Warning
If the current azd environment created the Foundry project, azd down permanently deletes the project's resource group and everything in it. If you selected an existing project during initialization, azd down leaves the project, its resource group, the hosted agent, and other quickstart resources in place. Delete any resources you no longer need from the existing project separately.
azd down
Troubleshooting
| Issue | Solution |
|---|---|
ModuleNotFoundError: azure.ai.agentserver |
Verify the protocol library is in requirements.txt and reinstall: pip install -r requirements.txt. |
C#: The type or namespace name 'AgentServer' could not be found |
Verify the protocol package is referenced (dotnet add package Azure.AI.AgentServer.Responses --prerelease or Azure.AI.AgentServer.Invocations) and that the project uses the web SDK (dotnet new web). |
C#: Cannot resolve scoped service ... Handler from root provider on dotnet run |
The default dotnet new web launch profile sets ASPNETCORE_ENVIRONMENT=Development, which enables strict scope validation. Use azd ai agent run to test (it runs in the hosted mode), or set ASPNETCORE_ENVIRONMENT=Production before dotnet run. |
FOUNDRY_PROJECT_ENDPOINT not set |
Use azd ai agent run (sets it automatically) instead of running the app directly. Or add it to your .env file. |
Connection refused on local run |
Ensure no other process is using port 8088. |
AuthorizationFailed during deploy |
You need Foundry Project Manager at project scope. |
Agent stuck in provisioning |
Run azd ai agent show to check status. First deploys can take 2–3 minutes while dependencies install. |
azd ai agent init fails |
Run azd version to verify 1.36.0 or later. Verify and upgrade the current Foundry extensions. |
For the full permission and role-assignment matrix, see Hosted agent permissions reference.
What you learned
In this quickstart, you:
- Added one hosting library to your existing agent code.
- Initialized an
azdproject from your source directory. - Tested locally with
azd ai agent runandazd ai agent invoke --local. - Deployed to Foundry Agent Service with
azd deploy.
Next steps
- Build a toolbox and use it with a hosted agent to combine tools behind one managed endpoint.