Site icon Blog.Chiffers.com

How To: Call an Azure AI Foundry Agent from a Logic App


Triggered by a Teams message – no custom connector required

TL;DR: This walkthrough shows you how to wire up a Logic App that listens for a Teams message, forwards it to an Azure AI Foundry agent via HTTP, and posts the agent’s reply back into Teams. No custom connector, no Azure Functions – just Logic Apps doing the heavy lifting.

Why Would You Do This?

Azure AI Foundry is great for building and hosting AI agents. But getting those agents to actually do something useful in the real world – responding to users, triggering workflows, sending replies – requires plumbing. Logic Apps is perfect for that plumbing.

In this post, I’ll show you how to build a Logic App that:

This is a great pattern for things like internal Q&A bots, helpdesk triage, or any scenario where you want an AI agent reachable directly from Teams – without building a full Copilot Studio agent.


What You’ll Need

Before you start, make sure you have the following in place:

⚠️ Note on Authentication: In this walkthrough we’re using API key auth to keep things simple. For production, you should use Managed Identity instead – I’ll call out where to swap that in.

Step 1: Get Your Agent’s Endpoint from Azure AI Foundry

First, grab the REST endpoint for your deployed agent. Head to Azure AI Foundry and open your project.

  1. Navigate to your AI Foundry project in the Azure portal
  2. Click on Agents in the left nav, and open the agent you want to call
  3. Click the </> API tab or look for the Endpoint section
  4. Copy the Endpoint URL and the API Key – you’ll need both shortly

The endpoint will look something like this:

https://<your-project>.openai.azure.com/openai/deployments/<agent-id>/chat/completions?api-version=2024-05-01-preview
💡 Tip: Store the API key in Azure Key Vault and reference it from Logic Apps using a Key Vault action or connection. Don’t paste it directly into the HTTP action – you’ll thank yourself later.

Step 2: Create the Logic App

If you don’t have a Logic App yet, create one now:

  1. In the Azure portal, search for Logic Apps and click Create
  2. Choose Consumption (pay-per-execution) unless you have a reason for Standard
  3. Pick your resource group, region, and give it a sensible name – something like la-teams-foundry-agent
  4. Hit Review + Create and wait for deployment


Step 3: Add the Teams Trigger

Now open your Logic App in the designer and add the trigger.

  1. In the Logic App Designer, click Add a trigger
  2. Search for Microsoft Teams and select it
  3. Choose the trigger: When a new channel message is added
  4. Sign in with your Microsoft 365 account when prompted
  5. Select the Team and Channel you want to monitor
💡 Tip: Consider creating a dedicated Teams channel for this – something like #ai-assistant or #ask-the-bot. It keeps things clean and makes it easy to scope the trigger without accidentally catching every message in a busy channel.

Step 4: Extract the Message Body

The Teams trigger gives you a lot of payload, but we only need the message text. Add a step to pull it out cleanly.

  1. Click + New step
  2. Search for and add a Compose action
  3. Set its Input to the dynamic value: Message body content from the trigger

The raw Teams message body comes through as HTML. You may want to add a second Compose step to strip the tags. Use this expression:

replace(replace(outputs('Compose'), '<p>', ''), '</p>', '')

For most agents, sending slightly messy HTML is fine and the model handles it gracefully – but stripping it gives you cleaner inputs.


Step 5: Call the Azure AI Foundry Agent via HTTP

This is the core step – an HTTP action that sends the user’s message to your Foundry agent and gets a response back.

  1. Add a new step and search for HTTP
  2. Select the HTTP action (not the HTTP + Swagger one)
  3. Configure it as follows:
Field Value
Method POST
URI Your Foundry agent endpoint URL (from Step 1)
Headers - Content-Type application/json
Headers - api-key Your API key (or Key Vault reference)
Body See JSON below

For the Body, use the following JSON – swap in the dynamic content from your Compose step for the user message:

{
  "messages": [
    {
      "role": "user",
      "content": "<output from your Compose step>"
    }
  ],
  "max_tokens": 800
}

⚠️ Using Managed Identity instead? Replace the api-key header with an Authentication block set to Managed Identity, and grant your Logic App’s managed identity the Cognitive Services OpenAI User role on your AI Foundry resource. Remove the api-key header entirely.

Step 6: Parse the Agent’s Response

The HTTP action returns the full Azure OpenAI response payload. We need to parse it and extract just the assistant’s reply text.

  1. Add a Parse JSON action after the HTTP step
  2. Set Content to the Body of the HTTP response (dynamic content)
  3. Use this schema to map the response:
{
  "type": "object",
  "properties": {
    "choices": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "message": {
            "type": "object",
            "properties": {
              "content": { "type": "string" }
            }
          }
        }
      }
    }
  }
}

Once parsed, you can reference the agent’s reply in subsequent steps using this expression:

body('Parse_JSON')?['choices'][0]?['message']?['content']

Step 7: Post the Reply Back to Teams

Almost there. Now add the action to send the agent’s response back into the Teams thread.

  1. Add a new step and search for Microsoft Teams
  2. Select Post a reply to a message (V2) – this keeps the reply in the same thread
  3. Set the Team and Channel to match your trigger
  4. For Message ID, use the dynamic value: Message ID from the trigger – this threads the reply correctly
  5. For Message, use the parsed content expression from Step 6

💡 Tip: Formatting the reply. Teams messages support basic HTML. You can wrap the agent’s response in <p> tags or add a bold prefix like <b>AI Assistant:</b> before the reply to make it clear the message is from your bot.

Step 8: Save, Enable, and Test

  1. Make sure the Logic App is Enabled (check the top bar in the designer)
  2. Post a message in the Teams channel you configured
  3. Head to the Logic App’s Run History and watch the run appear
  4. Check each step – green ticks all the way down means you’re done

Within a few seconds of posting your Teams message, you should see the agent’s reply appear in the same thread. If you hit an error, the most common culprits are:


Architecture Recap

Here’s the end-to-end flow you just built:

Teams Channel Message
    ↓  Logic App Trigger – When a new channel message is added
    ↓  Compose – extract message text
    ↓  HTTP POST – call Azure AI Foundry agent endpoint
    ↓  Parse JSON – extract choices[0].message.content
    ↓  Teams – Post a reply to a message (threaded)

What to Do Next

This is a solid foundation – here’s where you can take it:


Wrapping Up

Logic Apps + Azure AI Foundry is a genuinely underrated combination. You get the flexibility of a fully custom AI agent without having to write or host any middleware code. The whole thing runs serverless, scales automatically, and the run history gives you excellent visibility into exactly what’s happening at each step.

If you’ve got questions or you’re hitting a specific error, drop a comment below – and if you found this useful, share it with your team.

0 0 votes
Article Rating
Exit mobile version