Exercise: Connect MCP tools to Azure AI Agents
Unit 5 of 6 · Module 9: Integrate MCP Tools with Azure AI Agents
Before you start
Short explainers for ideas this lesson builds on. Watch these first if they're new to you.
Read this lesson
The plan
This lab has two parts: connect an agent to a remote MCP server, then build your own MCP server with custom tools. It takes about an hour, with VS Code, Git, an Azure subscription, and Python. Setup matches the last lab: create a project in the Foundry Toolkit, deploy a model, and copy the project endpoint. Clone the lab repository, open the MCP integration folder, create a virtual environment, install the requirements, and put the endpoint and model deployment name in the dot env file.
Part one: a remote MCP server
In agent dot py, you import the project client, the credential, the agent definition, the MCP tool, and the MCP approval response type. You create an MCP tool with the label API specs, pointed at the Microsoft Learn documentation MCP server, and require approval always. Then you create an agent with that tool, start a conversation, and ask for the Azure CLI commands to create a container app with a managed identity. Because approval is required, the response comes back with MCP approval requests. The agent may need several tool calls, so the code loops. For each approval request from that server, it adds an MCP approval response with approve set to true and the request's ID, and sends them back. When a response arrives with no approval requests left, that's the final answer: the az containerapp create command, with a system-assigned identity. Then the code deletes the agent version.
Part two: your own MCP server
Now the custom part: an inventory server for a cosmetics store. In server dot py, you create a FastMCP server named Inventory, and add the at mcp dot tool decorator to two functions: get inventory levels, and get weekly sales. Then mcp dot run starts it, with the banner turned off, because the server talks over standard input and output, and a banner printed there would corrupt the messages. In client dot py, the client starts the server itself as a local process, and talks to it over that same input and output channel. It opens a client session, initializes it, and lists the tools. For each tool, a small factory builds an async function that calls call tool, and a function tool gives the agent its name and description. The inventory agent's instructions set the rules: restock when stock is under ten and weekly sales are over fifteen, and clear stock when there's over twenty and sales are under five. When the agent asks for a function, the client runs it through the MCP session, and sends the text result back as a function call output. Run python client dot py, and ask for the current inventory levels, which products to restock, what to clear, and the week's best sellers. When you're done, type quit, then delete the model deployment and the resource group.
Facts from Microsoft's documentation: Connect agents to MCP server endpoints; What is Microsoft Foundry Agent Service? (CC BY 4.0), rewritten in Loop's own words and examples. Narration, animation, and quiz by Loop. The coding lab is based on Microsoft's lab files for the agent exercises (MicrosoftLearning/mslearn-ai-agents, MIT License; notice). Microsoft, Azure, and Microsoft Foundry are trademarks of Microsoft. Loop is independent and not affiliated with or endorsed by Microsoft.