Build Persistent Agents on Azure AI
Python SDK reference for building persistent Azure AI Foundry agents with function tools, hosted code/search/MCP tools, threads, and structured outputs.
Why it matters
Leverage the Microsoft Agent Framework Python SDK to build and deploy persistent AI agents on Azure AI Foundry. Integrate with various tools for enhanced capabilities.
Outcomes
What it gets done
Create and manage persistent AI agents using Azure AI.
Integrate function calling, code execution, file search, and web search tools.
Implement conversation persistence and structured output generation.
Stream responses for real-time interaction.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/ag-agent-framework-azure-ai-py | bash Overview
Agent Framework Azure Hosted Agents
A Python SDK reference for building persistent Azure AI Foundry agents via the Microsoft Agent Framework, covering function tools, hosted code/file/web search tools, MCP integration, streaming, multi-turn threads, and structured Pydantic outputs. Use when building Python agents specifically on Azure AI Foundry's hosted agent service. Scoped to that Azure backend, not other Agent Framework providers or non-Azure LLMs.
What it does
Covers building persistent agents on Azure AI Foundry using the Microsoft Agent Framework Python SDK. The architecture runs a user query through AzureAIAgentsProvider to the Azure AI Agent Service (a persistent, server-hosted agent), executed via agent.run() or agent.run_stream(), with three tool categories - plain functions, hosted tools (code interpreter, file search, web search), and MCP tools - and conversation state kept in an AgentThread.
Installed via pip install agent-framework --pre (or the Azure-only agent-framework-azure-ai package), configured through AZURE_AI_PROJECT_ENDPOINT, AZURE_AI_MODEL_DEPLOYMENT_NAME, and BING_CONNECTION_ID (for web search), and authenticated with AzureCliCredential for development or DefaultAzureCredential for production.
async with (
AzureCliCredential() as credential,
AzureAIAgentsProvider(credential=credential) as provider,
):
agent = await provider.create_agent(name="MyAgent", instructions="You are a helpful assistant.")
result = await agent.run("Hello!")
It walks through progressively richer patterns: a basic agent with just instructions; a function-tool agent where plain Python functions (with Annotated[type, Field(description=...)] parameter typing) are passed directly to tools= and auto-converted to callable tools; a hosted-tools agent combining HostedCodeInterpreterTool and HostedWebSearchTool for code execution and Bing search; streaming responses via agent.run_stream(), printing text chunks as they arrive; multi-turn conversation threads via agent.get_new_thread(), where a second agent.run() call on the same thread retains context from the first, with the thread's conversation_id saved for later resumption; and structured outputs, where a Pydantic model with extra="forbid" is passed as response_format and the result is parsed back with model_validate_json.
Three provider methods round out the surface: create_agent() creates a new agent on the Azure AI service, get_agent(agent_id) retrieves an existing one by ID, and as_agent(sdk_agent) wraps an existing SDK Agent object without an HTTP call. Five hosted/MCP tool types are documented: HostedCodeInterpreterTool (execute Python), HostedFileSearchTool (search vector stores), HostedWebSearchTool (Bing search), HostedMCPTool (service-managed MCP), and MCPStreamableHTTPTool (client-managed MCP, connected via URL).
A complete example combines all of this into a "ResearchAssistant" agent wired with a weather function, code interpreter, web search, and a client-managed MCP tool pointed at Microsoft's own documentation MCP endpoint, running non-streaming, streaming, and structured-output calls on the same conversation thread.
When to use - and when NOT to
Use this reference when building Python agents on Azure AI Foundry that need persistent, server-hosted execution, multi-turn conversation state, and a mix of custom function tools, hosted tools (code execution, file/web search), and MCP integrations. It is specific to the Azure-hosted agent path of the Microsoft Agent Framework - not other framework backends or non-Azure LLM providers.
Inputs and outputs
Input is agent instructions, tool definitions (functions, hosted tools, or MCP tools), and user messages, optionally scoped to a conversation thread. Output is agent text responses, streamed text chunks, or structured Pydantic-validated objects when a response_format is specified.
Integrations
Built on agent-framework-azure-ai, Azure AI Agent Service, azure.identity for authentication (AzureCliCredential/DefaultAzureCredential), Bing for web search, and the Model Context Protocol for both service-managed (HostedMCPTool) and client-managed (MCPStreamableHTTPTool) tool integration.
Who it's for
Python developers building persistent, tool-using agents on Azure AI Foundry who need function calling, hosted code/search tools, MCP integration, multi-turn threads, or structured output in one framework.
FAQ
Common questions
Discussion
Questions & comments · 0
Sign In Sign in to leave a comment.