Items marked (preview) in this article are currently in public preview. This preview is provided without a service-level agreement, and we don’t recommend it for production workloads. Certain features might not be supported or might have constrained capabilities. For more information, see Supplemental Terms of Use for Microsoft Azure Previews.
- Provision a knowledge base one time with a Python script that creates a search index, seeds it with sample documents, and builds the knowledge source and knowledge base.
- Create a toolbox connection that targets the knowledge base’s MCP endpoint and authenticates with the agent’s managed identity.
- Deploy a hosted agent that discovers the
knowledge_base_retrievetool through the toolbox and grounds its answers in the retrieved sources.
Prerequisites
This quickstart builds on the hosted-agent toolchain. Complete the Prerequisites in the hosted agent quickstart first, which cover the Azure subscription, project roles, Python, the Azure Developer CLI (azd), and the microsoft.foundry extension.
You also need:
- An Azure AI Search service that supports agentic retrieval. Enable a system-assigned managed identity on the service, and enable role-based access control. In the Azure portal, on the search service, go to Settings > Keys, and set API Access control to Both or Role-based access control.
- A chat model deployment in your Foundry project, such as
gpt-5.4-mini. The knowledge base uses the same model to synthesize answers.
Required roles
Assign the following roles before you provision. The knowledge base calls the model with the search service’s managed identity, so that identity needs access to your Foundry account.
The agent’s managed identity exists only after you deploy the agent, so you assign Search Index Data Reader in Step 6.
Step 1: Initialize the hosted agent
Initialize a hosted agent from the Foundry IQ sample. The sample includes the agent code, theprovision_kb.py script, the toolbox.yaml definition, and the automation hooks. Run these commands in an empty directory.
gpt-5.4-mini. If you don’t have a project, the flow guides you through creating one. Initialization sets the selected project as the active project and copies the sample files into a new service directory, src/agent-framework-foundry-iq-knowledge-base-responses/.
Step 2: Enable one-command provisioning
The sample includes apostprovision hook that runs the knowledge base script, creates the toolbox connection, creates the toolbox, and sets the TOOLBOX_ENDPOINT environment variable every time you provision. Wire the hook into the azure.yaml file that azd ai agent init generated.
Open azure.yaml and add this top-level block. The postprovision hook must be at the top level because service-scoped hooks support only the package and deploy lifecycle.
azd runs it from.
Step 3: Provision Azure resources and the knowledge base
-
Point the hook at your existing search service:
The hook derives the model endpoint for answer synthesis from your project endpoint. To use a different Azure OpenAI resource, set it first with
azd env set AZURE_OPENAI_ENDPOINT "https://<account>.openai.azure.com". -
Provision the resources and the knowledge base:
azd provisioncreates or reuses your Foundry project and model deployment. Thepostprovisionhook then:- Runs
provision_kb.pyto create thefoundry-iq-indexsearch index, seed the Earth at night documents, and build thefoundry-iq-ksknowledge source andfoundry-iq-kbknowledge base. The script stores the knowledge base’s MCP endpoint asKB_MCP_ENDPOINT. - Creates the
knowledge-base-mcpconnection. This connection targets the knowledge base’s MCP endpoint and uses Agentic Identity, so the agent’s managed identity authenticates with no stored secret. - Creates the
knowledge-basetoolbox fromtoolbox.yaml, which exposes theknowledge_base_retrievetool. - Sets
TOOLBOX_ENDPOINTso the agent connects to the toolbox.
- Runs
The connection uses the
agentic-identity authentication type, which forwards the agent’s managed identity to the search service. A user token is rejected on purpose, so retrieval succeeds only after you deploy the agent and grant its identity access in Step 6.Step 4: Run the agent locally
-
Start the agent:
This command creates a virtual environment, installs dependencies, and serves the agent on
http://localhost:8088. Preview packages can produce pip warnings during setup. These warnings are nonblocking. -
In a separate terminal, ask the agent a question that the knowledge base can answer:
The local agent connects to the toolbox, but retrieval from the knowledge base requires the deployed agent’s identity. Use the local run to confirm the agent starts and reaches the toolbox. You verify grounded answers after you deploy.
Step 5: Deploy to Foundry Agent Service
Build and deploy the agent container:Step 6: Grant the agent access to the knowledge base
The deployed agent retrieves from the knowledge base with its own managed identity. Grant that identity the Search Index Data Reader role on the search service.- Find the agent’s identity object ID in the Foundry portal. Go to Agents, select your agent, and then select Identity.
-
Assign the role on the search service:
In PowerShell, use a line continuation backtick (
`) instead of\.
Step 7: Invoke the deployed agent
Send a question to the deployed agent and confirm it answers from the knowledge base:Clean up resources
Delete the resources when you’re finished so you stop incurring charges.-
Delete the knowledge base, knowledge source, and index from your search service. In the Azure portal, on the search service, delete
foundry-iq-kb,foundry-iq-ks, and thefoundry-iq-indexindex. You can keep the search service for other projects. - Delete the agent and its Azure resources:
Troubleshooting
What you learned
In this quickstart, you:- Created a Foundry IQ knowledge base with a search index, knowledge source, and sample documents.
- Exposed the knowledge base through a toolbox connection that uses the agent’s managed identity.
- Deployed a hosted agent that grounds its answers in the knowledge base with citations.